| Daniel Erat | 35f6587 | 2015-08-17 20:59:29 -0600 | [diff] [blame] | 1 | // Copyright 2015 The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 14 | |
| 15 | #ifndef WEBSERVER_LIBWEBSERV_REQUEST_H_ |
| 16 | #define WEBSERVER_LIBWEBSERV_REQUEST_H_ |
| 17 | |
| 18 | #include <map> |
| 19 | #include <memory> |
| 20 | #include <string> |
| 21 | #include <utility> |
| 22 | #include <vector> |
| 23 | |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 24 | #include <base/callback_forward.h> |
| Alex Vakulenko | 0f6413a | 2015-09-21 11:06:58 -0700 | [diff] [blame] | 25 | #include <base/files/file.h> |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 26 | #include <base/macros.h> |
| 27 | #include <base/memory/ref_counted.h> |
| Alex Vakulenko | 75d6da2 | 2015-10-13 10:01:34 -0700 | [diff] [blame] | 28 | #include <brillo/errors/error.h> |
| 29 | #include <brillo/streams/stream.h> |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 30 | #include <libwebserv/export.h> |
| 31 | |
| 32 | struct MHD_Connection; |
| 33 | |
| 34 | namespace libwebserv { |
| 35 | |
| Christopher Wiley | 3320726 | 2015-12-15 18:33:50 -0800 | [diff] [blame] | 36 | class DBusProtocolHandler; |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 37 | |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 38 | using PairOfStrings = std::pair<std::string, std::string>; |
| 39 | |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 40 | // This class represents the file information about a file uploaded via |
| 41 | // POST request using multipart/form-data request. |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 42 | class LIBWEBSERV_EXPORT FileInfo final { |
| 43 | public: |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 44 | const std::string& GetFileName() const { return file_name_; } |
| 45 | const std::string& GetContentType() const { return content_type_; } |
| 46 | const std::string& GetTransferEncoding() const { return transfer_encoding_; } |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 47 | void GetData( |
| Alex Vakulenko | 75d6da2 | 2015-10-13 10:01:34 -0700 | [diff] [blame] | 48 | const base::Callback<void(brillo::StreamPtr)>& success_callback, |
| 49 | const base::Callback<void(brillo::Error*)>& error_callback) const; |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 50 | |
| 51 | private: |
| Christopher Wiley | 6a460fe | 2015-12-15 11:29:57 -0800 | [diff] [blame] | 52 | friend class DBusServer; |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 53 | |
| Christopher Wiley | 3320726 | 2015-12-15 18:33:50 -0800 | [diff] [blame] | 54 | LIBWEBSERV_PRIVATE FileInfo(DBusProtocolHandler* handler, |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 55 | int file_id, |
| 56 | const std::string& request_id, |
| 57 | const std::string& file_name, |
| 58 | const std::string& content_type, |
| 59 | const std::string& transfer_encoding); |
| 60 | |
| Christopher Wiley | 3320726 | 2015-12-15 18:33:50 -0800 | [diff] [blame] | 61 | DBusProtocolHandler* handler_{nullptr}; |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 62 | int file_id_{0}; |
| 63 | std::string request_id_; |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 64 | std::string file_name_; |
| 65 | std::string content_type_; |
| 66 | std::string transfer_encoding_; |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 67 | |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 68 | DISALLOW_COPY_AND_ASSIGN(FileInfo); |
| 69 | }; |
| 70 | |
| 71 | // A class that represents the HTTP request data. |
| Christopher Book | 692fdfd | 2015-11-24 08:53:31 -0500 | [diff] [blame] | 72 | class LIBWEBSERV_EXPORT Request { |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 73 | public: |
| Christopher Book | 692fdfd | 2015-11-24 08:53:31 -0500 | [diff] [blame] | 74 | Request(const std::string& url, const std::string& method) |
| 75 | : url_{url}, method_{method} {} |
| 76 | virtual ~Request() = default; |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 77 | |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 78 | // Gets the request body data stream. Note that the stream is available |
| 79 | // only for requests that provided data and if this data is not already |
| 80 | // pre-parsed by the server (e.g. "application/x-www-form-urlencoded" and |
| 81 | // "multipart/form-data"). If there is no request body, or the data has been |
| 82 | // pre-parsed by the server, the returned stream will be empty. |
| Alex Vakulenko | 0f6413a | 2015-09-21 11:06:58 -0700 | [diff] [blame] | 83 | // The stream returned is valid for as long as the Request object itself is |
| 84 | // alive. Accessing the stream after the Request object is destroyed will lead |
| 85 | // to an undefined behavior (will likely just crash). |
| Christopher Book | 692fdfd | 2015-11-24 08:53:31 -0500 | [diff] [blame] | 86 | virtual brillo::StreamPtr GetDataStream() = 0; |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 87 | |
| 88 | // Returns the request path (e.g. "/path/document"). |
| 89 | const std::string& GetPath() const { return url_; } |
| 90 | |
| 91 | // Returns the request method (e.g. "GET", "POST", etc). |
| 92 | const std::string& GetMethod() const { return method_; } |
| 93 | |
| 94 | // Returns a list of key-value pairs that include values provided on the URL |
| 95 | // (e.g. "http://server.com/?foo=bar") and the non-file form fields in the |
| 96 | // POST data. |
| 97 | std::vector<PairOfStrings> GetFormData() const; |
| 98 | |
| 99 | // Returns a list of key-value pairs for query parameters provided on the URL |
| 100 | // (e.g. "http://server.com/?foo=bar"). |
| 101 | std::vector<PairOfStrings> GetFormDataGet() const; |
| 102 | |
| 103 | // Returns a list of key-value pairs for the non-file form fields in the |
| 104 | // POST data. |
| 105 | std::vector<PairOfStrings> GetFormDataPost() const; |
| 106 | |
| 107 | // Returns a list of file information records for all the file uploads in |
| 108 | // the POST request. |
| 109 | std::vector<std::pair<std::string, const FileInfo*>> GetFiles() const; |
| 110 | |
| 111 | // Gets the values of form field with given |name|. This includes both |
| 112 | // values provided on the URL and as part of form data in POST request. |
| 113 | std::vector<std::string> GetFormField(const std::string& name) const; |
| 114 | |
| 115 | // Gets the values of form field with given |name| for form data in POST |
| 116 | // request. |
| 117 | std::vector<std::string> GetFormFieldPost(const std::string& name) const; |
| 118 | |
| 119 | // Gets the values of URL query parameters with given |name|. |
| 120 | std::vector<std::string> GetFormFieldGet(const std::string& name) const; |
| 121 | |
| 122 | // Gets the file upload parameters for a file form field of given |name|. |
| 123 | std::vector<const FileInfo*> GetFileInfo(const std::string& name) const; |
| 124 | |
| 125 | // Returns a list of key-value pairs for all the request headers. |
| 126 | std::vector<PairOfStrings> GetHeaders() const; |
| 127 | |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 128 | // Returns the value(s) of a request header of given |name|. |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 129 | std::vector<std::string> GetHeader(const std::string& name) const; |
| 130 | |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 131 | // Returns the value of a request header of given |name|. If there are more |
| 132 | // than one header with this name, the value of the first header is returned. |
| 133 | // An empty string is returned if the header does not exist in the request. |
| 134 | std::string GetFirstHeader(const std::string& name) const; |
| 135 | |
| Christopher Book | 692fdfd | 2015-11-24 08:53:31 -0500 | [diff] [blame] | 136 | protected: |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 137 | std::string url_; |
| 138 | std::string method_; |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 139 | std::multimap<std::string, std::string> post_data_; |
| 140 | std::multimap<std::string, std::string> get_data_; |
| 141 | std::multimap<std::string, std::unique_ptr<FileInfo>> file_info_; |
| 142 | std::multimap<std::string, std::string> headers_; |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 143 | }; |
| 144 | |
| 145 | } // namespace libwebserv |
| 146 | |
| 147 | #endif // WEBSERVER_LIBWEBSERV_REQUEST_H_ |