blob: 3924505748b9ab0505221317d0789021b57b2216 [file] [log] [blame]
// Copyright 2014 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "buffet/http_utils.h"
#include <algorithm>
#include <base/json/json_reader.h>
#include <base/json/json_writer.h>
#include <base/values.h>
#include "buffet/data_encoding.h"
#include "buffet/error_codes.h"
#include "buffet/mime_utils.h"
namespace buffet {
namespace http {
std::unique_ptr<Response> Get(const std::string& url,
const HeaderList& headers,
std::shared_ptr<Transport> transport,
ErrorPtr* error) {
return SendRequest(request_type::kGet, url, nullptr, 0, nullptr,
headers, transport, error);
}
std::string GetAsString(const std::string& url,
const HeaderList& headers,
std::shared_ptr<Transport> transport,
ErrorPtr* error) {
auto resp = Get(url, headers, transport, error);
return resp ? resp->GetDataAsString() : std::string();
}
std::unique_ptr<Response> Head(const std::string& url,
std::shared_ptr<Transport> transport,
ErrorPtr* error) {
Request request(url, request_type::kHead, transport);
return request.GetResponse(error);
}
std::unique_ptr<Response> PostText(const std::string& url,
const char* data,
const char* mime_type,
const HeaderList& headers,
std::shared_ptr<Transport> transport,
ErrorPtr* error) {
if (mime_type == nullptr) {
mime_type = mime::application::kWwwFormUrlEncoded;
}
return PostBinary(url, data, strlen(data), mime_type, headers, transport,
error);
}
std::unique_ptr<Response> SendRequest(const char * method,
const std::string& url,
const void* data,
size_t data_size,
const char* mime_type,
const HeaderList& headers,
std::shared_ptr<Transport> transport,
ErrorPtr* error) {
Request request(url, method, transport);
request.AddHeaders(headers);
if (data_size > 0) {
if (mime_type == nullptr) {
mime_type = mime::application::kOctet_stream;
}
request.SetContentType(mime_type);
if (!request.AddRequestBody(data, data_size, error))
return std::unique_ptr<Response>();
}
return request.GetResponse(error);
}
std::unique_ptr<Response> PostBinary(const std::string & url, const void* data,
size_t data_size, const char* mime_type,
const HeaderList& headers,
std::shared_ptr<Transport> transport,
ErrorPtr* error) {
return SendRequest(request_type::kPost, url,
data, data_size, mime_type, headers, transport, error);
}
std::unique_ptr<Response> PostFormData(const std::string& url,
const FormFieldList& data,
const HeaderList& headers,
std::shared_ptr<Transport> transport,
ErrorPtr* error) {
std::string encoded_data = data_encoding::WebParamsEncode(data);
return PostBinary(url, encoded_data.c_str(), encoded_data.size(),
mime::application::kWwwFormUrlEncoded,
headers, transport, error);
}
std::unique_ptr<Response> PostJson(const std::string& url,
const base::Value* json,
const HeaderList& headers,
std::shared_ptr<Transport> transport,
ErrorPtr* error) {
std::string data;
if (json)
base::JSONWriter::Write(json, &data);
std::string mime_type = mime::AppendParameter(mime::application::kJson,
mime::parameters::kCharset,
"utf-8");
return PostBinary(url, data.c_str(), data.size(),
mime_type.c_str(), headers, transport, error);
}
std::unique_ptr<Response> PatchJson(const std::string& url,
const base::Value* json,
const HeaderList& headers,
std::shared_ptr<Transport> transport,
ErrorPtr* error) {
std::string data;
if (json)
base::JSONWriter::Write(json, &data);
std::string mime_type = mime::AppendParameter(mime::application::kJson,
mime::parameters::kCharset,
"utf-8");
return SendRequest(request_type::kPatch, url, data.c_str(), data.size(),
mime_type.c_str(), headers, transport, error);
}
std::unique_ptr<base::DictionaryValue> ParseJsonResponse(
const Response* response, int* status_code, ErrorPtr* error) {
if (!response)
return std::unique_ptr<base::DictionaryValue>();
if (status_code)
*status_code = response->GetStatusCode();
// Make sure we have a correct content type. Do not try to parse
// binary files, or HTML output. Limit to application/json and text/plain.
auto content_type = mime::RemoveParameters(response->GetContentType());
if (content_type != mime::application::kJson &&
content_type != mime::text::kPlain) {
Error::AddTo(error, errors::json::kDomain, "non_json_content_type",
"Unexpected response content type: " + content_type);
return std::unique_ptr<base::DictionaryValue>();
}
std::string json = response->GetDataAsString();
std::string error_message;
base::Value* value = base::JSONReader::ReadAndReturnError(
json, base::JSON_PARSE_RFC, nullptr, &error_message);
if (!value) {
Error::AddTo(error, errors::json::kDomain, errors::json::kParseError,
error_message);
return std::unique_ptr<base::DictionaryValue>();
}
base::DictionaryValue* dict_value = nullptr;
if (!value->GetAsDictionary(&dict_value)) {
delete value;
Error::AddTo(error, errors::json::kDomain, errors::json::kObjectExpected,
"Response is not a valid JSON object");
return std::unique_ptr<base::DictionaryValue>();
}
return std::unique_ptr<base::DictionaryValue>(dict_value);
}
} // namespace http
} // namespace buffet