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 { |
Alex Vakulenko | 5a9e718 | 2014-08-11 15:59:58 -0700 | [diff] [blame^] | 16 | class Value; |
| 17 | class DictionaryValue; |
Alex Vakulenko | b645cc9 | 2014-04-15 11:34:35 -0700 | [diff] [blame] | 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 | b645cc9 | 2014-04-15 11:34:35 -0700 | [diff] [blame] | 23 | typedef std::vector<std::pair<std::string, std::string>> FormFieldList; |
| 24 | |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 25 | //////////////////////////////////////////////////////////////////////////////// |
| 26 | // The following are simple utility helper functions for common HTTP operations |
| 27 | // that use http::Request object behind the scenes and set it up accordingly. |
| 28 | // |
| 29 | // For more advanced functionality you need to use Request/Response objects |
| 30 | // directly. |
| 31 | //////////////////////////////////////////////////////////////////////////////// |
| 32 | |
Alex Vakulenko | b645cc9 | 2014-04-15 11:34:35 -0700 | [diff] [blame] | 33 | // Performs a generic HTTP request with binary data. Success status, |
| 34 | // returned data and additional information (such as returned HTTP headers) |
| 35 | // can be obtained from the returned Response object. |
| 36 | // If data MIME type is not specified, "application/octet-stream" is assumed. |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 37 | std::unique_ptr<Response> SendRequest( |
| 38 | const char* method, const std::string& url, |
| 39 | const void* data, size_t data_size, const char* mime_type, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 40 | const HeaderList& headers, std::shared_ptr<Transport> transport, |
| 41 | ErrorPtr* error); |
Alex Vakulenko | b645cc9 | 2014-04-15 11:34:35 -0700 | [diff] [blame] | 42 | |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 43 | // Performs a simple GET request and returns the data as a string. |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 44 | std::string GetAsString(const std::string& url, const HeaderList& headers, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 45 | std::shared_ptr<Transport> transport, |
| 46 | ErrorPtr* error); |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 47 | inline std::string GetAsString(const std::string& url, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 48 | std::shared_ptr<Transport> transport, |
| 49 | ErrorPtr* error) { |
| 50 | return GetAsString(url, HeaderList(), transport, error); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 51 | } |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 52 | |
| 53 | // Performs a GET request. Success status, returned data and additional |
| 54 | // information (such as returned HTTP headers) can be obtained from |
| 55 | // the returned Response object. |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 56 | std::unique_ptr<Response> Get(const std::string& url, |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 57 | const HeaderList& headers, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 58 | std::shared_ptr<Transport> transport, |
| 59 | ErrorPtr* error); |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 60 | inline std::unique_ptr<Response> Get( |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 61 | const std::string& url, std::shared_ptr<Transport> transport, |
| 62 | ErrorPtr* error) { |
| 63 | return Get(url, HeaderList(), transport, error); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 64 | } |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 65 | |
| 66 | // Performs a HEAD request. Success status and additional |
| 67 | // information (such as returned HTTP headers) can be obtained from |
| 68 | // the returned Response object. |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 69 | std::unique_ptr<Response> Head(const std::string& url, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 70 | std::shared_ptr<Transport> transport, |
| 71 | ErrorPtr* error); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 72 | |
| 73 | // Performs a POST request with binary data. Success status, returned data |
| 74 | // and additional information (such as returned HTTP headers) can be obtained |
| 75 | // from the returned Response object. |
| 76 | // If data MIME type is not specified, "application/octet-stream" is assumed |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 77 | std::unique_ptr<Response> PostBinary(const std::string& url, |
| 78 | const void* data, |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 79 | size_t data_size, |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 80 | const char* mime_type, |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 81 | const HeaderList& headers, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 82 | std::shared_ptr<Transport> transport, |
| 83 | ErrorPtr* error); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 84 | |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 85 | inline std::unique_ptr<Response> PostBinary( |
| 86 | const std::string& url, const void* data, size_t data_size, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 87 | const char* mime_type, std::shared_ptr<Transport> transport, |
| 88 | ErrorPtr* error) { |
| 89 | return PostBinary(url, data, data_size, mime_type, HeaderList(), transport, |
| 90 | error); |
Alex Vakulenko | b645cc9 | 2014-04-15 11:34:35 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 93 | inline std::unique_ptr<Response> PostBinary( |
| 94 | const std::string& url, const void* data, size_t data_size, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 95 | std::shared_ptr<Transport> transport, ErrorPtr* error) { |
| 96 | return PostBinary(url, data, data_size, nullptr, transport, error); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | // Performs a POST request with text data. Success status, returned data |
| 100 | // and additional information (such as returned HTTP headers) can be obtained |
| 101 | // from the returned Response object. |
| 102 | // If data MIME type is not specified, "application/x-www-form-urlencoded" |
Alex Vakulenko | b645cc9 | 2014-04-15 11:34:35 -0700 | [diff] [blame] | 103 | // is assumed. |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 104 | std::unique_ptr<Response> PostText(const std::string& url, |
| 105 | const char* data, |
| 106 | const char* mime_type, |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 107 | const HeaderList& headers, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 108 | std::shared_ptr<Transport> transport, |
| 109 | ErrorPtr* error); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 110 | |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 111 | inline std::unique_ptr<Response> PostText( |
| 112 | const std::string& url, const char* data, const char* mime_type, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 113 | std::shared_ptr<Transport> transport, ErrorPtr* error) { |
| 114 | return PostText(url, data, mime_type, HeaderList(), transport, error); |
Alex Vakulenko | b645cc9 | 2014-04-15 11:34:35 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 117 | inline std::unique_ptr<Response> PostText( |
| 118 | const std::string& url, const char* data, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 119 | std::shared_ptr<Transport> transport, ErrorPtr* error) { |
| 120 | return PostText(url, data, nullptr, transport, error); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Alex Vakulenko | b645cc9 | 2014-04-15 11:34:35 -0700 | [diff] [blame] | 123 | // Performs a POST request with form data. Success status, returned data |
| 124 | // and additional information (such as returned HTTP headers) can be obtained |
| 125 | // from the returned Response object. The form data is a list of key/value |
| 126 | // pairs. The data is posed as "application/x-www-form-urlencoded". |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 127 | std::unique_ptr<Response> PostFormData( |
| 128 | const std::string& url, const FormFieldList& data, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 129 | const HeaderList& headers, std::shared_ptr<Transport> transport, |
| 130 | ErrorPtr* error); |
Alex Vakulenko | b645cc9 | 2014-04-15 11:34:35 -0700 | [diff] [blame] | 131 | |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 132 | inline std::unique_ptr<Response> PostFormData( |
| 133 | const std::string& url, const FormFieldList& data, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 134 | std::shared_ptr<Transport> transport, ErrorPtr* error) { |
| 135 | return PostFormData(url, data, HeaderList(), transport, error); |
Alex Vakulenko | b645cc9 | 2014-04-15 11:34:35 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 138 | // Performs a POST request with JSON data. Success status, returned data |
| 139 | // and additional information (such as returned HTTP headers) can be obtained |
| 140 | // from the returned Response object. If a JSON response is expected, |
| 141 | // use ParseJsonResponse() method on the returned Response object. |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 142 | std::unique_ptr<Response> PostJson(const std::string& url, |
| 143 | const base::Value* json, |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 144 | const HeaderList& headers, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 145 | std::shared_ptr<Transport> transport, |
| 146 | ErrorPtr* error); |
Alex Vakulenko | b645cc9 | 2014-04-15 11:34:35 -0700 | [diff] [blame] | 147 | |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 148 | inline std::unique_ptr<Response> PostJson( |
| 149 | const std::string& url, const base::Value* json, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 150 | std::shared_ptr<Transport> transport, ErrorPtr* error) { |
| 151 | return PostJson(url, json, HeaderList(), transport, error); |
Alex Vakulenko | b645cc9 | 2014-04-15 11:34:35 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | // Performs a PATCH request with JSON data. Success status, returned data |
| 155 | // and additional information (such as returned HTTP headers) can be obtained |
| 156 | // from the returned Response object. If a JSON response is expected, |
| 157 | // use ParseJsonResponse() method on the returned Response object. |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 158 | std::unique_ptr<Response> PatchJson(const std::string& url, |
| 159 | const base::Value* json, |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 160 | const HeaderList& headers, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 161 | std::shared_ptr<Transport> transport, |
| 162 | ErrorPtr* error); |
Alex Vakulenko | b645cc9 | 2014-04-15 11:34:35 -0700 | [diff] [blame] | 163 | |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 164 | inline std::unique_ptr<Response> PatchJson( |
| 165 | const std::string& url, const base::Value* json, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 166 | std::shared_ptr<Transport> transport, ErrorPtr* error) { |
| 167 | return PatchJson(url, json, HeaderList(), transport, error); |
Alex Vakulenko | b645cc9 | 2014-04-15 11:34:35 -0700 | [diff] [blame] | 168 | } |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 169 | |
| 170 | // Given an http::Response object, parse the body data into Json object. |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 171 | // Returns null if failed. Optional |error| can be passed in to |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 172 | // get the extended error information as to why the parse failed. |
Alex Vakulenko | b645cc9 | 2014-04-15 11:34:35 -0700 | [diff] [blame] | 173 | std::unique_ptr<base::DictionaryValue> ParseJsonResponse( |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 174 | const Response* response, int* status_code, ErrorPtr* error); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 175 | |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 176 | } // namespace http |
Alex Vakulenko | af23b32 | 2014-05-08 16:25:45 -0700 | [diff] [blame] | 177 | } // namespace buffet |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 178 | |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 179 | #endif // BUFFET_HTTP_UTILS_H_ |