blob: da54b509501535dd33ba4d1846fb3af7c1d0bb0c [file] [log] [blame]
Vitaly Bukacad20f02015-10-16 17:27:15 -07001// Copyright 2015 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
Vitaly Buka00882b72015-08-06 00:32:11 -070014
15#include "buffet/http_transport_client.h"
16
17#include <base/bind.h>
Alex Vakulenko41705852015-10-13 10:12:06 -070018#include <brillo/errors/error.h>
19#include <brillo/http/http_request.h>
20#include <brillo/http/http_utils.h>
21#include <brillo/streams/memory_stream.h>
Alex Vakulenko297114c2015-10-12 13:45:43 -070022#include <weave/enum_to_string.h>
Vitaly Buka00882b72015-08-06 00:32:11 -070023
Vitaly Buka4f771532015-08-14 14:58:39 -070024#include "buffet/weave_error_conversion.h"
25
Vitaly Buka00882b72015-08-06 00:32:11 -070026namespace buffet {
27
28namespace {
29
Alex Vakulenkoe32375b2015-09-28 08:55:40 -070030using weave::provider::HttpClient;
31
Vitaly Buka00882b72015-08-06 00:32:11 -070032// The number of seconds each HTTP request will be allowed before timing out.
33const int kRequestTimeoutSeconds = 30;
34
Alex Vakulenkoe32375b2015-09-28 08:55:40 -070035class ResponseImpl : public HttpClient::Response {
Vitaly Buka00882b72015-08-06 00:32:11 -070036 public:
37 ~ResponseImpl() override = default;
Alex Vakulenko41705852015-10-13 10:12:06 -070038 explicit ResponseImpl(std::unique_ptr<brillo::http::Response> response)
Vitaly Buka00882b72015-08-06 00:32:11 -070039 : response_{std::move(response)},
40 data_{response_->ExtractDataAsString()} {}
41
Alex Vakulenkoe32375b2015-09-28 08:55:40 -070042 // HttpClient::Response implementation
Vitaly Buka00882b72015-08-06 00:32:11 -070043 int GetStatusCode() const override { return response_->GetStatusCode(); }
44
45 std::string GetContentType() const override {
46 return response_->GetContentType();
47 }
48
Alex Vakulenko297114c2015-10-12 13:45:43 -070049 std::string GetData() const override { return data_; }
Vitaly Buka00882b72015-08-06 00:32:11 -070050
51 private:
Alex Vakulenko41705852015-10-13 10:12:06 -070052 std::unique_ptr<brillo::http::Response> response_;
Vitaly Buka00882b72015-08-06 00:32:11 -070053 std::string data_;
54 DISALLOW_COPY_AND_ASSIGN(ResponseImpl);
55};
56
Alex Vakulenko94f8eba2015-10-14 08:52:45 -070057void OnSuccessCallback(const HttpClient::SendRequestCallback& callback,
Alex Vakulenkoe32375b2015-09-28 08:55:40 -070058 int id,
Alex Vakulenko41705852015-10-13 10:12:06 -070059 std::unique_ptr<brillo::http::Response> response) {
Alex Vakulenko94f8eba2015-10-14 08:52:45 -070060 callback.Run(std::unique_ptr<HttpClient::Response>{new ResponseImpl{
61 std::move(response)}},
62 nullptr);
Vitaly Buka00882b72015-08-06 00:32:11 -070063}
64
Alex Vakulenko94f8eba2015-10-14 08:52:45 -070065void OnErrorCallback(const HttpClient::SendRequestCallback& callback,
Vitaly Buka00882b72015-08-06 00:32:11 -070066 int id,
Alex Vakulenko94f8eba2015-10-14 08:52:45 -070067 const brillo::Error* brillo_error) {
Vitaly Buka4f771532015-08-14 14:58:39 -070068 weave::ErrorPtr error;
Alex Vakulenko94f8eba2015-10-14 08:52:45 -070069 ConvertError(*brillo_error, &error);
70 callback.Run(nullptr, std::move(error));
Vitaly Buka00882b72015-08-06 00:32:11 -070071}
72
73} // anonymous namespace
74
75HttpTransportClient::HttpTransportClient()
Alex Vakulenko41705852015-10-13 10:12:06 -070076 : transport_{brillo::http::Transport::CreateDefault()} {
Vitaly Buka00882b72015-08-06 00:32:11 -070077 transport_->SetDefaultTimeout(
78 base::TimeDelta::FromSeconds(kRequestTimeoutSeconds));
79}
80
81HttpTransportClient::~HttpTransportClient() {}
82
Alex Vakulenko94f8eba2015-10-14 08:52:45 -070083void HttpTransportClient::SendRequest(Method method,
84 const std::string& url,
85 const Headers& headers,
86 const std::string& data,
87 const SendRequestCallback& callback) {
Alex Vakulenko41705852015-10-13 10:12:06 -070088 brillo::http::Request request(url, weave::EnumToString(method), transport_);
Vitaly Buka86852562015-08-06 11:06:25 -070089 request.AddHeaders(headers);
90 if (!data.empty()) {
Alex Vakulenko41705852015-10-13 10:12:06 -070091 auto stream = brillo::MemoryStream::OpenCopyOf(data, nullptr);
Vitaly Buka3191e6b2015-08-20 23:17:13 -070092 CHECK(stream->GetRemainingSize());
Alex Vakulenko41705852015-10-13 10:12:06 -070093 brillo::ErrorPtr cromeos_error;
Vitaly Buka4f771532015-08-14 14:58:39 -070094 if (!request.AddRequestBody(std::move(stream), &cromeos_error)) {
95 weave::ErrorPtr error;
96 ConvertError(*cromeos_error, &error);
Vitaly Buka86852562015-08-06 11:06:25 -070097 transport_->RunCallbackAsync(
Alex Vakulenko94f8eba2015-10-14 08:52:45 -070098 FROM_HERE, base::Bind(callback, nullptr, base::Passed(&error)));
Alex Vakulenko297114c2015-10-12 13:45:43 -070099 return;
Vitaly Buka86852562015-08-06 11:06:25 -0700100 }
101 }
Alex Vakulenko94f8eba2015-10-14 08:52:45 -0700102 request.GetResponse(base::Bind(&OnSuccessCallback, callback),
103 base::Bind(&OnErrorCallback, callback));
Vitaly Buka00882b72015-08-06 00:32:11 -0700104}
105
106} // namespace buffet