blob: 20e255b77d1190dee10f0258b180abb8394f7a5e [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 {
16 class Value;
17 class DictionaryValue;
18} // 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 Vakulenkob3aac252014-05-07 17:35:24 -070023extern const char kErrorDomainJSON[];
24
Alex Vakulenkob645cc92014-04-15 11:34:35 -070025typedef std::vector<std::pair<std::string, std::string>> FormFieldList;
26
Chris Sosa45d9f102014-03-24 11:18:54 -070027////////////////////////////////////////////////////////////////////////////////
28// The following are simple utility helper functions for common HTTP operations
29// that use http::Request object behind the scenes and set it up accordingly.
30//
31// For more advanced functionality you need to use Request/Response objects
32// directly.
33////////////////////////////////////////////////////////////////////////////////
34
Alex Vakulenkob645cc92014-04-15 11:34:35 -070035// Performs a generic HTTP request with binary data. Success status,
36// returned data and additional information (such as returned HTTP headers)
37// can be obtained from the returned Response object.
38// If data MIME type is not specified, "application/octet-stream" is assumed.
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070039std::unique_ptr<Response> SendRequest(
40 const char* method, const std::string& url,
41 const void* data, size_t data_size, const char* mime_type,
Alex Vakulenkob3aac252014-05-07 17:35:24 -070042 const HeaderList& headers, std::shared_ptr<Transport> transport,
43 ErrorPtr* error);
Alex Vakulenkob645cc92014-04-15 11:34:35 -070044
Chris Sosa45d9f102014-03-24 11:18:54 -070045// Performs a simple GET request and returns the data as a string.
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070046std::string GetAsString(const std::string& url, const HeaderList& headers,
Alex Vakulenkob3aac252014-05-07 17:35:24 -070047 std::shared_ptr<Transport> transport,
48 ErrorPtr* error);
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070049inline std::string GetAsString(const std::string& url,
Alex Vakulenkob3aac252014-05-07 17:35:24 -070050 std::shared_ptr<Transport> transport,
51 ErrorPtr* error) {
52 return GetAsString(url, HeaderList(), transport, error);
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070053}
Chris Sosa45d9f102014-03-24 11:18:54 -070054
55// Performs a GET request. Success status, returned data and additional
56// information (such as returned HTTP headers) can be obtained from
57// the returned Response object.
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070058std::unique_ptr<Response> Get(const std::string& url,
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070059 const HeaderList& headers,
Alex Vakulenkob3aac252014-05-07 17:35:24 -070060 std::shared_ptr<Transport> transport,
61 ErrorPtr* error);
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070062inline std::unique_ptr<Response> Get(
Alex Vakulenkob3aac252014-05-07 17:35:24 -070063 const std::string& url, std::shared_ptr<Transport> transport,
64 ErrorPtr* error) {
65 return Get(url, HeaderList(), transport, error);
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070066}
Chris Sosa45d9f102014-03-24 11:18:54 -070067
68// Performs a HEAD request. Success status and additional
69// information (such as returned HTTP headers) can be obtained from
70// the returned Response object.
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070071std::unique_ptr<Response> Head(const std::string& url,
Alex Vakulenkob3aac252014-05-07 17:35:24 -070072 std::shared_ptr<Transport> transport,
73 ErrorPtr* error);
Chris Sosa45d9f102014-03-24 11:18:54 -070074
75// Performs a POST request with binary data. Success status, returned data
76// and additional information (such as returned HTTP headers) can be obtained
77// from the returned Response object.
78// If data MIME type is not specified, "application/octet-stream" is assumed
Alex Vakulenkob8ba5952014-04-17 11:35:56 -070079std::unique_ptr<Response> PostBinary(const std::string& url,
80 const void* data,
Chris Sosa45d9f102014-03-24 11:18:54 -070081 size_t data_size,
Alex Vakulenkob8ba5952014-04-17 11:35:56 -070082 const char* mime_type,
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070083 const HeaderList& headers,
Alex Vakulenkob3aac252014-05-07 17:35:24 -070084 std::shared_ptr<Transport> transport,
85 ErrorPtr* error);
Chris Sosa45d9f102014-03-24 11:18:54 -070086
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070087inline std::unique_ptr<Response> PostBinary(
88 const std::string& url, const void* data, size_t data_size,
Alex Vakulenkob3aac252014-05-07 17:35:24 -070089 const char* mime_type, std::shared_ptr<Transport> transport,
90 ErrorPtr* error) {
91 return PostBinary(url, data, data_size, mime_type, HeaderList(), transport,
92 error);
Alex Vakulenkob645cc92014-04-15 11:34:35 -070093}
94
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070095inline std::unique_ptr<Response> PostBinary(
96 const std::string& url, const void* data, size_t data_size,
Alex Vakulenkob3aac252014-05-07 17:35:24 -070097 std::shared_ptr<Transport> transport, ErrorPtr* error) {
98 return PostBinary(url, data, data_size, nullptr, transport, error);
Chris Sosa45d9f102014-03-24 11:18:54 -070099}
100
101// Performs a POST request with text data. Success status, returned data
102// and additional information (such as returned HTTP headers) can be obtained
103// from the returned Response object.
104// If data MIME type is not specified, "application/x-www-form-urlencoded"
Alex Vakulenkob645cc92014-04-15 11:34:35 -0700105// is assumed.
Alex Vakulenkob8ba5952014-04-17 11:35:56 -0700106std::unique_ptr<Response> PostText(const std::string& url,
107 const char* data,
108 const char* mime_type,
Alex Vakulenkoa3062c52014-04-21 17:05:51 -0700109 const HeaderList& headers,
Alex Vakulenkob3aac252014-05-07 17:35:24 -0700110 std::shared_ptr<Transport> transport,
111 ErrorPtr* error);
Chris Sosa45d9f102014-03-24 11:18:54 -0700112
Alex Vakulenkoa3062c52014-04-21 17:05:51 -0700113inline std::unique_ptr<Response> PostText(
114 const std::string& url, const char* data, const char* mime_type,
Alex Vakulenkob3aac252014-05-07 17:35:24 -0700115 std::shared_ptr<Transport> transport, ErrorPtr* error) {
116 return PostText(url, data, mime_type, HeaderList(), transport, error);
Alex Vakulenkob645cc92014-04-15 11:34:35 -0700117}
118
Alex Vakulenkoa3062c52014-04-21 17:05:51 -0700119inline std::unique_ptr<Response> PostText(
120 const std::string& url, const char* data,
Alex Vakulenkob3aac252014-05-07 17:35:24 -0700121 std::shared_ptr<Transport> transport, ErrorPtr* error) {
122 return PostText(url, data, nullptr, transport, error);
Chris Sosa45d9f102014-03-24 11:18:54 -0700123}
124
Alex Vakulenkob645cc92014-04-15 11:34:35 -0700125// Performs a POST request with form data. Success status, returned data
126// and additional information (such as returned HTTP headers) can be obtained
127// from the returned Response object. The form data is a list of key/value
128// pairs. The data is posed as "application/x-www-form-urlencoded".
Alex Vakulenkoa3062c52014-04-21 17:05:51 -0700129std::unique_ptr<Response> PostFormData(
130 const std::string& url, const FormFieldList& data,
Alex Vakulenkob3aac252014-05-07 17:35:24 -0700131 const HeaderList& headers, std::shared_ptr<Transport> transport,
132 ErrorPtr* error);
Alex Vakulenkob645cc92014-04-15 11:34:35 -0700133
Alex Vakulenkoa3062c52014-04-21 17:05:51 -0700134inline std::unique_ptr<Response> PostFormData(
135 const std::string& url, const FormFieldList& data,
Alex Vakulenkob3aac252014-05-07 17:35:24 -0700136 std::shared_ptr<Transport> transport, ErrorPtr* error) {
137 return PostFormData(url, data, HeaderList(), transport, error);
Alex Vakulenkob645cc92014-04-15 11:34:35 -0700138}
139
Chris Sosa45d9f102014-03-24 11:18:54 -0700140// Performs a POST request with JSON data. Success status, returned data
141// and additional information (such as returned HTTP headers) can be obtained
142// from the returned Response object. If a JSON response is expected,
143// use ParseJsonResponse() method on the returned Response object.
Alex Vakulenkob8ba5952014-04-17 11:35:56 -0700144std::unique_ptr<Response> PostJson(const std::string& url,
145 const base::Value* json,
Alex Vakulenkoa3062c52014-04-21 17:05:51 -0700146 const HeaderList& headers,
Alex Vakulenkob3aac252014-05-07 17:35:24 -0700147 std::shared_ptr<Transport> transport,
148 ErrorPtr* error);
Alex Vakulenkob645cc92014-04-15 11:34:35 -0700149
Alex Vakulenkoa3062c52014-04-21 17:05:51 -0700150inline std::unique_ptr<Response> PostJson(
151 const std::string& url, const base::Value* json,
Alex Vakulenkob3aac252014-05-07 17:35:24 -0700152 std::shared_ptr<Transport> transport, ErrorPtr* error) {
153 return PostJson(url, json, HeaderList(), transport, error);
Alex Vakulenkob645cc92014-04-15 11:34:35 -0700154}
155
156// Performs a PATCH request with JSON data. Success status, returned data
157// and additional information (such as returned HTTP headers) can be obtained
158// from the returned Response object. If a JSON response is expected,
159// use ParseJsonResponse() method on the returned Response object.
Alex Vakulenkob8ba5952014-04-17 11:35:56 -0700160std::unique_ptr<Response> PatchJson(const std::string& url,
161 const base::Value* json,
Alex Vakulenkoa3062c52014-04-21 17:05:51 -0700162 const HeaderList& headers,
Alex Vakulenkob3aac252014-05-07 17:35:24 -0700163 std::shared_ptr<Transport> transport,
164 ErrorPtr* error);
Alex Vakulenkob645cc92014-04-15 11:34:35 -0700165
Alex Vakulenkoa3062c52014-04-21 17:05:51 -0700166inline std::unique_ptr<Response> PatchJson(
167 const std::string& url, const base::Value* json,
Alex Vakulenkob3aac252014-05-07 17:35:24 -0700168 std::shared_ptr<Transport> transport, ErrorPtr* error) {
169 return PatchJson(url, json, HeaderList(), transport, error);
Alex Vakulenkob645cc92014-04-15 11:34:35 -0700170}
Chris Sosa45d9f102014-03-24 11:18:54 -0700171
172// Given an http::Response object, parse the body data into Json object.
Alex Vakulenkob3aac252014-05-07 17:35:24 -0700173// Returns null if failed. Optional |error| can be passed in to
Chris Sosa45d9f102014-03-24 11:18:54 -0700174// get the extended error information as to why the parse failed.
Alex Vakulenkob645cc92014-04-15 11:34:35 -0700175std::unique_ptr<base::DictionaryValue> ParseJsonResponse(
Alex Vakulenkob3aac252014-05-07 17:35:24 -0700176 const Response* response, int* status_code, ErrorPtr* error);
Chris Sosa45d9f102014-03-24 11:18:54 -0700177
Alex Vakulenkob3aac252014-05-07 17:35:24 -0700178} // namespace http
Alex Vakulenkoaf23b322014-05-08 16:25:45 -0700179} // namespace buffet
Chris Sosa45d9f102014-03-24 11:18:54 -0700180
Alex Vakulenkob3aac252014-05-07 17:35:24 -0700181#endif // BUFFET_HTTP_UTILS_H_