Alex Vakulenko | 9cd5e27 | 2014-04-25 17:26:11 -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_connection_fake.h" |
| 6 | |
| 7 | #include <base/logging.h> |
| 8 | |
| 9 | #include "buffet/http_request.h" |
| 10 | #include "buffet/mime_utils.h" |
| 11 | #include "buffet/string_utils.h" |
| 12 | |
| 13 | using namespace chromeos; |
| 14 | using namespace chromeos::http::fake; |
| 15 | |
| 16 | Connection::Connection(const std::string& url, const std::string& method, |
| 17 | std::shared_ptr<http::Transport> transport) : |
| 18 | http::Connection(transport), request_(url, method) { |
| 19 | VLOG(1) << "fake::Connection created: " << method; |
| 20 | } |
| 21 | |
| 22 | Connection::~Connection() { |
| 23 | VLOG(1) << "fake::Connection destroyed"; |
| 24 | } |
| 25 | |
| 26 | bool Connection::SendHeaders(const HeaderList& headers) { |
| 27 | request_.AddHeaders(headers); |
| 28 | return true; |
| 29 | } |
| 30 | |
| 31 | bool Connection::WriteRequestData(const void* data, size_t size) { |
| 32 | request_.AddData(data, size); |
| 33 | return true; |
| 34 | } |
| 35 | |
| 36 | bool Connection::FinishRequest() { |
| 37 | request_.AddHeaders({{request_header::kContentLength, |
| 38 | std::to_string(request_.GetData().size())}}); |
| 39 | fake::Transport* transport = dynamic_cast<fake::Transport*>(transport_.get()); |
| 40 | CHECK(transport) << "Expecting a fake transport"; |
| 41 | auto handler = transport->GetHandler(request_.GetURL(), request_.GetMethod()); |
| 42 | if (handler.is_null()) { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 43 | LOG(ERROR) << "Received unexpected " << request_.GetMethod() |
| 44 | << " request at " << request_.GetURL(); |
Alex Vakulenko | 9cd5e27 | 2014-04-25 17:26:11 -0700 | [diff] [blame] | 45 | response_.ReplyText(status_code::NotFound, |
| 46 | "<html><body>Not found</body></html>", |
| 47 | mime::text::kHtml); |
| 48 | } else { |
| 49 | handler.Run(request_, &response_); |
| 50 | } |
| 51 | return true; |
| 52 | } |
| 53 | |
| 54 | int Connection::GetResponseStatusCode() const { |
| 55 | return response_.GetStatusCode(); |
| 56 | } |
| 57 | |
| 58 | std::string Connection::GetResponseStatusText() const { |
| 59 | return response_.GetStatusText(); |
| 60 | } |
| 61 | |
| 62 | std::string Connection::GetProtocolVersion() const { |
| 63 | return response_.GetProtocolVersion(); |
| 64 | } |
| 65 | |
| 66 | std::string Connection::GetResponseHeader( |
| 67 | const std::string& header_name) const { |
| 68 | return response_.GetHeader(header_name); |
| 69 | } |
| 70 | |
| 71 | uint64_t Connection::GetResponseDataSize() const { |
Alex Vakulenko | 28b31f5 | 2014-04-28 12:40:05 -0700 | [diff] [blame] | 72 | // HEAD requests must not return body. |
| 73 | return (request_.GetMethod() != request_type::kHead) ? |
| 74 | response_.GetData().size() : 0; |
Alex Vakulenko | 9cd5e27 | 2014-04-25 17:26:11 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | bool Connection::ReadResponseData(void* data, size_t buffer_size, |
| 78 | size_t* size_read) { |
| 79 | size_t size_to_read = GetResponseDataSize() - response_data_ptr_; |
| 80 | if (size_to_read > buffer_size) |
| 81 | size_to_read = buffer_size; |
Alex Vakulenko | 28b31f5 | 2014-04-28 12:40:05 -0700 | [diff] [blame] | 82 | if (size_to_read > 0) |
| 83 | memcpy(data, response_.GetData().data() + response_data_ptr_, size_to_read); |
Alex Vakulenko | 9cd5e27 | 2014-04-25 17:26:11 -0700 | [diff] [blame] | 84 | if (size_read) |
| 85 | *size_read = size_to_read; |
| 86 | response_data_ptr_ += size_to_read; |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | std::string Connection::GetErrorMessage() const { |
| 91 | return std::string(); |
| 92 | } |