Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 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 "shill/http_request.h" |
| 6 | |
| 7 | #include <string> |
| 8 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 9 | #include <base/bind.h> |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 10 | #include <base/logging.h> |
| 11 | #include <base/string_number_conversions.h> |
| 12 | #include <base/stringprintf.h> |
| 13 | |
| 14 | #include "shill/async_connection.h" |
| 15 | #include "shill/connection.h" |
| 16 | #include "shill/dns_client.h" |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 17 | #include "shill/error.h" |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 18 | #include "shill/event_dispatcher.h" |
| 19 | #include "shill/http_url.h" |
| 20 | #include "shill/ip_address.h" |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 21 | #include "shill/scope_logger.h" |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 22 | #include "shill/sockets.h" |
| 23 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 24 | using base::Bind; |
| 25 | using base::Callback; |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 26 | using base::StringPrintf; |
| 27 | using std::string; |
| 28 | |
| 29 | namespace shill { |
| 30 | |
| 31 | const int HTTPRequest::kConnectTimeoutSeconds = 10; |
| 32 | const int HTTPRequest::kDNSTimeoutSeconds = 5; |
| 33 | const int HTTPRequest::kInputTimeoutSeconds = 10; |
| 34 | |
| 35 | const char HTTPRequest::kHTTPRequestTemplate[] = |
| 36 | "GET %s HTTP/1.1\r\n" |
| 37 | "Host: %s:%d\r\n" |
| 38 | "Connection: Close\r\n\r\n"; |
| 39 | |
| 40 | HTTPRequest::HTTPRequest(ConnectionRefPtr connection, |
| 41 | EventDispatcher *dispatcher, |
| 42 | Sockets *sockets) |
| 43 | : connection_(connection), |
| 44 | dispatcher_(dispatcher), |
| 45 | sockets_(sockets), |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 46 | weak_ptr_factory_(this), |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 47 | connect_completion_callback_( |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 48 | Bind(&HTTPRequest::OnConnectCompletion, |
| 49 | weak_ptr_factory_.GetWeakPtr())), |
| 50 | dns_client_callback_(Bind(&HTTPRequest::GetDNSResult, |
| 51 | weak_ptr_factory_.GetWeakPtr())), |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 52 | read_server_callback_(Bind(&HTTPRequest::ReadFromServer, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 53 | weak_ptr_factory_.GetWeakPtr())), |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 54 | write_server_callback_(Bind(&HTTPRequest::WriteToServer, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 55 | weak_ptr_factory_.GetWeakPtr())), |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 56 | dns_client_( |
| 57 | new DNSClient(IPAddress::kFamilyIPv4, |
| 58 | connection->interface_name(), |
| 59 | connection->dns_servers(), |
| 60 | kDNSTimeoutSeconds * 1000, |
| 61 | dispatcher, |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 62 | dns_client_callback_)), |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 63 | server_async_connection_( |
| 64 | new AsyncConnection(connection_->interface_name(), |
| 65 | dispatcher_, sockets, |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 66 | connect_completion_callback_)), |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 67 | server_port_(-1), |
| 68 | server_socket_(-1), |
| 69 | timeout_result_(kResultUnknown), |
| 70 | is_running_(false) { } |
| 71 | |
| 72 | HTTPRequest::~HTTPRequest() { |
| 73 | Stop(); |
| 74 | } |
| 75 | |
| 76 | HTTPRequest::Result HTTPRequest::Start( |
| 77 | const HTTPURL &url, |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 78 | const Callback<void(const ByteString &)> &read_event_callback, |
| 79 | const Callback<void(Result, const ByteString &)> &result_callback) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 80 | SLOG(HTTP, 3) << "In " << __func__; |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 81 | |
| 82 | DCHECK(!is_running_); |
| 83 | |
| 84 | is_running_ = true; |
| 85 | request_data_ = ByteString(StringPrintf(kHTTPRequestTemplate, |
| 86 | url.path().c_str(), |
| 87 | url.host().c_str(), |
| 88 | url.port()), false); |
| 89 | server_hostname_ = url.host(); |
| 90 | server_port_ = url.port(); |
| 91 | connection_->RequestRouting(); |
| 92 | |
| 93 | IPAddress addr(IPAddress::kFamilyIPv4); |
| 94 | if (addr.SetAddressFromString(server_hostname_)) { |
| 95 | if (!ConnectServer(addr, server_port_)) { |
| 96 | LOG(ERROR) << "Connect to " |
| 97 | << server_hostname_ |
| 98 | << " failed synchronously"; |
| 99 | return kResultConnectionFailure; |
| 100 | } |
| 101 | } else { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 102 | SLOG(HTTP, 3) << "Looking up host: " << server_hostname_; |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 103 | Error error; |
| 104 | if (!dns_client_->Start(server_hostname_, &error)) { |
| 105 | LOG(ERROR) << "Failed to start DNS client: " << error.message(); |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 106 | Stop(); |
| 107 | return kResultDNSFailure; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // Only install callbacks after connection succeeds in starting. |
| 112 | read_event_callback_ = read_event_callback; |
| 113 | result_callback_ = result_callback; |
| 114 | |
| 115 | return kResultInProgress; |
| 116 | } |
| 117 | |
| 118 | void HTTPRequest::Stop() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 119 | SLOG(HTTP, 3) << "In " << __func__ << "; running is " << is_running_; |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 120 | |
| 121 | if (!is_running_) { |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | // Clear IO handlers first so that closing the socket doesn't cause |
| 126 | // events to fire. |
| 127 | write_server_handler_.reset(); |
| 128 | read_server_handler_.reset(); |
| 129 | |
| 130 | connection_->ReleaseRouting(); |
| 131 | dns_client_->Stop(); |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 132 | is_running_ = false; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 133 | result_callback_.Reset(); |
| 134 | read_event_callback_.Reset(); |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 135 | request_data_.Clear(); |
| 136 | response_data_.Clear(); |
| 137 | server_async_connection_->Stop(); |
| 138 | server_hostname_.clear(); |
| 139 | server_port_ = -1; |
| 140 | if (server_socket_ != -1) { |
| 141 | sockets_->Close(server_socket_); |
| 142 | server_socket_ = -1; |
| 143 | } |
Paul Stewart | f582b50 | 2012-04-04 21:39:22 -0700 | [diff] [blame] | 144 | timeout_closure_.Cancel(); |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 145 | timeout_result_ = kResultUnknown; |
| 146 | } |
| 147 | |
| 148 | bool HTTPRequest::ConnectServer(const IPAddress &address, int port) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 149 | SLOG(HTTP, 3) << "In " << __func__; |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 150 | if (!server_async_connection_->Start(address, port)) { |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 151 | LOG(ERROR) << "Could not create socket to connect to server at " |
mukesh agrawal | 2c15d2c | 2012-02-21 16:09:21 -0800 | [diff] [blame] | 152 | << address.ToString(); |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 153 | SendStatus(kResultConnectionFailure); |
| 154 | return false; |
| 155 | } |
| 156 | // Start a connection timeout only if we didn't synchronously connect. |
| 157 | if (server_socket_ == -1) { |
| 158 | StartIdleTimeout(kConnectTimeoutSeconds, kResultConnectionTimeout); |
| 159 | } |
| 160 | return true; |
| 161 | } |
| 162 | |
| 163 | // DNSClient callback that fires when the DNS request completes. |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 164 | void HTTPRequest::GetDNSResult(const Error &error, const IPAddress &address) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 165 | SLOG(HTTP, 3) << "In " << __func__; |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 166 | if (!error.IsSuccess()) { |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 167 | LOG(ERROR) << "Could not resolve hostname " |
| 168 | << server_hostname_ |
| 169 | << ": " |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 170 | << error.message(); |
| 171 | if (error.message() == DNSClient::kErrorTimedOut) { |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 172 | SendStatus(kResultDNSTimeout); |
| 173 | } else { |
| 174 | SendStatus(kResultDNSFailure); |
| 175 | } |
| 176 | return; |
| 177 | } |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 178 | ConnectServer(address, server_port_); |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | // AsyncConnection callback routine which fires when the asynchronous Connect() |
| 182 | // to the remote server completes (or fails). |
| 183 | void HTTPRequest::OnConnectCompletion(bool success, int fd) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 184 | SLOG(HTTP, 3) << "In " << __func__; |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 185 | if (!success) { |
| 186 | LOG(ERROR) << "Socket connection delayed failure to " |
| 187 | << server_hostname_ |
| 188 | << ": " |
| 189 | << server_async_connection_->error(); |
| 190 | SendStatus(kResultConnectionFailure); |
| 191 | return; |
| 192 | } |
| 193 | server_socket_ = fd; |
| 194 | write_server_handler_.reset( |
| 195 | dispatcher_->CreateReadyHandler(server_socket_, |
| 196 | IOHandler::kModeOutput, |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 197 | write_server_callback_)); |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 198 | StartIdleTimeout(kInputTimeoutSeconds, kResultRequestTimeout); |
| 199 | } |
| 200 | |
| 201 | // IOInputHandler callback which fires when data has been read from the |
| 202 | // server. |
| 203 | void HTTPRequest::ReadFromServer(InputData *data) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 204 | SLOG(HTTP, 3) << "In " << __func__ << " length " << data->len; |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 205 | if (data->len == 0) { |
| 206 | SendStatus(kResultSuccess); |
| 207 | return; |
| 208 | } |
| 209 | |
| 210 | response_data_.Append(ByteString(data->buf, data->len)); |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 211 | StartIdleTimeout(kInputTimeoutSeconds, kResultResponseTimeout); |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 212 | if (!read_event_callback_.is_null()) { |
| 213 | read_event_callback_.Run(response_data_); |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 214 | } |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | void HTTPRequest::SendStatus(Result result) { |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 218 | // Save copies on the stack, since Stop() will remove them. |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 219 | Callback<void(Result, const ByteString &)> result_callback = result_callback_; |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 220 | const ByteString response_data(response_data_); |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 221 | Stop(); |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 222 | |
| 223 | // Call the callback last, since it may delete us and |this| may no longer |
| 224 | // be valid. |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 225 | if (!result_callback.is_null()) { |
| 226 | result_callback.Run(result, response_data); |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 227 | } |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | // Start a timeout for "the next event". |
| 231 | void HTTPRequest::StartIdleTimeout(int timeout_seconds, Result timeout_result) { |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 232 | timeout_result_ = timeout_result; |
Paul Stewart | f582b50 | 2012-04-04 21:39:22 -0700 | [diff] [blame] | 233 | timeout_closure_.Reset( |
| 234 | Bind(&HTTPRequest::TimeoutTask, weak_ptr_factory_.GetWeakPtr())); |
| 235 | dispatcher_->PostDelayedTask(timeout_closure_.callback(), |
| 236 | timeout_seconds * 1000); |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | void HTTPRequest::TimeoutTask() { |
| 240 | LOG(ERROR) << "Connection with " |
| 241 | << server_hostname_ |
| 242 | << " timed out"; |
| 243 | SendStatus(timeout_result_); |
| 244 | } |
| 245 | |
| 246 | // Output ReadyHandler callback which fires when the server socket is |
| 247 | // ready for data to be sent to it. |
| 248 | void HTTPRequest::WriteToServer(int fd) { |
| 249 | CHECK_EQ(server_socket_, fd); |
| 250 | int ret = sockets_->Send(fd, request_data_.GetConstData(), |
| 251 | request_data_.GetLength(), 0); |
| 252 | CHECK(static_cast<size_t>(ret) <= request_data_.GetLength()); |
| 253 | |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 254 | SLOG(HTTP, 3) << "In " << __func__ << " wrote " << ret << " of " |
| 255 | << request_data_.GetLength(); |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 256 | |
| 257 | if (ret < 0) { |
| 258 | LOG(ERROR) << "Client write failed to " |
| 259 | << server_hostname_; |
| 260 | SendStatus(kResultRequestFailure); |
| 261 | return; |
| 262 | } |
| 263 | |
| 264 | request_data_ = ByteString(request_data_.GetConstData() + ret, |
| 265 | request_data_.GetLength() - ret); |
| 266 | |
| 267 | if (request_data_.IsEmpty()) { |
| 268 | write_server_handler_->Stop(); |
| 269 | read_server_handler_.reset( |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 270 | dispatcher_->CreateInputHandler(server_socket_, read_server_callback_)); |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 271 | StartIdleTimeout(kInputTimeoutSeconds, kResultResponseTimeout); |
| 272 | } else { |
| 273 | StartIdleTimeout(kInputTimeoutSeconds, kResultRequestTimeout); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | } // namespace shill |