blob: 2950d1b742defbc3b5f8c84507525930d316f45a [file] [log] [blame]
Daniel Erat35f65872015-08-17 20:59:29 -06001// 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 Vakulenko039da312015-02-03 08:58:55 -080014
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 Vakulenko31a63792015-02-03 12:44:57 -080024#include <base/callback_forward.h>
Alex Vakulenko0f6413a2015-09-21 11:06:58 -070025#include <base/files/file.h>
Alex Vakulenko039da312015-02-03 08:58:55 -080026#include <base/macros.h>
27#include <base/memory/ref_counted.h>
Alex Vakulenko75d6da22015-10-13 10:01:34 -070028#include <brillo/errors/error.h>
29#include <brillo/streams/stream.h>
Alex Vakulenko039da312015-02-03 08:58:55 -080030#include <libwebserv/export.h>
31
32struct MHD_Connection;
33
34namespace libwebserv {
35
Christopher Wiley33207262015-12-15 18:33:50 -080036class DBusProtocolHandler;
Alex Vakulenko31a63792015-02-03 12:44:57 -080037
Alex Vakulenko039da312015-02-03 08:58:55 -080038using PairOfStrings = std::pair<std::string, std::string>;
39
Alex Vakulenko31a63792015-02-03 12:44:57 -080040// This class represents the file information about a file uploaded via
41// POST request using multipart/form-data request.
Alex Vakulenko039da312015-02-03 08:58:55 -080042class LIBWEBSERV_EXPORT FileInfo final {
43 public:
Alex Vakulenko039da312015-02-03 08:58:55 -080044 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 Vakulenko31a63792015-02-03 12:44:57 -080047 void GetData(
Alex Vakulenko75d6da22015-10-13 10:01:34 -070048 const base::Callback<void(brillo::StreamPtr)>& success_callback,
49 const base::Callback<void(brillo::Error*)>& error_callback) const;
Alex Vakulenko039da312015-02-03 08:58:55 -080050
51 private:
Christopher Wiley6a460fe2015-12-15 11:29:57 -080052 friend class DBusServer;
Alex Vakulenko31a63792015-02-03 12:44:57 -080053
Christopher Wiley33207262015-12-15 18:33:50 -080054 LIBWEBSERV_PRIVATE FileInfo(DBusProtocolHandler* handler,
Alex Vakulenko31a63792015-02-03 12:44:57 -080055 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 Wiley33207262015-12-15 18:33:50 -080061 DBusProtocolHandler* handler_{nullptr};
Alex Vakulenko31a63792015-02-03 12:44:57 -080062 int file_id_{0};
63 std::string request_id_;
Alex Vakulenko039da312015-02-03 08:58:55 -080064 std::string file_name_;
65 std::string content_type_;
66 std::string transfer_encoding_;
Alex Vakulenko039da312015-02-03 08:58:55 -080067
Alex Vakulenko039da312015-02-03 08:58:55 -080068 DISALLOW_COPY_AND_ASSIGN(FileInfo);
69};
70
71// A class that represents the HTTP request data.
Christopher Book692fdfd2015-11-24 08:53:31 -050072class LIBWEBSERV_EXPORT Request {
Alex Vakulenko039da312015-02-03 08:58:55 -080073 public:
Christopher Book692fdfd2015-11-24 08:53:31 -050074 Request(const std::string& url, const std::string& method)
75 : url_{url}, method_{method} {}
76 virtual ~Request() = default;
Alex Vakulenko039da312015-02-03 08:58:55 -080077
Alex Vakulenko039da312015-02-03 08:58:55 -080078 // 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 Vakulenko0f6413a2015-09-21 11:06:58 -070083 // 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 Book692fdfd2015-11-24 08:53:31 -050086 virtual brillo::StreamPtr GetDataStream() = 0;
Alex Vakulenko039da312015-02-03 08:58:55 -080087
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 Vakulenko31a63792015-02-03 12:44:57 -0800128 // Returns the value(s) of a request header of given |name|.
Alex Vakulenko039da312015-02-03 08:58:55 -0800129 std::vector<std::string> GetHeader(const std::string& name) const;
130
Alex Vakulenko31a63792015-02-03 12:44:57 -0800131 // 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 Book692fdfd2015-11-24 08:53:31 -0500136 protected:
Alex Vakulenko039da312015-02-03 08:58:55 -0800137 std::string url_;
138 std::string method_;
Alex Vakulenko039da312015-02-03 08:58:55 -0800139 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 Vakulenko039da312015-02-03 08:58:55 -0800143};
144
145} // namespace libwebserv
146
147#endif // WEBSERVER_LIBWEBSERV_REQUEST_H_