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