Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 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 BUFFET_HTTP_UTILS_H_ |
| 6 | #define BUFFET_HTTP_UTILS_H_ |
| 7 | |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 8 | #include "buffet/error.h" |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 9 | #include "buffet/http_request.h" |
| 10 | |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 11 | #include <string> |
| 12 | #include <utility> |
| 13 | #include <vector> |
| 14 | |
Alex Vakulenko | b645cc9 | 2014-04-15 11:34:35 -0700 | [diff] [blame] | 15 | namespace base { |
| 16 | class Value; |
| 17 | class DictionaryValue; |
| 18 | } // namespace base |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 19 | |
Alex Vakulenko | af23b32 | 2014-05-08 16:25:45 -0700 | [diff] [blame] | 20 | namespace buffet { |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 21 | namespace http { |
| 22 | |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 23 | extern const char kErrorDomainJSON[]; |
| 24 | |
Alex Vakulenko | b645cc9 | 2014-04-15 11:34:35 -0700 | [diff] [blame] | 25 | typedef std::vector<std::pair<std::string, std::string>> FormFieldList; |
| 26 | |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 27 | //////////////////////////////////////////////////////////////////////////////// |
| 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 Vakulenko | b645cc9 | 2014-04-15 11:34:35 -0700 | [diff] [blame] | 35 | // 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 Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 39 | std::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 Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 42 | const HeaderList& headers, std::shared_ptr<Transport> transport, |
| 43 | ErrorPtr* error); |
Alex Vakulenko | b645cc9 | 2014-04-15 11:34:35 -0700 | [diff] [blame] | 44 | |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 45 | // Performs a simple GET request and returns the data as a string. |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 46 | std::string GetAsString(const std::string& url, const HeaderList& headers, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 47 | std::shared_ptr<Transport> transport, |
| 48 | ErrorPtr* error); |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 49 | inline std::string GetAsString(const std::string& url, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 50 | std::shared_ptr<Transport> transport, |
| 51 | ErrorPtr* error) { |
| 52 | return GetAsString(url, HeaderList(), transport, error); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 53 | } |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 54 | |
| 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 Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 58 | std::unique_ptr<Response> Get(const std::string& url, |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 59 | const HeaderList& headers, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 60 | std::shared_ptr<Transport> transport, |
| 61 | ErrorPtr* error); |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 62 | inline std::unique_ptr<Response> Get( |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 63 | const std::string& url, std::shared_ptr<Transport> transport, |
| 64 | ErrorPtr* error) { |
| 65 | return Get(url, HeaderList(), transport, error); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 66 | } |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 67 | |
| 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 Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 71 | std::unique_ptr<Response> Head(const std::string& url, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 72 | std::shared_ptr<Transport> transport, |
| 73 | ErrorPtr* error); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 74 | |
| 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 Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 79 | std::unique_ptr<Response> PostBinary(const std::string& url, |
| 80 | const void* data, |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 81 | size_t data_size, |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 82 | const char* mime_type, |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 83 | const HeaderList& headers, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 84 | std::shared_ptr<Transport> transport, |
| 85 | ErrorPtr* error); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 86 | |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 87 | inline std::unique_ptr<Response> PostBinary( |
| 88 | const std::string& url, const void* data, size_t data_size, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 89 | 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 Vakulenko | b645cc9 | 2014-04-15 11:34:35 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 95 | inline std::unique_ptr<Response> PostBinary( |
| 96 | const std::string& url, const void* data, size_t data_size, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 97 | std::shared_ptr<Transport> transport, ErrorPtr* error) { |
| 98 | return PostBinary(url, data, data_size, nullptr, transport, error); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 99 | } |
| 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 Vakulenko | b645cc9 | 2014-04-15 11:34:35 -0700 | [diff] [blame] | 105 | // is assumed. |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 106 | std::unique_ptr<Response> PostText(const std::string& url, |
| 107 | const char* data, |
| 108 | const char* mime_type, |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 109 | const HeaderList& headers, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 110 | std::shared_ptr<Transport> transport, |
| 111 | ErrorPtr* error); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 112 | |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 113 | inline std::unique_ptr<Response> PostText( |
| 114 | const std::string& url, const char* data, const char* mime_type, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 115 | std::shared_ptr<Transport> transport, ErrorPtr* error) { |
| 116 | return PostText(url, data, mime_type, HeaderList(), transport, error); |
Alex Vakulenko | b645cc9 | 2014-04-15 11:34:35 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 119 | inline std::unique_ptr<Response> PostText( |
| 120 | const std::string& url, const char* data, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 121 | std::shared_ptr<Transport> transport, ErrorPtr* error) { |
| 122 | return PostText(url, data, nullptr, transport, error); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Alex Vakulenko | b645cc9 | 2014-04-15 11:34:35 -0700 | [diff] [blame] | 125 | // 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 Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 129 | std::unique_ptr<Response> PostFormData( |
| 130 | const std::string& url, const FormFieldList& data, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 131 | const HeaderList& headers, std::shared_ptr<Transport> transport, |
| 132 | ErrorPtr* error); |
Alex Vakulenko | b645cc9 | 2014-04-15 11:34:35 -0700 | [diff] [blame] | 133 | |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 134 | inline std::unique_ptr<Response> PostFormData( |
| 135 | const std::string& url, const FormFieldList& data, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 136 | std::shared_ptr<Transport> transport, ErrorPtr* error) { |
| 137 | return PostFormData(url, data, HeaderList(), transport, error); |
Alex Vakulenko | b645cc9 | 2014-04-15 11:34:35 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 140 | // 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 Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 144 | std::unique_ptr<Response> PostJson(const std::string& url, |
| 145 | const base::Value* json, |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 146 | const HeaderList& headers, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 147 | std::shared_ptr<Transport> transport, |
| 148 | ErrorPtr* error); |
Alex Vakulenko | b645cc9 | 2014-04-15 11:34:35 -0700 | [diff] [blame] | 149 | |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 150 | inline std::unique_ptr<Response> PostJson( |
| 151 | const std::string& url, const base::Value* json, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 152 | std::shared_ptr<Transport> transport, ErrorPtr* error) { |
| 153 | return PostJson(url, json, HeaderList(), transport, error); |
Alex Vakulenko | b645cc9 | 2014-04-15 11:34:35 -0700 | [diff] [blame] | 154 | } |
| 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 Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 160 | std::unique_ptr<Response> PatchJson(const std::string& url, |
| 161 | const base::Value* json, |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 162 | const HeaderList& headers, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 163 | std::shared_ptr<Transport> transport, |
| 164 | ErrorPtr* error); |
Alex Vakulenko | b645cc9 | 2014-04-15 11:34:35 -0700 | [diff] [blame] | 165 | |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 166 | inline std::unique_ptr<Response> PatchJson( |
| 167 | const std::string& url, const base::Value* json, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 168 | std::shared_ptr<Transport> transport, ErrorPtr* error) { |
| 169 | return PatchJson(url, json, HeaderList(), transport, error); |
Alex Vakulenko | b645cc9 | 2014-04-15 11:34:35 -0700 | [diff] [blame] | 170 | } |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 171 | |
| 172 | // Given an http::Response object, parse the body data into Json object. |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 173 | // Returns null if failed. Optional |error| can be passed in to |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 174 | // get the extended error information as to why the parse failed. |
Alex Vakulenko | b645cc9 | 2014-04-15 11:34:35 -0700 | [diff] [blame] | 175 | std::unique_ptr<base::DictionaryValue> ParseJsonResponse( |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 176 | const Response* response, int* status_code, ErrorPtr* error); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 177 | |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 178 | } // namespace http |
Alex Vakulenko | af23b32 | 2014-05-08 16:25:45 -0700 | [diff] [blame] | 179 | } // namespace buffet |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 180 | |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 181 | #endif // BUFFET_HTTP_UTILS_H_ |