blob: 894546b49173f2f3e55a29bc034a089f773bcc10 [file] [log] [blame]
Alex Vakulenko039da312015-02-03 08:58:55 -08001// Copyright 2014 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef WEBSERVER_LIBWEBSERV_REQUEST_H_
6#define WEBSERVER_LIBWEBSERV_REQUEST_H_
7
8#include <map>
9#include <memory>
10#include <string>
11#include <utility>
12#include <vector>
13
Alex Vakulenko31a63792015-02-03 12:44:57 -080014#include <base/callback_forward.h>
Alex Vakulenko039da312015-02-03 08:58:55 -080015#include <base/macros.h>
16#include <base/memory/ref_counted.h>
Alex Vakulenko31a63792015-02-03 12:44:57 -080017#include <chromeos/errors/error.h>
Alex Vakulenko039da312015-02-03 08:58:55 -080018#include <libwebserv/export.h>
19
20struct MHD_Connection;
21
22namespace libwebserv {
23
Alex Vakulenko31a63792015-02-03 12:44:57 -080024class ProtocolHandler;
25
Alex Vakulenko039da312015-02-03 08:58:55 -080026using PairOfStrings = std::pair<std::string, std::string>;
27
Alex Vakulenko31a63792015-02-03 12:44:57 -080028// This class represents the file information about a file uploaded via
29// POST request using multipart/form-data request.
Alex Vakulenko039da312015-02-03 08:58:55 -080030class LIBWEBSERV_EXPORT FileInfo final {
31 public:
Alex Vakulenko039da312015-02-03 08:58:55 -080032 const std::string& GetFileName() const { return file_name_; }
33 const std::string& GetContentType() const { return content_type_; }
34 const std::string& GetTransferEncoding() const { return transfer_encoding_; }
Alex Vakulenko31a63792015-02-03 12:44:57 -080035 void GetData(
36 const base::Callback<void(const std::vector<uint8_t>&)>& success_callback,
37 const base::Callback<void(chromeos::Error*)>& error_callback);
Alex Vakulenko039da312015-02-03 08:58:55 -080038
39 private:
Alex Vakulenko31a63792015-02-03 12:44:57 -080040 friend class Server;
41
42 LIBWEBSERV_PRIVATE FileInfo(ProtocolHandler* handler,
43 int file_id,
44 const std::string& request_id,
45 const std::string& file_name,
46 const std::string& content_type,
47 const std::string& transfer_encoding);
48
49 ProtocolHandler* handler_{nullptr};
50 int file_id_{0};
51 std::string request_id_;
Alex Vakulenko039da312015-02-03 08:58:55 -080052 std::string file_name_;
53 std::string content_type_;
54 std::string transfer_encoding_;
55 std::vector<uint8_t> data_;
56
Alex Vakulenko039da312015-02-03 08:58:55 -080057 DISALLOW_COPY_AND_ASSIGN(FileInfo);
58};
59
60// A class that represents the HTTP request data.
61class LIBWEBSERV_EXPORT Request final {
62 public:
63 ~Request();
64
Alex Vakulenko039da312015-02-03 08:58:55 -080065 // Gets the request body data stream. Note that the stream is available
66 // only for requests that provided data and if this data is not already
67 // pre-parsed by the server (e.g. "application/x-www-form-urlencoded" and
68 // "multipart/form-data"). If there is no request body, or the data has been
69 // pre-parsed by the server, the returned stream will be empty.
70 const std::vector<uint8_t>& GetData() const;
71
72 // Returns the request path (e.g. "/path/document").
73 const std::string& GetPath() const { return url_; }
74
75 // Returns the request method (e.g. "GET", "POST", etc).
76 const std::string& GetMethod() const { return method_; }
77
78 // Returns a list of key-value pairs that include values provided on the URL
79 // (e.g. "http://server.com/?foo=bar") and the non-file form fields in the
80 // POST data.
81 std::vector<PairOfStrings> GetFormData() const;
82
83 // Returns a list of key-value pairs for query parameters provided on the URL
84 // (e.g. "http://server.com/?foo=bar").
85 std::vector<PairOfStrings> GetFormDataGet() const;
86
87 // Returns a list of key-value pairs for the non-file form fields in the
88 // POST data.
89 std::vector<PairOfStrings> GetFormDataPost() const;
90
91 // Returns a list of file information records for all the file uploads in
92 // the POST request.
93 std::vector<std::pair<std::string, const FileInfo*>> GetFiles() const;
94
95 // Gets the values of form field with given |name|. This includes both
96 // values provided on the URL and as part of form data in POST request.
97 std::vector<std::string> GetFormField(const std::string& name) const;
98
99 // Gets the values of form field with given |name| for form data in POST
100 // request.
101 std::vector<std::string> GetFormFieldPost(const std::string& name) const;
102
103 // Gets the values of URL query parameters with given |name|.
104 std::vector<std::string> GetFormFieldGet(const std::string& name) const;
105
106 // Gets the file upload parameters for a file form field of given |name|.
107 std::vector<const FileInfo*> GetFileInfo(const std::string& name) const;
108
109 // Returns a list of key-value pairs for all the request headers.
110 std::vector<PairOfStrings> GetHeaders() const;
111
Alex Vakulenko31a63792015-02-03 12:44:57 -0800112 // Returns the value(s) of a request header of given |name|.
Alex Vakulenko039da312015-02-03 08:58:55 -0800113 std::vector<std::string> GetHeader(const std::string& name) const;
114
Alex Vakulenko31a63792015-02-03 12:44:57 -0800115 // Returns the value of a request header of given |name|. If there are more
116 // than one header with this name, the value of the first header is returned.
117 // An empty string is returned if the header does not exist in the request.
118 std::string GetFirstHeader(const std::string& name) const;
119
Alex Vakulenko039da312015-02-03 08:58:55 -0800120 private:
Alex Vakulenko31a63792015-02-03 12:44:57 -0800121 friend class Server;
Alex Vakulenko039da312015-02-03 08:58:55 -0800122
Alex Vakulenko31a63792015-02-03 12:44:57 -0800123 LIBWEBSERV_PRIVATE Request(ProtocolHandler* handler,
124 const std::string& url,
125 const std::string& method);
Alex Vakulenko039da312015-02-03 08:58:55 -0800126
Alex Vakulenko31a63792015-02-03 12:44:57 -0800127 ProtocolHandler* handler_{nullptr};
Alex Vakulenko039da312015-02-03 08:58:55 -0800128 std::string url_;
129 std::string method_;
130 std::vector<uint8_t> raw_data_;
131 bool last_posted_data_was_file_{false};
132
133 std::multimap<std::string, std::string> post_data_;
134 std::multimap<std::string, std::string> get_data_;
135 std::multimap<std::string, std::unique_ptr<FileInfo>> file_info_;
136 std::multimap<std::string, std::string> headers_;
137
Alex Vakulenko039da312015-02-03 08:58:55 -0800138 DISALLOW_COPY_AND_ASSIGN(Request);
139};
140
141} // namespace libwebserv
142
143#endif // WEBSERVER_LIBWEBSERV_REQUEST_H_