blob: 87b9b3adf1b9f6ef77a84d3e0fff1409586f839d [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_UTILS_H_
6#define BUFFET_HTTP_UTILS_H_
7
Alex Vakulenkob3aac252014-05-07 17:35:24 -07008#include "buffet/error.h"
Chris Sosa45d9f102014-03-24 11:18:54 -07009#include "buffet/http_request.h"
10
Alex Vakulenkob3aac252014-05-07 17:35:24 -070011#include <string>
12#include <utility>
13#include <vector>
14
Alex Vakulenkob645cc92014-04-15 11:34:35 -070015namespace base {
Alex Vakulenko5a9e7182014-08-11 15:59:58 -070016class Value;
17class DictionaryValue;
Alex Vakulenkob645cc92014-04-15 11:34:35 -070018} // namespace base
Chris Sosa45d9f102014-03-24 11:18:54 -070019
Alex Vakulenkoaf23b322014-05-08 16:25:45 -070020namespace buffet {
Chris Sosa45d9f102014-03-24 11:18:54 -070021namespace http {
22
Alex Vakulenkob645cc92014-04-15 11:34:35 -070023typedef std::vector<std::pair<std::string, std::string>> FormFieldList;
24
Chris Sosa45d9f102014-03-24 11:18:54 -070025////////////////////////////////////////////////////////////////////////////////
26// The following are simple utility helper functions for common HTTP operations
27// that use http::Request object behind the scenes and set it up accordingly.
28//
29// For more advanced functionality you need to use Request/Response objects
30// directly.
31////////////////////////////////////////////////////////////////////////////////
32
Alex Vakulenkob645cc92014-04-15 11:34:35 -070033// Performs a generic HTTP request with binary data. Success status,
34// returned data and additional information (such as returned HTTP headers)
35// can be obtained from the returned Response object.
36// If data MIME type is not specified, "application/octet-stream" is assumed.
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070037std::unique_ptr<Response> SendRequest(
38 const char* method, const std::string& url,
39 const void* data, size_t data_size, const char* mime_type,
Alex Vakulenkob3aac252014-05-07 17:35:24 -070040 const HeaderList& headers, std::shared_ptr<Transport> transport,
41 ErrorPtr* error);
Alex Vakulenkob645cc92014-04-15 11:34:35 -070042
Chris Sosa45d9f102014-03-24 11:18:54 -070043// Performs a simple GET request and returns the data as a string.
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070044std::string GetAsString(const std::string& url, const HeaderList& headers,
Alex Vakulenkob3aac252014-05-07 17:35:24 -070045 std::shared_ptr<Transport> transport,
46 ErrorPtr* error);
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070047inline std::string GetAsString(const std::string& url,
Alex Vakulenkob3aac252014-05-07 17:35:24 -070048 std::shared_ptr<Transport> transport,
49 ErrorPtr* error) {
50 return GetAsString(url, HeaderList(), transport, error);
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070051}
Chris Sosa45d9f102014-03-24 11:18:54 -070052
53// Performs a GET request. Success status, returned data and additional
54// information (such as returned HTTP headers) can be obtained from
55// the returned Response object.
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070056std::unique_ptr<Response> Get(const std::string& url,
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070057 const HeaderList& headers,
Alex Vakulenkob3aac252014-05-07 17:35:24 -070058 std::shared_ptr<Transport> transport,
59 ErrorPtr* error);
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070060inline std::unique_ptr<Response> Get(
Alex Vakulenkob3aac252014-05-07 17:35:24 -070061 const std::string& url, std::shared_ptr<Transport> transport,
62 ErrorPtr* error) {
63 return Get(url, HeaderList(), transport, error);
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070064}
Chris Sosa45d9f102014-03-24 11:18:54 -070065
66// Performs a HEAD request. Success status and additional
67// information (such as returned HTTP headers) can be obtained from
68// the returned Response object.
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070069std::unique_ptr<Response> Head(const std::string& url,
Alex Vakulenkob3aac252014-05-07 17:35:24 -070070 std::shared_ptr<Transport> transport,
71 ErrorPtr* error);
Chris Sosa45d9f102014-03-24 11:18:54 -070072
73// Performs a POST request with binary data. Success status, returned data
74// and additional information (such as returned HTTP headers) can be obtained
75// from the returned Response object.
76// If data MIME type is not specified, "application/octet-stream" is assumed
Alex Vakulenkob8ba5952014-04-17 11:35:56 -070077std::unique_ptr<Response> PostBinary(const std::string& url,
78 const void* data,
Chris Sosa45d9f102014-03-24 11:18:54 -070079 size_t data_size,
Alex Vakulenkob8ba5952014-04-17 11:35:56 -070080 const char* mime_type,
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070081 const HeaderList& headers,
Alex Vakulenkob3aac252014-05-07 17:35:24 -070082 std::shared_ptr<Transport> transport,
83 ErrorPtr* error);
Chris Sosa45d9f102014-03-24 11:18:54 -070084
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070085inline std::unique_ptr<Response> PostBinary(
86 const std::string& url, const void* data, size_t data_size,
Alex Vakulenkob3aac252014-05-07 17:35:24 -070087 const char* mime_type, std::shared_ptr<Transport> transport,
88 ErrorPtr* error) {
89 return PostBinary(url, data, data_size, mime_type, HeaderList(), transport,
90 error);
Alex Vakulenkob645cc92014-04-15 11:34:35 -070091}
92
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070093inline std::unique_ptr<Response> PostBinary(
94 const std::string& url, const void* data, size_t data_size,
Alex Vakulenkob3aac252014-05-07 17:35:24 -070095 std::shared_ptr<Transport> transport, ErrorPtr* error) {
96 return PostBinary(url, data, data_size, nullptr, transport, error);
Chris Sosa45d9f102014-03-24 11:18:54 -070097}
98
99// Performs a POST request with text data. Success status, returned data
100// and additional information (such as returned HTTP headers) can be obtained
101// from the returned Response object.
102// If data MIME type is not specified, "application/x-www-form-urlencoded"
Alex Vakulenkob645cc92014-04-15 11:34:35 -0700103// is assumed.
Alex Vakulenkob8ba5952014-04-17 11:35:56 -0700104std::unique_ptr<Response> PostText(const std::string& url,
105 const char* data,
106 const char* mime_type,
Alex Vakulenkoa3062c52014-04-21 17:05:51 -0700107 const HeaderList& headers,
Alex Vakulenkob3aac252014-05-07 17:35:24 -0700108 std::shared_ptr<Transport> transport,
109 ErrorPtr* error);
Chris Sosa45d9f102014-03-24 11:18:54 -0700110
Alex Vakulenkoa3062c52014-04-21 17:05:51 -0700111inline std::unique_ptr<Response> PostText(
112 const std::string& url, const char* data, const char* mime_type,
Alex Vakulenkob3aac252014-05-07 17:35:24 -0700113 std::shared_ptr<Transport> transport, ErrorPtr* error) {
114 return PostText(url, data, mime_type, HeaderList(), transport, error);
Alex Vakulenkob645cc92014-04-15 11:34:35 -0700115}
116
Alex Vakulenkoa3062c52014-04-21 17:05:51 -0700117inline std::unique_ptr<Response> PostText(
118 const std::string& url, const char* data,
Alex Vakulenkob3aac252014-05-07 17:35:24 -0700119 std::shared_ptr<Transport> transport, ErrorPtr* error) {
120 return PostText(url, data, nullptr, transport, error);
Chris Sosa45d9f102014-03-24 11:18:54 -0700121}
122
Alex Vakulenkob645cc92014-04-15 11:34:35 -0700123// Performs a POST request with form data. Success status, returned data
124// and additional information (such as returned HTTP headers) can be obtained
125// from the returned Response object. The form data is a list of key/value
126// pairs. The data is posed as "application/x-www-form-urlencoded".
Alex Vakulenkoa3062c52014-04-21 17:05:51 -0700127std::unique_ptr<Response> PostFormData(
128 const std::string& url, const FormFieldList& data,
Alex Vakulenkob3aac252014-05-07 17:35:24 -0700129 const HeaderList& headers, std::shared_ptr<Transport> transport,
130 ErrorPtr* error);
Alex Vakulenkob645cc92014-04-15 11:34:35 -0700131
Alex Vakulenkoa3062c52014-04-21 17:05:51 -0700132inline std::unique_ptr<Response> PostFormData(
133 const std::string& url, const FormFieldList& data,
Alex Vakulenkob3aac252014-05-07 17:35:24 -0700134 std::shared_ptr<Transport> transport, ErrorPtr* error) {
135 return PostFormData(url, data, HeaderList(), transport, error);
Alex Vakulenkob645cc92014-04-15 11:34:35 -0700136}
137
Chris Sosa45d9f102014-03-24 11:18:54 -0700138// Performs a POST request with JSON data. Success status, returned data
139// and additional information (such as returned HTTP headers) can be obtained
140// from the returned Response object. If a JSON response is expected,
141// use ParseJsonResponse() method on the returned Response object.
Alex Vakulenkob8ba5952014-04-17 11:35:56 -0700142std::unique_ptr<Response> PostJson(const std::string& url,
143 const base::Value* json,
Alex Vakulenkoa3062c52014-04-21 17:05:51 -0700144 const HeaderList& headers,
Alex Vakulenkob3aac252014-05-07 17:35:24 -0700145 std::shared_ptr<Transport> transport,
146 ErrorPtr* error);
Alex Vakulenkob645cc92014-04-15 11:34:35 -0700147
Alex Vakulenkoa3062c52014-04-21 17:05:51 -0700148inline std::unique_ptr<Response> PostJson(
149 const std::string& url, const base::Value* json,
Alex Vakulenkob3aac252014-05-07 17:35:24 -0700150 std::shared_ptr<Transport> transport, ErrorPtr* error) {
151 return PostJson(url, json, HeaderList(), transport, error);
Alex Vakulenkob645cc92014-04-15 11:34:35 -0700152}
153
154// Performs a PATCH request with JSON data. Success status, returned data
155// and additional information (such as returned HTTP headers) can be obtained
156// from the returned Response object. If a JSON response is expected,
157// use ParseJsonResponse() method on the returned Response object.
Alex Vakulenkob8ba5952014-04-17 11:35:56 -0700158std::unique_ptr<Response> PatchJson(const std::string& url,
159 const base::Value* json,
Alex Vakulenkoa3062c52014-04-21 17:05:51 -0700160 const HeaderList& headers,
Alex Vakulenkob3aac252014-05-07 17:35:24 -0700161 std::shared_ptr<Transport> transport,
162 ErrorPtr* error);
Alex Vakulenkob645cc92014-04-15 11:34:35 -0700163
Alex Vakulenkoa3062c52014-04-21 17:05:51 -0700164inline std::unique_ptr<Response> PatchJson(
165 const std::string& url, const base::Value* json,
Alex Vakulenkob3aac252014-05-07 17:35:24 -0700166 std::shared_ptr<Transport> transport, ErrorPtr* error) {
167 return PatchJson(url, json, HeaderList(), transport, error);
Alex Vakulenkob645cc92014-04-15 11:34:35 -0700168}
Chris Sosa45d9f102014-03-24 11:18:54 -0700169
170// Given an http::Response object, parse the body data into Json object.
Alex Vakulenkob3aac252014-05-07 17:35:24 -0700171// Returns null if failed. Optional |error| can be passed in to
Chris Sosa45d9f102014-03-24 11:18:54 -0700172// get the extended error information as to why the parse failed.
Alex Vakulenkob645cc92014-04-15 11:34:35 -0700173std::unique_ptr<base::DictionaryValue> ParseJsonResponse(
Alex Vakulenkob3aac252014-05-07 17:35:24 -0700174 const Response* response, int* status_code, ErrorPtr* error);
Chris Sosa45d9f102014-03-24 11:18:54 -0700175
Alex Vakulenkob3aac252014-05-07 17:35:24 -0700176} // namespace http
Alex Vakulenkoaf23b322014-05-08 16:25:45 -0700177} // namespace buffet
Chris Sosa45d9f102014-03-24 11:18:54 -0700178
Alex Vakulenkob3aac252014-05-07 17:35:24 -0700179#endif // BUFFET_HTTP_UTILS_H_