Vitaly Buka | cad20f0 | 2015-10-16 17:27:15 -0700 | [diff] [blame^] | 1 | // 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 Buka | 00882b7 | 2015-08-06 00:32:11 -0700 | [diff] [blame] | 14 | |
| 15 | #include "buffet/http_transport_client.h" |
| 16 | |
| 17 | #include <base/bind.h> |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 18 | #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 Vakulenko | 297114c | 2015-10-12 13:45:43 -0700 | [diff] [blame] | 22 | #include <weave/enum_to_string.h> |
Vitaly Buka | 00882b7 | 2015-08-06 00:32:11 -0700 | [diff] [blame] | 23 | |
Vitaly Buka | 4f77153 | 2015-08-14 14:58:39 -0700 | [diff] [blame] | 24 | #include "buffet/weave_error_conversion.h" |
| 25 | |
Vitaly Buka | 00882b7 | 2015-08-06 00:32:11 -0700 | [diff] [blame] | 26 | namespace buffet { |
| 27 | |
| 28 | namespace { |
| 29 | |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 30 | using weave::provider::HttpClient; |
| 31 | |
Vitaly Buka | 00882b7 | 2015-08-06 00:32:11 -0700 | [diff] [blame] | 32 | // The number of seconds each HTTP request will be allowed before timing out. |
| 33 | const int kRequestTimeoutSeconds = 30; |
| 34 | |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 35 | class ResponseImpl : public HttpClient::Response { |
Vitaly Buka | 00882b7 | 2015-08-06 00:32:11 -0700 | [diff] [blame] | 36 | public: |
| 37 | ~ResponseImpl() override = default; |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 38 | explicit ResponseImpl(std::unique_ptr<brillo::http::Response> response) |
Vitaly Buka | 00882b7 | 2015-08-06 00:32:11 -0700 | [diff] [blame] | 39 | : response_{std::move(response)}, |
| 40 | data_{response_->ExtractDataAsString()} {} |
| 41 | |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 42 | // HttpClient::Response implementation |
Vitaly Buka | 00882b7 | 2015-08-06 00:32:11 -0700 | [diff] [blame] | 43 | int GetStatusCode() const override { return response_->GetStatusCode(); } |
| 44 | |
| 45 | std::string GetContentType() const override { |
| 46 | return response_->GetContentType(); |
| 47 | } |
| 48 | |
Alex Vakulenko | 297114c | 2015-10-12 13:45:43 -0700 | [diff] [blame] | 49 | std::string GetData() const override { return data_; } |
Vitaly Buka | 00882b7 | 2015-08-06 00:32:11 -0700 | [diff] [blame] | 50 | |
| 51 | private: |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 52 | std::unique_ptr<brillo::http::Response> response_; |
Vitaly Buka | 00882b7 | 2015-08-06 00:32:11 -0700 | [diff] [blame] | 53 | std::string data_; |
| 54 | DISALLOW_COPY_AND_ASSIGN(ResponseImpl); |
| 55 | }; |
| 56 | |
Alex Vakulenko | 94f8eba | 2015-10-14 08:52:45 -0700 | [diff] [blame] | 57 | void OnSuccessCallback(const HttpClient::SendRequestCallback& callback, |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 58 | int id, |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 59 | std::unique_ptr<brillo::http::Response> response) { |
Alex Vakulenko | 94f8eba | 2015-10-14 08:52:45 -0700 | [diff] [blame] | 60 | callback.Run(std::unique_ptr<HttpClient::Response>{new ResponseImpl{ |
| 61 | std::move(response)}}, |
| 62 | nullptr); |
Vitaly Buka | 00882b7 | 2015-08-06 00:32:11 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Alex Vakulenko | 94f8eba | 2015-10-14 08:52:45 -0700 | [diff] [blame] | 65 | void OnErrorCallback(const HttpClient::SendRequestCallback& callback, |
Vitaly Buka | 00882b7 | 2015-08-06 00:32:11 -0700 | [diff] [blame] | 66 | int id, |
Alex Vakulenko | 94f8eba | 2015-10-14 08:52:45 -0700 | [diff] [blame] | 67 | const brillo::Error* brillo_error) { |
Vitaly Buka | 4f77153 | 2015-08-14 14:58:39 -0700 | [diff] [blame] | 68 | weave::ErrorPtr error; |
Alex Vakulenko | 94f8eba | 2015-10-14 08:52:45 -0700 | [diff] [blame] | 69 | ConvertError(*brillo_error, &error); |
| 70 | callback.Run(nullptr, std::move(error)); |
Vitaly Buka | 00882b7 | 2015-08-06 00:32:11 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | } // anonymous namespace |
| 74 | |
| 75 | HttpTransportClient::HttpTransportClient() |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 76 | : transport_{brillo::http::Transport::CreateDefault()} { |
Vitaly Buka | 00882b7 | 2015-08-06 00:32:11 -0700 | [diff] [blame] | 77 | transport_->SetDefaultTimeout( |
| 78 | base::TimeDelta::FromSeconds(kRequestTimeoutSeconds)); |
| 79 | } |
| 80 | |
| 81 | HttpTransportClient::~HttpTransportClient() {} |
| 82 | |
Alex Vakulenko | 94f8eba | 2015-10-14 08:52:45 -0700 | [diff] [blame] | 83 | void HttpTransportClient::SendRequest(Method method, |
| 84 | const std::string& url, |
| 85 | const Headers& headers, |
| 86 | const std::string& data, |
| 87 | const SendRequestCallback& callback) { |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 88 | brillo::http::Request request(url, weave::EnumToString(method), transport_); |
Vitaly Buka | 8685256 | 2015-08-06 11:06:25 -0700 | [diff] [blame] | 89 | request.AddHeaders(headers); |
| 90 | if (!data.empty()) { |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 91 | auto stream = brillo::MemoryStream::OpenCopyOf(data, nullptr); |
Vitaly Buka | 3191e6b | 2015-08-20 23:17:13 -0700 | [diff] [blame] | 92 | CHECK(stream->GetRemainingSize()); |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 93 | brillo::ErrorPtr cromeos_error; |
Vitaly Buka | 4f77153 | 2015-08-14 14:58:39 -0700 | [diff] [blame] | 94 | if (!request.AddRequestBody(std::move(stream), &cromeos_error)) { |
| 95 | weave::ErrorPtr error; |
| 96 | ConvertError(*cromeos_error, &error); |
Vitaly Buka | 8685256 | 2015-08-06 11:06:25 -0700 | [diff] [blame] | 97 | transport_->RunCallbackAsync( |
Alex Vakulenko | 94f8eba | 2015-10-14 08:52:45 -0700 | [diff] [blame] | 98 | FROM_HERE, base::Bind(callback, nullptr, base::Passed(&error))); |
Alex Vakulenko | 297114c | 2015-10-12 13:45:43 -0700 | [diff] [blame] | 99 | return; |
Vitaly Buka | 8685256 | 2015-08-06 11:06:25 -0700 | [diff] [blame] | 100 | } |
| 101 | } |
Alex Vakulenko | 94f8eba | 2015-10-14 08:52:45 -0700 | [diff] [blame] | 102 | request.GetResponse(base::Bind(&OnSuccessCallback, callback), |
| 103 | base::Bind(&OnErrorCallback, callback)); |
Vitaly Buka | 00882b7 | 2015-08-06 00:32:11 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | } // namespace buffet |