summaryrefslogtreecommitdiff
path: root/brillo/http/http_utils.h
blob: 7d5f937611430b59228bbe6d6b51e6a89cb4ef07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// Copyright 2014 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef LIBCHROMEOS_BRILLO_HTTP_HTTP_UTILS_H_
#define LIBCHROMEOS_BRILLO_HTTP_HTTP_UTILS_H_

#include <string>
#include <utility>
#include <vector>

#include <brillo/brillo_export.h>
#include <brillo/errors/error.h>
#include <brillo/http/http_form_data.h>
#include <brillo/http/http_request.h>

namespace base {
class Value;
class DictionaryValue;
}  // namespace base

namespace brillo {
namespace http {

using FormFieldList = std::vector<std::pair<std::string, std::string>>;

////////////////////////////////////////////////////////////////////////////////
// The following are simple utility helper functions for common HTTP operations
// that use http::Request object behind the scenes and set it up accordingly.
// The values for request method, data MIME type, request header names should
// not be directly encoded in most cases, but use predefined constants from
// http_request.h.
// So, instead of calling:
//    SendRequestAndBlock("POST",
//                        "http://url",
//                        "data", 4,
//                        "text/plain",
//                        {{"Authorization", "Bearer TOKEN"}},
//                        transport, error);
// You should do use this instead:
//    SendRequestAndBlock(brillo::http::request_type::kPost,
//                        "http://url",
//                        "data", 4,
//                        brillo::mime::text::kPlain,
//                        {{brillo::http::request_header::kAuthorization,
//                          "Bearer TOKEN"}},
//                        transport, error);
//
// For more advanced functionality you need to use Request/Response objects
// directly.
////////////////////////////////////////////////////////////////////////////////

// Performs a generic HTTP request with binary data. Success status,
// returned data and additional information (such as returned HTTP headers)
// can be obtained from the returned Response object.
BRILLO_EXPORT std::unique_ptr<Response> SendRequestAndBlock(
    const std::string& method,
    const std::string& url,
    const void* data,
    size_t data_size,
    const std::string& mime_type,
    const HeaderList& headers,
    std::shared_ptr<Transport> transport,
    brillo::ErrorPtr* error);

// Same as above, but without sending the request body.
// This is especially useful for requests like "GET" and "HEAD".
BRILLO_EXPORT std::unique_ptr<Response> SendRequestWithNoDataAndBlock(
    const std::string& method,
    const std::string& url,
    const HeaderList& headers,
    std::shared_ptr<Transport> transport,
    brillo::ErrorPtr* error);

// Same as above but asynchronous. On success, |success_callback| is called
// with the response object. On failure, |error_callback| is called with the
// error details.
// Returns the ID of the request which can be used to cancel the pending
// request using Transport::CancelRequest().
BRILLO_EXPORT RequestID SendRequest(
    const std::string& method,
    const std::string& url,
    StreamPtr stream,
    const std::string& mime_type,
    const HeaderList& headers,
    std::shared_ptr<Transport> transport,
    const SuccessCallback& success_callback,
    const ErrorCallback& error_callback);

// Same as above, but takes a memory buffer. The pointer should be valid only
// until the function returns. The data is copied into an internal buffer to be
// available for the duration of the asynchronous operation.
// Returns the ID of the request which can be used to cancel the pending
// request using Transport::CancelRequest().
BRILLO_EXPORT RequestID SendRequest(
    const std::string& method,
    const std::string& url,
    const void* data,
    size_t data_size,
    const std::string& mime_type,
    const HeaderList& headers,
    std::shared_ptr<Transport> transport,
    const SuccessCallback& success_callback,
    const ErrorCallback& error_callback);

// Asynchronous version of SendRequestNoData().
// Returns the ID of the request which can be used to cancel the pending
// request using Transport::CancelRequest().
BRILLO_EXPORT RequestID SendRequestWithNoData(
    const std::string& method,
    const std::string& url,
    const HeaderList& headers,
    std::shared_ptr<Transport> transport,
    const SuccessCallback& success_callback,
    const ErrorCallback& error_callback);

// Performs a GET request. Success status, returned data and additional
// information (such as returned HTTP headers) can be obtained from
// the returned Response object.
BRILLO_EXPORT std::unique_ptr<Response> GetAndBlock(
    const std::string& url,
    const HeaderList& headers,
    std::shared_ptr<Transport> transport,
    brillo::ErrorPtr* error);

// Asynchronous version of http::Get().
// Returns the ID of the request which can be used to cancel the pending
// request using Transport::CancelRequest().
BRILLO_EXPORT RequestID Get(
    const std::string& url,
    const HeaderList& headers,
    std::shared_ptr<Transport> transport,
    const SuccessCallback& success_callback,
    const ErrorCallback& error_callback);

// Performs a HEAD request. Success status and additional
// information (such as returned HTTP headers) can be obtained from
// the returned Response object.
BRILLO_EXPORT std::unique_ptr<Response> HeadAndBlock(
    const std::string& url,
    std::shared_ptr<Transport> transport,
    brillo::ErrorPtr* error);

// Performs an asynchronous HEAD request.
// Returns the ID of the request which can be used to cancel the pending
// request using Transport::CancelRequest().
BRILLO_EXPORT RequestID Head(
    const std::string& url,
    std::shared_ptr<Transport> transport,
    const SuccessCallback& success_callback,
    const ErrorCallback& error_callback);

// Performs a POST request with binary data. Success status, returned data
// and additional information (such as returned HTTP headers) can be obtained
// from the returned Response object.
BRILLO_EXPORT std::unique_ptr<Response> PostBinaryAndBlock(
    const std::string& url,
    const void* data,
    size_t data_size,
    const std::string& mime_type,
    const HeaderList& headers,
    std::shared_ptr<Transport> transport,
    brillo::ErrorPtr* error);

// Async version of PostBinary().
// Returns the ID of the request which can be used to cancel the pending
// request using Transport::CancelRequest().
BRILLO_EXPORT RequestID PostBinary(
    const std::string& url,
    StreamPtr stream,
    const std::string& mime_type,
    const HeaderList& headers,
    std::shared_ptr<Transport> transport,
    const SuccessCallback& success_callback,
    const ErrorCallback& error_callback);

// Same as above, but takes a memory buffer. The pointer should be valid only
// until the function returns. The data is copied into an internal buffer
// to be available for the duration of the asynchronous operation.
// Returns the ID of the request which can be used to cancel the pending
// request using Transport::CancelRequest().
BRILLO_EXPORT RequestID PostBinary(
    const std::string& url,
    const void* data,
    size_t data_size,
    const std::string& mime_type,
    const HeaderList& headers,
    std::shared_ptr<Transport> transport,
    const SuccessCallback& success_callback,
    const ErrorCallback& error_callback);

// Performs a POST request with text data. Success status, returned data
// and additional information (such as returned HTTP headers) can be obtained
// from the returned Response object.
BRILLO_EXPORT std::unique_ptr<Response> PostTextAndBlock(
    const std::string& url,
    const std::string& data,
    const std::string& mime_type,
    const HeaderList& headers,
    std::shared_ptr<Transport> transport,
    brillo::ErrorPtr* error);

// Async version of PostText().
// Returns the ID of the request which can be used to cancel the pending
// request using Transport::CancelRequest().
BRILLO_EXPORT RequestID PostText(
    const std::string& url,
    const std::string& data,
    const std::string& mime_type,
    const HeaderList& headers,
    std::shared_ptr<Transport> transport,
    const SuccessCallback& success_callback,
    const ErrorCallback& error_callback);

// Performs a POST request with form data. Success status, returned data
// and additional information (such as returned HTTP headers) can be obtained
// from the returned Response object. The form data is a list of key/value
// pairs. The data is posed as "application/x-www-form-urlencoded".
BRILLO_EXPORT std::unique_ptr<Response> PostFormDataAndBlock(
    const std::string& url,
    const FormFieldList& data,
    const HeaderList& headers,
    std::shared_ptr<Transport> transport,
    brillo::ErrorPtr* error);

// Async version of PostFormData() above.
// Returns the ID of the request which can be used to cancel the pending
// request using Transport::CancelRequest().
BRILLO_EXPORT RequestID PostFormData(
    const std::string& url,
    const FormFieldList& data,
    const HeaderList& headers,
    std::shared_ptr<Transport> transport,
    const SuccessCallback& success_callback,
    const ErrorCallback& error_callback);

// Performs a POST request with form data, including binary file uploads.
// Success status, returned data and additional information (such as returned
// HTTP headers) can be obtained from the returned Response object.
// The data is posed as "multipart/form-data".
BRILLO_EXPORT std::unique_ptr<Response> PostFormDataAndBlock(
    const std::string& url,
    std::unique_ptr<FormData> form_data,
    const HeaderList& headers,
    std::shared_ptr<Transport> transport,
    brillo::ErrorPtr* error);

// Async version of PostFormData() above.
// Returns the ID of the request which can be used to cancel the pending
// request using Transport::CancelRequest().
BRILLO_EXPORT RequestID PostFormData(
    const std::string& url,
    std::unique_ptr<FormData> form_data,
    const HeaderList& headers,
    std::shared_ptr<Transport> transport,
    const SuccessCallback& success_callback,
    const ErrorCallback& error_callback);

// Performs a POST request with JSON data. Success status, returned data
// and additional information (such as returned HTTP headers) can be obtained
// from the returned Response object. If a JSON response is expected,
// use ParseJsonResponse() method on the returned Response object.
BRILLO_EXPORT std::unique_ptr<Response> PostJsonAndBlock(
    const std::string& url,
    const base::Value* json,
    const HeaderList& headers,
    std::shared_ptr<Transport> transport,
    brillo::ErrorPtr* error);

// Async version of PostJson().
// Returns the ID of the request which can be used to cancel the pending
// request using Transport::CancelRequest().
BRILLO_EXPORT RequestID PostJson(
    const std::string& url,
    std::unique_ptr<base::Value> json,
    const HeaderList& headers,
    std::shared_ptr<Transport> transport,
    const SuccessCallback& success_callback,
    const ErrorCallback& error_callback);

// Performs a PATCH request with JSON data. Success status, returned data
// and additional information (such as returned HTTP headers) can be obtained
// from the returned Response object. If a JSON response is expected,
// use ParseJsonResponse() method on the returned Response object.
BRILLO_EXPORT std::unique_ptr<Response> PatchJsonAndBlock(
    const std::string& url,
    const base::Value* json,
    const HeaderList& headers,
    std::shared_ptr<Transport> transport,
    brillo::ErrorPtr* error);

// Async version of PatchJson().
// Returns the ID of the request which can be used to cancel the pending
// request using Transport::CancelRequest().
BRILLO_EXPORT RequestID PatchJson(
    const std::string& url,
    std::unique_ptr<base::Value> json,
    const HeaderList& headers,
    std::shared_ptr<Transport> transport,
    const SuccessCallback& success_callback,
    const ErrorCallback& error_callback);

// Given an http::Response object, parse the body data into Json object.
// Returns null if failed. Optional |error| can be passed in to
// get the extended error information as to why the parse failed.
BRILLO_EXPORT std::unique_ptr<base::DictionaryValue> ParseJsonResponse(
    Response* response,
    int* status_code,
    brillo::ErrorPtr* error);

// Converts a request header name to canonical form (lowercase with uppercase
// first letter and each letter after a hyphen ('-')).
// "content-TYPE" will be converted to "Content-Type".
BRILLO_EXPORT std::string GetCanonicalHeaderName(const std::string& name);

}  // namespace http
}  // namespace brillo

#endif  // LIBCHROMEOS_BRILLO_HTTP_HTTP_UTILS_H_