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