[email protected] | d102f54 | 2010-06-30 14:51:05 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 3 | // found in the LICENSE file. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4 | |
| 5 | #include "net/http/http_network_transaction.h" |
| 6 | |
[email protected] | 2fbaecf2 | 2010-07-22 22:20:35 | [diff] [blame] | 7 | #include <set> |
| 8 | #include <vector> |
| 9 | |
[email protected] | 68bf915 | 2008-09-25 19:47:30 | [diff] [blame] | 10 | #include "base/compiler_specific.h" |
[email protected] | 0b48db4 | 2009-03-23 02:45:11 | [diff] [blame] | 11 | #include "base/field_trial.h" |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 12 | #include "base/format_macros.h" |
[email protected] | 2198394 | 2009-08-18 06:17:50 | [diff] [blame] | 13 | #include "base/histogram.h" |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 14 | #include "base/scoped_ptr.h" |
[email protected] | 5e2e6c77d1 | 2009-12-24 21:57:16 | [diff] [blame] | 15 | #include "base/stats_counters.h" |
[email protected] | aeaca1f | 2010-04-20 22:05:21 | [diff] [blame] | 16 | #include "base/stl_util-inl.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 17 | #include "base/string_util.h" |
[email protected] | 528c56d | 2010-07-30 19:28:44 | [diff] [blame] | 18 | #include "base/string_number_conversions.h" |
[email protected] | 68bf915 | 2008-09-25 19:47:30 | [diff] [blame] | 19 | #include "build/build_config.h" |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 20 | #include "googleurl/src/gurl.h" |
[email protected] | 277d594 | 2010-08-11 21:02:35 | [diff] [blame] | 21 | #include "net/base/auth.h" |
[email protected] | 74a85ce | 2009-02-12 00:03:19 | [diff] [blame] | 22 | #include "net/base/io_buffer.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 23 | #include "net/base/load_flags.h" |
[email protected] | 597cf6e | 2009-05-29 09:43:26 | [diff] [blame] | 24 | #include "net/base/net_errors.h" |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 25 | #include "net/base/net_util.h" |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 26 | #include "net/base/ssl_cert_request_info.h" |
[email protected] | fc7de49 | 2010-07-12 14:49:04 | [diff] [blame] | 27 | #include "net/base/ssl_connection_status_flags.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 28 | #include "net/base/upload_data_stream.h" |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 29 | #include "net/http/http_auth.h" |
| 30 | #include "net/http/http_auth_handler.h" |
[email protected] | fa82f93 | 2010-05-20 11:09:24 | [diff] [blame] | 31 | #include "net/http/http_auth_handler_factory.h" |
[email protected] | 8d5a34e | 2009-06-11 21:21:36 | [diff] [blame] | 32 | #include "net/http/http_basic_stream.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 33 | #include "net/http/http_chunked_decoder.h" |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 34 | #include "net/http/http_net_log_params.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 35 | #include "net/http/http_network_session.h" |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 36 | #include "net/http/http_proxy_client_socket.h" |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 37 | #include "net/http/http_proxy_client_socket_pool.h" |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 38 | #include "net/http/http_request_headers.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 39 | #include "net/http/http_request_info.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 40 | #include "net/http/http_response_headers.h" |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 41 | #include "net/http/http_response_info.h" |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 42 | #include "net/http/http_stream_request.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 43 | #include "net/http/http_util.h" |
[email protected] | d7f1663 | 2010-03-29 18:02:36 | [diff] [blame] | 44 | #include "net/http/url_security_manager.h" |
[email protected] | f7984fc6 | 2009-06-22 23:26:44 | [diff] [blame] | 45 | #include "net/socket/client_socket_factory.h" |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 46 | #include "net/socket/socks_client_socket_pool.h" |
[email protected] | f7984fc6 | 2009-06-22 23:26:44 | [diff] [blame] | 47 | #include "net/socket/ssl_client_socket.h" |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 48 | #include "net/socket/ssl_client_socket_pool.h" |
[email protected] | 7fc5b09a | 2010-02-27 00:07:38 | [diff] [blame] | 49 | #include "net/socket/tcp_client_socket_pool.h" |
[email protected] | 65d56aa | 2010-06-14 04:13:40 | [diff] [blame] | 50 | #include "net/spdy/spdy_http_stream.h" |
[email protected] | dab9c7d | 2010-02-06 21:44:32 | [diff] [blame] | 51 | #include "net/spdy/spdy_session.h" |
| 52 | #include "net/spdy/spdy_session_pool.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 53 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 54 | using base::Time; |
| 55 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 56 | namespace net { |
| 57 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 58 | namespace { |
| 59 | |
| 60 | void BuildRequestHeaders(const HttpRequestInfo* request_info, |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 61 | const HttpRequestHeaders& authorization_headers, |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 62 | const UploadDataStream* upload_data_stream, |
| 63 | bool using_proxy, |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 64 | std::string* request_line, |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 65 | HttpRequestHeaders* request_headers) { |
| 66 | const std::string path = using_proxy ? |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 67 | HttpUtil::SpecForRequest(request_info->url) : |
| 68 | HttpUtil::PathForRequest(request_info->url); |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 69 | *request_line = StringPrintf( |
| 70 | "%s %s HTTP/1.1\r\n", request_info->method.c_str(), path.c_str()); |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 71 | request_headers->SetHeader(HttpRequestHeaders::kHost, |
| 72 | GetHostAndOptionalPort(request_info->url)); |
| 73 | |
| 74 | // For compat with HTTP/1.0 servers and proxies: |
| 75 | if (using_proxy) { |
| 76 | request_headers->SetHeader(HttpRequestHeaders::kProxyConnection, |
| 77 | "keep-alive"); |
| 78 | } else { |
| 79 | request_headers->SetHeader(HttpRequestHeaders::kConnection, "keep-alive"); |
| 80 | } |
| 81 | |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 82 | // Our consumer should have made sure that this is a safe referrer. See for |
| 83 | // instance WebCore::FrameLoader::HideReferrer. |
| 84 | if (request_info->referrer.is_valid()) { |
| 85 | request_headers->SetHeader(HttpRequestHeaders::kReferer, |
| 86 | request_info->referrer.spec()); |
| 87 | } |
| 88 | |
| 89 | // Add a content length header? |
| 90 | if (upload_data_stream) { |
| 91 | request_headers->SetHeader( |
| 92 | HttpRequestHeaders::kContentLength, |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 93 | base::Uint64ToString(upload_data_stream->size())); |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 94 | } else if (request_info->method == "POST" || request_info->method == "PUT" || |
| 95 | request_info->method == "HEAD") { |
| 96 | // An empty POST/PUT request still needs a content length. As for HEAD, |
| 97 | // IE and Safari also add a content length header. Presumably it is to |
| 98 | // support sending a HEAD request to an URL that only expects to be sent a |
| 99 | // POST or some other method that normally would have a message body. |
| 100 | request_headers->SetHeader(HttpRequestHeaders::kContentLength, "0"); |
| 101 | } |
| 102 | |
| 103 | // Honor load flags that impact proxy caches. |
| 104 | if (request_info->load_flags & LOAD_BYPASS_CACHE) { |
| 105 | request_headers->SetHeader(HttpRequestHeaders::kPragma, "no-cache"); |
| 106 | request_headers->SetHeader(HttpRequestHeaders::kCacheControl, "no-cache"); |
| 107 | } else if (request_info->load_flags & LOAD_VALIDATE_CACHE) { |
| 108 | request_headers->SetHeader(HttpRequestHeaders::kCacheControl, "max-age=0"); |
| 109 | } |
| 110 | |
| 111 | request_headers->MergeFrom(authorization_headers); |
| 112 | |
[email protected] | 860c85d | 2010-02-10 07:22:40 | [diff] [blame] | 113 | // Headers that will be stripped from request_info->extra_headers to prevent, |
| 114 | // e.g., plugins from overriding headers that are controlled using other |
| 115 | // means. Otherwise a plugin could set a referrer although sending the |
| 116 | // referrer is inhibited. |
| 117 | // TODO(jochen): check whether also other headers should be stripped. |
| 118 | static const char* const kExtraHeadersToBeStripped[] = { |
| 119 | "Referer" |
| 120 | }; |
| 121 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 122 | HttpRequestHeaders stripped_extra_headers; |
| 123 | stripped_extra_headers.CopyFrom(request_info->extra_headers); |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 124 | for (size_t i = 0; i < arraysize(kExtraHeadersToBeStripped); ++i) |
| 125 | stripped_extra_headers.RemoveHeader(kExtraHeadersToBeStripped[i]); |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 126 | request_headers->MergeFrom(stripped_extra_headers); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 127 | } |
| 128 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 129 | void ProcessAlternateProtocol(HttpStreamFactory* factory, |
| 130 | HttpAlternateProtocols* alternate_protocols, |
| 131 | const HttpResponseHeaders& headers, |
| 132 | const HostPortPair& http_host_port_pair) { |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 133 | std::string alternate_protocol_str; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 134 | |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 135 | if (!headers.EnumerateHeader(NULL, HttpAlternateProtocols::kHeader, |
| 136 | &alternate_protocol_str)) { |
| 137 | // Header is not present. |
| 138 | return; |
| 139 | } |
| 140 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 141 | factory->ProcessAlternateProtocol(alternate_protocols, |
| 142 | alternate_protocol_str, |
| 143 | http_host_port_pair); |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 144 | } |
| 145 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 146 | } // namespace |
| 147 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 148 | //----------------------------------------------------------------------------- |
| 149 | |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 150 | HttpNetworkTransaction::HttpNetworkTransaction(HttpNetworkSession* session) |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 151 | : pending_auth_target_(HttpAuth::AUTH_NONE), |
| 152 | ALLOW_THIS_IN_INITIALIZER_LIST( |
[email protected] | 68bf915 | 2008-09-25 19:47:30 | [diff] [blame] | 153 | io_callback_(this, &HttpNetworkTransaction::OnIOComplete)), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 154 | user_callback_(NULL), |
| 155 | session_(session), |
| 156 | request_(NULL), |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 157 | headers_valid_(false), |
[email protected] | 8e3d2d3 | 2010-06-13 18:46:23 | [diff] [blame] | 158 | logged_response_time_(false), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 159 | read_buf_len_(0), |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 160 | next_state_(STATE_NONE), |
| 161 | establishing_tunnel_(false) { |
[email protected] | 2cd713f | 2008-10-21 17:54:28 | [diff] [blame] | 162 | session->ssl_config_service()->GetSSLConfig(&ssl_config_); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 163 | if (session->http_stream_factory()->next_protos()) |
| 164 | ssl_config_.next_protos = *session->http_stream_factory()->next_protos(); |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 165 | |
[email protected] | 3ce7df0f | 2010-03-03 00:30:50 | [diff] [blame] | 166 | } |
| 167 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 168 | HttpNetworkTransaction::~HttpNetworkTransaction() { |
| 169 | if (stream_.get()) { |
| 170 | HttpResponseHeaders* headers = GetResponseHeaders(); |
| 171 | // TODO(mbelshe): The stream_ should be able to compute whether or not the |
| 172 | // stream should be kept alive. No reason to compute here |
| 173 | // and pass it in. |
| 174 | bool try_to_keep_alive = |
| 175 | next_state_ == STATE_NONE && |
| 176 | stream_->CanFindEndOfResponse() && |
| 177 | (!headers || headers->IsKeepAlive()); |
| 178 | if (!try_to_keep_alive) { |
| 179 | stream_->Close(true /* not reusable */); |
| 180 | } else { |
| 181 | if (stream_->IsResponseBodyComplete()) { |
| 182 | // If the response body is complete, we can just reuse the socket. |
| 183 | stream_->Close(false /* reusable */); |
| 184 | } else { |
| 185 | // Otherwise, we try to drain the response body. |
| 186 | // TODO(willchan): Consider moving this response body draining to the |
| 187 | // stream implementation. For SPDY, there's clearly no point. For |
| 188 | // HTTP, it can vary depending on whether or not we're pipelining. It's |
| 189 | // stream dependent, so the different subtypes should be implementing |
| 190 | // their solutions. |
| 191 | HttpUtil::DrainStreamBodyAndClose(stream_.release()); |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | if (stream_request_.get()) { |
| 197 | stream_request_->Cancel(); |
| 198 | stream_request_ = NULL; |
| 199 | } |
| 200 | } |
| 201 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 202 | int HttpNetworkTransaction::Start(const HttpRequestInfo* request_info, |
| 203 | CompletionCallback* callback, |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 204 | const BoundNetLog& net_log) { |
[email protected] | 5e2e6c77d1 | 2009-12-24 21:57:16 | [diff] [blame] | 205 | SIMPLE_STATS_COUNTER("HttpNetworkTransaction.Count"); |
[email protected] | 5d0153c51 | 2009-01-12 19:08:36 | [diff] [blame] | 206 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 207 | net_log_ = net_log; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 208 | request_ = request_info; |
[email protected] | 21b316a | 2009-03-23 18:25:06 | [diff] [blame] | 209 | start_time_ = base::Time::Now(); |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 210 | |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 211 | next_state_ = STATE_CREATE_STREAM; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 212 | int rv = DoLoop(OK); |
| 213 | if (rv == ERR_IO_PENDING) |
| 214 | user_callback_ = callback; |
| 215 | return rv; |
| 216 | } |
| 217 | |
| 218 | int HttpNetworkTransaction::RestartIgnoringLastError( |
| 219 | CompletionCallback* callback) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 220 | DCHECK(!stream_.get()); |
| 221 | DCHECK(!stream_request_.get()); |
| 222 | DCHECK_EQ(STATE_NONE, next_state_); |
| 223 | |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 224 | next_state_ = STATE_CREATE_STREAM; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 225 | |
[email protected] | ccb40e5 | 2008-09-17 20:54:40 | [diff] [blame] | 226 | int rv = DoLoop(OK); |
| 227 | if (rv == ERR_IO_PENDING) |
| 228 | user_callback_ = callback; |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 229 | return rv; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 230 | } |
| 231 | |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 232 | int HttpNetworkTransaction::RestartWithCertificate( |
| 233 | X509Certificate* client_cert, |
| 234 | CompletionCallback* callback) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 235 | // In HandleCertificateRequest(), we always tear down existing stream |
| 236 | // requests to force a new connection. So we shouldn't have one here. |
| 237 | DCHECK(!stream_request_.get()); |
| 238 | DCHECK(!stream_.get()); |
| 239 | DCHECK_EQ(STATE_NONE, next_state_); |
| 240 | |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 241 | ssl_config_.client_cert = client_cert; |
[email protected] | 5e36396 | 2009-06-19 19:57:01 | [diff] [blame] | 242 | if (client_cert) { |
| 243 | session_->ssl_client_auth_cache()->Add(GetHostAndPort(request_->url), |
| 244 | client_cert); |
| 245 | } |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 246 | ssl_config_.send_client_cert = true; |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 247 | // Reset the other member variables. |
| 248 | // Note: this is necessary only with SSL renegotiation. |
| 249 | ResetStateForRestart(); |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 250 | next_state_ = STATE_CREATE_STREAM; |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 251 | int rv = DoLoop(OK); |
| 252 | if (rv == ERR_IO_PENDING) |
| 253 | user_callback_ = callback; |
| 254 | return rv; |
| 255 | } |
| 256 | |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 257 | int HttpNetworkTransaction::RestartWithAuth( |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 258 | const string16& username, |
| 259 | const string16& password, |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 260 | CompletionCallback* callback) { |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 261 | HttpAuth::Target target = pending_auth_target_; |
| 262 | if (target == HttpAuth::AUTH_NONE) { |
| 263 | NOTREACHED(); |
| 264 | return ERR_UNEXPECTED; |
| 265 | } |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 266 | pending_auth_target_ = HttpAuth::AUTH_NONE; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 267 | |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 268 | auth_controllers_[target]->ResetAuth(username, password); |
| 269 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 270 | DCHECK(user_callback_ == NULL); |
| 271 | |
| 272 | int rv = OK; |
| 273 | if (target == HttpAuth::AUTH_PROXY && establishing_tunnel_) { |
| 274 | // In this case, we've gathered credentials for use with proxy |
| 275 | // authentication of a tunnel. |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 276 | DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 277 | DCHECK(stream_request_ != NULL); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 278 | auth_controllers_[target] = NULL; |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 279 | ResetStateForRestart(); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 280 | rv = stream_request_->RestartTunnelWithProxyAuth(username, password); |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 281 | } else { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 282 | // In this case, we've gathered credentials for the server or the proxy |
| 283 | // but it is not during the tunneling phase. |
| 284 | DCHECK(stream_request_ == NULL); |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 285 | PrepareForAuthRestart(target); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 286 | rv = DoLoop(OK); |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 287 | } |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 288 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 289 | if (rv == ERR_IO_PENDING) |
| 290 | user_callback_ = callback; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 291 | return rv; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 292 | } |
| 293 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 294 | void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target) { |
| 295 | DCHECK(HaveAuth(target)); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 296 | DCHECK(!stream_request_.get()); |
| 297 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 298 | bool keep_alive = false; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 299 | // Even if the server says the connection is keep-alive, we have to be |
| 300 | // able to find the end of each response in order to reuse the connection. |
| 301 | if (GetResponseHeaders()->IsKeepAlive() && |
[email protected] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 302 | stream_->CanFindEndOfResponse()) { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 303 | // If the response body hasn't been completely read, we need to drain |
| 304 | // it first. |
[email protected] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 305 | if (!stream_->IsResponseBodyComplete()) { |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 306 | next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 307 | read_buf_ = new IOBuffer(kDrainBodyBufferSize); // A bit bucket. |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 308 | read_buf_len_ = kDrainBodyBufferSize; |
| 309 | return; |
| 310 | } |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 311 | keep_alive = true; |
[email protected] | 37832c6d | 2009-06-05 19:44:09 | [diff] [blame] | 312 | } |
| 313 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 314 | // We don't need to drain the response body, so we act as if we had drained |
| 315 | // the response body. |
| 316 | DidDrainBodyForAuthRestart(keep_alive); |
| 317 | } |
| 318 | |
| 319 | void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 320 | DCHECK(!stream_request_.get()); |
| 321 | |
| 322 | if (stream_.get()) { |
| 323 | if (keep_alive) { |
| 324 | // We should call connection_->set_idle_time(), but this doesn't occur |
| 325 | // often enough to be worth the trouble. |
| 326 | stream_->SetConnectionReused(); |
| 327 | } |
| 328 | stream_->Close(!keep_alive); |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 329 | next_state_ = STATE_CREATE_STREAM; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 330 | } |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 331 | |
| 332 | // Reset the other member variables. |
| 333 | ResetStateForRestart(); |
| 334 | } |
| 335 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 336 | bool HttpNetworkTransaction::IsReadyToRestartForAuth() { |
| 337 | return pending_auth_target_ != HttpAuth::AUTH_NONE && |
| 338 | HaveAuth(pending_auth_target_); |
| 339 | } |
| 340 | |
[email protected] | 9dea9e1f | 2009-01-29 00:30:47 | [diff] [blame] | 341 | int HttpNetworkTransaction::Read(IOBuffer* buf, int buf_len, |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 342 | CompletionCallback* callback) { |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 343 | DCHECK(buf); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 344 | DCHECK_LT(0, buf_len); |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 345 | |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 346 | State next_state = STATE_NONE; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 347 | |
[email protected] | 8a1f331 | 2010-05-25 19:25:04 | [diff] [blame] | 348 | scoped_refptr<HttpResponseHeaders> headers = GetResponseHeaders(); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 349 | if (headers_valid_ && headers.get() && stream_request_.get()) { |
[email protected] | 8a1f331 | 2010-05-25 19:25:04 | [diff] [blame] | 350 | // We're trying to read the body of the response but we're still trying |
| 351 | // to establish an SSL tunnel through the proxy. We can't read these |
| 352 | // bytes when establishing a tunnel because they might be controlled by |
| 353 | // an active network attacker. We don't worry about this for HTTP |
| 354 | // because an active network attacker can already control HTTP sessions. |
| 355 | // We reach this case when the user cancels a 407 proxy auth prompt. |
| 356 | // See http://crbug.com/8473. |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 357 | DCHECK(proxy_info_.is_http() || proxy_info_.is_https()); |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 358 | DCHECK_EQ(headers->response_code(), 407); |
| 359 | LOG(WARNING) << "Blocked proxy response with status " |
| 360 | << headers->response_code() << " to CONNECT request for " |
| 361 | << GetHostAndPort(request_->url) << "."; |
[email protected] | 8a1f331 | 2010-05-25 19:25:04 | [diff] [blame] | 362 | return ERR_TUNNEL_CONNECTION_FAILED; |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 363 | } |
| 364 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 365 | // Are we using SPDY or HTTP? |
[email protected] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 366 | next_state = STATE_READ_BODY; |
| 367 | DCHECK(stream_->GetResponseInfo()->headers); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 368 | |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 369 | read_buf_ = buf; |
| 370 | read_buf_len_ = buf_len; |
| 371 | |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 372 | next_state_ = next_state; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 373 | int rv = DoLoop(OK); |
| 374 | if (rv == ERR_IO_PENDING) |
| 375 | user_callback_ = callback; |
| 376 | return rv; |
| 377 | } |
| 378 | |
| 379 | const HttpResponseInfo* HttpNetworkTransaction::GetResponseInfo() const { |
[email protected] | a7e4131 | 2009-12-16 23:18:14 | [diff] [blame] | 380 | return ((headers_valid_ && response_.headers) || response_.ssl_info.cert || |
| 381 | response_.cert_request_info) ? &response_ : NULL; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | LoadState HttpNetworkTransaction::GetLoadState() const { |
| 385 | // TODO(wtc): Define a new LoadState value for the |
| 386 | // STATE_INIT_CONNECTION_COMPLETE state, which delays the HTTP request. |
| 387 | switch (next_state_) { |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 388 | case STATE_CREATE_STREAM_COMPLETE: |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 389 | return stream_request_->GetLoadState(); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 390 | case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE: |
| 391 | case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE: |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 392 | case STATE_SEND_REQUEST_COMPLETE: |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 393 | return LOAD_STATE_SENDING_REQUEST; |
| 394 | case STATE_READ_HEADERS_COMPLETE: |
| 395 | return LOAD_STATE_WAITING_FOR_RESPONSE; |
| 396 | case STATE_READ_BODY_COMPLETE: |
| 397 | return LOAD_STATE_READING_RESPONSE; |
| 398 | default: |
| 399 | return LOAD_STATE_IDLE; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | uint64 HttpNetworkTransaction::GetUploadProgress() const { |
[email protected] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 404 | if (!stream_.get()) |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 405 | return 0; |
| 406 | |
[email protected] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 407 | return stream_->GetUploadProgress(); |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 408 | } |
| 409 | |
[email protected] | 4d4a516 | 2010-09-21 22:44:04 | [diff] [blame^] | 410 | void HttpNetworkTransaction::OnStreamReady(HttpStream* stream) { |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 411 | DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 412 | DCHECK(stream_request_.get()); |
| 413 | |
| 414 | stream_.reset(stream); |
| 415 | response_.was_alternate_protocol_available = |
| 416 | stream_request_->was_alternate_protocol_available(); |
| 417 | response_.was_npn_negotiated = stream_request_->was_npn_negotiated(); |
| 418 | response_.was_fetched_via_spdy = stream_request_->using_spdy(); |
| 419 | response_.was_fetched_via_proxy = !proxy_info_.is_direct(); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 420 | |
| 421 | OnIOComplete(OK); |
| 422 | } |
| 423 | |
| 424 | void HttpNetworkTransaction::OnStreamFailed(int result) { |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 425 | DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 426 | DCHECK_NE(OK, result); |
| 427 | DCHECK(stream_request_.get()); |
| 428 | DCHECK(!stream_.get()); |
| 429 | |
| 430 | OnIOComplete(result); |
| 431 | } |
| 432 | |
| 433 | void HttpNetworkTransaction::OnCertificateError(int result, |
| 434 | const SSLInfo& ssl_info) { |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 435 | DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 436 | DCHECK_NE(OK, result); |
| 437 | DCHECK(stream_request_.get()); |
| 438 | DCHECK(!stream_.get()); |
| 439 | |
| 440 | response_.ssl_info = ssl_info; |
| 441 | |
| 442 | // TODO(mbelshe): For now, we're going to pass the error through, and that |
| 443 | // will close the stream_request in all cases. This means that we're always |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 444 | // going to restart an entire STATE_CREATE_STREAM, even if the connection is |
| 445 | // good and the user chooses to ignore the error. This is not ideal, but not |
| 446 | // the end of the world either. |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 447 | |
| 448 | OnIOComplete(result); |
| 449 | } |
| 450 | |
| 451 | void HttpNetworkTransaction::OnNeedsProxyAuth( |
[email protected] | 6dc476da | 2010-09-01 04:43:50 | [diff] [blame] | 452 | const HttpResponseInfo& proxy_response, |
| 453 | HttpAuthController* auth_controller) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 454 | DCHECK(stream_request_.get()); |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 455 | DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 456 | |
| 457 | establishing_tunnel_ = true; |
| 458 | response_.headers = proxy_response.headers; |
| 459 | response_.auth_challenge = proxy_response.auth_challenge; |
| 460 | headers_valid_ = true; |
| 461 | |
| 462 | auth_controllers_[HttpAuth::AUTH_PROXY] = auth_controller; |
| 463 | pending_auth_target_ = HttpAuth::AUTH_PROXY; |
| 464 | |
| 465 | DoCallback(OK); |
| 466 | } |
| 467 | |
| 468 | void HttpNetworkTransaction::OnNeedsClientAuth( |
[email protected] | 6dc476da | 2010-09-01 04:43:50 | [diff] [blame] | 469 | SSLCertRequestInfo* cert_info) { |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 470 | DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 471 | |
| 472 | response_.cert_request_info = cert_info; |
[email protected] | 65a3b91 | 2010-08-21 05:46:58 | [diff] [blame] | 473 | OnIOComplete(ERR_SSL_CLIENT_AUTH_CERT_NEEDED); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 474 | } |
| 475 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 476 | bool HttpNetworkTransaction::is_https_request() const { |
| 477 | return request_->url.SchemeIs("https"); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 478 | } |
| 479 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 480 | void HttpNetworkTransaction::DoCallback(int rv) { |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 481 | DCHECK_NE(rv, ERR_IO_PENDING); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 482 | DCHECK(user_callback_); |
| 483 | |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 484 | // Since Run may result in Read being called, clear user_callback_ up front. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 485 | CompletionCallback* c = user_callback_; |
| 486 | user_callback_ = NULL; |
| 487 | c->Run(rv); |
| 488 | } |
| 489 | |
| 490 | void HttpNetworkTransaction::OnIOComplete(int result) { |
| 491 | int rv = DoLoop(result); |
| 492 | if (rv != ERR_IO_PENDING) |
| 493 | DoCallback(rv); |
| 494 | } |
| 495 | |
| 496 | int HttpNetworkTransaction::DoLoop(int result) { |
| 497 | DCHECK(next_state_ != STATE_NONE); |
| 498 | |
| 499 | int rv = result; |
| 500 | do { |
| 501 | State state = next_state_; |
| 502 | next_state_ = STATE_NONE; |
| 503 | switch (state) { |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 504 | case STATE_CREATE_STREAM: |
| 505 | DCHECK_EQ(OK, rv); |
| 506 | rv = DoCreateStream(); |
| 507 | break; |
| 508 | case STATE_CREATE_STREAM_COMPLETE: |
| 509 | rv = DoCreateStreamComplete(rv); |
| 510 | break; |
[email protected] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 511 | case STATE_INIT_STREAM: |
| 512 | DCHECK_EQ(OK, rv); |
| 513 | rv = DoInitStream(); |
| 514 | break; |
| 515 | case STATE_INIT_STREAM_COMPLETE: |
| 516 | rv = DoInitStreamComplete(rv); |
| 517 | break; |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 518 | case STATE_GENERATE_PROXY_AUTH_TOKEN: |
| 519 | DCHECK_EQ(OK, rv); |
| 520 | rv = DoGenerateProxyAuthToken(); |
| 521 | break; |
| 522 | case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE: |
| 523 | rv = DoGenerateProxyAuthTokenComplete(rv); |
| 524 | break; |
| 525 | case STATE_GENERATE_SERVER_AUTH_TOKEN: |
| 526 | DCHECK_EQ(OK, rv); |
| 527 | rv = DoGenerateServerAuthToken(); |
| 528 | break; |
| 529 | case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE: |
| 530 | rv = DoGenerateServerAuthTokenComplete(rv); |
| 531 | break; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 532 | case STATE_SEND_REQUEST: |
[email protected] | 725355a | 2009-03-25 20:42:55 | [diff] [blame] | 533 | DCHECK_EQ(OK, rv); |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 534 | net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST, NULL); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 535 | rv = DoSendRequest(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 536 | break; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 537 | case STATE_SEND_REQUEST_COMPLETE: |
| 538 | rv = DoSendRequestComplete(rv); |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 539 | net_log_.EndEvent(NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST, NULL); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 540 | break; |
| 541 | case STATE_READ_HEADERS: |
[email protected] | 725355a | 2009-03-25 20:42:55 | [diff] [blame] | 542 | DCHECK_EQ(OK, rv); |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 543 | net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS, NULL); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 544 | rv = DoReadHeaders(); |
| 545 | break; |
| 546 | case STATE_READ_HEADERS_COMPLETE: |
| 547 | rv = DoReadHeadersComplete(rv); |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 548 | net_log_.EndEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS, NULL); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 549 | break; |
| 550 | case STATE_READ_BODY: |
[email protected] | 725355a | 2009-03-25 20:42:55 | [diff] [blame] | 551 | DCHECK_EQ(OK, rv); |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 552 | net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_BODY, NULL); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 553 | rv = DoReadBody(); |
| 554 | break; |
| 555 | case STATE_READ_BODY_COMPLETE: |
| 556 | rv = DoReadBodyComplete(rv); |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 557 | net_log_.EndEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_BODY, NULL); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 558 | break; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 559 | case STATE_DRAIN_BODY_FOR_AUTH_RESTART: |
[email protected] | 725355a | 2009-03-25 20:42:55 | [diff] [blame] | 560 | DCHECK_EQ(OK, rv); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 561 | net_log_.BeginEvent( |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 562 | NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART, NULL); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 563 | rv = DoDrainBodyForAuthRestart(); |
| 564 | break; |
| 565 | case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE: |
| 566 | rv = DoDrainBodyForAuthRestartComplete(rv); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 567 | net_log_.EndEvent( |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 568 | NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART, NULL); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 569 | break; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 570 | default: |
| 571 | NOTREACHED() << "bad state"; |
| 572 | rv = ERR_FAILED; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 573 | break; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 574 | } |
| 575 | } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); |
| 576 | |
| 577 | return rv; |
| 578 | } |
| 579 | |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 580 | int HttpNetworkTransaction::DoCreateStream() { |
| 581 | next_state_ = STATE_CREATE_STREAM_COMPLETE; |
[email protected] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 582 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 583 | session_->http_stream_factory()->RequestStream(request_, |
| 584 | &ssl_config_, |
| 585 | &proxy_info_, |
| 586 | this, |
| 587 | net_log_, |
| 588 | session_, |
| 589 | &stream_request_); |
| 590 | return ERR_IO_PENDING; |
[email protected] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 591 | } |
| 592 | |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 593 | int HttpNetworkTransaction::DoCreateStreamComplete(int result) { |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 594 | if (result == OK) { |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 595 | next_state_ = STATE_INIT_STREAM; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 596 | DCHECK(stream_.get()); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 597 | } |
| 598 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 599 | // At this point we are done with the stream_request_. |
| 600 | stream_request_ = NULL; |
| 601 | return result; |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 602 | } |
| 603 | |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 604 | int HttpNetworkTransaction::DoInitStream() { |
| 605 | DCHECK(stream_.get()); |
| 606 | next_state_ = STATE_INIT_STREAM_COMPLETE; |
| 607 | return stream_->InitializeStream(request_, net_log_, &io_callback_); |
| 608 | } |
| 609 | |
| 610 | int HttpNetworkTransaction::DoInitStreamComplete(int result) { |
| 611 | if (result == OK) { |
| 612 | next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN; |
| 613 | |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 614 | if (is_https_request()) |
| 615 | stream_->GetSSLInfo(&response_.ssl_info); |
| 616 | } else { |
| 617 | if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) |
| 618 | result = HandleCertificateRequest(result); |
| 619 | |
| 620 | if (result < 0) |
[email protected] | 044dcc5 | 2010-09-17 15:44:26 | [diff] [blame] | 621 | result = HandleIOError(result); |
| 622 | |
| 623 | // The stream initialization failed, so this stream will never be useful. |
| 624 | stream_.reset(); |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | return result; |
| 628 | } |
| 629 | |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 630 | int HttpNetworkTransaction::DoGenerateProxyAuthToken() { |
| 631 | next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE; |
| 632 | if (!ShouldApplyProxyAuth()) |
| 633 | return OK; |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 634 | HttpAuth::Target target = HttpAuth::AUTH_PROXY; |
| 635 | if (!auth_controllers_[target].get()) |
[email protected] | 3598c602 | 2010-09-17 23:13:09 | [diff] [blame] | 636 | auth_controllers_[target] = |
| 637 | new HttpAuthController(target, |
| 638 | AuthURL(target), |
| 639 | session_->auth_cache(), |
| 640 | session_->http_auth_handler_factory()); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 641 | return auth_controllers_[target]->MaybeGenerateAuthToken(request_, |
| 642 | &io_callback_, |
| 643 | net_log_); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | int HttpNetworkTransaction::DoGenerateProxyAuthTokenComplete(int rv) { |
| 647 | DCHECK_NE(ERR_IO_PENDING, rv); |
| 648 | if (rv == OK) |
| 649 | next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN; |
| 650 | return rv; |
| 651 | } |
| 652 | |
| 653 | int HttpNetworkTransaction::DoGenerateServerAuthToken() { |
| 654 | next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE; |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 655 | HttpAuth::Target target = HttpAuth::AUTH_SERVER; |
| 656 | if (!auth_controllers_[target].get()) |
[email protected] | 3598c602 | 2010-09-17 23:13:09 | [diff] [blame] | 657 | auth_controllers_[target] = |
| 658 | new HttpAuthController(target, |
| 659 | AuthURL(target), |
| 660 | session_->auth_cache(), |
| 661 | session_->http_auth_handler_factory()); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 662 | if (!ShouldApplyServerAuth()) |
| 663 | return OK; |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 664 | return auth_controllers_[target]->MaybeGenerateAuthToken(request_, |
| 665 | &io_callback_, |
| 666 | net_log_); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | int HttpNetworkTransaction::DoGenerateServerAuthTokenComplete(int rv) { |
| 670 | DCHECK_NE(ERR_IO_PENDING, rv); |
| 671 | if (rv == OK) |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 672 | next_state_ = STATE_SEND_REQUEST; |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 673 | return rv; |
| 674 | } |
| 675 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 676 | int HttpNetworkTransaction::DoSendRequest() { |
| 677 | next_state_ = STATE_SEND_REQUEST_COMPLETE; |
| 678 | |
| 679 | UploadDataStream* request_body = NULL; |
[email protected] | 8a1f331 | 2010-05-25 19:25:04 | [diff] [blame] | 680 | if (request_->upload_data) { |
[email protected] | 7a6db402 | 2010-03-24 23:37:50 | [diff] [blame] | 681 | int error_code; |
| 682 | request_body = UploadDataStream::Create(request_->upload_data, &error_code); |
| 683 | if (!request_body) |
| 684 | return error_code; |
| 685 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 686 | |
| 687 | // This is constructed lazily (instead of within our Start method), so that |
| 688 | // we have proxy info available. |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 689 | if (request_headers_.empty() && !response_.was_fetched_via_spdy) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 690 | // Figure out if we can/should add Proxy-Authentication & Authentication |
| 691 | // headers. |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 692 | HttpRequestHeaders authorization_headers; |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 693 | bool have_proxy_auth = (ShouldApplyProxyAuth() && |
| 694 | HaveAuth(HttpAuth::AUTH_PROXY)); |
| 695 | bool have_server_auth = (ShouldApplyServerAuth() && |
| 696 | HaveAuth(HttpAuth::AUTH_SERVER)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 697 | if (have_proxy_auth) |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 698 | auth_controllers_[HttpAuth::AUTH_PROXY]->AddAuthorizationHeader( |
| 699 | &authorization_headers); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 700 | if (have_server_auth) |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 701 | auth_controllers_[HttpAuth::AUTH_SERVER]->AddAuthorizationHeader( |
| 702 | &authorization_headers); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 703 | std::string request_line; |
| 704 | HttpRequestHeaders request_headers; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 705 | |
[email protected] | 8a1f331 | 2010-05-25 19:25:04 | [diff] [blame] | 706 | BuildRequestHeaders(request_, authorization_headers, request_body, |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 707 | !is_https_request() && (proxy_info_.is_http() || |
| 708 | proxy_info_.is_https()), |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 709 | &request_line, &request_headers); |
[email protected] | ac03952 | 2010-06-15 16:39:44 | [diff] [blame] | 710 | |
| 711 | if (session_->network_delegate()) |
| 712 | session_->network_delegate()->OnSendHttpRequest(&request_headers); |
| 713 | |
[email protected] | 095c7cf | 2010-08-31 21:07:33 | [diff] [blame] | 714 | if (net_log_.IsLoggingAll()) { |
[email protected] | 8a1f331 | 2010-05-25 19:25:04 | [diff] [blame] | 715 | net_log_.AddEvent( |
| 716 | NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST_HEADERS, |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 717 | new NetLogHttpRequestParameter(request_line, request_headers)); |
[email protected] | 6b9833e | 2008-09-10 20:32:25 | [diff] [blame] | 718 | } |
[email protected] | ac03952 | 2010-06-15 16:39:44 | [diff] [blame] | 719 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 720 | request_headers_ = request_line + request_headers.ToString(); |
[email protected] | 490937a | 2010-09-01 07:57:35 | [diff] [blame] | 721 | } else { |
| 722 | if (net_log_.IsLoggingAll()) { |
| 723 | net_log_.AddEvent( |
| 724 | NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST_HEADERS, |
| 725 | new NetLogHttpRequestParameter(request_->url.spec(), |
| 726 | request_->extra_headers)); |
| 727 | } |
[email protected] | 6b9833e | 2008-09-10 20:32:25 | [diff] [blame] | 728 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 729 | |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 730 | headers_valid_ = false; |
[email protected] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 731 | return stream_->SendRequest(request_headers_, request_body, &response_, |
| 732 | &io_callback_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 733 | } |
| 734 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 735 | int HttpNetworkTransaction::DoSendRequestComplete(int result) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 736 | if (result < 0) |
| 737 | return HandleIOError(result); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 738 | next_state_ = STATE_READ_HEADERS; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 739 | return OK; |
| 740 | } |
| 741 | |
| 742 | int HttpNetworkTransaction::DoReadHeaders() { |
| 743 | next_state_ = STATE_READ_HEADERS_COMPLETE; |
[email protected] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 744 | return stream_->ReadResponseHeaders(&io_callback_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 745 | } |
| 746 | |
[email protected] | 0e75a73 | 2008-10-16 20:36:09 | [diff] [blame] | 747 | int HttpNetworkTransaction::HandleConnectionClosedBeforeEndOfHeaders() { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 748 | if (!response_.headers && !stream_->IsConnectionReused()) { |
[email protected] | 0e75a73 | 2008-10-16 20:36:09 | [diff] [blame] | 749 | // The connection was closed before any data was sent. Likely an error |
| 750 | // rather than empty HTTP/0.9 response. |
[email protected] | aecfbf2 | 2008-10-16 02:02:47 | [diff] [blame] | 751 | return ERR_EMPTY_RESPONSE; |
| 752 | } |
| 753 | |
[email protected] | aecfbf2 | 2008-10-16 02:02:47 | [diff] [blame] | 754 | return OK; |
| 755 | } |
| 756 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 757 | int HttpNetworkTransaction::DoReadHeadersComplete(int result) { |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 758 | // We can get a certificate error or ERR_SSL_CLIENT_AUTH_CERT_NEEDED here |
| 759 | // due to SSL renegotiation. |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 760 | if (IsCertificateError(result)) { |
| 761 | // We don't handle a certificate error during SSL renegotiation, so we |
| 762 | // have to return an error that's not in the certificate error range |
| 763 | // (-2xx). |
| 764 | LOG(ERROR) << "Got a server certificate with error " << result |
| 765 | << " during SSL renegotiation"; |
| 766 | result = ERR_CERT_ERROR_IN_SSL_RENEGOTIATION; |
| 767 | } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) { |
| 768 | // TODO(wtc): Need a test case for this code path! |
| 769 | DCHECK(stream_.get()); |
| 770 | DCHECK(is_https_request()); |
| 771 | response_.cert_request_info = new SSLCertRequestInfo; |
| 772 | stream_->GetSSLCertRequestInfo(response_.cert_request_info); |
| 773 | result = HandleCertificateRequest(result); |
| 774 | if (result == OK) |
| 775 | return result; |
| 776 | } else if ((result == ERR_SSL_DECOMPRESSION_FAILURE_ALERT || |
| 777 | result == ERR_SSL_BAD_RECORD_MAC_ALERT) && |
| 778 | ssl_config_.tls1_enabled && |
| 779 | !SSLConfigService::IsKnownStrictTLSServer(request_->url.host())) { |
| 780 | // Some buggy servers select DEFLATE compression when offered and then |
| 781 | // fail to ever decompress anything. They will send a fatal alert telling |
| 782 | // us this. Normally we would pick this up during the handshake because |
| 783 | // our Finished message is compressed and we'll never get the server's |
| 784 | // Finished if it fails to process ours. |
| 785 | // |
| 786 | // However, with False Start, we'll believe that the handshake is |
| 787 | // complete as soon as we've /sent/ our Finished message. In this case, |
| 788 | // we only find out that the server is buggy here, when we try to read |
| 789 | // the initial reply. |
| 790 | session_->http_stream_factory()->AddTLSIntolerantServer(request_->url); |
| 791 | ResetConnectionAndRequestForResend(); |
| 792 | return OK; |
[email protected] | 2181ea00 | 2009-06-09 01:37:27 | [diff] [blame] | 793 | } |
| 794 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 795 | if (result < 0 && result != ERR_CONNECTION_CLOSED) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 796 | return HandleIOError(result); |
| 797 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 798 | if (result == ERR_CONNECTION_CLOSED && ShouldResendRequest(result)) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 799 | ResetConnectionAndRequestForResend(); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 800 | return OK; |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 801 | } |
[email protected] | 2a5c76b | 2008-09-25 22:15:16 | [diff] [blame] | 802 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 803 | // After we call RestartWithAuth a new response_time will be recorded, and |
| 804 | // we need to be cautious about incorrectly logging the duration across the |
| 805 | // authentication activity. |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 806 | if (result == OK) |
| 807 | LogTransactionConnectedMetrics(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 808 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 809 | if (result == ERR_CONNECTION_CLOSED) { |
[email protected] | 02c92c49 | 2010-03-08 21:28:14 | [diff] [blame] | 810 | // For now, if we get at least some data, we do the best we can to make |
[email protected] | 9492e4a | 2010-02-24 00:58:46 | [diff] [blame] | 811 | // sense of it and send it back up the stack. |
[email protected] | 0e75a73 | 2008-10-16 20:36:09 | [diff] [blame] | 812 | int rv = HandleConnectionClosedBeforeEndOfHeaders(); |
[email protected] | aecfbf2 | 2008-10-16 02:02:47 | [diff] [blame] | 813 | if (rv != OK) |
| 814 | return rv; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 815 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 816 | |
[email protected] | 095c7cf | 2010-08-31 21:07:33 | [diff] [blame] | 817 | if (net_log_.IsLoggingAll()) { |
[email protected] | 8a1f331 | 2010-05-25 19:25:04 | [diff] [blame] | 818 | net_log_.AddEvent( |
| 819 | NetLog::TYPE_HTTP_TRANSACTION_READ_RESPONSE_HEADERS, |
| 820 | new NetLogHttpResponseParameter(response_.headers)); |
[email protected] | dbb83db | 2010-05-11 18:13:39 | [diff] [blame] | 821 | } |
| 822 | |
[email protected] | a7e4131 | 2009-12-16 23:18:14 | [diff] [blame] | 823 | if (response_.headers->GetParsedHttpVersion() < HttpVersion(1, 0)) { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 824 | // HTTP/0.9 doesn't support the PUT method, so lack of response headers |
| 825 | // indicates a buggy server. See: |
| 826 | // https://bugzilla.mozilla.org/show_bug.cgi?id=193921 |
| 827 | if (request_->method == "PUT") |
| 828 | return ERR_METHOD_NOT_SUPPORTED; |
| 829 | } |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 830 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 831 | // Check for an intermediate 100 Continue response. An origin server is |
| 832 | // allowed to send this response even if we didn't ask for it, so we just |
| 833 | // need to skip over it. |
| 834 | // We treat any other 1xx in this same way (although in practice getting |
| 835 | // a 1xx that isn't a 100 is rare). |
[email protected] | a7e4131 | 2009-12-16 23:18:14 | [diff] [blame] | 836 | if (response_.headers->response_code() / 100 == 1) { |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 837 | response_.headers = new HttpResponseHeaders(""); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 838 | next_state_ = STATE_READ_HEADERS; |
| 839 | return OK; |
| 840 | } |
| 841 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 842 | HostPortPair endpoint = HostPortPair(request_->url.HostNoBrackets(), |
| 843 | request_->url.EffectiveIntPort()); |
| 844 | ProcessAlternateProtocol(session_->http_stream_factory(), |
| 845 | session_->mutable_alternate_protocols(), |
| 846 | *response_.headers, |
| 847 | endpoint); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 848 | |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 849 | int rv = HandleAuthChallenge(); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 850 | if (rv != OK) |
| 851 | return rv; |
| 852 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 853 | headers_valid_ = true; |
| 854 | return OK; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | int HttpNetworkTransaction::DoReadBody() { |
| 858 | DCHECK(read_buf_); |
[email protected] | 6501bc0 | 2009-06-25 20:55:13 | [diff] [blame] | 859 | DCHECK_GT(read_buf_len_, 0); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 860 | DCHECK(stream_ != NULL); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 861 | |
| 862 | next_state_ = STATE_READ_BODY_COMPLETE; |
[email protected] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 863 | return stream_->ReadResponseBody(read_buf_, read_buf_len_, &io_callback_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | int HttpNetworkTransaction::DoReadBodyComplete(int result) { |
| 867 | // We are done with the Read call. |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 868 | bool done = false; |
| 869 | if (result <= 0) { |
| 870 | DCHECK_NE(ERR_IO_PENDING, result); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 871 | done = true; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 872 | } |
[email protected] | 9492e4a | 2010-02-24 00:58:46 | [diff] [blame] | 873 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 874 | bool keep_alive = false; |
[email protected] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 875 | if (stream_->IsResponseBodyComplete()) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 876 | // Note: Just because IsResponseBodyComplete is true, we're not |
| 877 | // necessarily "done". We're only "done" when it is the last |
| 878 | // read on this HttpNetworkTransaction, which will be signified |
| 879 | // by a zero-length read. |
| 880 | // TODO(mbelshe): The keepalive property is really a property of |
| 881 | // the stream. No need to compute it here just to pass back |
| 882 | // to the stream's Close function. |
[email protected] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 883 | if (stream_->CanFindEndOfResponse()) |
[email protected] | 02c92c49 | 2010-03-08 21:28:14 | [diff] [blame] | 884 | keep_alive = GetResponseHeaders()->IsKeepAlive(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 885 | } |
| 886 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 887 | // Clean up connection if we are done. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 888 | if (done) { |
[email protected] | 5630017 | 2008-11-06 18:42:55 | [diff] [blame] | 889 | LogTransactionMetrics(); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 890 | stream_->Close(!keep_alive); |
[email protected] | e2a915a | 2010-08-19 07:55:01 | [diff] [blame] | 891 | // Note: we don't reset the stream here. We've closed it, but we still |
| 892 | // need it around so that callers can call methods such as |
| 893 | // GetUploadProgress() and have them be meaningful. |
| 894 | // TODO(mbelshe): This means we closed the stream here, and we close it |
| 895 | // again in ~HttpNetworkTransaction. Clean that up. |
| 896 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 897 | // The next Read call will return 0 (EOF). |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | // Clear these to avoid leaving around old state. |
| 901 | read_buf_ = NULL; |
| 902 | read_buf_len_ = 0; |
| 903 | |
| 904 | return result; |
| 905 | } |
| 906 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 907 | int HttpNetworkTransaction::DoDrainBodyForAuthRestart() { |
| 908 | // This method differs from DoReadBody only in the next_state_. So we just |
| 909 | // call DoReadBody and override the next_state_. Perhaps there is a more |
| 910 | // elegant way for these two methods to share code. |
| 911 | int rv = DoReadBody(); |
| 912 | DCHECK(next_state_ == STATE_READ_BODY_COMPLETE); |
| 913 | next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE; |
| 914 | return rv; |
| 915 | } |
| 916 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 917 | // TODO(wtc): This method and the DoReadBodyComplete method are almost |
| 918 | // the same. Figure out a good way for these two methods to share code. |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 919 | int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result) { |
[email protected] | 68873ba | 2009-06-04 21:49:23 | [diff] [blame] | 920 | // keep_alive defaults to true because the very reason we're draining the |
| 921 | // response body is to reuse the connection for auth restart. |
| 922 | bool done = false, keep_alive = true; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 923 | if (result < 0) { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 924 | // Error or closed connection while reading the socket. |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 925 | done = true; |
[email protected] | 68873ba | 2009-06-04 21:49:23 | [diff] [blame] | 926 | keep_alive = false; |
[email protected] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 927 | } else if (stream_->IsResponseBodyComplete()) { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 928 | done = true; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | if (done) { |
| 932 | DidDrainBodyForAuthRestart(keep_alive); |
| 933 | } else { |
| 934 | // Keep draining. |
| 935 | next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART; |
| 936 | } |
| 937 | |
| 938 | return OK; |
| 939 | } |
| 940 | |
[email protected] | 8e3d2d3 | 2010-06-13 18:46:23 | [diff] [blame] | 941 | void HttpNetworkTransaction::LogTransactionConnectedMetrics() { |
| 942 | if (logged_response_time_) |
| 943 | return; |
| 944 | |
| 945 | logged_response_time_ = true; |
| 946 | |
[email protected] | a7e4131 | 2009-12-16 23:18:14 | [diff] [blame] | 947 | base::TimeDelta total_duration = response_.response_time - start_time_; |
[email protected] | 9a0a55f | 2009-04-13 23:23:03 | [diff] [blame] | 948 | |
[email protected] | 510e854f | 2009-04-20 18:39:08 | [diff] [blame] | 949 | UMA_HISTOGRAM_CLIPPED_TIMES( |
[email protected] | f929f2f2 | 2009-06-12 16:56:58 | [diff] [blame] | 950 | "Net.Transaction_Connected_Under_10", |
[email protected] | 510e854f | 2009-04-20 18:39:08 | [diff] [blame] | 951 | total_duration, |
[email protected] | 9a0a55f | 2009-04-13 23:23:03 | [diff] [blame] | 952 | base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(10), |
| 953 | 100); |
[email protected] | 1fa4759 | 2009-07-27 22:45:00 | [diff] [blame] | 954 | |
[email protected] | c38ec5a | 2010-08-25 23:32:38 | [diff] [blame] | 955 | bool reused_socket = stream_->IsConnectionReused(); |
| 956 | if (!reused_socket) { |
[email protected] | b01998a | 2009-04-21 01:01:11 | [diff] [blame] | 957 | UMA_HISTOGRAM_CLIPPED_TIMES( |
[email protected] | f929f2f2 | 2009-06-12 16:56:58 | [diff] [blame] | 958 | "Net.Transaction_Connected_New", |
[email protected] | b01998a | 2009-04-21 01:01:11 | [diff] [blame] | 959 | total_duration, |
| 960 | base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(10), |
| 961 | 100); |
[email protected] | d068f7a | 2010-06-07 15:12:59 | [diff] [blame] | 962 | |
| 963 | static bool use_conn_impact_histogram( |
| 964 | FieldTrialList::Find("ConnCountImpact") && |
| 965 | !FieldTrialList::Find("ConnCountImpact")->group_name().empty()); |
| 966 | if (use_conn_impact_histogram) { |
| 967 | UMA_HISTOGRAM_CLIPPED_TIMES( |
| 968 | FieldTrial::MakeName("Net.Transaction_Connected_New", |
| 969 | "ConnCountImpact"), |
| 970 | total_duration, |
| 971 | base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(10), |
| 972 | 100); |
| 973 | } |
[email protected] | 0310d43 | 2009-08-25 07:49:52 | [diff] [blame] | 974 | } |
| 975 | |
[email protected] | 8e3d2d3 | 2010-06-13 18:46:23 | [diff] [blame] | 976 | static bool use_spdy_histogram(FieldTrialList::Find("SpdyImpact") && |
| 977 | !FieldTrialList::Find("SpdyImpact")->group_name().empty()); |
| 978 | if (use_spdy_histogram && response_.was_npn_negotiated) { |
| 979 | UMA_HISTOGRAM_CLIPPED_TIMES( |
| 980 | FieldTrial::MakeName("Net.Transaction_Connected_Under_10", "SpdyImpact"), |
| 981 | total_duration, base::TimeDelta::FromMilliseconds(1), |
| 982 | base::TimeDelta::FromMinutes(10), 100); |
| 983 | |
[email protected] | c38ec5a | 2010-08-25 23:32:38 | [diff] [blame] | 984 | if (!reused_socket) { |
[email protected] | 8e3d2d3 | 2010-06-13 18:46:23 | [diff] [blame] | 985 | UMA_HISTOGRAM_CLIPPED_TIMES( |
| 986 | FieldTrial::MakeName("Net.Transaction_Connected_New", "SpdyImpact"), |
| 987 | total_duration, base::TimeDelta::FromMilliseconds(1), |
| 988 | base::TimeDelta::FromMinutes(10), 100); |
| 989 | } |
| 990 | } |
| 991 | |
[email protected] | 510e854f | 2009-04-20 18:39:08 | [diff] [blame] | 992 | // Currently, non-zero priority requests are frame or sub-frame resource |
| 993 | // types. This will change when we also prioritize certain subresources like |
| 994 | // css, js, etc. |
| 995 | if (request_->priority) { |
| 996 | UMA_HISTOGRAM_CLIPPED_TIMES( |
[email protected] | f929f2f2 | 2009-06-12 16:56:58 | [diff] [blame] | 997 | "Net.Priority_High_Latency", |
[email protected] | 510e854f | 2009-04-20 18:39:08 | [diff] [blame] | 998 | total_duration, |
| 999 | base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(10), |
| 1000 | 100); |
| 1001 | } else { |
| 1002 | UMA_HISTOGRAM_CLIPPED_TIMES( |
[email protected] | f929f2f2 | 2009-06-12 16:56:58 | [diff] [blame] | 1003 | "Net.Priority_Low_Latency", |
[email protected] | 510e854f | 2009-04-20 18:39:08 | [diff] [blame] | 1004 | total_duration, |
| 1005 | base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(10), |
| 1006 | 100); |
| 1007 | } |
[email protected] | 9a0a55f | 2009-04-13 23:23:03 | [diff] [blame] | 1008 | } |
| 1009 | |
[email protected] | 5630017 | 2008-11-06 18:42:55 | [diff] [blame] | 1010 | void HttpNetworkTransaction::LogTransactionMetrics() const { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1011 | base::TimeDelta duration = base::Time::Now() - |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 1012 | response_.request_time; |
[email protected] | 5630017 | 2008-11-06 18:42:55 | [diff] [blame] | 1013 | if (60 < duration.InMinutes()) |
| 1014 | return; |
[email protected] | 0b48db4 | 2009-03-23 02:45:11 | [diff] [blame] | 1015 | |
[email protected] | 21b316a | 2009-03-23 18:25:06 | [diff] [blame] | 1016 | base::TimeDelta total_duration = base::Time::Now() - start_time_; |
| 1017 | |
[email protected] | f929f2f2 | 2009-06-12 16:56:58 | [diff] [blame] | 1018 | UMA_HISTOGRAM_LONG_TIMES("Net.Transaction_Latency", duration); |
| 1019 | UMA_HISTOGRAM_CLIPPED_TIMES("Net.Transaction_Latency_Under_10", duration, |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 1020 | base::TimeDelta::FromMilliseconds(1), |
| 1021 | base::TimeDelta::FromMinutes(10), |
| 1022 | 100); |
[email protected] | f929f2f2 | 2009-06-12 16:56:58 | [diff] [blame] | 1023 | UMA_HISTOGRAM_CLIPPED_TIMES("Net.Transaction_Latency_Total_Under_10", |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 1024 | total_duration, |
| 1025 | base::TimeDelta::FromMilliseconds(1), |
| 1026 | base::TimeDelta::FromMinutes(10), 100); |
[email protected] | c38ec5a | 2010-08-25 23:32:38 | [diff] [blame] | 1027 | if (!stream_->IsConnectionReused()) { |
[email protected] | f929f2f2 | 2009-06-12 16:56:58 | [diff] [blame] | 1028 | UMA_HISTOGRAM_CLIPPED_TIMES( |
[email protected] | 808f640 | 2009-03-30 20:02:07 | [diff] [blame] | 1029 | "Net.Transaction_Latency_Total_New_Connection_Under_10", |
[email protected] | 808f640 | 2009-03-30 20:02:07 | [diff] [blame] | 1030 | total_duration, base::TimeDelta::FromMilliseconds(1), |
| 1031 | base::TimeDelta::FromMinutes(10), 100); |
| 1032 | } |
[email protected] | 5630017 | 2008-11-06 18:42:55 | [diff] [blame] | 1033 | } |
| 1034 | |
[email protected] | 5e36396 | 2009-06-19 19:57:01 | [diff] [blame] | 1035 | int HttpNetworkTransaction::HandleCertificateRequest(int error) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1036 | // There are two paths through which the server can request a certificate |
| 1037 | // from us. The first is during the initial handshake, the second is |
| 1038 | // during SSL renegotiation. |
| 1039 | // |
| 1040 | // In both cases, we want to close the connection before proceeding. |
| 1041 | // We do this for two reasons: |
| 1042 | // First, we don't want to keep the connection to the server hung for a |
| 1043 | // long time while the user selects a certificate. |
| 1044 | // Second, even if we did keep the connection open, NSS has a bug where |
| 1045 | // restarting the handshake for ClientAuth is currently broken. |
[email protected] | 65a3b91 | 2010-08-21 05:46:58 | [diff] [blame] | 1046 | DCHECK_EQ(error, ERR_SSL_CLIENT_AUTH_CERT_NEEDED); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1047 | |
| 1048 | if (stream_.get()) { |
| 1049 | // Since we already have a stream, we're being called as part of SSL |
| 1050 | // renegotiation. |
| 1051 | DCHECK(!stream_request_.get()); |
| 1052 | stream_->Close(true); |
| 1053 | stream_.reset(); |
| 1054 | } |
| 1055 | |
| 1056 | if (stream_request_.get()) { |
| 1057 | // The server is asking for a client certificate during the initial |
| 1058 | // handshake. |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1059 | stream_request_->Cancel(); |
| 1060 | stream_request_ = NULL; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1061 | } |
[email protected] | 5e36396 | 2009-06-19 19:57:01 | [diff] [blame] | 1062 | |
| 1063 | // If the user selected one of the certificate in client_certs for this |
| 1064 | // server before, use it automatically. |
| 1065 | X509Certificate* client_cert = session_->ssl_client_auth_cache()-> |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 1066 | Lookup(GetHostAndPort(request_->url)); |
[email protected] | 5e36396 | 2009-06-19 19:57:01 | [diff] [blame] | 1067 | if (client_cert) { |
| 1068 | const std::vector<scoped_refptr<X509Certificate> >& client_certs = |
[email protected] | a7e4131 | 2009-12-16 23:18:14 | [diff] [blame] | 1069 | response_.cert_request_info->client_certs; |
[email protected] | 5e36396 | 2009-06-19 19:57:01 | [diff] [blame] | 1070 | for (size_t i = 0; i < client_certs.size(); ++i) { |
[email protected] | bd0b743 | 2009-06-23 21:03:42 | [diff] [blame] | 1071 | if (client_cert->fingerprint().Equals(client_certs[i]->fingerprint())) { |
[email protected] | 65a3b91 | 2010-08-21 05:46:58 | [diff] [blame] | 1072 | // TODO(davidben): Add a unit test which covers this path; we need to be |
| 1073 | // able to send a legitimate certificate and also bypass/clear the |
| 1074 | // SSL session cache. |
[email protected] | 5e36396 | 2009-06-19 19:57:01 | [diff] [blame] | 1075 | ssl_config_.client_cert = client_cert; |
| 1076 | ssl_config_.send_client_cert = true; |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 1077 | next_state_ = STATE_CREATE_STREAM; |
[email protected] | 5e36396 | 2009-06-19 19:57:01 | [diff] [blame] | 1078 | // Reset the other member variables. |
| 1079 | // Note: this is necessary only with SSL renegotiation. |
| 1080 | ResetStateForRestart(); |
| 1081 | return OK; |
| 1082 | } |
| 1083 | } |
| 1084 | } |
| 1085 | return error; |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 1086 | } |
| 1087 | |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 1088 | // This method determines whether it is safe to resend the request after an |
| 1089 | // IO error. It can only be called in response to request header or body |
| 1090 | // write errors or response header read errors. It should not be used in |
| 1091 | // other cases, such as a Connect error. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1092 | int HttpNetworkTransaction::HandleIOError(int error) { |
| 1093 | switch (error) { |
| 1094 | // If we try to reuse a connection that the server is in the process of |
| 1095 | // closing, we may end up successfully writing out our request (or a |
| 1096 | // portion of our request) only to find a connection error when we try to |
| 1097 | // read from (or finish writing to) the socket. |
| 1098 | case ERR_CONNECTION_RESET: |
| 1099 | case ERR_CONNECTION_CLOSED: |
| 1100 | case ERR_CONNECTION_ABORTED: |
[email protected] | a19f1c60 | 2009-08-24 21:35:28 | [diff] [blame] | 1101 | if (ShouldResendRequest(error)) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1102 | ResetConnectionAndRequestForResend(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1103 | error = OK; |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1104 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1105 | break; |
| 1106 | } |
| 1107 | return error; |
| 1108 | } |
| 1109 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1110 | void HttpNetworkTransaction::ResetStateForRestart() { |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 1111 | pending_auth_target_ = HttpAuth::AUTH_NONE; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1112 | read_buf_ = NULL; |
| 1113 | read_buf_len_ = 0; |
[email protected] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 1114 | stream_.reset(); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1115 | headers_valid_ = false; |
| 1116 | request_headers_.clear(); |
[email protected] | a7e4131 | 2009-12-16 23:18:14 | [diff] [blame] | 1117 | response_ = HttpResponseInfo(); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1118 | establishing_tunnel_ = false; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1119 | } |
| 1120 | |
| 1121 | HttpResponseHeaders* HttpNetworkTransaction::GetResponseHeaders() const { |
[email protected] | a7e4131 | 2009-12-16 23:18:14 | [diff] [blame] | 1122 | return response_.headers; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1123 | } |
| 1124 | |
[email protected] | a19f1c60 | 2009-08-24 21:35:28 | [diff] [blame] | 1125 | bool HttpNetworkTransaction::ShouldResendRequest(int error) const { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1126 | bool connection_is_proven = stream_->IsConnectionReused(); |
| 1127 | bool has_received_headers = GetResponseHeaders() != NULL; |
[email protected] | 58cebf8f | 2010-07-31 19:20:16 | [diff] [blame] | 1128 | |
[email protected] | 2a5c76b | 2008-09-25 22:15:16 | [diff] [blame] | 1129 | // NOTE: we resend a request only if we reused a keep-alive connection. |
| 1130 | // This automatically prevents an infinite resend loop because we'll run |
| 1131 | // out of the cached keep-alive connections eventually. |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1132 | if (connection_is_proven && !has_received_headers) |
| 1133 | return true; |
| 1134 | return false; |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1135 | } |
| 1136 | |
| 1137 | void HttpNetworkTransaction::ResetConnectionAndRequestForResend() { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1138 | if (stream_.get()) { |
| 1139 | stream_->Close(true); |
| 1140 | stream_.reset(); |
[email protected] | 58cebf8f | 2010-07-31 19:20:16 | [diff] [blame] | 1141 | } |
| 1142 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1143 | // We need to clear request_headers_ because it contains the real request |
| 1144 | // headers, but we may need to resend the CONNECT request first to recreate |
| 1145 | // the SSL tunnel. |
| 1146 | request_headers_.clear(); |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 1147 | next_state_ = STATE_CREATE_STREAM; // Resend the request. |
[email protected] | 86ec30d | 2008-09-29 21:53:54 | [diff] [blame] | 1148 | } |
| 1149 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1150 | bool HttpNetworkTransaction::ShouldApplyProxyAuth() const { |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 1151 | return !is_https_request() && |
| 1152 | (proxy_info_.is_https() || proxy_info_.is_http()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1153 | } |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1154 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1155 | bool HttpNetworkTransaction::ShouldApplyServerAuth() const { |
[email protected] | 8a1f331 | 2010-05-25 19:25:04 | [diff] [blame] | 1156 | return !(request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1157 | } |
| 1158 | |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 1159 | int HttpNetworkTransaction::HandleAuthChallenge() { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1160 | scoped_refptr<HttpResponseHeaders> headers = GetResponseHeaders(); |
| 1161 | DCHECK(headers); |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1162 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1163 | int status = headers->response_code(); |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1164 | if (status != 401 && status != 407) |
| 1165 | return OK; |
| 1166 | HttpAuth::Target target = status == 407 ? |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 1167 | HttpAuth::AUTH_PROXY : HttpAuth::AUTH_SERVER; |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1168 | if (target == HttpAuth::AUTH_PROXY && proxy_info_.is_direct()) |
| 1169 | return ERR_UNEXPECTED_PROXY_AUTH; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1170 | |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 1171 | int rv = auth_controllers_[target]->HandleAuthChallenge( |
[email protected] | 560c043 | 2010-07-13 20:45:31 | [diff] [blame] | 1172 | headers, (request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA) != 0, false, |
| 1173 | net_log_); |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 1174 | if (auth_controllers_[target]->HaveAuthHandler()) |
| 1175 | pending_auth_target_ = target; |
| 1176 | |
| 1177 | scoped_refptr<AuthChallengeInfo> auth_info = |
| 1178 | auth_controllers_[target]->auth_info(); |
| 1179 | if (auth_info.get()) |
| 1180 | response_.auth_challenge = auth_info; |
| 1181 | |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 1182 | return rv; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1183 | } |
| 1184 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1185 | bool HttpNetworkTransaction::HaveAuth(HttpAuth::Target target) const { |
| 1186 | return auth_controllers_[target].get() && |
| 1187 | auth_controllers_[target]->HaveAuth(); |
| 1188 | } |
| 1189 | |
| 1190 | |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 1191 | GURL HttpNetworkTransaction::AuthURL(HttpAuth::Target target) const { |
| 1192 | switch (target) { |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 1193 | case HttpAuth::AUTH_PROXY: { |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 1194 | if (!proxy_info_.proxy_server().is_valid() || |
| 1195 | proxy_info_.proxy_server().is_direct()) { |
| 1196 | return GURL(); // There is no proxy server. |
| 1197 | } |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 1198 | const char* scheme = proxy_info_.is_https() ? "https://" : "http://"; |
| 1199 | return GURL(scheme + |
[email protected] | 2fbaecf2 | 2010-07-22 22:20:35 | [diff] [blame] | 1200 | proxy_info_.proxy_server().host_port_pair().ToString()); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 1201 | } |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 1202 | case HttpAuth::AUTH_SERVER: |
| 1203 | return request_->url; |
| 1204 | default: |
| 1205 | return GURL(); |
| 1206 | } |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1207 | } |
| 1208 | |
[email protected] | aef0427 | 2010-06-28 18:03:04 | [diff] [blame] | 1209 | #define STATE_CASE(s) case s: \ |
| 1210 | description = StringPrintf("%s (0x%08X)", #s, s); \ |
| 1211 | break |
| 1212 | |
| 1213 | std::string HttpNetworkTransaction::DescribeState(State state) { |
| 1214 | std::string description; |
| 1215 | switch (state) { |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 1216 | STATE_CASE(STATE_CREATE_STREAM); |
| 1217 | STATE_CASE(STATE_CREATE_STREAM_COMPLETE); |
[email protected] | aef0427 | 2010-06-28 18:03:04 | [diff] [blame] | 1218 | STATE_CASE(STATE_SEND_REQUEST); |
| 1219 | STATE_CASE(STATE_SEND_REQUEST_COMPLETE); |
| 1220 | STATE_CASE(STATE_READ_HEADERS); |
| 1221 | STATE_CASE(STATE_READ_HEADERS_COMPLETE); |
[email protected] | aef0427 | 2010-06-28 18:03:04 | [diff] [blame] | 1222 | STATE_CASE(STATE_READ_BODY); |
| 1223 | STATE_CASE(STATE_READ_BODY_COMPLETE); |
| 1224 | STATE_CASE(STATE_DRAIN_BODY_FOR_AUTH_RESTART); |
| 1225 | STATE_CASE(STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE); |
[email protected] | aef0427 | 2010-06-28 18:03:04 | [diff] [blame] | 1226 | STATE_CASE(STATE_NONE); |
| 1227 | default: |
| 1228 | description = StringPrintf("Unknown state 0x%08X (%u)", state, state); |
| 1229 | break; |
| 1230 | } |
| 1231 | return description; |
| 1232 | } |
| 1233 | |
[email protected] | c9c6f5c | 2010-07-31 01:30:03 | [diff] [blame] | 1234 | // TODO(gavinp): re-adjust this once SPDY v3 has three priority bits, |
| 1235 | // eliminating the need for this folding. |
| 1236 | int ConvertRequestPriorityToSpdyPriority(const RequestPriority priority) { |
| 1237 | DCHECK(HIGHEST <= priority && priority < NUM_PRIORITIES); |
| 1238 | switch (priority) { |
| 1239 | case LOWEST: |
| 1240 | return SPDY_PRIORITY_LOWEST-1; |
| 1241 | case IDLE: |
| 1242 | return SPDY_PRIORITY_LOWEST; |
| 1243 | default: |
| 1244 | return priority; |
| 1245 | } |
| 1246 | } |
| 1247 | |
| 1248 | |
| 1249 | |
[email protected] | aef0427 | 2010-06-28 18:03:04 | [diff] [blame] | 1250 | #undef STATE_CASE |
| 1251 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1252 | } // namespace net |