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