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