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