1 // 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 LIBBRILLO_BRILLO_HTTP_HTTP_UTILS_H_ 6 #define LIBBRILLO_BRILLO_HTTP_HTTP_UTILS_H_ 7 8 #include <memory> 9 #include <string> 10 #include <utility> 11 #include <vector> 12 13 #include <brillo/brillo_export.h> 14 #include <brillo/errors/error.h> 15 #include <brillo/http/http_form_data.h> 16 #include <brillo/http/http_request.h> 17 18 namespace base { 19 class Value; 20 class DictionaryValue; 21 } // namespace base 22 23 namespace brillo { 24 namespace http { 25 26 using FormFieldList = std::vector<std::pair<std::string, std::string>>; 27 28 //////////////////////////////////////////////////////////////////////////////// 29 // The following are simple utility helper functions for common HTTP operations 30 // that use http::Request object behind the scenes and set it up accordingly. 31 // The values for request method, data MIME type, request header names should 32 // not be directly encoded in most cases, but use predefined constants from 33 // http_request.h. 34 // So, instead of calling: 35 // SendRequestAndBlock("POST", 36 // "http://url", 37 // "data", 4, 38 // "text/plain", 39 // {{"Authorization", "Bearer TOKEN"}}, 40 // transport, error); 41 // You should do use this instead: 42 // SendRequestAndBlock(brillo::http::request_type::kPost, 43 // "http://url", 44 // "data", 4, 45 // brillo::mime::text::kPlain, 46 // {{brillo::http::request_header::kAuthorization, 47 // "Bearer TOKEN"}}, 48 // transport, error); 49 // 50 // For more advanced functionality you need to use Request/Response objects 51 // directly. 52 //////////////////////////////////////////////////////////////////////////////// 53 54 // Performs a generic HTTP request with binary data. Success status, 55 // returned data and additional information (such as returned HTTP headers) 56 // can be obtained from the returned Response object. 57 BRILLO_EXPORT std::unique_ptr<Response> SendRequestAndBlock( 58 const std::string& method, 59 const std::string& url, 60 const void* data, 61 size_t data_size, 62 const std::string& mime_type, 63 const HeaderList& headers, 64 std::shared_ptr<Transport> transport, 65 brillo::ErrorPtr* error); 66 67 // Same as above, but without sending the request body. 68 // This is especially useful for requests like "GET" and "HEAD". 69 BRILLO_EXPORT std::unique_ptr<Response> SendRequestWithNoDataAndBlock( 70 const std::string& method, 71 const std::string& url, 72 const HeaderList& headers, 73 std::shared_ptr<Transport> transport, 74 brillo::ErrorPtr* error); 75 76 // Same as above but asynchronous. On success, |success_callback| is called 77 // with the response object. On failure, |error_callback| is called with the 78 // error details. 79 // Returns the ID of the request which can be used to cancel the pending 80 // request using Transport::CancelRequest(). 81 BRILLO_EXPORT RequestID SendRequest( 82 const std::string& method, 83 const std::string& url, 84 StreamPtr stream, 85 const std::string& mime_type, 86 const HeaderList& headers, 87 std::shared_ptr<Transport> transport, 88 const SuccessCallback& success_callback, 89 const ErrorCallback& error_callback); 90 91 // Same as above, but takes a memory buffer. The pointer should be valid only 92 // until the function returns. The data is copied into an internal buffer to be 93 // available for the duration of the asynchronous operation. 94 // Returns the ID of the request which can be used to cancel the pending 95 // request using Transport::CancelRequest(). 96 BRILLO_EXPORT RequestID SendRequest( 97 const std::string& method, 98 const std::string& url, 99 const void* data, 100 size_t data_size, 101 const std::string& mime_type, 102 const HeaderList& headers, 103 std::shared_ptr<Transport> transport, 104 const SuccessCallback& success_callback, 105 const ErrorCallback& error_callback); 106 107 // Asynchronous version of SendRequestNoData(). 108 // Returns the ID of the request which can be used to cancel the pending 109 // request using Transport::CancelRequest(). 110 BRILLO_EXPORT RequestID SendRequestWithNoData( 111 const std::string& method, 112 const std::string& url, 113 const HeaderList& headers, 114 std::shared_ptr<Transport> transport, 115 const SuccessCallback& success_callback, 116 const ErrorCallback& error_callback); 117 118 // Performs a GET request. Success status, returned data and additional 119 // information (such as returned HTTP headers) can be obtained from 120 // the returned Response object. 121 BRILLO_EXPORT std::unique_ptr<Response> GetAndBlock( 122 const std::string& url, 123 const HeaderList& headers, 124 std::shared_ptr<Transport> transport, 125 brillo::ErrorPtr* error); 126 127 // Asynchronous version of http::Get(). 128 // Returns the ID of the request which can be used to cancel the pending 129 // request using Transport::CancelRequest(). 130 BRILLO_EXPORT RequestID Get( 131 const std::string& url, 132 const HeaderList& headers, 133 std::shared_ptr<Transport> transport, 134 const SuccessCallback& success_callback, 135 const ErrorCallback& error_callback); 136 137 // Performs a HEAD request. Success status and additional 138 // information (such as returned HTTP headers) can be obtained from 139 // the returned Response object. 140 BRILLO_EXPORT std::unique_ptr<Response> HeadAndBlock( 141 const std::string& url, 142 std::shared_ptr<Transport> transport, 143 brillo::ErrorPtr* error); 144 145 // Performs an asynchronous HEAD request. 146 // Returns the ID of the request which can be used to cancel the pending 147 // request using Transport::CancelRequest(). 148 BRILLO_EXPORT RequestID Head( 149 const std::string& url, 150 std::shared_ptr<Transport> transport, 151 const SuccessCallback& success_callback, 152 const ErrorCallback& error_callback); 153 154 // Performs a POST request with binary data. Success status, returned data 155 // and additional information (such as returned HTTP headers) can be obtained 156 // from the returned Response object. 157 BRILLO_EXPORT std::unique_ptr<Response> PostBinaryAndBlock( 158 const std::string& url, 159 const void* data, 160 size_t data_size, 161 const std::string& mime_type, 162 const HeaderList& headers, 163 std::shared_ptr<Transport> transport, 164 brillo::ErrorPtr* error); 165 166 // Async version of PostBinary(). 167 // Returns the ID of the request which can be used to cancel the pending 168 // request using Transport::CancelRequest(). 169 BRILLO_EXPORT RequestID PostBinary( 170 const std::string& url, 171 StreamPtr stream, 172 const std::string& mime_type, 173 const HeaderList& headers, 174 std::shared_ptr<Transport> transport, 175 const SuccessCallback& success_callback, 176 const ErrorCallback& error_callback); 177 178 // Same as above, but takes a memory buffer. The pointer should be valid only 179 // until the function returns. The data is copied into an internal buffer 180 // to be available for the duration of the asynchronous operation. 181 // Returns the ID of the request which can be used to cancel the pending 182 // request using Transport::CancelRequest(). 183 BRILLO_EXPORT RequestID PostBinary( 184 const std::string& url, 185 const void* data, 186 size_t data_size, 187 const std::string& mime_type, 188 const HeaderList& headers, 189 std::shared_ptr<Transport> transport, 190 const SuccessCallback& success_callback, 191 const ErrorCallback& error_callback); 192 193 // Performs a POST request with text data. Success status, returned data 194 // and additional information (such as returned HTTP headers) can be obtained 195 // from the returned Response object. 196 BRILLO_EXPORT std::unique_ptr<Response> PostTextAndBlock( 197 const std::string& url, 198 const std::string& data, 199 const std::string& mime_type, 200 const HeaderList& headers, 201 std::shared_ptr<Transport> transport, 202 brillo::ErrorPtr* error); 203 204 // Async version of PostText(). 205 // Returns the ID of the request which can be used to cancel the pending 206 // request using Transport::CancelRequest(). 207 BRILLO_EXPORT RequestID PostText( 208 const std::string& url, 209 const std::string& data, 210 const std::string& mime_type, 211 const HeaderList& headers, 212 std::shared_ptr<Transport> transport, 213 const SuccessCallback& success_callback, 214 const ErrorCallback& error_callback); 215 216 // Performs a POST request with form data. Success status, returned data 217 // and additional information (such as returned HTTP headers) can be obtained 218 // from the returned Response object. The form data is a list of key/value 219 // pairs. The data is posed as "application/x-www-form-urlencoded". 220 BRILLO_EXPORT std::unique_ptr<Response> PostFormDataAndBlock( 221 const std::string& url, 222 const FormFieldList& data, 223 const HeaderList& headers, 224 std::shared_ptr<Transport> transport, 225 brillo::ErrorPtr* error); 226 227 // Async version of PostFormData() above. 228 // Returns the ID of the request which can be used to cancel the pending 229 // request using Transport::CancelRequest(). 230 BRILLO_EXPORT RequestID PostFormData( 231 const std::string& url, 232 const FormFieldList& data, 233 const HeaderList& headers, 234 std::shared_ptr<Transport> transport, 235 const SuccessCallback& success_callback, 236 const ErrorCallback& error_callback); 237 238 // Performs a POST request with form data, including binary file uploads. 239 // Success status, returned data and additional information (such as returned 240 // HTTP headers) can be obtained from the returned Response object. 241 // The data is posed as "multipart/form-data". 242 BRILLO_EXPORT std::unique_ptr<Response> PostFormDataAndBlock( 243 const std::string& url, 244 std::unique_ptr<FormData> form_data, 245 const HeaderList& headers, 246 std::shared_ptr<Transport> transport, 247 brillo::ErrorPtr* error); 248 249 // Async version of PostFormData() above. 250 // Returns the ID of the request which can be used to cancel the pending 251 // request using Transport::CancelRequest(). 252 BRILLO_EXPORT RequestID PostFormData( 253 const std::string& url, 254 std::unique_ptr<FormData> form_data, 255 const HeaderList& headers, 256 std::shared_ptr<Transport> transport, 257 const SuccessCallback& success_callback, 258 const ErrorCallback& error_callback); 259 260 // Performs a POST request with JSON data. Success status, returned data 261 // and additional information (such as returned HTTP headers) can be obtained 262 // from the returned Response object. If a JSON response is expected, 263 // use ParseJsonResponse() method on the returned Response object. 264 BRILLO_EXPORT std::unique_ptr<Response> PostJsonAndBlock( 265 const std::string& url, 266 const base::Value* json, 267 const HeaderList& headers, 268 std::shared_ptr<Transport> transport, 269 brillo::ErrorPtr* error); 270 271 // Async version of PostJson(). 272 // Returns the ID of the request which can be used to cancel the pending 273 // request using Transport::CancelRequest(). 274 BRILLO_EXPORT RequestID PostJson( 275 const std::string& url, 276 std::unique_ptr<base::Value> json, 277 const HeaderList& headers, 278 std::shared_ptr<Transport> transport, 279 const SuccessCallback& success_callback, 280 const ErrorCallback& error_callback); 281 282 // Performs a PATCH request with JSON data. Success status, returned data 283 // and additional information (such as returned HTTP headers) can be obtained 284 // from the returned Response object. If a JSON response is expected, 285 // use ParseJsonResponse() method on the returned Response object. 286 BRILLO_EXPORT std::unique_ptr<Response> PatchJsonAndBlock( 287 const std::string& url, 288 const base::Value* json, 289 const HeaderList& headers, 290 std::shared_ptr<Transport> transport, 291 brillo::ErrorPtr* error); 292 293 // Async version of PatchJson(). 294 // Returns the ID of the request which can be used to cancel the pending 295 // request using Transport::CancelRequest(). 296 BRILLO_EXPORT RequestID PatchJson( 297 const std::string& url, 298 std::unique_ptr<base::Value> json, 299 const HeaderList& headers, 300 std::shared_ptr<Transport> transport, 301 const SuccessCallback& success_callback, 302 const ErrorCallback& error_callback); 303 304 // Given an http::Response object, parse the body data into Json object. 305 // Returns null if failed. Optional |error| can be passed in to 306 // get the extended error information as to why the parse failed. 307 BRILLO_EXPORT std::unique_ptr<base::DictionaryValue> ParseJsonResponse( 308 Response* response, 309 int* status_code, 310 brillo::ErrorPtr* error); 311 312 // Converts a request header name to canonical form (lowercase with uppercase 313 // first letter and each letter after a hyphen ('-')). 314 // "content-TYPE" will be converted to "Content-Type". 315 BRILLO_EXPORT std::string GetCanonicalHeaderName(const std::string& name); 316 317 } // namespace http 318 } // namespace brillo 319 320 #endif // LIBBRILLO_BRILLO_HTTP_HTTP_UTILS_H_ 321