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