blob: f75622bfdde611cc9860ffd5e9b5e2f2a1b528bf [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>
17#include <base/memory/scoped_ptr.h>
Alex Vakulenko31a63792015-02-03 12:44:57 -080018#include <chromeos/errors/error.h>
Alex Vakulenko039da312015-02-03 08:58:55 -080019#include <libwebserv/export.h>
20
21struct MHD_Connection;
22
23namespace libwebserv {
24
Alex Vakulenko31a63792015-02-03 12:44:57 -080025class ProtocolHandler;
26
Alex Vakulenko039da312015-02-03 08:58:55 -080027using PairOfStrings = std::pair<std::string, std::string>;
28
Alex Vakulenko31a63792015-02-03 12:44:57 -080029// This class represents the file information about a file uploaded via
30// POST request using multipart/form-data request.
Alex Vakulenko039da312015-02-03 08:58:55 -080031class LIBWEBSERV_EXPORT FileInfo final {
32 public:
Alex Vakulenko039da312015-02-03 08:58:55 -080033 const std::string& GetFileName() const { return file_name_; }
34 const std::string& GetContentType() const { return content_type_; }
35 const std::string& GetTransferEncoding() const { return transfer_encoding_; }
Alex Vakulenko31a63792015-02-03 12:44:57 -080036 void GetData(
37 const base::Callback<void(const std::vector<uint8_t>&)>& success_callback,
38 const base::Callback<void(chromeos::Error*)>& error_callback);
Alex Vakulenko039da312015-02-03 08:58:55 -080039
40 private:
Alex Vakulenko31a63792015-02-03 12:44:57 -080041 friend class Server;
42
43 LIBWEBSERV_PRIVATE FileInfo(ProtocolHandler* handler,
44 int file_id,
45 const std::string& request_id,
46 const std::string& file_name,
47 const std::string& content_type,
48 const std::string& transfer_encoding);
49
50 ProtocolHandler* handler_{nullptr};
51 int file_id_{0};
52 std::string request_id_;
Alex Vakulenko039da312015-02-03 08:58:55 -080053 std::string file_name_;
54 std::string content_type_;
55 std::string transfer_encoding_;
56 std::vector<uint8_t> data_;
57
Alex Vakulenko039da312015-02-03 08:58:55 -080058 DISALLOW_COPY_AND_ASSIGN(FileInfo);
59};
60
61// A class that represents the HTTP request data.
62class LIBWEBSERV_EXPORT Request final {
63 public:
64 ~Request();
65
Alex Vakulenko039da312015-02-03 08:58:55 -080066 // Gets the request body data stream. Note that the stream is available
67 // only for requests that provided data and if this data is not already
68 // pre-parsed by the server (e.g. "application/x-www-form-urlencoded" and
69 // "multipart/form-data"). If there is no request body, or the data has been
70 // pre-parsed by the server, the returned stream will be empty.
71 const std::vector<uint8_t>& GetData() const;
72
73 // Returns the request path (e.g. "/path/document").
74 const std::string& GetPath() const { return url_; }
75
76 // Returns the request method (e.g. "GET", "POST", etc).
77 const std::string& GetMethod() const { return method_; }
78
79 // Returns a list of key-value pairs that include values provided on the URL
80 // (e.g. "http://server.com/?foo=bar") and the non-file form fields in the
81 // POST data.
82 std::vector<PairOfStrings> GetFormData() const;
83
84 // Returns a list of key-value pairs for query parameters provided on the URL
85 // (e.g. "http://server.com/?foo=bar").
86 std::vector<PairOfStrings> GetFormDataGet() const;
87
88 // Returns a list of key-value pairs for the non-file form fields in the
89 // POST data.
90 std::vector<PairOfStrings> GetFormDataPost() const;
91
92 // Returns a list of file information records for all the file uploads in
93 // the POST request.
94 std::vector<std::pair<std::string, const FileInfo*>> GetFiles() const;
95
96 // Gets the values of form field with given |name|. This includes both
97 // values provided on the URL and as part of form data in POST request.
98 std::vector<std::string> GetFormField(const std::string& name) const;
99
100 // Gets the values of form field with given |name| for form data in POST
101 // request.
102 std::vector<std::string> GetFormFieldPost(const std::string& name) const;
103
104 // Gets the values of URL query parameters with given |name|.
105 std::vector<std::string> GetFormFieldGet(const std::string& name) const;
106
107 // Gets the file upload parameters for a file form field of given |name|.
108 std::vector<const FileInfo*> GetFileInfo(const std::string& name) const;
109
110 // Returns a list of key-value pairs for all the request headers.
111 std::vector<PairOfStrings> GetHeaders() const;
112
Alex Vakulenko31a63792015-02-03 12:44:57 -0800113 // Returns the value(s) of a request header of given |name|.
Alex Vakulenko039da312015-02-03 08:58:55 -0800114 std::vector<std::string> GetHeader(const std::string& name) const;
115
Alex Vakulenko31a63792015-02-03 12:44:57 -0800116 // Returns the value of a request header of given |name|. If there are more
117 // than one header with this name, the value of the first header is returned.
118 // An empty string is returned if the header does not exist in the request.
119 std::string GetFirstHeader(const std::string& name) const;
120
Alex Vakulenko039da312015-02-03 08:58:55 -0800121 private:
Alex Vakulenko31a63792015-02-03 12:44:57 -0800122 friend class Server;
Alex Vakulenko039da312015-02-03 08:58:55 -0800123
Alex Vakulenko31a63792015-02-03 12:44:57 -0800124 LIBWEBSERV_PRIVATE Request(ProtocolHandler* handler,
125 const std::string& url,
126 const std::string& method);
Alex Vakulenko039da312015-02-03 08:58:55 -0800127
Alex Vakulenko31a63792015-02-03 12:44:57 -0800128 ProtocolHandler* handler_{nullptr};
Alex Vakulenko039da312015-02-03 08:58:55 -0800129 std::string url_;
130 std::string method_;
131 std::vector<uint8_t> raw_data_;
132 bool last_posted_data_was_file_{false};
133
134 std::multimap<std::string, std::string> post_data_;
135 std::multimap<std::string, std::string> get_data_;
136 std::multimap<std::string, std::unique_ptr<FileInfo>> file_info_;
137 std::multimap<std::string, std::string> headers_;
138
Alex Vakulenko039da312015-02-03 08:58:55 -0800139 DISALLOW_COPY_AND_ASSIGN(Request);
140};
141
142} // namespace libwebserv
143
144#endif // WEBSERVER_LIBWEBSERV_REQUEST_H_