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