blob: acdab890fba7e03f929e833736ba871400d7a38b [file] [log] [blame]
Chris Sosa45d9f102014-03-24 11:18:54 -07001// 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 BUFFET_HTTP_REQUEST_H_
6#define BUFFET_HTTP_REQUEST_H_
7
8#include <vector>
9#include <memory>
10#include <string>
11#include <base/basictypes.h>
12
13#include "buffet/transport_interface.h"
14
15namespace chromeos {
16namespace http {
17
18// HTTP request verbs
19namespace request_type {
20 extern const char kOptions[];
21 extern const char kGet[];
22 extern const char kHead[];
23 extern const char kPost[];
24 extern const char kPut[];
25 extern const char kPatch[]; // Not a standard HTTP/1.1 request method
26 extern const char kDelete[];
27 extern const char kTrace[];
28 extern const char kConnect[];
29 extern const char kCopy[]; // Not a standard HTTP/1.1 request method
30 extern const char kMove[]; // Not a standard HTTP/1.1 request method
31} // namespace request_type
32
33// HTTP request header names
34namespace request_header {
35 extern const char kAccept[];
36 extern const char kAcceptCharset[];
37 extern const char kAcceptEncoding[];
38 extern const char kAcceptLanguage[];
39 extern const char kAllow[];
40 extern const char kAuthorization[];
41 extern const char kCacheControl[];
42 extern const char kConnection[];
43 extern const char kContentEncoding[];
44 extern const char kContentLanguage[];
45 extern const char kContentLength[];
46 extern const char kContentLocation[];
47 extern const char kContentMd5[];
48 extern const char kContentRange[];
49 extern const char kContentType[];
50 extern const char kCookie[];
51 extern const char kDate[];
52 extern const char kExpect[];
53 extern const char kExpires[];
54 extern const char kFrom[];
55 extern const char kHost[];
56 extern const char kIfMatch[];
57 extern const char kIfModifiedSince[];
58 extern const char kIfNoneMatch[];
59 extern const char kIfRange[];
60 extern const char kIfUnmodifiedSince[];
61 extern const char kLastModified[];
62 extern const char kMaxForwards[];
63 extern const char kPragma[];
64 extern const char kProxyAuthorization[];
65 extern const char kRange[];
66 extern const char kReferer[];
67 extern const char kTE[];
68 extern const char kTrailer[];
69 extern const char kTransferEncoding[];
70 extern const char kUpgrade[];
71 extern const char kUserAgent[];
72 extern const char kVia[];
73 extern const char kWarning[];
74} // namespace request_header
75
76// HTTP response header names
77namespace response_header {
78 extern const char kAcceptRanges[];
79 extern const char kAge[];
80 extern const char kAllow[];
81 extern const char kCacheControl[];
82 extern const char kConnection[];
83 extern const char kContentEncoding[];
84 extern const char kContentLanguage[];
85 extern const char kContentLength[];
86 extern const char kContentLocation[];
87 extern const char kContentMd5[];
88 extern const char kContentRange[];
89 extern const char kContentType[];
90 extern const char kDate[];
91 extern const char kETag[];
92 extern const char kExpires[];
93 extern const char kLastModified[];
94 extern const char kLocation[];
95 extern const char kPragma[];
96 extern const char kProxyAuthenticate[];
97 extern const char kRetryAfter[];
98 extern const char kServer[];
99 extern const char kSetCookie[];
100 extern const char kTrailer[];
101 extern const char kTransferEncoding[];
102 extern const char kUpgrade[];
103 extern const char kVary[];
104 extern const char kVia[];
105 extern const char kWarning[];
106 extern const char kWwwAuthenticate[];
107} // namespace response_header
108
109// HTTP request status (error) codes
110namespace status_code {
111 // OK to continue with request
112 static const int Continue = 100;
113 // Server has switched protocols in upgrade header
114 static const int SwitchProtocols = 101;
115
116 // Request completed
117 static const int Ok = 200;
118 // Object created, reason = new URI
119 static const int Created = 201;
120 // Async completion (TBS)
121 static const int Accepted = 202;
122 // Partial completion
123 static const int Partial = 203;
124 // No info to return
125 static const int NoContent = 204;
126 // Request completed, but clear form
127 static const int ResetContent = 205;
128 // Partial GET furfilled
129 static const int PartialContent = 206;
130
131 // Server couldn't decide what to return
132 static const int Ambiguous = 300;
133 // Object permanently moved
134 static const int Moved = 301;
135 // Object temporarily moved
136 static const int Redirect = 302;
137 // Redirection w/ new access method
138 static const int RedirectMethod = 303;
139 // If-Modified-Since was not modified
140 static const int NotModified = 304;
141 // Redirection to proxy, location header specifies proxy to use
142 static const int UseProxy = 305;
143 // HTTP/1.1: keep same verb
144 static const int RedirectKeepVerb = 307;
145
146 // Invalid syntax
147 static const int BadRequest = 400;
148 // Access denied
149 static const int Denied = 401;
150 // Payment required
151 static const int PaymentRequired = 402;
152 // Request forbidden
153 static const int Forbidden = 403;
154 // Object not found
155 static const int NotFound = 404;
156 // Method is not allowed
157 static const int BadMethod = 405;
158 // No response acceptable to client found
159 static const int NoneAcceptable = 406;
160 // Proxy authentication required
161 static const int ProxyAuthRequired = 407;
162 // Server timed out waiting for request
163 static const int RequestTimeout = 408;
164 // User should resubmit with more info
165 static const int Conflict = 409;
166 // The resource is no longer available
167 static const int Gone = 410;
168 // The server refused to accept request w/o a length
169 static const int LengthRequired = 411;
170 // Precondition given in request failed
171 static const int PrecondionFailed = 412;
172 // Request entity was too large
173 static const int RequestTooLarge = 413;
174 // Request URI too long
175 static const int UriTooLong = 414;
176 // Unsupported media type
177 static const int UnsupportedMedia = 415;
178 // Retry after doing the appropriate action.
179 static const int RetryWith = 449;
180
181 // Internal server error
182 static const int InternalServerError = 500;
183 // Request not supported
184 static const int NotSupported = 501;
185 // Error response received from gateway
186 static const int BadGateway = 502;
187 // Temporarily overloaded
188 static const int ServiceUnavailable = 503;
189 // Timed out waiting for gateway
190 static const int GatewayTimeout = 504;
191 // HTTP version not supported
192 static const int VersionNotSupported = 505;
193} // namespace status_code
194
195class Response; // Just a forward-declarartion
196
197///////////////////////////////////////////////////////////////////////////////
198// Request class is the main object used to set up and initiate an HTTP
199// communication session. It is used to specify the HTTP request method,
200// request URL and many optional parameters (such as HTTP headers, user agent,
201// referer URL and so on.
202//
203// Once everything is setup, GetResponse() method is used to send the request
204// and obtain the server response. The returned Response onject can be
205// used to inspect the response code, HTTP headers and/or response body.
206///////////////////////////////////////////////////////////////////////////////
207class Request {
208 public:
209 // The main constructor. |url| specifies the remote host address/path
210 // to send the request to. Optional |method| is the HTTP request verb. If
211 // omitted, "GET" is used.
212 // Uses the default libcurl-based implementation of TransportInterface
213 Request(std::string const& url, char const* method);
214 Request(std::string const& url);
215
216 // Custom constructor that allows non-default implementations
217 // of TransportInterface to be used.
218 Request(std::shared_ptr<TransportInterface> transport);
219
220 // Gets/Sets "Accept:" header value. The default value is "*/*" if not set.
221 void SetAccept(char const* accept_mime_types);
222 std::string GetAccept() const;
223
224 // Gets/Sets "Content-Type:" header value
225 void SetContentType(char const* content_type);
226 std::string GetContentType() const;
227
228 // Adds additional HTTP request header
229 void AddHeader(char const* header, char const* value);
230
231 // Removes HTTP request header
232 void RemoveHeader(char const* header);
233
234 // Adds a request body. This is not to be used with GET method
235 bool AddRequestBody(void const* data, size_t size);
236
237 // Makes a request for a subrange of data. Specifies a partial range with
238 // either from beginning of the data to the specified offset (if |bytes| is
239 // negative) or from the specified offset to the end of data (if |bytes| is
240 // positive).
241 // All individual ranges will be sent as part of "Range:" HTTP request header.
242 void AddRange(int64_t bytes);
243
244 // Makes a request for a subrange of data. Specifies a full range with
245 // start and end bytes from the beginning of the requested data.
246 // All individual ranges will be sent as part of "Range:" HTTP request header.
247 void AddRange(uint64_t from_byte, uint64_t to_byte);
248
249 // Gets/Sets an HTTP request verb to be used with request
250 void SetMethod(char const* method);
251 std::string GetMethod() const;
252
253 // Returns the request URL
254 std::string GetRequestURL() const;
255
256 // Gets/Sets a request referer URL (sent as "Referer:" request header).
257 void SetReferer(char const* referer);
258 std::string GetReferer() const;
259
260 // Gets/Sets a user agent string (sent as "User-Agent:" request header).
261 void SetUserAgent(char const* user_agent);
262 std::string GetUserAgent() const;
263
264 // Sends the request to the server and returns the response object.
265 // In case the server couldn't be reached for whatever reason, returns
266 // empty unique_ptr (null). Calling GetErrorMessage() provides additional
267 // information in such as case.
268 std::unique_ptr<Response> GetResponse();
269
270 // If the request failed before reaching the server, returns additional
271 // information about the error occurred.
272 std::string GetErrorMessage() const;
273
274 private:
275 std::shared_ptr<TransportInterface> transport_;
276 DISALLOW_COPY_AND_ASSIGN(Request);
277};
278
279///////////////////////////////////////////////////////////////////////////////
280// Response class is returned from Request::GetResponse() and is a way
281// to get to response status, error codes, response HTTP headers and response
282// data (body) if available.
283///////////////////////////////////////////////////////////////////////////////
284class Response {
285 public:
286 Response(std::shared_ptr<TransportInterface> transport);
287
288 // Returns true if server returned a success code (status code below 400).
289 bool IsSuccessful() const;
290
291 // Returns the HTTP status code (e.g. 200 for success)
292 int GetStatusCode() const;
293
294 // Returns the status text (e.g. for error 403 it could be "NOT AUTHORIZED").
295 std::string GetStatusText() const;
296
297 // Returns the content type of the response data.
298 std::string GetContentType() const;
299
300 // Returns response data as a byte array
301 std::vector<unsigned char> GetData() const;
302
303 // Returns response data as a string
304 std::string GetDataAsString() const;
305
306 // Returns a value of a given response HTTP header.
307 std::string GetHeader(char const* header_name) const;
308
309 private:
310 std::shared_ptr<TransportInterface> transport_;
311 DISALLOW_COPY_AND_ASSIGN(Response);
312};
313
314} // namespace http
315} // namespace chromeos
316
317#endif // BUFFET_HTTP_REQUEST_H_