blob: 6899b3da79edef7ff7a4c8910fe4513cf92ac45a [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
14#include <base/macros.h>
15#include <base/memory/ref_counted.h>
16#include <base/memory/scoped_ptr.h>
17#include <libwebserv/export.h>
18
19struct MHD_Connection;
20
21namespace libwebserv {
22
23class Connection;
24using PairOfStrings = std::pair<std::string, std::string>;
25
26class LIBWEBSERV_EXPORT FileInfo final {
27 public:
28 FileInfo(const std::string& file_name,
29 const std::string& content_type,
30 const std::string& transfer_encoding);
31
32 const std::vector<uint8_t>& GetData() const;
33 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_; }
36
37 private:
38 std::string file_name_;
39 std::string content_type_;
40 std::string transfer_encoding_;
41 std::vector<uint8_t> data_;
42
43 friend class Request;
44 DISALLOW_COPY_AND_ASSIGN(FileInfo);
45};
46
47// A class that represents the HTTP request data.
48class LIBWEBSERV_EXPORT Request final {
49 public:
50 ~Request();
51
52 // Factory constructor method.
53 static scoped_ptr<Request> Create(const std::string& url,
54 const std::string& method);
55
56 // Gets the request body data stream. Note that the stream is available
57 // only for requests that provided data and if this data is not already
58 // pre-parsed by the server (e.g. "application/x-www-form-urlencoded" and
59 // "multipart/form-data"). If there is no request body, or the data has been
60 // pre-parsed by the server, the returned stream will be empty.
61 const std::vector<uint8_t>& GetData() const;
62
63 // Returns the request path (e.g. "/path/document").
64 const std::string& GetPath() const { return url_; }
65
66 // Returns the request method (e.g. "GET", "POST", etc).
67 const std::string& GetMethod() const { return method_; }
68
69 // Returns a list of key-value pairs that include values provided on the URL
70 // (e.g. "http://server.com/?foo=bar") and the non-file form fields in the
71 // POST data.
72 std::vector<PairOfStrings> GetFormData() const;
73
74 // Returns a list of key-value pairs for query parameters provided on the URL
75 // (e.g. "http://server.com/?foo=bar").
76 std::vector<PairOfStrings> GetFormDataGet() const;
77
78 // Returns a list of key-value pairs for the non-file form fields in the
79 // POST data.
80 std::vector<PairOfStrings> GetFormDataPost() const;
81
82 // Returns a list of file information records for all the file uploads in
83 // the POST request.
84 std::vector<std::pair<std::string, const FileInfo*>> GetFiles() const;
85
86 // Gets the values of form field with given |name|. This includes both
87 // values provided on the URL and as part of form data in POST request.
88 std::vector<std::string> GetFormField(const std::string& name) const;
89
90 // Gets the values of form field with given |name| for form data in POST
91 // request.
92 std::vector<std::string> GetFormFieldPost(const std::string& name) const;
93
94 // Gets the values of URL query parameters with given |name|.
95 std::vector<std::string> GetFormFieldGet(const std::string& name) const;
96
97 // Gets the file upload parameters for a file form field of given |name|.
98 std::vector<const FileInfo*> GetFileInfo(const std::string& name) const;
99
100 // Returns a list of key-value pairs for all the request headers.
101 std::vector<PairOfStrings> GetHeaders() const;
102
103 // Returns the value(s) of a request head of given |name|.
104 std::vector<std::string> GetHeader(const std::string& name) const;
105
106 private:
107 LIBWEBSERV_PRIVATE Request(const std::string& url, const std::string& method);
108
109 // Helper methods for processing request data coming from the raw HTTP
110 // connection.
111 // These methods parse the request headers and data so they can be accessed
112 // by request handlers later.
113 LIBWEBSERV_PRIVATE bool AddRawRequestData(const void* data, size_t size);
114 LIBWEBSERV_PRIVATE bool AddPostFieldData(const char* key,
115 const char* filename,
116 const char* content_type,
117 const char* transfer_encoding,
118 const char* data,
119 size_t size);
120 LIBWEBSERV_PRIVATE bool AppendPostFieldData(const char* key,
121 const char* data,
122 size_t size);
123 // Converts a request header name to canonical form (lowercase with uppercase
124 // first letter and each letter after a hyphen ('-')).
125 // "content-TYPE" will be converted to "Content-Type".
126 LIBWEBSERV_PRIVATE static std::string GetCanonicalHeaderName(
127 const std::string& name);
128
129 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
139 friend class Connection;
140 friend class RequestHelper;
141 DISALLOW_COPY_AND_ASSIGN(Request);
142};
143
144} // namespace libwebserv
145
146#endif // WEBSERVER_LIBWEBSERV_REQUEST_H_