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> |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 10 | #include <base/strings/string_number_conversions.h> |
| 11 | #include <base/strings/stringprintf.h> |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 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" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 19 | #include "shill/logging.h" |
Peter Qiu | 8d6b597 | 2014-10-28 15:33:34 -0700 | [diff] [blame] | 20 | #include "shill/net/ip_address.h" |
| 21 | #include "shill/net/sockets.h" |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 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_( |
Peter Qiu | f3a8f90 | 2014-08-20 10:05:42 -0700 | [diff] [blame] | 56 | new DNSClient(connection->IsIPv6() ? IPAddress::kFamilyIPv6 |
| 57 | : IPAddress::kFamilyIPv4, |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 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); |
Peter Qiu | f3a8f90 | 2014-08-20 10:05:42 -0700 | [diff] [blame] | 94 | if (connection_->IsIPv6()) { |
| 95 | addr.set_family(IPAddress::kFamilyIPv6); |
| 96 | } |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 97 | if (addr.SetAddressFromString(server_hostname_)) { |
| 98 | if (!ConnectServer(addr, server_port_)) { |
| 99 | LOG(ERROR) << "Connect to " |
| 100 | << server_hostname_ |
| 101 | << " failed synchronously"; |
| 102 | return kResultConnectionFailure; |
| 103 | } |
| 104 | } else { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 105 | SLOG(HTTP, 3) << "Looking up host: " << server_hostname_; |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 106 | Error error; |
| 107 | if (!dns_client_->Start(server_hostname_, &error)) { |
| 108 | LOG(ERROR) << "Failed to start DNS client: " << error.message(); |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 109 | Stop(); |
| 110 | return kResultDNSFailure; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // Only install callbacks after connection succeeds in starting. |
| 115 | read_event_callback_ = read_event_callback; |
| 116 | result_callback_ = result_callback; |
| 117 | |
| 118 | return kResultInProgress; |
| 119 | } |
| 120 | |
| 121 | void HTTPRequest::Stop() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 122 | SLOG(HTTP, 3) << "In " << __func__ << "; running is " << is_running_; |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 123 | |
| 124 | if (!is_running_) { |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | // Clear IO handlers first so that closing the socket doesn't cause |
| 129 | // events to fire. |
| 130 | write_server_handler_.reset(); |
| 131 | read_server_handler_.reset(); |
| 132 | |
| 133 | connection_->ReleaseRouting(); |
| 134 | dns_client_->Stop(); |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 135 | is_running_ = false; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 136 | result_callback_.Reset(); |
| 137 | read_event_callback_.Reset(); |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 138 | request_data_.Clear(); |
| 139 | response_data_.Clear(); |
| 140 | server_async_connection_->Stop(); |
| 141 | server_hostname_.clear(); |
| 142 | server_port_ = -1; |
| 143 | if (server_socket_ != -1) { |
| 144 | sockets_->Close(server_socket_); |
| 145 | server_socket_ = -1; |
| 146 | } |
Paul Stewart | f582b50 | 2012-04-04 21:39:22 -0700 | [diff] [blame] | 147 | timeout_closure_.Cancel(); |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 148 | timeout_result_ = kResultUnknown; |
| 149 | } |
| 150 | |
| 151 | bool HTTPRequest::ConnectServer(const IPAddress &address, int port) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 152 | SLOG(HTTP, 3) << "In " << __func__; |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 153 | if (!server_async_connection_->Start(address, port)) { |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 154 | LOG(ERROR) << "Could not create socket to connect to server at " |
mukesh agrawal | 2c15d2c | 2012-02-21 16:09:21 -0800 | [diff] [blame] | 155 | << address.ToString(); |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 156 | SendStatus(kResultConnectionFailure); |
| 157 | return false; |
| 158 | } |
| 159 | // Start a connection timeout only if we didn't synchronously connect. |
| 160 | if (server_socket_ == -1) { |
| 161 | StartIdleTimeout(kConnectTimeoutSeconds, kResultConnectionTimeout); |
| 162 | } |
| 163 | return true; |
| 164 | } |
| 165 | |
| 166 | // DNSClient callback that fires when the DNS request completes. |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 167 | void HTTPRequest::GetDNSResult(const Error &error, const IPAddress &address) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 168 | SLOG(HTTP, 3) << "In " << __func__; |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 169 | if (!error.IsSuccess()) { |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 170 | LOG(ERROR) << "Could not resolve hostname " |
| 171 | << server_hostname_ |
| 172 | << ": " |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 173 | << error.message(); |
| 174 | if (error.message() == DNSClient::kErrorTimedOut) { |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 175 | SendStatus(kResultDNSTimeout); |
| 176 | } else { |
| 177 | SendStatus(kResultDNSFailure); |
| 178 | } |
| 179 | return; |
| 180 | } |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 181 | ConnectServer(address, server_port_); |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | // AsyncConnection callback routine which fires when the asynchronous Connect() |
| 185 | // to the remote server completes (or fails). |
| 186 | void HTTPRequest::OnConnectCompletion(bool success, int fd) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 187 | SLOG(HTTP, 3) << "In " << __func__; |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 188 | if (!success) { |
| 189 | LOG(ERROR) << "Socket connection delayed failure to " |
| 190 | << server_hostname_ |
| 191 | << ": " |
| 192 | << server_async_connection_->error(); |
Paul Stewart | 8585b20 | 2012-09-21 14:03:01 -0700 | [diff] [blame] | 193 | // |this| could be freed as a result of calling SendStatus(). |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 194 | SendStatus(kResultConnectionFailure); |
| 195 | return; |
| 196 | } |
| 197 | server_socket_ = fd; |
| 198 | write_server_handler_.reset( |
| 199 | dispatcher_->CreateReadyHandler(server_socket_, |
| 200 | IOHandler::kModeOutput, |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 201 | write_server_callback_)); |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 202 | StartIdleTimeout(kInputTimeoutSeconds, kResultRequestTimeout); |
| 203 | } |
| 204 | |
Peter Qiu | 3161caa | 2014-10-29 09:47:22 -0700 | [diff] [blame] | 205 | void HTTPRequest::OnServerReadError(const string &/*error_msg*/) { |
Paul Stewart | 5f06a0e | 2012-12-20 11:11:33 -0800 | [diff] [blame] | 206 | SendStatus(kResultResponseFailure); |
| 207 | } |
| 208 | |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 209 | // IOInputHandler callback which fires when data has been read from the |
| 210 | // server. |
| 211 | void HTTPRequest::ReadFromServer(InputData *data) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 212 | SLOG(HTTP, 3) << "In " << __func__ << " length " << data->len; |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 213 | if (data->len == 0) { |
| 214 | SendStatus(kResultSuccess); |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | response_data_.Append(ByteString(data->buf, data->len)); |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 219 | StartIdleTimeout(kInputTimeoutSeconds, kResultResponseTimeout); |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 220 | if (!read_event_callback_.is_null()) { |
| 221 | read_event_callback_.Run(response_data_); |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 222 | } |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | void HTTPRequest::SendStatus(Result result) { |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 226 | // Save copies on the stack, since Stop() will remove them. |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 227 | Callback<void(Result, const ByteString &)> result_callback = result_callback_; |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 228 | const ByteString response_data(response_data_); |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 229 | Stop(); |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 230 | |
| 231 | // Call the callback last, since it may delete us and |this| may no longer |
| 232 | // be valid. |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 233 | if (!result_callback.is_null()) { |
| 234 | result_callback.Run(result, response_data); |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 235 | } |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | // Start a timeout for "the next event". |
| 239 | void HTTPRequest::StartIdleTimeout(int timeout_seconds, Result timeout_result) { |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 240 | timeout_result_ = timeout_result; |
Paul Stewart | f582b50 | 2012-04-04 21:39:22 -0700 | [diff] [blame] | 241 | timeout_closure_.Reset( |
| 242 | Bind(&HTTPRequest::TimeoutTask, weak_ptr_factory_.GetWeakPtr())); |
| 243 | dispatcher_->PostDelayedTask(timeout_closure_.callback(), |
| 244 | timeout_seconds * 1000); |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | void HTTPRequest::TimeoutTask() { |
| 248 | LOG(ERROR) << "Connection with " |
| 249 | << server_hostname_ |
| 250 | << " timed out"; |
| 251 | SendStatus(timeout_result_); |
| 252 | } |
| 253 | |
| 254 | // Output ReadyHandler callback which fires when the server socket is |
| 255 | // ready for data to be sent to it. |
| 256 | void HTTPRequest::WriteToServer(int fd) { |
| 257 | CHECK_EQ(server_socket_, fd); |
| 258 | int ret = sockets_->Send(fd, request_data_.GetConstData(), |
| 259 | request_data_.GetLength(), 0); |
Ben Chan | 2697aa9 | 2012-10-22 22:25:10 -0700 | [diff] [blame] | 260 | CHECK(ret < 0 || static_cast<size_t>(ret) <= request_data_.GetLength()); |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 261 | |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 262 | SLOG(HTTP, 3) << "In " << __func__ << " wrote " << ret << " of " |
| 263 | << request_data_.GetLength(); |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 264 | |
| 265 | if (ret < 0) { |
| 266 | LOG(ERROR) << "Client write failed to " |
| 267 | << server_hostname_; |
| 268 | SendStatus(kResultRequestFailure); |
| 269 | return; |
| 270 | } |
| 271 | |
| 272 | request_data_ = ByteString(request_data_.GetConstData() + ret, |
| 273 | request_data_.GetLength() - ret); |
| 274 | |
| 275 | if (request_data_.IsEmpty()) { |
| 276 | write_server_handler_->Stop(); |
Paul Stewart | 5f06a0e | 2012-12-20 11:11:33 -0800 | [diff] [blame] | 277 | read_server_handler_.reset(dispatcher_->CreateInputHandler( |
| 278 | server_socket_, |
| 279 | read_server_callback_, |
| 280 | Bind(&HTTPRequest::OnServerReadError, weak_ptr_factory_.GetWeakPtr()))); |
Paul Stewart | 188a84a | 2012-01-20 16:28:15 -0800 | [diff] [blame] | 281 | StartIdleTimeout(kInputTimeoutSeconds, kResultResponseTimeout); |
| 282 | } else { |
| 283 | StartIdleTimeout(kInputTimeoutSeconds, kResultRequestTimeout); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | } // namespace shill |