• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
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 
24 #include <base/callback_forward.h>
25 #include <base/files/file.h>
26 #include <base/macros.h>
27 #include <base/memory/ref_counted.h>
28 #include <brillo/errors/error.h>
29 #include <brillo/streams/stream.h>
30 #include <libwebserv/export.h>
31 
32 struct MHD_Connection;
33 
34 namespace libwebserv {
35 
36 class DBusProtocolHandler;
37 
38 using PairOfStrings = std::pair<std::string, std::string>;
39 
40 // This class represents the file information about a file uploaded via
41 // POST request using multipart/form-data request.
42 class LIBWEBSERV_EXPORT FileInfo final {
43  public:
GetFileName()44   const std::string& GetFileName() const { return file_name_; }
GetContentType()45   const std::string& GetContentType() const { return content_type_; }
GetTransferEncoding()46   const std::string& GetTransferEncoding() const { return transfer_encoding_; }
47   void GetData(
48       const base::Callback<void(brillo::StreamPtr)>& success_callback,
49       const base::Callback<void(brillo::Error*)>& error_callback) const;
50 
51  private:
52   friend class DBusServer;
53 
54   LIBWEBSERV_PRIVATE FileInfo(DBusProtocolHandler* handler,
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 
61   DBusProtocolHandler* handler_{nullptr};
62   int file_id_{0};
63   std::string request_id_;
64   std::string file_name_;
65   std::string content_type_;
66   std::string transfer_encoding_;
67 
68   DISALLOW_COPY_AND_ASSIGN(FileInfo);
69 };
70 
71 // A class that represents the HTTP request data.
72 class LIBWEBSERV_EXPORT Request {
73  public:
Request(const std::string & url,const std::string & method)74   Request(const std::string& url, const std::string& method)
75     : url_{url}, method_{method} {}
76   virtual ~Request() = default;
77 
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.
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).
86   virtual brillo::StreamPtr GetDataStream() = 0;
87 
88   // Returns the request path (e.g. "/path/document").
GetPath()89   const std::string& GetPath() const { return url_; }
90 
91   // Returns the request method (e.g. "GET", "POST", etc).
GetMethod()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 
128   // Returns the value(s) of a request header of given |name|.
129   std::vector<std::string> GetHeader(const std::string& name) const;
130 
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 
136  protected:
137   std::string url_;
138   std::string method_;
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_;
143 };
144 
145 }  // namespace libwebserv
146 
147 #endif  // WEBSERVER_LIBWEBSERV_REQUEST_H_
148