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 | #include "buffet/http_utils.h" |
| 6 | |
| 7 | #include <algorithm> |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 8 | |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 9 | #include <base/json/json_reader.h> |
| 10 | #include <base/json/json_writer.h> |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 11 | #include <base/values.h> |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 12 | #include <chromeos/error_codes.h> |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 13 | |
Alex Vakulenko | b645cc9 | 2014-04-15 11:34:35 -0700 | [diff] [blame] | 14 | #include "buffet/data_encoding.h" |
Alex Vakulenko | c30f821 | 2014-07-22 07:27:22 -0700 | [diff] [blame] | 15 | #include "buffet/mime_utils.h" |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 16 | |
Alex Vakulenko | af23b32 | 2014-05-08 16:25:45 -0700 | [diff] [blame] | 17 | namespace buffet { |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 18 | namespace http { |
| 19 | |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 20 | std::unique_ptr<Response> Get(const std::string& url, |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 21 | const HeaderList& headers, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 22 | std::shared_ptr<Transport> transport, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 23 | chromeos::ErrorPtr* error) { |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 24 | return SendRequest(request_type::kGet, url, nullptr, 0, nullptr, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 25 | headers, transport, error); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 26 | } |
| 27 | |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 28 | std::string GetAsString(const std::string& url, |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 29 | const HeaderList& headers, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 30 | std::shared_ptr<Transport> transport, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 31 | chromeos::ErrorPtr* error) { |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 32 | auto resp = Get(url, headers, transport, error); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 33 | return resp ? resp->GetDataAsString() : std::string(); |
| 34 | } |
| 35 | |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 36 | std::unique_ptr<Response> Head(const std::string& url, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 37 | std::shared_ptr<Transport> transport, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 38 | chromeos::ErrorPtr* error) { |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 39 | Request request(url, request_type::kHead, transport); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 40 | return request.GetResponse(error); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 43 | std::unique_ptr<Response> PostText(const std::string& url, |
| 44 | const char* data, |
| 45 | const char* mime_type, |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 46 | const HeaderList& headers, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 47 | std::shared_ptr<Transport> transport, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 48 | chromeos::ErrorPtr* error) { |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 49 | if (mime_type == nullptr) { |
Alex Vakulenko | af23b32 | 2014-05-08 16:25:45 -0700 | [diff] [blame] | 50 | mime_type = mime::application::kWwwFormUrlEncoded; |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 53 | return PostBinary(url, data, strlen(data), mime_type, headers, transport, |
| 54 | error); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 57 | std::unique_ptr<Response> SendRequest(const char * method, |
| 58 | const std::string& url, |
| 59 | const void* data, |
Alex Vakulenko | b645cc9 | 2014-04-15 11:34:35 -0700 | [diff] [blame] | 60 | size_t data_size, |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 61 | const char* mime_type, |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 62 | const HeaderList& headers, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 63 | std::shared_ptr<Transport> transport, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 64 | chromeos::ErrorPtr* error) { |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 65 | Request request(url, method, transport); |
Alex Vakulenko | b645cc9 | 2014-04-15 11:34:35 -0700 | [diff] [blame] | 66 | request.AddHeaders(headers); |
| 67 | if (data_size > 0) { |
| 68 | if (mime_type == nullptr) { |
Alex Vakulenko | af23b32 | 2014-05-08 16:25:45 -0700 | [diff] [blame] | 69 | mime_type = mime::application::kOctet_stream; |
Alex Vakulenko | b645cc9 | 2014-04-15 11:34:35 -0700 | [diff] [blame] | 70 | } |
| 71 | request.SetContentType(mime_type); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 72 | if (!request.AddRequestBody(data, data_size, error)) |
| 73 | return std::unique_ptr<Response>(); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 74 | } |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 75 | return request.GetResponse(error); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 78 | std::unique_ptr<Response> PostBinary(const std::string & url, const void* data, |
| 79 | size_t data_size, const char* mime_type, |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 80 | const HeaderList& headers, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 81 | std::shared_ptr<Transport> transport, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 82 | chromeos::ErrorPtr* error) { |
Alex Vakulenko | b645cc9 | 2014-04-15 11:34:35 -0700 | [diff] [blame] | 83 | return SendRequest(request_type::kPost, url, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 84 | data, data_size, mime_type, headers, transport, error); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 87 | std::unique_ptr<Response> PostFormData(const std::string& url, |
| 88 | const FormFieldList& data, |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 89 | const HeaderList& headers, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 90 | std::shared_ptr<Transport> transport, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 91 | chromeos::ErrorPtr* error) { |
Alex Vakulenko | af23b32 | 2014-05-08 16:25:45 -0700 | [diff] [blame] | 92 | std::string encoded_data = data_encoding::WebParamsEncode(data); |
Alex Vakulenko | b645cc9 | 2014-04-15 11:34:35 -0700 | [diff] [blame] | 93 | return PostBinary(url, encoded_data.c_str(), encoded_data.size(), |
Alex Vakulenko | af23b32 | 2014-05-08 16:25:45 -0700 | [diff] [blame] | 94 | mime::application::kWwwFormUrlEncoded, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 95 | headers, transport, error); |
Alex Vakulenko | b645cc9 | 2014-04-15 11:34:35 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 99 | std::unique_ptr<Response> PostJson(const std::string& url, |
| 100 | const base::Value* json, |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 101 | const HeaderList& headers, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 102 | std::shared_ptr<Transport> transport, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 103 | chromeos::ErrorPtr* error) { |
Alex Vakulenko | b645cc9 | 2014-04-15 11:34:35 -0700 | [diff] [blame] | 104 | std::string data; |
| 105 | if (json) |
| 106 | base::JSONWriter::Write(json, &data); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 107 | std::string mime_type = mime::AppendParameter(mime::application::kJson, |
| 108 | mime::parameters::kCharset, |
| 109 | "utf-8"); |
Alex Vakulenko | b645cc9 | 2014-04-15 11:34:35 -0700 | [diff] [blame] | 110 | return PostBinary(url, data.c_str(), data.size(), |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 111 | mime_type.c_str(), headers, transport, error); |
Alex Vakulenko | b645cc9 | 2014-04-15 11:34:35 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 114 | std::unique_ptr<Response> PatchJson(const std::string& url, |
| 115 | const base::Value* json, |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 116 | const HeaderList& headers, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 117 | std::shared_ptr<Transport> transport, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 118 | chromeos::ErrorPtr* error) { |
Alex Vakulenko | b645cc9 | 2014-04-15 11:34:35 -0700 | [diff] [blame] | 119 | std::string data; |
| 120 | if (json) |
| 121 | base::JSONWriter::Write(json, &data); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 122 | std::string mime_type = mime::AppendParameter(mime::application::kJson, |
| 123 | mime::parameters::kCharset, |
| 124 | "utf-8"); |
Alex Vakulenko | b645cc9 | 2014-04-15 11:34:35 -0700 | [diff] [blame] | 125 | return SendRequest(request_type::kPatch, url, data.c_str(), data.size(), |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 126 | mime_type.c_str(), headers, transport, error); |
Alex Vakulenko | b645cc9 | 2014-04-15 11:34:35 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | std::unique_ptr<base::DictionaryValue> ParseJsonResponse( |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 130 | const Response* response, int* status_code, chromeos::ErrorPtr* error) { |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 131 | if (!response) |
| 132 | return std::unique_ptr<base::DictionaryValue>(); |
Alex Vakulenko | b645cc9 | 2014-04-15 11:34:35 -0700 | [diff] [blame] | 133 | |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 134 | if (status_code) |
| 135 | *status_code = response->GetStatusCode(); |
| 136 | |
| 137 | // Make sure we have a correct content type. Do not try to parse |
| 138 | // binary files, or HTML output. Limit to application/json and text/plain. |
| 139 | auto content_type = mime::RemoveParameters(response->GetContentType()); |
| 140 | if (content_type != mime::application::kJson && |
| 141 | content_type != mime::text::kPlain) { |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 142 | chromeos::Error::AddTo(error, chromeos::errors::json::kDomain, |
| 143 | "non_json_content_type", |
| 144 | "Unexpected response content type: " + content_type); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 145 | return std::unique_ptr<base::DictionaryValue>(); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 146 | } |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 147 | |
| 148 | std::string json = response->GetDataAsString(); |
| 149 | std::string error_message; |
| 150 | base::Value* value = base::JSONReader::ReadAndReturnError( |
| 151 | json, base::JSON_PARSE_RFC, nullptr, &error_message); |
| 152 | if (!value) { |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 153 | chromeos::Error::AddTo(error, chromeos::errors::json::kDomain, |
| 154 | chromeos::errors::json::kParseError, error_message); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 155 | return std::unique_ptr<base::DictionaryValue>(); |
| 156 | } |
| 157 | base::DictionaryValue* dict_value = nullptr; |
| 158 | if (!value->GetAsDictionary(&dict_value)) { |
| 159 | delete value; |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 160 | chromeos::Error::AddTo(error, chromeos::errors::json::kDomain, |
| 161 | chromeos::errors::json::kObjectExpected, |
| 162 | "Response is not a valid JSON object"); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 163 | return std::unique_ptr<base::DictionaryValue>(); |
| 164 | } |
| 165 | return std::unique_ptr<base::DictionaryValue>(dict_value); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 168 | } // namespace http |
Alex Vakulenko | af23b32 | 2014-05-08 16:25:45 -0700 | [diff] [blame] | 169 | } // namespace buffet |