[email protected] | ac3fa8e2 | 2010-02-05 19:13:29 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 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] | 68bf915 | 2008-09-25 19:47:30 | [diff] [blame] | 7 | #include "base/compiler_specific.h" |
[email protected] | 0b48db4 | 2009-03-23 02:45:11 | [diff] [blame] | 8 | #include "base/field_trial.h" |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 9 | #include "base/format_macros.h" |
[email protected] | 2198394 | 2009-08-18 06:17:50 | [diff] [blame] | 10 | #include "base/histogram.h" |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 11 | #include "base/scoped_ptr.h" |
[email protected] | 5e2e6c77d1 | 2009-12-24 21:57:16 | [diff] [blame] | 12 | #include "base/stats_counters.h" |
[email protected] | aeaca1f | 2010-04-20 22:05:21 | [diff] [blame] | 13 | #include "base/stl_util-inl.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 14 | #include "base/string_util.h" |
[email protected] | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 15 | #include "base/trace_event.h" |
[email protected] | 169d001 | 2010-05-10 23:20:12 | [diff] [blame] | 16 | #include "base/values.h" |
[email protected] | 68bf915 | 2008-09-25 19:47:30 | [diff] [blame] | 17 | #include "build/build_config.h" |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 18 | #include "googleurl/src/gurl.h" |
[email protected] | 5d0153c51 | 2009-01-12 19:08:36 | [diff] [blame] | 19 | #include "net/base/connection_type_histograms.h" |
[email protected] | b6a5018 | 2010-05-12 22:47:14 | [diff] [blame] | 20 | #include "net/base/host_mapping_rules.h" |
[email protected] | 74a85ce | 2009-02-12 00:03:19 | [diff] [blame] | 21 | #include "net/base/io_buffer.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 22 | #include "net/base/load_flags.h" |
[email protected] | 597cf6e | 2009-05-29 09:43:26 | [diff] [blame] | 23 | #include "net/base/net_errors.h" |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 24 | #include "net/base/net_util.h" |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 25 | #include "net/base/ssl_cert_request_info.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 26 | #include "net/base/upload_data_stream.h" |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 27 | #include "net/http/http_auth.h" |
| 28 | #include "net/http/http_auth_handler.h" |
[email protected] | fa82f93 | 2010-05-20 11:09:24 | [diff] [blame] | 29 | #include "net/http/http_auth_handler_factory.h" |
[email protected] | 8d5a34e | 2009-06-11 21:21:36 | [diff] [blame] | 30 | #include "net/http/http_basic_stream.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 31 | #include "net/http/http_chunked_decoder.h" |
| 32 | #include "net/http/http_network_session.h" |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 33 | #include "net/http/http_request_headers.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 34 | #include "net/http/http_request_info.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 35 | #include "net/http/http_response_headers.h" |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 36 | #include "net/http/http_response_info.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 37 | #include "net/http/http_util.h" |
[email protected] | d7f1663 | 2010-03-29 18:02:36 | [diff] [blame] | 38 | #include "net/http/url_security_manager.h" |
[email protected] | f7984fc6 | 2009-06-22 23:26:44 | [diff] [blame] | 39 | #include "net/socket/client_socket_factory.h" |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 40 | #include "net/socket/socks_client_socket_pool.h" |
[email protected] | f7984fc6 | 2009-06-22 23:26:44 | [diff] [blame] | 41 | #include "net/socket/ssl_client_socket.h" |
[email protected] | 7fc5b09a | 2010-02-27 00:07:38 | [diff] [blame] | 42 | #include "net/socket/tcp_client_socket_pool.h" |
[email protected] | dab9c7d | 2010-02-06 21:44:32 | [diff] [blame] | 43 | #include "net/spdy/spdy_session.h" |
| 44 | #include "net/spdy/spdy_session_pool.h" |
| 45 | #include "net/spdy/spdy_stream.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 46 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 47 | using base::Time; |
| 48 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 49 | namespace net { |
| 50 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 51 | namespace { |
| 52 | |
[email protected] | b6a5018 | 2010-05-12 22:47:14 | [diff] [blame] | 53 | const HostMappingRules* g_host_mapping_rules = NULL; |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 54 | const std::string* g_next_protos = NULL; |
[email protected] | 31e2c69e | 2010-04-15 18:06:06 | [diff] [blame] | 55 | bool g_use_alternate_protocols = false; |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 56 | |
[email protected] | aeaca1f | 2010-04-20 22:05:21 | [diff] [blame] | 57 | // A set of host:port strings. These are servers which we have needed to back |
| 58 | // off to SSLv3 for. |
| 59 | std::set<std::string>* g_tls_intolerant_servers = NULL; |
| 60 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 61 | void BuildRequestHeaders(const HttpRequestInfo* request_info, |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 62 | const HttpRequestHeaders& authorization_headers, |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 63 | const UploadDataStream* upload_data_stream, |
| 64 | bool using_proxy, |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 65 | std::string* request_line, |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 66 | HttpRequestHeaders* request_headers) { |
| 67 | const std::string path = using_proxy ? |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 68 | HttpUtil::SpecForRequest(request_info->url) : |
| 69 | HttpUtil::PathForRequest(request_info->url); |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 70 | *request_line = StringPrintf( |
| 71 | "%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] | 72 | request_headers->SetHeader(HttpRequestHeaders::kHost, |
| 73 | GetHostAndOptionalPort(request_info->url)); |
| 74 | |
| 75 | // For compat with HTTP/1.0 servers and proxies: |
| 76 | if (using_proxy) { |
| 77 | request_headers->SetHeader(HttpRequestHeaders::kProxyConnection, |
| 78 | "keep-alive"); |
| 79 | } else { |
| 80 | request_headers->SetHeader(HttpRequestHeaders::kConnection, "keep-alive"); |
| 81 | } |
| 82 | |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 83 | // Our consumer should have made sure that this is a safe referrer. See for |
| 84 | // instance WebCore::FrameLoader::HideReferrer. |
| 85 | if (request_info->referrer.is_valid()) { |
| 86 | request_headers->SetHeader(HttpRequestHeaders::kReferer, |
| 87 | request_info->referrer.spec()); |
| 88 | } |
| 89 | |
| 90 | // Add a content length header? |
| 91 | if (upload_data_stream) { |
| 92 | request_headers->SetHeader( |
| 93 | HttpRequestHeaders::kContentLength, |
| 94 | Uint64ToString(upload_data_stream->size())); |
| 95 | } else if (request_info->method == "POST" || request_info->method == "PUT" || |
| 96 | request_info->method == "HEAD") { |
| 97 | // An empty POST/PUT request still needs a content length. As for HEAD, |
| 98 | // IE and Safari also add a content length header. Presumably it is to |
| 99 | // support sending a HEAD request to an URL that only expects to be sent a |
| 100 | // POST or some other method that normally would have a message body. |
| 101 | request_headers->SetHeader(HttpRequestHeaders::kContentLength, "0"); |
| 102 | } |
| 103 | |
| 104 | // Honor load flags that impact proxy caches. |
| 105 | if (request_info->load_flags & LOAD_BYPASS_CACHE) { |
| 106 | request_headers->SetHeader(HttpRequestHeaders::kPragma, "no-cache"); |
| 107 | request_headers->SetHeader(HttpRequestHeaders::kCacheControl, "no-cache"); |
| 108 | } else if (request_info->load_flags & LOAD_VALIDATE_CACHE) { |
| 109 | request_headers->SetHeader(HttpRequestHeaders::kCacheControl, "max-age=0"); |
| 110 | } |
| 111 | |
| 112 | request_headers->MergeFrom(authorization_headers); |
| 113 | |
[email protected] | 860c85d | 2010-02-10 07:22:40 | [diff] [blame] | 114 | // Headers that will be stripped from request_info->extra_headers to prevent, |
| 115 | // e.g., plugins from overriding headers that are controlled using other |
| 116 | // means. Otherwise a plugin could set a referrer although sending the |
| 117 | // referrer is inhibited. |
| 118 | // TODO(jochen): check whether also other headers should be stripped. |
| 119 | static const char* const kExtraHeadersToBeStripped[] = { |
| 120 | "Referer" |
| 121 | }; |
| 122 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 123 | HttpRequestHeaders stripped_extra_headers; |
| 124 | stripped_extra_headers.CopyFrom(request_info->extra_headers); |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 125 | for (size_t i = 0; i < arraysize(kExtraHeadersToBeStripped); ++i) |
| 126 | stripped_extra_headers.RemoveHeader(kExtraHeadersToBeStripped[i]); |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 127 | request_headers->MergeFrom(stripped_extra_headers); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | // The HTTP CONNECT method for establishing a tunnel connection is documented |
| 131 | // in draft-luotonen-web-proxy-tunneling-01.txt and RFC 2817, Sections 5.2 and |
| 132 | // 5.3. |
| 133 | void BuildTunnelRequest(const HttpRequestInfo* request_info, |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 134 | const HttpRequestHeaders& authorization_headers, |
[email protected] | 9faeded9 | 2010-04-29 20:03:05 | [diff] [blame] | 135 | const HostPortPair& endpoint, |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 136 | std::string* request_line, |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 137 | HttpRequestHeaders* request_headers) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 138 | // RFC 2616 Section 9 says the Host request-header field MUST accompany all |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 139 | // HTTP/1.1 requests. Add "Proxy-Connection: keep-alive" for compat with |
| 140 | // HTTP/1.0 proxies such as Squid (required for NTLM authentication). |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 141 | *request_line = StringPrintf( |
[email protected] | 9faeded9 | 2010-04-29 20:03:05 | [diff] [blame] | 142 | "CONNECT %s HTTP/1.1\r\n", endpoint.ToString().c_str()); |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 143 | request_headers->SetHeader(HttpRequestHeaders::kHost, |
| 144 | GetHostAndOptionalPort(request_info->url)); |
| 145 | request_headers->SetHeader(HttpRequestHeaders::kProxyConnection, |
| 146 | "keep-alive"); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 147 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 148 | std::string user_agent; |
| 149 | if (request_info->extra_headers.GetHeader(HttpRequestHeaders::kUserAgent, |
| 150 | &user_agent)) |
| 151 | request_headers->SetHeader(HttpRequestHeaders::kUserAgent, user_agent); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 152 | |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 153 | request_headers->MergeFrom(authorization_headers); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 154 | } |
| 155 | |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 156 | void ProcessAlternateProtocol(const HttpResponseHeaders& headers, |
| 157 | const HostPortPair& http_host_port_pair, |
| 158 | HttpAlternateProtocols* alternate_protocols) { |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 159 | if (!g_next_protos || g_next_protos->empty()) { |
[email protected] | 31e2c69e | 2010-04-15 18:06:06 | [diff] [blame] | 160 | // This implies that NPN is not supported. We don't currently support any |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 161 | // alternate protocols that don't use NPN. |
| 162 | return; |
| 163 | } |
| 164 | |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 165 | std::string alternate_protocol_str; |
| 166 | if (!headers.EnumerateHeader(NULL, HttpAlternateProtocols::kHeader, |
| 167 | &alternate_protocol_str)) { |
| 168 | // Header is not present. |
| 169 | return; |
| 170 | } |
| 171 | |
| 172 | std::vector<std::string> port_protocol_vector; |
| 173 | SplitString(alternate_protocol_str, ':', &port_protocol_vector); |
| 174 | if (port_protocol_vector.size() != 2) { |
| 175 | DLOG(WARNING) << HttpAlternateProtocols::kHeader |
| 176 | << " header has too many tokens: " |
| 177 | << alternate_protocol_str; |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | int port; |
| 182 | if (!StringToInt(port_protocol_vector[0], &port) || |
| 183 | port <= 0 || port >= 1 << 16) { |
| 184 | DLOG(WARNING) << HttpAlternateProtocols::kHeader |
| 185 | << " header has unrecognizable port: " |
| 186 | << port_protocol_vector[0]; |
| 187 | return; |
| 188 | } |
| 189 | |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 190 | if (port_protocol_vector[1] != |
| 191 | HttpAlternateProtocols::kProtocolStrings[ |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 192 | HttpAlternateProtocols::NPN_SPDY_1]) { |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 193 | // Currently, we only recognize the npn-spdy protocol. |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 194 | DLOG(WARNING) << HttpAlternateProtocols::kHeader |
| 195 | << " header has unrecognized protocol: " |
| 196 | << port_protocol_vector[1]; |
| 197 | return; |
| 198 | } |
| 199 | |
[email protected] | b6a5018 | 2010-05-12 22:47:14 | [diff] [blame] | 200 | HostPortPair host_port(http_host_port_pair); |
| 201 | if (g_host_mapping_rules) |
| 202 | g_host_mapping_rules->RewriteHost(&host_port); |
| 203 | |
| 204 | if (alternate_protocols->HasAlternateProtocolFor(host_port)) { |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 205 | const HttpAlternateProtocols::PortProtocolPair existing_alternate = |
[email protected] | b6a5018 | 2010-05-12 22:47:14 | [diff] [blame] | 206 | alternate_protocols->GetAlternateProtocolFor(host_port); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 207 | // If we think the alternate protocol is broken, don't change it. |
| 208 | if (existing_alternate.protocol == HttpAlternateProtocols::BROKEN) |
| 209 | return; |
| 210 | } |
| 211 | |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 212 | alternate_protocols->SetAlternateProtocolFor( |
[email protected] | b6a5018 | 2010-05-12 22:47:14 | [diff] [blame] | 213 | host_port, port, HttpAlternateProtocols::NPN_SPDY_1); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 214 | } |
| 215 | |
[email protected] | 169d001 | 2010-05-10 23:20:12 | [diff] [blame] | 216 | class NetLogHttpRequestParameter : public NetLog::EventParameters { |
| 217 | public: |
| 218 | NetLogHttpRequestParameter(const std::string& line, |
| 219 | const HttpRequestHeaders& headers) |
| 220 | : line_(line) { |
| 221 | headers_.CopyFrom(headers); |
| 222 | } |
| 223 | |
| 224 | Value* ToValue() const { |
| 225 | DictionaryValue* dict = new DictionaryValue(); |
| 226 | dict->SetString(L"line", line_); |
| 227 | ListValue* headers = new ListValue(); |
| 228 | HttpRequestHeaders::Iterator iterator(headers_); |
| 229 | while (iterator.GetNext()) { |
| 230 | headers->Append( |
| 231 | new StringValue(StringPrintf("%s: %s", |
| 232 | iterator.name().c_str(), |
| 233 | iterator.value().c_str()))); |
| 234 | } |
| 235 | dict->Set(L"headers", headers); |
| 236 | return dict; |
| 237 | } |
| 238 | |
| 239 | private: |
| 240 | ~NetLogHttpRequestParameter() {} |
| 241 | |
| 242 | const std::string line_; |
| 243 | HttpRequestHeaders headers_; |
| 244 | |
| 245 | DISALLOW_COPY_AND_ASSIGN(NetLogHttpRequestParameter); |
| 246 | }; |
| 247 | |
[email protected] | dbb83db | 2010-05-11 18:13:39 | [diff] [blame] | 248 | class NetLogHttpResponseParameter : public NetLog::EventParameters { |
| 249 | public: |
| 250 | explicit NetLogHttpResponseParameter( |
| 251 | const scoped_refptr<HttpResponseHeaders>& headers) |
| 252 | : headers_(headers) {} |
| 253 | |
| 254 | Value* ToValue() const { |
| 255 | DictionaryValue* dict = new DictionaryValue(); |
| 256 | ListValue* headers = new ListValue(); |
[email protected] | eed8c288 | 2010-05-14 18:10:42 | [diff] [blame] | 257 | headers->Append(new StringValue(headers_->GetStatusLine())); |
[email protected] | dbb83db | 2010-05-11 18:13:39 | [diff] [blame] | 258 | void* iterator = NULL; |
| 259 | std::string name; |
| 260 | std::string value; |
| 261 | while (headers_->EnumerateHeaderLines(&iterator, &name, &value)) { |
| 262 | headers->Append( |
| 263 | new StringValue(StringPrintf("%s: %s", name.c_str(), value.c_str()))); |
| 264 | } |
| 265 | dict->Set(L"headers", headers); |
| 266 | return dict; |
| 267 | } |
| 268 | |
| 269 | private: |
| 270 | ~NetLogHttpResponseParameter() {} |
| 271 | |
| 272 | const scoped_refptr<HttpResponseHeaders> headers_; |
| 273 | |
| 274 | DISALLOW_COPY_AND_ASSIGN(NetLogHttpResponseParameter); |
| 275 | }; |
| 276 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 277 | } // namespace |
| 278 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 279 | //----------------------------------------------------------------------------- |
| 280 | |
[email protected] | 3ce7df0f | 2010-03-03 00:30:50 | [diff] [blame] | 281 | bool HttpNetworkTransaction::g_ignore_certificate_errors = false; |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 282 | |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 283 | HttpNetworkTransaction::HttpNetworkTransaction(HttpNetworkSession* session) |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 284 | : pending_auth_target_(HttpAuth::AUTH_NONE), |
| 285 | ALLOW_THIS_IN_INITIALIZER_LIST( |
[email protected] | 68bf915 | 2008-09-25 19:47:30 | [diff] [blame] | 286 | io_callback_(this, &HttpNetworkTransaction::OnIOComplete)), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 287 | user_callback_(NULL), |
| 288 | session_(session), |
| 289 | request_(NULL), |
| 290 | pac_request_(NULL), |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 291 | connection_(new ClientSocketHandle), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 292 | reused_socket_(false), |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 293 | headers_valid_(false), |
| 294 | logged_response_time(false), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 295 | using_ssl_(false), |
[email protected] | 6b9833e | 2008-09-10 20:32:25 | [diff] [blame] | 296 | establishing_tunnel_(false), |
[email protected] | e099391 | 2010-02-22 23:57:11 | [diff] [blame] | 297 | using_spdy_(false), |
[email protected] | 31e2c69e | 2010-04-15 18:06:06 | [diff] [blame] | 298 | alternate_protocol_mode_( |
| 299 | g_use_alternate_protocols ? kUnspecified : |
| 300 | kDoNotUseAlternateProtocol), |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 301 | embedded_identity_used_(false), |
[email protected] | d7f1663 | 2010-03-29 18:02:36 | [diff] [blame] | 302 | default_credentials_used_(false), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 303 | read_buf_len_(0), |
| 304 | next_state_(STATE_NONE) { |
[email protected] | 2cd713f | 2008-10-21 17:54:28 | [diff] [blame] | 305 | session->ssl_config_service()->GetSSLConfig(&ssl_config_); |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 306 | if (g_next_protos) |
| 307 | ssl_config_.next_protos = *g_next_protos; |
[email protected] | aeaca1f | 2010-04-20 22:05:21 | [diff] [blame] | 308 | if (!g_tls_intolerant_servers) |
| 309 | g_tls_intolerant_servers = new std::set<std::string>; |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | // static |
[email protected] | b6a5018 | 2010-05-12 22:47:14 | [diff] [blame] | 313 | void HttpNetworkTransaction::SetHostMappingRules(const std::string& rules) { |
| 314 | HostMappingRules* host_mapping_rules = new HostMappingRules(); |
| 315 | host_mapping_rules->SetRulesFromString(rules); |
| 316 | delete g_host_mapping_rules; |
| 317 | g_host_mapping_rules = host_mapping_rules; |
| 318 | } |
| 319 | |
| 320 | // static |
[email protected] | 31e2c69e | 2010-04-15 18:06:06 | [diff] [blame] | 321 | void HttpNetworkTransaction::SetUseAlternateProtocols(bool value) { |
| 322 | g_use_alternate_protocols = value; |
| 323 | } |
| 324 | |
| 325 | // static |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 326 | void HttpNetworkTransaction::SetNextProtos(const std::string& next_protos) { |
| 327 | delete g_next_protos; |
| 328 | g_next_protos = new std::string(next_protos); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 329 | } |
| 330 | |
[email protected] | 3ce7df0f | 2010-03-03 00:30:50 | [diff] [blame] | 331 | // static |
| 332 | void HttpNetworkTransaction::IgnoreCertificateErrors(bool enabled) { |
| 333 | g_ignore_certificate_errors = enabled; |
| 334 | } |
| 335 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 336 | int HttpNetworkTransaction::Start(const HttpRequestInfo* request_info, |
| 337 | CompletionCallback* callback, |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 338 | const BoundNetLog& net_log) { |
[email protected] | 5e2e6c77d1 | 2009-12-24 21:57:16 | [diff] [blame] | 339 | SIMPLE_STATS_COUNTER("HttpNetworkTransaction.Count"); |
[email protected] | 5d0153c51 | 2009-01-12 19:08:36 | [diff] [blame] | 340 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 341 | net_log_ = net_log; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 342 | request_ = request_info; |
[email protected] | 21b316a | 2009-03-23 18:25:06 | [diff] [blame] | 343 | start_time_ = base::Time::Now(); |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 344 | |
| 345 | next_state_ = STATE_RESOLVE_PROXY; |
| 346 | int rv = DoLoop(OK); |
| 347 | if (rv == ERR_IO_PENDING) |
| 348 | user_callback_ = callback; |
| 349 | return rv; |
| 350 | } |
| 351 | |
| 352 | int HttpNetworkTransaction::RestartIgnoringLastError( |
| 353 | CompletionCallback* callback) { |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 354 | if (connection_->socket()->IsConnectedAndIdle()) { |
[email protected] | fab9ca5 | 2010-02-19 23:14:48 | [diff] [blame] | 355 | // TODO(wtc): Should we update any of the connection histograms that we |
| 356 | // update in DoSSLConnectComplete if |result| is OK? |
[email protected] | e099391 | 2010-02-22 23:57:11 | [diff] [blame] | 357 | if (using_spdy_) { |
[email protected] | fab9ca5 | 2010-02-19 23:14:48 | [diff] [blame] | 358 | next_state_ = STATE_SPDY_SEND_REQUEST; |
| 359 | } else { |
| 360 | next_state_ = STATE_SEND_REQUEST; |
| 361 | } |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 362 | } else { |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 363 | connection_->socket()->Disconnect(); |
| 364 | connection_->Reset(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 365 | next_state_ = STATE_INIT_CONNECTION; |
| 366 | } |
[email protected] | ccb40e5 | 2008-09-17 20:54:40 | [diff] [blame] | 367 | int rv = DoLoop(OK); |
| 368 | if (rv == ERR_IO_PENDING) |
| 369 | user_callback_ = callback; |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 370 | return rv; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 371 | } |
| 372 | |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 373 | int HttpNetworkTransaction::RestartWithCertificate( |
| 374 | X509Certificate* client_cert, |
| 375 | CompletionCallback* callback) { |
| 376 | ssl_config_.client_cert = client_cert; |
[email protected] | 5e36396 | 2009-06-19 19:57:01 | [diff] [blame] | 377 | if (client_cert) { |
| 378 | session_->ssl_client_auth_cache()->Add(GetHostAndPort(request_->url), |
| 379 | client_cert); |
| 380 | } |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 381 | ssl_config_.send_client_cert = true; |
| 382 | next_state_ = STATE_INIT_CONNECTION; |
| 383 | // Reset the other member variables. |
| 384 | // Note: this is necessary only with SSL renegotiation. |
| 385 | ResetStateForRestart(); |
| 386 | int rv = DoLoop(OK); |
| 387 | if (rv == ERR_IO_PENDING) |
| 388 | user_callback_ = callback; |
| 389 | return rv; |
| 390 | } |
| 391 | |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 392 | int HttpNetworkTransaction::RestartWithAuth( |
| 393 | const std::wstring& username, |
| 394 | const std::wstring& password, |
| 395 | CompletionCallback* callback) { |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 396 | HttpAuth::Target target = pending_auth_target_; |
| 397 | if (target == HttpAuth::AUTH_NONE) { |
| 398 | NOTREACHED(); |
| 399 | return ERR_UNEXPECTED; |
| 400 | } |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 401 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 402 | pending_auth_target_ = HttpAuth::AUTH_NONE; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 403 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 404 | DCHECK(auth_identity_[target].invalid || |
| 405 | (username.empty() && password.empty())); |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 406 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 407 | if (auth_identity_[target].invalid) { |
| 408 | // Update the username/password. |
| 409 | auth_identity_[target].source = HttpAuth::IDENT_SRC_EXTERNAL; |
| 410 | auth_identity_[target].invalid = false; |
| 411 | auth_identity_[target].username = username; |
| 412 | auth_identity_[target].password = password; |
| 413 | } |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 414 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 415 | PrepareForAuthRestart(target); |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 416 | |
| 417 | DCHECK(user_callback_ == NULL); |
| 418 | int rv = DoLoop(OK); |
| 419 | if (rv == ERR_IO_PENDING) |
| 420 | user_callback_ = callback; |
| 421 | |
| 422 | return rv; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 423 | } |
| 424 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 425 | void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target) { |
| 426 | DCHECK(HaveAuth(target)); |
| 427 | DCHECK(auth_identity_[target].source != HttpAuth::IDENT_SRC_PATH_LOOKUP); |
| 428 | |
| 429 | // Add the auth entry to the cache before restarting. We don't know whether |
| 430 | // the identity is valid yet, but if it is valid we want other transactions |
| 431 | // to know about it. If an entry for (origin, handler->realm()) already |
| 432 | // exists, we update it. |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 433 | // |
[email protected] | d7f1663 | 2010-03-29 18:02:36 | [diff] [blame] | 434 | // If auth_identity_[target].source is HttpAuth::IDENT_SRC_NONE or |
| 435 | // HttpAuth::IDENT_SRC_DEFAULT_CREDENTIALS, auth_identity_[target] contains |
| 436 | // no identity because identity is not required yet or we're using default |
| 437 | // credentials. |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 438 | // |
| 439 | // TODO(wtc): For NTLM_SSPI, we add the same auth entry to the cache in |
| 440 | // round 1 and round 2, which is redundant but correct. It would be nice |
| 441 | // to add an auth entry to the cache only once, preferrably in round 1. |
| 442 | // See http://crbug.com/21015. |
[email protected] | d7f1663 | 2010-03-29 18:02:36 | [diff] [blame] | 443 | switch (auth_identity_[target].source) { |
| 444 | case HttpAuth::IDENT_SRC_NONE: |
| 445 | case HttpAuth::IDENT_SRC_DEFAULT_CREDENTIALS: |
| 446 | break; |
| 447 | default: |
| 448 | session_->auth_cache()->Add( |
[email protected] | fa82f93 | 2010-05-20 11:09:24 | [diff] [blame] | 449 | AuthOrigin(target), |
| 450 | auth_handler_[target]->realm(), |
| 451 | auth_handler_[target]->scheme(), |
| 452 | auth_handler_[target]->challenge(), |
| 453 | auth_identity_[target].username, |
| 454 | auth_identity_[target].password, |
[email protected] | d7f1663 | 2010-03-29 18:02:36 | [diff] [blame] | 455 | AuthPath(target)); |
| 456 | break; |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 457 | } |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 458 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 459 | bool keep_alive = false; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 460 | // Even if the server says the connection is keep-alive, we have to be |
| 461 | // able to find the end of each response in order to reuse the connection. |
| 462 | if (GetResponseHeaders()->IsKeepAlive() && |
| 463 | http_stream_->CanFindEndOfResponse()) { |
| 464 | // If the response body hasn't been completely read, we need to drain |
| 465 | // it first. |
| 466 | if (!http_stream_->IsResponseBodyComplete()) { |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 467 | next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 468 | read_buf_ = new IOBuffer(kDrainBodyBufferSize); // A bit bucket. |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 469 | read_buf_len_ = kDrainBodyBufferSize; |
| 470 | return; |
| 471 | } |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 472 | keep_alive = true; |
[email protected] | 37832c6d | 2009-06-05 19:44:09 | [diff] [blame] | 473 | } |
| 474 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 475 | // We don't need to drain the response body, so we act as if we had drained |
| 476 | // the response body. |
| 477 | DidDrainBodyForAuthRestart(keep_alive); |
| 478 | } |
| 479 | |
| 480 | void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive) { |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 481 | if (keep_alive && connection_->socket()->IsConnectedAndIdle()) { |
| 482 | // We should call connection_->set_idle_time(), but this doesn't occur |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 483 | // often enough to be worth the trouble. |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 484 | next_state_ = STATE_SEND_REQUEST; |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 485 | connection_->set_is_reused(true); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 486 | reused_socket_ = true; |
| 487 | } else { |
| 488 | next_state_ = STATE_INIT_CONNECTION; |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 489 | connection_->socket()->Disconnect(); |
| 490 | connection_->Reset(); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 491 | } |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 492 | |
| 493 | // Reset the other member variables. |
| 494 | ResetStateForRestart(); |
| 495 | } |
| 496 | |
[email protected] | 9dea9e1f | 2009-01-29 00:30:47 | [diff] [blame] | 497 | int HttpNetworkTransaction::Read(IOBuffer* buf, int buf_len, |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 498 | CompletionCallback* callback) { |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 499 | DCHECK(buf); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 500 | DCHECK_LT(0, buf_len); |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 501 | |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 502 | State next_state = STATE_NONE; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 503 | |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 504 | // Are we using SPDY or HTTP? |
[email protected] | e099391 | 2010-02-22 23:57:11 | [diff] [blame] | 505 | if (using_spdy_) { |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 506 | DCHECK(!http_stream_.get()); |
| 507 | DCHECK(spdy_stream_->GetResponseInfo()->headers); |
| 508 | next_state = STATE_SPDY_READ_BODY; |
| 509 | } else { |
[email protected] | e099391 | 2010-02-22 23:57:11 | [diff] [blame] | 510 | DCHECK(!spdy_stream_.get()); |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 511 | scoped_refptr<HttpResponseHeaders> headers = GetResponseHeaders(); |
| 512 | DCHECK(headers.get()); |
| 513 | next_state = STATE_READ_BODY; |
| 514 | |
| 515 | if (!connection_->is_initialized()) |
| 516 | return 0; // connection_->has been reset. Treat like EOF. |
| 517 | |
| 518 | if (establishing_tunnel_) { |
| 519 | // We're trying to read the body of the response but we're still trying |
| 520 | // to establish an SSL tunnel through the proxy. We can't read these |
| 521 | // bytes when establishing a tunnel because they might be controlled by |
| 522 | // an active network attacker. We don't worry about this for HTTP |
| 523 | // because an active network attacker can already control HTTP sessions. |
| 524 | // We reach this case when the user cancels a 407 proxy auth prompt. |
| 525 | // See http://crbug.com/8473. |
| 526 | DCHECK_EQ(407, headers->response_code()); |
| 527 | LogBlockedTunnelResponse(headers->response_code()); |
| 528 | return ERR_TUNNEL_CONNECTION_FAILED; |
| 529 | } |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 530 | } |
| 531 | |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 532 | read_buf_ = buf; |
| 533 | read_buf_len_ = buf_len; |
| 534 | |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 535 | next_state_ = next_state; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 536 | int rv = DoLoop(OK); |
| 537 | if (rv == ERR_IO_PENDING) |
| 538 | user_callback_ = callback; |
| 539 | return rv; |
| 540 | } |
| 541 | |
| 542 | const HttpResponseInfo* HttpNetworkTransaction::GetResponseInfo() const { |
[email protected] | a7e4131 | 2009-12-16 23:18:14 | [diff] [blame] | 543 | return ((headers_valid_ && response_.headers) || response_.ssl_info.cert || |
| 544 | response_.cert_request_info) ? &response_ : NULL; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | LoadState HttpNetworkTransaction::GetLoadState() const { |
| 548 | // TODO(wtc): Define a new LoadState value for the |
| 549 | // STATE_INIT_CONNECTION_COMPLETE state, which delays the HTTP request. |
| 550 | switch (next_state_) { |
| 551 | case STATE_RESOLVE_PROXY_COMPLETE: |
| 552 | return LOAD_STATE_RESOLVING_PROXY_FOR_URL; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 553 | case STATE_INIT_CONNECTION_COMPLETE: |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 554 | return connection_->GetLoadState(); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 555 | case STATE_SEND_REQUEST_COMPLETE: |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 556 | return LOAD_STATE_SENDING_REQUEST; |
| 557 | case STATE_READ_HEADERS_COMPLETE: |
| 558 | return LOAD_STATE_WAITING_FOR_RESPONSE; |
| 559 | case STATE_READ_BODY_COMPLETE: |
| 560 | return LOAD_STATE_READING_RESPONSE; |
| 561 | default: |
| 562 | return LOAD_STATE_IDLE; |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | uint64 HttpNetworkTransaction::GetUploadProgress() const { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 567 | if (!http_stream_.get()) |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 568 | return 0; |
| 569 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 570 | return http_stream_->GetUploadProgress(); |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 571 | } |
| 572 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 573 | HttpNetworkTransaction::~HttpNetworkTransaction() { |
[email protected] | 92d9cad | 2009-06-25 23:40:24 | [diff] [blame] | 574 | // If we still have an open socket, then make sure to disconnect it so it |
| 575 | // won't call us back and we don't try to reuse it later on. |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 576 | if (connection_.get() && connection_->is_initialized()) |
| 577 | connection_->socket()->Disconnect(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 578 | |
| 579 | if (pac_request_) |
| 580 | session_->proxy_service()->CancelPacRequest(pac_request_); |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 581 | |
| 582 | if (spdy_stream_.get()) |
| 583 | spdy_stream_->Cancel(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 584 | } |
| 585 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 586 | void HttpNetworkTransaction::DoCallback(int rv) { |
| 587 | DCHECK(rv != ERR_IO_PENDING); |
| 588 | DCHECK(user_callback_); |
| 589 | |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 590 | // Since Run may result in Read being called, clear user_callback_ up front. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 591 | CompletionCallback* c = user_callback_; |
| 592 | user_callback_ = NULL; |
| 593 | c->Run(rv); |
| 594 | } |
| 595 | |
| 596 | void HttpNetworkTransaction::OnIOComplete(int result) { |
| 597 | int rv = DoLoop(result); |
| 598 | if (rv != ERR_IO_PENDING) |
| 599 | DoCallback(rv); |
| 600 | } |
| 601 | |
| 602 | int HttpNetworkTransaction::DoLoop(int result) { |
| 603 | DCHECK(next_state_ != STATE_NONE); |
| 604 | |
| 605 | int rv = result; |
| 606 | do { |
| 607 | State state = next_state_; |
| 608 | next_state_ = STATE_NONE; |
| 609 | switch (state) { |
| 610 | case STATE_RESOLVE_PROXY: |
[email protected] | 725355a | 2009-03-25 20:42:55 | [diff] [blame] | 611 | DCHECK_EQ(OK, rv); |
[email protected] | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 612 | TRACE_EVENT_BEGIN("http.resolve_proxy", request_, request_->url.spec()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 613 | rv = DoResolveProxy(); |
| 614 | break; |
| 615 | case STATE_RESOLVE_PROXY_COMPLETE: |
| 616 | rv = DoResolveProxyComplete(rv); |
[email protected] | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 617 | TRACE_EVENT_END("http.resolve_proxy", request_, request_->url.spec()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 618 | break; |
| 619 | case STATE_INIT_CONNECTION: |
[email protected] | 725355a | 2009-03-25 20:42:55 | [diff] [blame] | 620 | DCHECK_EQ(OK, rv); |
[email protected] | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 621 | TRACE_EVENT_BEGIN("http.init_conn", request_, request_->url.spec()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 622 | rv = DoInitConnection(); |
| 623 | break; |
| 624 | case STATE_INIT_CONNECTION_COMPLETE: |
| 625 | rv = DoInitConnectionComplete(rv); |
[email protected] | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 626 | TRACE_EVENT_END("http.init_conn", request_, request_->url.spec()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 627 | break; |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 628 | case STATE_SSL_CONNECT: |
[email protected] | 725355a | 2009-03-25 20:42:55 | [diff] [blame] | 629 | DCHECK_EQ(OK, rv); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 630 | TRACE_EVENT_BEGIN("http.ssl_connect", request_, request_->url.spec()); |
| 631 | rv = DoSSLConnect(); |
[email protected] | c7af8b2 | 2008-08-25 20:41:46 | [diff] [blame] | 632 | break; |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 633 | case STATE_SSL_CONNECT_COMPLETE: |
| 634 | rv = DoSSLConnectComplete(rv); |
| 635 | TRACE_EVENT_END("http.ssl_connect", request_, request_->url.spec()); |
[email protected] | c7af8b2 | 2008-08-25 20:41:46 | [diff] [blame] | 636 | break; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 637 | case STATE_SEND_REQUEST: |
[email protected] | 725355a | 2009-03-25 20:42:55 | [diff] [blame] | 638 | DCHECK_EQ(OK, rv); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 639 | TRACE_EVENT_BEGIN("http.send_request", request_, request_->url.spec()); |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 640 | net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST, NULL); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 641 | rv = DoSendRequest(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 642 | break; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 643 | case STATE_SEND_REQUEST_COMPLETE: |
| 644 | rv = DoSendRequestComplete(rv); |
| 645 | TRACE_EVENT_END("http.send_request", request_, request_->url.spec()); |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 646 | net_log_.EndEvent(NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST, NULL); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 647 | break; |
| 648 | case STATE_READ_HEADERS: |
[email protected] | 725355a | 2009-03-25 20:42:55 | [diff] [blame] | 649 | DCHECK_EQ(OK, rv); |
[email protected] | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 650 | TRACE_EVENT_BEGIN("http.read_headers", request_, request_->url.spec()); |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 651 | net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS, NULL); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 652 | rv = DoReadHeaders(); |
| 653 | break; |
| 654 | case STATE_READ_HEADERS_COMPLETE: |
| 655 | rv = DoReadHeadersComplete(rv); |
[email protected] | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 656 | TRACE_EVENT_END("http.read_headers", request_, request_->url.spec()); |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 657 | net_log_.EndEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS, NULL); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 658 | break; |
[email protected] | e5ae96a | 2010-04-14 20:12:45 | [diff] [blame] | 659 | case STATE_RESOLVE_CANONICAL_NAME: |
[email protected] | bd78665 | 2010-04-19 15:17:36 | [diff] [blame] | 660 | DCHECK_EQ(OK, rv); |
| 661 | net_log_.BeginEvent( |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 662 | NetLog::TYPE_HTTP_TRANSACTION_RESOLVE_CANONICAL_NAME, NULL); |
[email protected] | e5ae96a | 2010-04-14 20:12:45 | [diff] [blame] | 663 | rv = DoResolveCanonicalName(); |
| 664 | break; |
| 665 | case STATE_RESOLVE_CANONICAL_NAME_COMPLETE: |
| 666 | rv = DoResolveCanonicalNameComplete(rv); |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 667 | net_log_.EndEvent(NetLog::TYPE_HTTP_TRANSACTION_RESOLVE_CANONICAL_NAME, |
| 668 | NULL); |
[email protected] | e5ae96a | 2010-04-14 20:12:45 | [diff] [blame] | 669 | break; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 670 | case STATE_READ_BODY: |
[email protected] | 725355a | 2009-03-25 20:42:55 | [diff] [blame] | 671 | DCHECK_EQ(OK, rv); |
[email protected] | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 672 | TRACE_EVENT_BEGIN("http.read_body", request_, request_->url.spec()); |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 673 | net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_BODY, NULL); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 674 | rv = DoReadBody(); |
| 675 | break; |
| 676 | case STATE_READ_BODY_COMPLETE: |
| 677 | rv = DoReadBodyComplete(rv); |
[email protected] | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 678 | TRACE_EVENT_END("http.read_body", request_, request_->url.spec()); |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 679 | net_log_.EndEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_BODY, NULL); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 680 | break; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 681 | case STATE_DRAIN_BODY_FOR_AUTH_RESTART: |
[email protected] | 725355a | 2009-03-25 20:42:55 | [diff] [blame] | 682 | DCHECK_EQ(OK, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 683 | TRACE_EVENT_BEGIN("http.drain_body_for_auth_restart", |
| 684 | request_, request_->url.spec()); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 685 | net_log_.BeginEvent( |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 686 | NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART, NULL); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 687 | rv = DoDrainBodyForAuthRestart(); |
| 688 | break; |
| 689 | case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE: |
| 690 | rv = DoDrainBodyForAuthRestartComplete(rv); |
| 691 | TRACE_EVENT_END("http.drain_body_for_auth_restart", |
| 692 | request_, request_->url.spec()); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 693 | net_log_.EndEvent( |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 694 | NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART, NULL); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 695 | break; |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 696 | case STATE_SPDY_SEND_REQUEST: |
| 697 | DCHECK_EQ(OK, rv); |
| 698 | TRACE_EVENT_BEGIN("http.send_request", request_, request_->url.spec()); |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 699 | net_log_.BeginEvent(NetLog::TYPE_SPDY_TRANSACTION_SEND_REQUEST, NULL); |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 700 | rv = DoSpdySendRequest(); |
| 701 | break; |
| 702 | case STATE_SPDY_SEND_REQUEST_COMPLETE: |
| 703 | rv = DoSpdySendRequestComplete(rv); |
| 704 | TRACE_EVENT_END("http.send_request", request_, request_->url.spec()); |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 705 | net_log_.EndEvent(NetLog::TYPE_SPDY_TRANSACTION_SEND_REQUEST, NULL); |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 706 | break; |
| 707 | case STATE_SPDY_READ_HEADERS: |
| 708 | DCHECK_EQ(OK, rv); |
| 709 | TRACE_EVENT_BEGIN("http.read_headers", request_, request_->url.spec()); |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 710 | net_log_.BeginEvent(NetLog::TYPE_SPDY_TRANSACTION_READ_HEADERS, NULL); |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 711 | rv = DoSpdyReadHeaders(); |
| 712 | break; |
| 713 | case STATE_SPDY_READ_HEADERS_COMPLETE: |
| 714 | rv = DoSpdyReadHeadersComplete(rv); |
| 715 | TRACE_EVENT_END("http.read_headers", request_, request_->url.spec()); |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 716 | net_log_.EndEvent(NetLog::TYPE_SPDY_TRANSACTION_READ_HEADERS, NULL); |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 717 | break; |
| 718 | case STATE_SPDY_READ_BODY: |
| 719 | DCHECK_EQ(OK, rv); |
| 720 | TRACE_EVENT_BEGIN("http.read_body", request_, request_->url.spec()); |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 721 | net_log_.BeginEvent(NetLog::TYPE_SPDY_TRANSACTION_READ_BODY, NULL); |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 722 | rv = DoSpdyReadBody(); |
| 723 | break; |
| 724 | case STATE_SPDY_READ_BODY_COMPLETE: |
| 725 | rv = DoSpdyReadBodyComplete(rv); |
| 726 | TRACE_EVENT_END("http.read_body", request_, request_->url.spec()); |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 727 | net_log_.EndEvent(NetLog::TYPE_SPDY_TRANSACTION_READ_BODY, NULL); |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 728 | break; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 729 | default: |
| 730 | NOTREACHED() << "bad state"; |
| 731 | rv = ERR_FAILED; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 732 | break; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 733 | } |
| 734 | } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); |
| 735 | |
| 736 | return rv; |
| 737 | } |
| 738 | |
| 739 | int HttpNetworkTransaction::DoResolveProxy() { |
| 740 | DCHECK(!pac_request_); |
| 741 | |
| 742 | next_state_ = STATE_RESOLVE_PROXY_COMPLETE; |
| 743 | |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 744 | // |endpoint_| indicates the final destination endpoint. |
| 745 | endpoint_ = HostPortPair(request_->url.HostNoBrackets(), |
| 746 | request_->url.EffectiveIntPort()); |
| 747 | |
[email protected] | 28ed3b4 | 2010-05-13 19:39:56 | [diff] [blame] | 748 | // Extra URL we might be attempting to resolve to. |
| 749 | GURL alternate_endpoint_url; |
[email protected] | b6a5018 | 2010-05-12 22:47:14 | [diff] [blame] | 750 | |
[email protected] | 28ed3b4 | 2010-05-13 19:39:56 | [diff] [blame] | 751 | // Tracks whether we are using |request_->url| or |alternate_endpoint_url|. |
| 752 | const GURL *curr_endpoint_url = &request_->url; |
| 753 | |
| 754 | if (g_host_mapping_rules && g_host_mapping_rules->RewriteHost(&endpoint_)) { |
| 755 | url_canon::Replacements<char> replacements; |
| 756 | const std::string port_str = IntToString(endpoint_.port); |
| 757 | replacements.SetPort(port_str.c_str(), |
| 758 | url_parse::Component(0, port_str.size())); |
| 759 | replacements.SetHost(endpoint_.host.c_str(), |
| 760 | url_parse::Component(0, endpoint_.host.size())); |
| 761 | alternate_endpoint_url = curr_endpoint_url->ReplaceComponents(replacements); |
| 762 | curr_endpoint_url = &alternate_endpoint_url; |
| 763 | } |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 764 | |
| 765 | if (alternate_protocol_mode_ == kUnspecified) { |
| 766 | const HttpAlternateProtocols& alternate_protocols = |
| 767 | session_->alternate_protocols(); |
| 768 | if (alternate_protocols.HasAlternateProtocolFor(endpoint_)) { |
| 769 | HttpAlternateProtocols::PortProtocolPair alternate = |
| 770 | alternate_protocols.GetAlternateProtocolFor(endpoint_); |
| 771 | if (alternate.protocol != HttpAlternateProtocols::BROKEN) { |
| 772 | DCHECK_EQ(HttpAlternateProtocols::NPN_SPDY_1, alternate.protocol); |
| 773 | endpoint_.port = alternate.port; |
| 774 | alternate_protocol_ = HttpAlternateProtocols::NPN_SPDY_1; |
| 775 | alternate_protocol_mode_ = kUsingAlternateProtocol; |
| 776 | |
| 777 | url_canon::Replacements<char> replacements; |
| 778 | replacements.SetScheme("https", |
| 779 | url_parse::Component(0, strlen("https"))); |
| 780 | const std::string port_str = IntToString(endpoint_.port); |
| 781 | replacements.SetPort(port_str.c_str(), |
| 782 | url_parse::Component(0, port_str.size())); |
[email protected] | 28ed3b4 | 2010-05-13 19:39:56 | [diff] [blame] | 783 | alternate_endpoint_url = |
| 784 | curr_endpoint_url->ReplaceComponents(replacements); |
| 785 | curr_endpoint_url = &alternate_endpoint_url; |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 786 | } |
| 787 | } |
| 788 | } |
| 789 | |
[email protected] | 677c9057 | 2008-12-10 09:03:15 | [diff] [blame] | 790 | if (request_->load_flags & LOAD_BYPASS_PROXY) { |
| 791 | proxy_info_.UseDirect(); |
| 792 | return OK; |
| 793 | } |
| 794 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 795 | return session_->proxy_service()->ResolveProxy( |
[email protected] | 28ed3b4 | 2010-05-13 19:39:56 | [diff] [blame] | 796 | *curr_endpoint_url, &proxy_info_, &io_callback_, &pac_request_, net_log_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | int HttpNetworkTransaction::DoResolveProxyComplete(int result) { |
[email protected] | 6971906 | 2010-01-05 20:09:21 | [diff] [blame] | 800 | pac_request_ = NULL; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 801 | |
[email protected] | dded3e2 | 2010-02-05 04:08:37 | [diff] [blame] | 802 | if (result != OK) |
| 803 | return result; |
[email protected] | 59a1601 | 2010-01-29 23:45:29 | [diff] [blame] | 804 | |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 805 | // Remove unsupported proxies from the list. |
[email protected] | f6fb2de | 2009-02-19 08:11:42 | [diff] [blame] | 806 | proxy_info_.RemoveProxiesWithoutScheme( |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 807 | ProxyServer::SCHEME_DIRECT | ProxyServer::SCHEME_HTTP | |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 808 | ProxyServer::SCHEME_SOCKS4 | ProxyServer::SCHEME_SOCKS5); |
[email protected] | f6fb2de | 2009-02-19 08:11:42 | [diff] [blame] | 809 | |
[email protected] | 6971906 | 2010-01-05 20:09:21 | [diff] [blame] | 810 | if (proxy_info_.is_empty()) { |
[email protected] | 02cf5a4 | 2010-01-12 22:10:25 | [diff] [blame] | 811 | // No proxies/direct to choose from. This happens when we don't support any |
| 812 | // of the proxies in the returned list. |
| 813 | return ERR_NO_SUPPORTED_PROXIES; |
[email protected] | 6971906 | 2010-01-05 20:09:21 | [diff] [blame] | 814 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 815 | |
[email protected] | 6971906 | 2010-01-05 20:09:21 | [diff] [blame] | 816 | next_state_ = STATE_INIT_CONNECTION; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 817 | return OK; |
| 818 | } |
| 819 | |
| 820 | int HttpNetworkTransaction::DoInitConnection() { |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 821 | DCHECK(!connection_->is_initialized()); |
[email protected] | 6971906 | 2010-01-05 20:09:21 | [diff] [blame] | 822 | DCHECK(proxy_info_.proxy_server().is_valid()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 823 | |
| 824 | next_state_ = STATE_INIT_CONNECTION_COMPLETE; |
| 825 | |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 826 | using_ssl_ = request_->url.SchemeIs("https") || |
| 827 | (alternate_protocol_mode_ == kUsingAlternateProtocol && |
| 828 | alternate_protocol_ == HttpAlternateProtocols::NPN_SPDY_1); |
| 829 | |
[email protected] | e099391 | 2010-02-22 23:57:11 | [diff] [blame] | 830 | using_spdy_ = false; |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 831 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 832 | // Build the string used to uniquely identify connections of this type. |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 833 | // Determine the host and port to connect to. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 834 | std::string connection_group; |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 835 | |
[email protected] | 85c0ed8 | 2009-12-15 23:14:14 | [diff] [blame] | 836 | // Use the fixed testing ports if they've been provided. |
| 837 | if (using_ssl_) { |
| 838 | if (session_->fixed_https_port() != 0) |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 839 | endpoint_.port = session_->fixed_https_port(); |
[email protected] | 85c0ed8 | 2009-12-15 23:14:14 | [diff] [blame] | 840 | } else if (session_->fixed_http_port() != 0) { |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 841 | endpoint_.port = session_->fixed_http_port(); |
[email protected] | 85c0ed8 | 2009-12-15 23:14:14 | [diff] [blame] | 842 | } |
| 843 | |
[email protected] | 0b0c008 | 2010-05-21 02:08:35 | [diff] [blame^] | 844 | response_.was_fetched_via_proxy = !proxy_info_.is_direct(); |
| 845 | |
[email protected] | 7fc5b09a | 2010-02-27 00:07:38 | [diff] [blame] | 846 | // Check first if we have a spdy session for this group. If so, then go |
| 847 | // straight to using that. |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 848 | if (session_->spdy_session_pool()->HasSession(endpoint_)) { |
[email protected] | 7fc5b09a | 2010-02-27 00:07:38 | [diff] [blame] | 849 | using_spdy_ = true; |
| 850 | return OK; |
| 851 | } |
| 852 | |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 853 | connection_group = endpoint_.ToString(); |
[email protected] | a0ef376 | 2009-12-22 02:09:45 | [diff] [blame] | 854 | DCHECK(!connection_group.empty()); |
[email protected] | 2884a46 | 2009-06-15 05:08:42 | [diff] [blame] | 855 | |
[email protected] | 0e88ad60 | 2010-05-04 23:47:02 | [diff] [blame] | 856 | if (using_ssl_) |
| 857 | connection_group = StringPrintf("ssl/%s", connection_group.c_str()); |
| 858 | |
[email protected] | 2884a46 | 2009-06-15 05:08:42 | [diff] [blame] | 859 | // If the user is refreshing the page, bypass the host cache. |
[email protected] | 7fc5b09a | 2010-02-27 00:07:38 | [diff] [blame] | 860 | bool disable_resolver_cache = request_->load_flags & LOAD_BYPASS_CACHE || |
[email protected] | 685af59 | 2010-05-11 19:31:24 | [diff] [blame] | 861 | request_->load_flags & LOAD_VALIDATE_CACHE || |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 862 | request_->load_flags & LOAD_DISABLE_CACHE; |
[email protected] | 2884a46 | 2009-06-15 05:08:42 | [diff] [blame] | 863 | |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 864 | int rv; |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 865 | if (!proxy_info_.is_direct()) { |
| 866 | ProxyServer proxy_server = proxy_info_.proxy_server(); |
| 867 | HostPortPair proxy_host_port_pair(proxy_server.HostNoBrackets(), |
| 868 | proxy_server.port()); |
| 869 | |
| 870 | TCPSocketParams tcp_params(proxy_host_port_pair, request_->priority, |
| 871 | request_->referrer, disable_resolver_cache); |
| 872 | |
| 873 | if (proxy_info_.is_socks()) { |
| 874 | const char* socks_version; |
| 875 | bool socks_v5; |
| 876 | if (proxy_info_.proxy_server().scheme() == ProxyServer::SCHEME_SOCKS5) { |
| 877 | socks_version = "5"; |
| 878 | socks_v5 = true; |
| 879 | } else { |
| 880 | socks_version = "4"; |
| 881 | socks_v5 = false; |
| 882 | } |
| 883 | |
| 884 | connection_group = |
| 885 | StringPrintf("socks%s/%s", socks_version, connection_group.c_str()); |
| 886 | |
| 887 | SOCKSSocketParams socks_params(tcp_params, socks_v5, endpoint_, |
| 888 | request_->priority, request_->referrer); |
| 889 | |
| 890 | rv = connection_->Init( |
| 891 | connection_group, socks_params, request_->priority, |
| 892 | &io_callback_, |
| 893 | session_->GetSocketPoolForSOCKSProxy(proxy_host_port_pair), net_log_); |
| 894 | } else { |
| 895 | rv = connection_->Init( |
| 896 | connection_group, tcp_params, request_->priority, |
| 897 | &io_callback_, |
| 898 | session_->GetSocketPoolForHTTPProxy(proxy_host_port_pair), net_log_); |
| 899 | } |
| 900 | } else { |
| 901 | TCPSocketParams tcp_params(endpoint_, request_->priority, |
| 902 | request_->referrer, disable_resolver_cache); |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 903 | rv = connection_->Init(connection_group, tcp_params, request_->priority, |
| 904 | &io_callback_, session_->tcp_socket_pool(), |
| 905 | net_log_); |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 906 | } |
| 907 | |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 908 | return rv; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | int HttpNetworkTransaction::DoInitConnectionComplete(int result) { |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 912 | if (result < 0) { |
| 913 | if (alternate_protocol_mode_ == kUsingAlternateProtocol) { |
| 914 | // Mark the alternate protocol as broken and fallback. |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 915 | MarkBrokenAlternateProtocolAndFallback(); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 916 | return OK; |
| 917 | } |
| 918 | |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 919 | return ReconsiderProxyAfterError(result); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 920 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 921 | |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 922 | DCHECK_EQ(OK, result); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 923 | |
[email protected] | e099391 | 2010-02-22 23:57:11 | [diff] [blame] | 924 | if (using_spdy_) { |
| 925 | DCHECK(!connection_->is_initialized()); |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 926 | next_state_ = STATE_SPDY_SEND_REQUEST; |
| 927 | return OK; |
| 928 | } |
| 929 | |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 930 | LogHttpConnectedMetrics(*connection_); |
[email protected] | f9d285c | 2009-08-17 19:54:29 | [diff] [blame] | 931 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 932 | // Set the reused_socket_ flag to indicate that we are using a keep-alive |
| 933 | // connection. This flag is used to handle errors that occur while we are |
| 934 | // trying to reuse a keep-alive connection. |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 935 | reused_socket_ = connection_->is_reused(); |
[email protected] | 049d4ee | 2008-10-23 21:42:07 | [diff] [blame] | 936 | if (reused_socket_) { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 937 | next_state_ = STATE_SEND_REQUEST; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 938 | } else { |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 939 | // Now we have a TCP connected socket. Perform other connection setup as |
| 940 | // needed. |
[email protected] | 616925a | 2010-03-02 19:02:38 | [diff] [blame] | 941 | UpdateConnectionTypeHistograms(CONNECTION_HTTP); |
[email protected] | 874190c | 2010-04-28 05:35:40 | [diff] [blame] | 942 | if (using_ssl_ && (proxy_info_.is_direct() || proxy_info_.is_socks())) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 943 | next_state_ = STATE_SSL_CONNECT; |
| 944 | } else { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 945 | next_state_ = STATE_SEND_REQUEST; |
[email protected] | 874190c | 2010-04-28 05:35:40 | [diff] [blame] | 946 | if (using_ssl_) |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 947 | establishing_tunnel_ = true; |
| 948 | } |
[email protected] | c7af8b2 | 2008-08-25 20:41:46 | [diff] [blame] | 949 | } |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 950 | |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 951 | return OK; |
[email protected] | c7af8b2 | 2008-08-25 20:41:46 | [diff] [blame] | 952 | } |
| 953 | |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 954 | int HttpNetworkTransaction::DoSSLConnect() { |
| 955 | next_state_ = STATE_SSL_CONNECT_COMPLETE; |
[email protected] | c7af8b2 | 2008-08-25 20:41:46 | [diff] [blame] | 956 | |
[email protected] | aeaca1f | 2010-04-20 22:05:21 | [diff] [blame] | 957 | if (ContainsKey(*g_tls_intolerant_servers, GetHostAndPort(request_->url))) { |
| 958 | LOG(WARNING) << "Falling back to SSLv3 because host is TLS intolerant: " |
| 959 | << GetHostAndPort(request_->url); |
| 960 | ssl_config_.tls1_enabled = false; |
| 961 | } |
| 962 | |
[email protected] | f6555ad | 2009-06-23 06:35:05 | [diff] [blame] | 963 | if (request_->load_flags & LOAD_VERIFY_EV_CERT) |
| 964 | ssl_config_.verify_ev_cert = true; |
| 965 | |
[email protected] | b7b7678 | 2009-09-11 00:31:43 | [diff] [blame] | 966 | ssl_connect_start_time_ = base::TimeTicks::Now(); |
| 967 | |
[email protected] | 86ec30d | 2008-09-29 21:53:54 | [diff] [blame] | 968 | // Add a SSL socket on top of our existing transport socket. |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 969 | ClientSocket* s = connection_->release_socket(); |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 970 | s = session_->socket_factory()->CreateSSLClientSocket( |
[email protected] | facc826 | 2009-05-16 00:01:00 | [diff] [blame] | 971 | s, request_->url.HostNoBrackets(), ssl_config_); |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 972 | connection_->set_socket(s); |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 973 | return connection_->socket()->Connect(&io_callback_); |
[email protected] | c7af8b2 | 2008-08-25 20:41:46 | [diff] [blame] | 974 | } |
| 975 | |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 976 | int HttpNetworkTransaction::DoSSLConnectComplete(int result) { |
[email protected] | 37a6792 | 2010-03-05 00:16:02 | [diff] [blame] | 977 | SSLClientSocket* ssl_socket = |
| 978 | reinterpret_cast<SSLClientSocket*>(connection_->socket()); |
| 979 | |
| 980 | SSLClientSocket::NextProtoStatus status = |
| 981 | SSLClientSocket::kNextProtoUnsupported; |
| 982 | std::string proto; |
| 983 | // GetNextProto will fail and and trigger a NOTREACHED if we pass in a socket |
| 984 | // that hasn't had SSL_ImportFD called on it. If we get a certificate error |
| 985 | // here, then we know that we called SSL_ImportFD. |
| 986 | if (result == OK || IsCertificateError(result)) |
| 987 | status = ssl_socket->GetNextProto(&proto); |
[email protected] | 37a6792 | 2010-03-05 00:16:02 | [diff] [blame] | 988 | using_spdy_ = (status == SSLClientSocket::kNextProtoNegotiated && |
[email protected] | 87f64d0 | 2010-04-19 21:39:26 | [diff] [blame] | 989 | SSLClientSocket::NextProtoFromString(proto) == |
| 990 | SSLClientSocket::kProtoSPDY1); |
[email protected] | 37a6792 | 2010-03-05 00:16:02 | [diff] [blame] | 991 | |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 992 | if (alternate_protocol_mode_ == kUsingAlternateProtocol && |
[email protected] | 31e2c69e | 2010-04-15 18:06:06 | [diff] [blame] | 993 | alternate_protocol_ == HttpAlternateProtocols::NPN_SPDY_1 && |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 994 | !using_spdy_) { |
[email protected] | 31e2c69e | 2010-04-15 18:06:06 | [diff] [blame] | 995 | // We tried using the NPN_SPDY_1 alternate protocol, but failed, so we |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 996 | // fallback. |
| 997 | MarkBrokenAlternateProtocolAndFallback(); |
| 998 | return OK; |
| 999 | } |
| 1000 | |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 1001 | if (IsCertificateError(result)) { |
[email protected] | d7660f1c6 | 2010-02-15 02:57:29 | [diff] [blame] | 1002 | result = HandleCertificateError(result); |
[email protected] | fab9ca5 | 2010-02-19 23:14:48 | [diff] [blame] | 1003 | if (result == OK && !connection_->socket()->IsConnectedAndIdle()) { |
| 1004 | connection_->socket()->Disconnect(); |
| 1005 | connection_->Reset(); |
| 1006 | next_state_ = STATE_INIT_CONNECTION; |
| 1007 | return result; |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 1008 | } |
| 1009 | } |
[email protected] | 771d0c2b | 2008-09-30 00:26:17 | [diff] [blame] | 1010 | |
[email protected] | c5949a3 | 2008-10-08 17:28:23 | [diff] [blame] | 1011 | if (result == OK) { |
[email protected] | b7b7678 | 2009-09-11 00:31:43 | [diff] [blame] | 1012 | DCHECK(ssl_connect_start_time_ != base::TimeTicks()); |
| 1013 | base::TimeDelta connect_duration = |
| 1014 | base::TimeTicks::Now() - ssl_connect_start_time_; |
| 1015 | |
[email protected] | e099391 | 2010-02-22 23:57:11 | [diff] [blame] | 1016 | if (using_spdy_) { |
[email protected] | 6aad1f60 | 2010-01-13 23:17:05 | [diff] [blame] | 1017 | UMA_HISTOGRAM_CUSTOM_TIMES("Net.SpdyConnectionLatency", |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 1018 | connect_duration, |
| 1019 | base::TimeDelta::FromMilliseconds(1), |
| 1020 | base::TimeDelta::FromMinutes(10), |
| 1021 | 100); |
[email protected] | 6aad1f60 | 2010-01-13 23:17:05 | [diff] [blame] | 1022 | |
[email protected] | 616925a | 2010-03-02 19:02:38 | [diff] [blame] | 1023 | UpdateConnectionTypeHistograms(CONNECTION_SPDY); |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 1024 | next_state_ = STATE_SPDY_SEND_REQUEST; |
| 1025 | } else { |
[email protected] | 6aad1f60 | 2010-01-13 23:17:05 | [diff] [blame] | 1026 | UMA_HISTOGRAM_CUSTOM_TIMES("Net.SSL_Connection_Latency", |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 1027 | connect_duration, |
| 1028 | base::TimeDelta::FromMilliseconds(1), |
| 1029 | base::TimeDelta::FromMinutes(10), |
| 1030 | 100); |
[email protected] | 6aad1f60 | 2010-01-13 23:17:05 | [diff] [blame] | 1031 | |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 1032 | next_state_ = STATE_SEND_REQUEST; |
| 1033 | } |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 1034 | } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) { |
[email protected] | 5e36396 | 2009-06-19 19:57:01 | [diff] [blame] | 1035 | result = HandleCertificateRequest(result); |
[email protected] | 5a179bcc | 2008-10-13 18:10:59 | [diff] [blame] | 1036 | } else { |
[email protected] | c5949a3 | 2008-10-08 17:28:23 | [diff] [blame] | 1037 | result = HandleSSLHandshakeError(result); |
| 1038 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1039 | return result; |
| 1040 | } |
| 1041 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1042 | int HttpNetworkTransaction::DoSendRequest() { |
| 1043 | next_state_ = STATE_SEND_REQUEST_COMPLETE; |
| 1044 | |
| 1045 | UploadDataStream* request_body = NULL; |
[email protected] | 7a6db402 | 2010-03-24 23:37:50 | [diff] [blame] | 1046 | if (!establishing_tunnel_ && request_->upload_data) { |
| 1047 | int error_code; |
| 1048 | request_body = UploadDataStream::Create(request_->upload_data, &error_code); |
| 1049 | if (!request_body) |
| 1050 | return error_code; |
| 1051 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1052 | |
| 1053 | // This is constructed lazily (instead of within our Start method), so that |
| 1054 | // we have proxy info available. |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1055 | if (request_headers_.empty()) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1056 | // Figure out if we can/should add Proxy-Authentication & Authentication |
| 1057 | // headers. |
| 1058 | bool have_proxy_auth = |
| 1059 | ShouldApplyProxyAuth() && |
| 1060 | (HaveAuth(HttpAuth::AUTH_PROXY) || |
| 1061 | SelectPreemptiveAuth(HttpAuth::AUTH_PROXY)); |
| 1062 | bool have_server_auth = |
| 1063 | ShouldApplyServerAuth() && |
| 1064 | (HaveAuth(HttpAuth::AUTH_SERVER) || |
| 1065 | SelectPreemptiveAuth(HttpAuth::AUTH_SERVER)); |
| 1066 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 1067 | std::string request_line; |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 1068 | HttpRequestHeaders request_headers; |
| 1069 | HttpRequestHeaders authorization_headers; |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1070 | |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 1071 | // TODO(wtc): If BuildAuthorizationHeader fails (returns an authorization |
| 1072 | // header with no credentials), we should return an error to prevent |
| 1073 | // entering an infinite auth restart loop. See http://crbug.com/21050. |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1074 | if (have_proxy_auth) |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 1075 | AddAuthorizationHeader(HttpAuth::AUTH_PROXY, &authorization_headers); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1076 | if (have_server_auth) |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 1077 | AddAuthorizationHeader(HttpAuth::AUTH_SERVER, &authorization_headers); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1078 | |
[email protected] | 6b9833e | 2008-09-10 20:32:25 | [diff] [blame] | 1079 | if (establishing_tunnel_) { |
[email protected] | 9faeded9 | 2010-04-29 20:03:05 | [diff] [blame] | 1080 | BuildTunnelRequest(request_, authorization_headers, endpoint_, |
| 1081 | &request_line, &request_headers); |
[email protected] | 169d001 | 2010-05-10 23:20:12 | [diff] [blame] | 1082 | if (net_log_.HasListener()) { |
| 1083 | net_log_.AddEvent( |
| 1084 | NetLog::TYPE_HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, |
| 1085 | new NetLogHttpRequestParameter( |
| 1086 | request_line, request_headers)); |
| 1087 | } |
[email protected] | 6b9833e | 2008-09-10 20:32:25 | [diff] [blame] | 1088 | } else { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1089 | BuildRequestHeaders(request_, authorization_headers, request_body, |
[email protected] | 874190c | 2010-04-28 05:35:40 | [diff] [blame] | 1090 | !using_ssl_ && proxy_info_.is_http(), &request_line, |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 1091 | &request_headers); |
[email protected] | 169d001 | 2010-05-10 23:20:12 | [diff] [blame] | 1092 | if (net_log_.HasListener()) { |
| 1093 | net_log_.AddEvent( |
| 1094 | NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST_HEADERS, |
| 1095 | new NetLogHttpRequestParameter( |
| 1096 | request_line, request_headers)); |
| 1097 | } |
[email protected] | 6b9833e | 2008-09-10 20:32:25 | [diff] [blame] | 1098 | } |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 1099 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 1100 | request_headers_ = request_line + request_headers.ToString(); |
[email protected] | 6b9833e | 2008-09-10 20:32:25 | [diff] [blame] | 1101 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1102 | |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 1103 | headers_valid_ = false; |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1104 | http_stream_.reset(new HttpBasicStream(connection_.get(), net_log_)); |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 1105 | |
[email protected] | a7e4131 | 2009-12-16 23:18:14 | [diff] [blame] | 1106 | return http_stream_->SendRequest(request_, request_headers_, |
| 1107 | request_body, &response_, &io_callback_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1108 | } |
| 1109 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1110 | int HttpNetworkTransaction::DoSendRequestComplete(int result) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1111 | if (result < 0) |
| 1112 | return HandleIOError(result); |
| 1113 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1114 | next_state_ = STATE_READ_HEADERS; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1115 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1116 | return OK; |
| 1117 | } |
| 1118 | |
| 1119 | int HttpNetworkTransaction::DoReadHeaders() { |
| 1120 | next_state_ = STATE_READ_HEADERS_COMPLETE; |
| 1121 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1122 | return http_stream_->ReadResponseHeaders(&io_callback_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1123 | } |
| 1124 | |
[email protected] | 0e75a73 | 2008-10-16 20:36:09 | [diff] [blame] | 1125 | int HttpNetworkTransaction::HandleConnectionClosedBeforeEndOfHeaders() { |
[email protected] | aecfbf2 | 2008-10-16 02:02:47 | [diff] [blame] | 1126 | if (establishing_tunnel_) { |
[email protected] | 0e75a73 | 2008-10-16 20:36:09 | [diff] [blame] | 1127 | // The connection was closed before the tunnel could be established. |
[email protected] | aecfbf2 | 2008-10-16 02:02:47 | [diff] [blame] | 1128 | return ERR_TUNNEL_CONNECTION_FAILED; |
| 1129 | } |
| 1130 | |
[email protected] | a7e4131 | 2009-12-16 23:18:14 | [diff] [blame] | 1131 | if (!response_.headers) { |
[email protected] | 0e75a73 | 2008-10-16 20:36:09 | [diff] [blame] | 1132 | // The connection was closed before any data was sent. Likely an error |
| 1133 | // rather than empty HTTP/0.9 response. |
[email protected] | aecfbf2 | 2008-10-16 02:02:47 | [diff] [blame] | 1134 | return ERR_EMPTY_RESPONSE; |
| 1135 | } |
| 1136 | |
[email protected] | aecfbf2 | 2008-10-16 02:02:47 | [diff] [blame] | 1137 | return OK; |
| 1138 | } |
| 1139 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1140 | int HttpNetworkTransaction::DoReadHeadersComplete(int result) { |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 1141 | // We can get a certificate error or ERR_SSL_CLIENT_AUTH_CERT_NEEDED here |
| 1142 | // due to SSL renegotiation. |
| 1143 | if (using_ssl_) { |
| 1144 | if (IsCertificateError(result)) { |
| 1145 | // We don't handle a certificate error during SSL renegotiation, so we |
| 1146 | // have to return an error that's not in the certificate error range |
| 1147 | // (-2xx). |
| 1148 | LOG(ERROR) << "Got a server certificate with error " << result |
| 1149 | << " during SSL renegotiation"; |
| 1150 | result = ERR_CERT_ERROR_IN_SSL_RENEGOTIATION; |
| 1151 | } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) { |
[email protected] | 5e36396 | 2009-06-19 19:57:01 | [diff] [blame] | 1152 | result = HandleCertificateRequest(result); |
| 1153 | if (result == OK) |
| 1154 | return result; |
[email protected] | 0ed9468 | 2010-05-18 15:09:00 | [diff] [blame] | 1155 | } else if ((result == ERR_SSL_DECOMPRESSION_FAILURE_ALERT || |
| 1156 | result == ERR_SSL_BAD_RECORD_MAC_ALERT ) && |
[email protected] | aeaca1f | 2010-04-20 22:05:21 | [diff] [blame] | 1157 | ssl_config_.tls1_enabled) { |
| 1158 | // Some buggy servers select DEFLATE compression when offered and then |
| 1159 | // fail to ever decompress anything. They will send a fatal alert telling |
| 1160 | // us this. Normally we would pick this up during the handshake because |
| 1161 | // our Finished message is compressed and we'll never get the server's |
| 1162 | // Finished if it fails to process ours. |
| 1163 | // |
| 1164 | // However, with False Start, we'll believe that the handshake is |
| 1165 | // complete as soon as we've /sent/ our Finished message. In this case, |
| 1166 | // we only find out that the server is buggy here, when we try to read |
| 1167 | // the initial reply. |
| 1168 | g_tls_intolerant_servers->insert(GetHostAndPort(request_->url)); |
| 1169 | ResetConnectionAndRequestForResend(); |
| 1170 | return OK; |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 1171 | } |
[email protected] | 2181ea00 | 2009-06-09 01:37:27 | [diff] [blame] | 1172 | } |
| 1173 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1174 | if (result < 0 && result != ERR_CONNECTION_CLOSED) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1175 | return HandleIOError(result); |
| 1176 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1177 | if (result == ERR_CONNECTION_CLOSED && ShouldResendRequest(result)) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1178 | ResetConnectionAndRequestForResend(); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1179 | return OK; |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1180 | } |
[email protected] | 2a5c76b | 2008-09-25 22:15:16 | [diff] [blame] | 1181 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1182 | // After we call RestartWithAuth a new response_time will be recorded, and |
| 1183 | // we need to be cautious about incorrectly logging the duration across the |
| 1184 | // authentication activity. |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1185 | if (!logged_response_time) { |
| 1186 | LogTransactionConnectedMetrics(); |
| 1187 | logged_response_time = true; |
[email protected] | 9a0a55f | 2009-04-13 23:23:03 | [diff] [blame] | 1188 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1189 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1190 | if (result == ERR_CONNECTION_CLOSED) { |
[email protected] | 02c92c49 | 2010-03-08 21:28:14 | [diff] [blame] | 1191 | // 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] | 1192 | // sense of it and send it back up the stack. |
[email protected] | 0e75a73 | 2008-10-16 20:36:09 | [diff] [blame] | 1193 | int rv = HandleConnectionClosedBeforeEndOfHeaders(); |
[email protected] | aecfbf2 | 2008-10-16 02:02:47 | [diff] [blame] | 1194 | if (rv != OK) |
| 1195 | return rv; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1196 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1197 | |
[email protected] | dbb83db | 2010-05-11 18:13:39 | [diff] [blame] | 1198 | if (net_log_.HasListener()) { |
| 1199 | if (establishing_tunnel_) { |
| 1200 | net_log_.AddEvent( |
| 1201 | NetLog::TYPE_HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, |
| 1202 | new NetLogHttpResponseParameter(response_.headers)); |
| 1203 | } else { |
| 1204 | net_log_.AddEvent( |
| 1205 | NetLog::TYPE_HTTP_TRANSACTION_READ_RESPONSE_HEADERS, |
| 1206 | new NetLogHttpResponseParameter(response_.headers)); |
| 1207 | } |
| 1208 | } |
| 1209 | |
[email protected] | a7e4131 | 2009-12-16 23:18:14 | [diff] [blame] | 1210 | if (response_.headers->GetParsedHttpVersion() < HttpVersion(1, 0)) { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1211 | // Require the "HTTP/1.x" status line for SSL CONNECT. |
| 1212 | if (establishing_tunnel_) |
| 1213 | return ERR_TUNNEL_CONNECTION_FAILED; |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 1214 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1215 | // HTTP/0.9 doesn't support the PUT method, so lack of response headers |
| 1216 | // indicates a buggy server. See: |
| 1217 | // https://bugzilla.mozilla.org/show_bug.cgi?id=193921 |
| 1218 | if (request_->method == "PUT") |
| 1219 | return ERR_METHOD_NOT_SUPPORTED; |
| 1220 | } |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 1221 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1222 | if (establishing_tunnel_) { |
[email protected] | a7e4131 | 2009-12-16 23:18:14 | [diff] [blame] | 1223 | switch (response_.headers->response_code()) { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1224 | case 200: // OK |
| 1225 | if (http_stream_->IsMoreDataBuffered()) { |
| 1226 | // The proxy sent extraneous data after the headers. |
| 1227 | return ERR_TUNNEL_CONNECTION_FAILED; |
| 1228 | } |
| 1229 | next_state_ = STATE_SSL_CONNECT; |
| 1230 | // Reset for the real request and response headers. |
| 1231 | request_headers_.clear(); |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 1232 | http_stream_.reset(NULL); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1233 | headers_valid_ = false; |
| 1234 | establishing_tunnel_ = false; |
[email protected] | 5d0429b | 2010-04-02 15:46:11 | [diff] [blame] | 1235 | // TODO(mbelshe): We should put in a test case to trip this code path. |
| 1236 | response_ = HttpResponseInfo(); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 1237 | return OK; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1238 | |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 1239 | // We aren't able to CONNECT to the remote host through the proxy. We |
| 1240 | // need to be very suspicious about the response because an active |
| 1241 | // network attacker can force us into this state by masquerading as the |
| 1242 | // proxy. The only safe thing to do here is to fail the connection |
| 1243 | // because our client is expecting an SSL protected response. |
| 1244 | // See http://crbug.com/7338. |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1245 | case 407: // Proxy Authentication Required |
| 1246 | // We need this status code to allow proxy authentication. Our |
| 1247 | // authentication code is smart enough to avoid being tricked by an |
| 1248 | // active network attacker. |
| 1249 | break; |
| 1250 | default: |
| 1251 | // For all other status codes, we conservatively fail the CONNECT |
| 1252 | // request. |
| 1253 | // We lose something by doing this. We have seen proxy 403, 404, and |
| 1254 | // 501 response bodies that contain a useful error message. For |
| 1255 | // example, Squid uses a 404 response to report the DNS error: "The |
| 1256 | // domain name does not exist." |
[email protected] | a7e4131 | 2009-12-16 23:18:14 | [diff] [blame] | 1257 | LogBlockedTunnelResponse(response_.headers->response_code()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1258 | return ERR_TUNNEL_CONNECTION_FAILED; |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 1259 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1260 | } |
[email protected] | 65f1140 | 2008-10-31 17:39:44 | [diff] [blame] | 1261 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1262 | // Check for an intermediate 100 Continue response. An origin server is |
| 1263 | // allowed to send this response even if we didn't ask for it, so we just |
| 1264 | // need to skip over it. |
| 1265 | // We treat any other 1xx in this same way (although in practice getting |
| 1266 | // a 1xx that isn't a 100 is rare). |
[email protected] | a7e4131 | 2009-12-16 23:18:14 | [diff] [blame] | 1267 | if (response_.headers->response_code() / 100 == 1) { |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 1268 | response_.headers = new HttpResponseHeaders(""); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1269 | next_state_ = STATE_READ_HEADERS; |
| 1270 | return OK; |
| 1271 | } |
| 1272 | |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 1273 | ProcessAlternateProtocol(*response_.headers, |
[email protected] | b6a5018 | 2010-05-12 22:47:14 | [diff] [blame] | 1274 | endpoint_, |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 1275 | session_->mutable_alternate_protocols()); |
| 1276 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1277 | int rv = HandleAuthChallenge(); |
| 1278 | if (rv != OK) |
| 1279 | return rv; |
| 1280 | |
| 1281 | if (using_ssl_ && !establishing_tunnel_) { |
| 1282 | SSLClientSocket* ssl_socket = |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 1283 | reinterpret_cast<SSLClientSocket*>(connection_->socket()); |
[email protected] | a7e4131 | 2009-12-16 23:18:14 | [diff] [blame] | 1284 | ssl_socket->GetSSLInfo(&response_.ssl_info); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1285 | } |
| 1286 | |
| 1287 | headers_valid_ = true; |
| 1288 | return OK; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1289 | } |
| 1290 | |
[email protected] | e5ae96a | 2010-04-14 20:12:45 | [diff] [blame] | 1291 | int HttpNetworkTransaction::DoResolveCanonicalName() { |
| 1292 | HttpAuthHandler* auth_handler = auth_handler_[pending_auth_target_]; |
| 1293 | DCHECK(auth_handler); |
| 1294 | next_state_ = STATE_RESOLVE_CANONICAL_NAME_COMPLETE; |
| 1295 | return auth_handler->ResolveCanonicalName(session_->host_resolver(), |
| 1296 | &io_callback_, net_log_); |
| 1297 | } |
| 1298 | |
| 1299 | int HttpNetworkTransaction::DoResolveCanonicalNameComplete(int result) { |
| 1300 | // The STATE_RESOLVE_CANONICAL_NAME state ends the Start sequence when the |
| 1301 | // canonical name of the server needs to be determined. Normally |
| 1302 | // DoReadHeadersComplete completes the sequence. The next state is |
| 1303 | // intentionally not set as it should be STATE_NONE; |
| 1304 | DCHECK_EQ(STATE_NONE, next_state_); |
| 1305 | return result; |
| 1306 | } |
| 1307 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1308 | int HttpNetworkTransaction::DoReadBody() { |
| 1309 | DCHECK(read_buf_); |
[email protected] | 6501bc0 | 2009-06-25 20:55:13 | [diff] [blame] | 1310 | DCHECK_GT(read_buf_len_, 0); |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 1311 | DCHECK(connection_->is_initialized()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1312 | |
| 1313 | next_state_ = STATE_READ_BODY_COMPLETE; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1314 | return http_stream_->ReadResponseBody(read_buf_, read_buf_len_, |
| 1315 | &io_callback_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | int HttpNetworkTransaction::DoReadBodyComplete(int result) { |
| 1319 | // We are done with the Read call. |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 1320 | DCHECK(!establishing_tunnel_) << |
| 1321 | "We should never read a response body of a tunnel."; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1322 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1323 | bool done = false, keep_alive = false; |
[email protected] | 02c92c49 | 2010-03-08 21:28:14 | [diff] [blame] | 1324 | if (result <= 0) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1325 | done = true; |
[email protected] | 9492e4a | 2010-02-24 00:58:46 | [diff] [blame] | 1326 | |
| 1327 | if (http_stream_->IsResponseBodyComplete()) { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1328 | done = true; |
[email protected] | 9492e4a | 2010-02-24 00:58:46 | [diff] [blame] | 1329 | if (http_stream_->CanFindEndOfResponse()) |
[email protected] | 02c92c49 | 2010-03-08 21:28:14 | [diff] [blame] | 1330 | keep_alive = GetResponseHeaders()->IsKeepAlive(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1331 | } |
| 1332 | |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 1333 | // Clean up connection_->if we are done. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1334 | if (done) { |
[email protected] | 5630017 | 2008-11-06 18:42:55 | [diff] [blame] | 1335 | LogTransactionMetrics(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1336 | if (!keep_alive) |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 1337 | connection_->socket()->Disconnect(); |
| 1338 | connection_->Reset(); |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 1339 | // The next Read call will return 0 (EOF). |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | // Clear these to avoid leaving around old state. |
| 1343 | read_buf_ = NULL; |
| 1344 | read_buf_len_ = 0; |
| 1345 | |
| 1346 | return result; |
| 1347 | } |
| 1348 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1349 | int HttpNetworkTransaction::DoDrainBodyForAuthRestart() { |
| 1350 | // This method differs from DoReadBody only in the next_state_. So we just |
| 1351 | // call DoReadBody and override the next_state_. Perhaps there is a more |
| 1352 | // elegant way for these two methods to share code. |
| 1353 | int rv = DoReadBody(); |
| 1354 | DCHECK(next_state_ == STATE_READ_BODY_COMPLETE); |
| 1355 | next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE; |
| 1356 | return rv; |
| 1357 | } |
| 1358 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1359 | // TODO(wtc): This method and the DoReadBodyComplete method are almost |
| 1360 | // 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] | 1361 | int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result) { |
[email protected] | 68873ba | 2009-06-04 21:49:23 | [diff] [blame] | 1362 | // keep_alive defaults to true because the very reason we're draining the |
| 1363 | // response body is to reuse the connection for auth restart. |
| 1364 | bool done = false, keep_alive = true; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1365 | if (result < 0) { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1366 | // Error or closed connection while reading the socket. |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1367 | done = true; |
[email protected] | 68873ba | 2009-06-04 21:49:23 | [diff] [blame] | 1368 | keep_alive = false; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1369 | } else if (http_stream_->IsResponseBodyComplete()) { |
| 1370 | done = true; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1371 | } |
| 1372 | |
| 1373 | if (done) { |
| 1374 | DidDrainBodyForAuthRestart(keep_alive); |
| 1375 | } else { |
| 1376 | // Keep draining. |
| 1377 | next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART; |
| 1378 | } |
| 1379 | |
| 1380 | return OK; |
| 1381 | } |
| 1382 | |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 1383 | int HttpNetworkTransaction::DoSpdySendRequest() { |
| 1384 | next_state_ = STATE_SPDY_SEND_REQUEST_COMPLETE; |
| 1385 | CHECK(!spdy_stream_.get()); |
| 1386 | |
| 1387 | // First we get a SPDY session. Theoretically, we've just negotiated one, but |
| 1388 | // if one already exists, then screw it, use the existing one! Otherwise, |
| 1389 | // use the existing TCP socket. |
| 1390 | |
[email protected] | 955fc2e7 | 2010-02-08 20:37:30 | [diff] [blame] | 1391 | const scoped_refptr<SpdySessionPool> spdy_pool = |
| 1392 | session_->spdy_session_pool(); |
| 1393 | scoped_refptr<SpdySession> spdy_session; |
[email protected] | 5e2e6c77d1 | 2009-12-24 21:57:16 | [diff] [blame] | 1394 | |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 1395 | if (spdy_pool->HasSession(endpoint_)) { |
[email protected] | 635909f | 2010-05-12 18:19:36 | [diff] [blame] | 1396 | spdy_session = spdy_pool->Get(endpoint_, session_, net_log_); |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 1397 | } else { |
[email protected] | 5fe524e | 2010-02-20 00:43:22 | [diff] [blame] | 1398 | // SPDY is negotiated using the TLS next protocol negotiation (NPN) |
| 1399 | // extension, so |connection_| must contain an SSLClientSocket. |
| 1400 | DCHECK(using_ssl_); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 1401 | CHECK(connection_->socket()); |
[email protected] | 5fe524e | 2010-02-20 00:43:22 | [diff] [blame] | 1402 | spdy_session = spdy_pool->GetSpdySessionFromSSLSocket( |
[email protected] | 635909f | 2010-05-12 18:19:36 | [diff] [blame] | 1403 | endpoint_, session_, connection_.release(), net_log_); |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 1404 | } |
| 1405 | |
| 1406 | CHECK(spdy_session.get()); |
| 1407 | |
[email protected] | 7a6db402 | 2010-03-24 23:37:50 | [diff] [blame] | 1408 | UploadDataStream* upload_data = NULL; |
| 1409 | if (request_->upload_data) { |
| 1410 | int error_code = OK; |
| 1411 | upload_data = UploadDataStream::Create(request_->upload_data, &error_code); |
| 1412 | if (!upload_data) |
| 1413 | return error_code; |
| 1414 | } |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 1415 | headers_valid_ = false; |
[email protected] | 33c477b | 2010-05-13 19:21:07 | [diff] [blame] | 1416 | spdy_stream_ = spdy_session->GetOrCreateStream( |
| 1417 | *request_, upload_data, net_log_); |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 1418 | return spdy_stream_->SendRequest(upload_data, &response_, &io_callback_); |
| 1419 | } |
| 1420 | |
| 1421 | int HttpNetworkTransaction::DoSpdySendRequestComplete(int result) { |
| 1422 | if (result < 0) |
| 1423 | return result; |
| 1424 | |
| 1425 | next_state_ = STATE_SPDY_READ_HEADERS; |
| 1426 | return OK; |
| 1427 | } |
| 1428 | |
| 1429 | int HttpNetworkTransaction::DoSpdyReadHeaders() { |
| 1430 | next_state_ = STATE_SPDY_READ_HEADERS_COMPLETE; |
| 1431 | return spdy_stream_->ReadResponseHeaders(&io_callback_); |
| 1432 | } |
| 1433 | |
| 1434 | int HttpNetworkTransaction::DoSpdyReadHeadersComplete(int result) { |
| 1435 | // TODO(willchan): Flesh out the support for HTTP authentication here. |
| 1436 | if (result == OK) |
| 1437 | headers_valid_ = true; |
| 1438 | return result; |
| 1439 | } |
| 1440 | |
| 1441 | int HttpNetworkTransaction::DoSpdyReadBody() { |
| 1442 | next_state_ = STATE_SPDY_READ_BODY_COMPLETE; |
| 1443 | |
| 1444 | return spdy_stream_->ReadResponseBody( |
| 1445 | read_buf_, read_buf_len_, &io_callback_); |
| 1446 | } |
| 1447 | |
| 1448 | int HttpNetworkTransaction::DoSpdyReadBodyComplete(int result) { |
| 1449 | read_buf_ = NULL; |
| 1450 | read_buf_len_ = 0; |
| 1451 | |
| 1452 | if (result <= 0) |
| 1453 | spdy_stream_ = NULL; |
| 1454 | |
| 1455 | return result; |
| 1456 | } |
| 1457 | |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 1458 | void HttpNetworkTransaction::LogHttpConnectedMetrics( |
[email protected] | f9d285c | 2009-08-17 19:54:29 | [diff] [blame] | 1459 | const ClientSocketHandle& handle) { |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 1460 | UMA_HISTOGRAM_ENUMERATION("Net.HttpSocketType", handle.reuse_type(), |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 1461 | ClientSocketHandle::NUM_TYPES); |
[email protected] | f9d285c | 2009-08-17 19:54:29 | [diff] [blame] | 1462 | |
[email protected] | bc3875bbc | 2009-08-24 19:44:20 | [diff] [blame] | 1463 | switch (handle.reuse_type()) { |
| 1464 | case ClientSocketHandle::UNUSED: |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 1465 | UMA_HISTOGRAM_CUSTOM_TIMES("Net.HttpConnectionLatency", |
| 1466 | handle.setup_time(), |
| 1467 | base::TimeDelta::FromMilliseconds(1), |
| 1468 | base::TimeDelta::FromMinutes(10), |
| 1469 | 100); |
[email protected] | bc3875bbc | 2009-08-24 19:44:20 | [diff] [blame] | 1470 | break; |
| 1471 | case ClientSocketHandle::UNUSED_IDLE: |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 1472 | UMA_HISTOGRAM_CUSTOM_TIMES("Net.SocketIdleTimeBeforeNextUse_UnusedSocket", |
| 1473 | handle.idle_time(), |
| 1474 | base::TimeDelta::FromMilliseconds(1), |
| 1475 | base::TimeDelta::FromMinutes(6), |
| 1476 | 100); |
[email protected] | bc3875bbc | 2009-08-24 19:44:20 | [diff] [blame] | 1477 | break; |
| 1478 | case ClientSocketHandle::REUSED_IDLE: |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 1479 | UMA_HISTOGRAM_CUSTOM_TIMES("Net.SocketIdleTimeBeforeNextUse_ReusedSocket", |
| 1480 | handle.idle_time(), |
| 1481 | base::TimeDelta::FromMilliseconds(1), |
| 1482 | base::TimeDelta::FromMinutes(6), |
| 1483 | 100); |
[email protected] | bc3875bbc | 2009-08-24 19:44:20 | [diff] [blame] | 1484 | break; |
| 1485 | default: |
| 1486 | NOTREACHED(); |
| 1487 | break; |
| 1488 | } |
[email protected] | 42afa7c | 2009-04-17 23:51:24 | [diff] [blame] | 1489 | } |
| 1490 | |
[email protected] | f9d285c | 2009-08-17 19:54:29 | [diff] [blame] | 1491 | void HttpNetworkTransaction::LogIOErrorMetrics( |
| 1492 | const ClientSocketHandle& handle) { |
[email protected] | 2753b39 | 2009-12-28 06:59:52 | [diff] [blame] | 1493 | UMA_HISTOGRAM_ENUMERATION("Net.IOError_SocketReuseType", |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 1494 | handle.reuse_type(), ClientSocketHandle::NUM_TYPES); |
[email protected] | f9d285c | 2009-08-17 19:54:29 | [diff] [blame] | 1495 | |
[email protected] | f9d285c | 2009-08-17 19:54:29 | [diff] [blame] | 1496 | switch (handle.reuse_type()) { |
| 1497 | case ClientSocketHandle::UNUSED: |
| 1498 | break; |
| 1499 | case ClientSocketHandle::UNUSED_IDLE: |
[email protected] | bc3875bbc | 2009-08-24 19:44:20 | [diff] [blame] | 1500 | UMA_HISTOGRAM_CUSTOM_TIMES( |
| 1501 | "Net.SocketIdleTimeOnIOError2_UnusedSocket", |
| 1502 | handle.idle_time(), base::TimeDelta::FromMilliseconds(1), |
| 1503 | base::TimeDelta::FromMinutes(6), 100); |
[email protected] | f9d285c | 2009-08-17 19:54:29 | [diff] [blame] | 1504 | break; |
| 1505 | case ClientSocketHandle::REUSED_IDLE: |
[email protected] | bc3875bbc | 2009-08-24 19:44:20 | [diff] [blame] | 1506 | UMA_HISTOGRAM_CUSTOM_TIMES( |
| 1507 | "Net.SocketIdleTimeOnIOError2_ReusedSocket", |
| 1508 | handle.idle_time(), base::TimeDelta::FromMilliseconds(1), |
| 1509 | base::TimeDelta::FromMinutes(6), 100); |
[email protected] | f9d285c | 2009-08-17 19:54:29 | [diff] [blame] | 1510 | break; |
| 1511 | default: |
| 1512 | NOTREACHED(); |
| 1513 | break; |
| 1514 | } |
| 1515 | } |
| 1516 | |
[email protected] | 9a0a55f | 2009-04-13 23:23:03 | [diff] [blame] | 1517 | void HttpNetworkTransaction::LogTransactionConnectedMetrics() const { |
[email protected] | a7e4131 | 2009-12-16 23:18:14 | [diff] [blame] | 1518 | base::TimeDelta total_duration = response_.response_time - start_time_; |
[email protected] | 9a0a55f | 2009-04-13 23:23:03 | [diff] [blame] | 1519 | |
[email protected] | 510e854f | 2009-04-20 18:39:08 | [diff] [blame] | 1520 | UMA_HISTOGRAM_CLIPPED_TIMES( |
[email protected] | f929f2f2 | 2009-06-12 16:56:58 | [diff] [blame] | 1521 | "Net.Transaction_Connected_Under_10", |
[email protected] | 510e854f | 2009-04-20 18:39:08 | [diff] [blame] | 1522 | total_duration, |
[email protected] | 9a0a55f | 2009-04-13 23:23:03 | [diff] [blame] | 1523 | base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(10), |
| 1524 | 100); |
[email protected] | 1fa4759 | 2009-07-27 22:45:00 | [diff] [blame] | 1525 | |
[email protected] | 0310d43 | 2009-08-25 07:49:52 | [diff] [blame] | 1526 | if (!reused_socket_) { |
[email protected] | b01998a | 2009-04-21 01:01:11 | [diff] [blame] | 1527 | UMA_HISTOGRAM_CLIPPED_TIMES( |
[email protected] | f929f2f2 | 2009-06-12 16:56:58 | [diff] [blame] | 1528 | "Net.Transaction_Connected_New", |
[email protected] | b01998a | 2009-04-21 01:01:11 | [diff] [blame] | 1529 | total_duration, |
| 1530 | base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(10), |
| 1531 | 100); |
[email protected] | 0310d43 | 2009-08-25 07:49:52 | [diff] [blame] | 1532 | } |
| 1533 | |
[email protected] | 510e854f | 2009-04-20 18:39:08 | [diff] [blame] | 1534 | // Currently, non-zero priority requests are frame or sub-frame resource |
| 1535 | // types. This will change when we also prioritize certain subresources like |
| 1536 | // css, js, etc. |
| 1537 | if (request_->priority) { |
| 1538 | UMA_HISTOGRAM_CLIPPED_TIMES( |
[email protected] | f929f2f2 | 2009-06-12 16:56:58 | [diff] [blame] | 1539 | "Net.Priority_High_Latency", |
[email protected] | 510e854f | 2009-04-20 18:39:08 | [diff] [blame] | 1540 | total_duration, |
| 1541 | base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(10), |
| 1542 | 100); |
| 1543 | } else { |
| 1544 | UMA_HISTOGRAM_CLIPPED_TIMES( |
[email protected] | f929f2f2 | 2009-06-12 16:56:58 | [diff] [blame] | 1545 | "Net.Priority_Low_Latency", |
[email protected] | 510e854f | 2009-04-20 18:39:08 | [diff] [blame] | 1546 | total_duration, |
| 1547 | base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(10), |
| 1548 | 100); |
| 1549 | } |
[email protected] | 9a0a55f | 2009-04-13 23:23:03 | [diff] [blame] | 1550 | } |
| 1551 | |
[email protected] | 5630017 | 2008-11-06 18:42:55 | [diff] [blame] | 1552 | void HttpNetworkTransaction::LogTransactionMetrics() const { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1553 | base::TimeDelta duration = base::Time::Now() - |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 1554 | response_.request_time; |
[email protected] | 5630017 | 2008-11-06 18:42:55 | [diff] [blame] | 1555 | if (60 < duration.InMinutes()) |
| 1556 | return; |
[email protected] | 0b48db4 | 2009-03-23 02:45:11 | [diff] [blame] | 1557 | |
[email protected] | 21b316a | 2009-03-23 18:25:06 | [diff] [blame] | 1558 | base::TimeDelta total_duration = base::Time::Now() - start_time_; |
| 1559 | |
[email protected] | f929f2f2 | 2009-06-12 16:56:58 | [diff] [blame] | 1560 | UMA_HISTOGRAM_LONG_TIMES("Net.Transaction_Latency", duration); |
| 1561 | UMA_HISTOGRAM_CLIPPED_TIMES("Net.Transaction_Latency_Under_10", duration, |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 1562 | base::TimeDelta::FromMilliseconds(1), |
| 1563 | base::TimeDelta::FromMinutes(10), |
| 1564 | 100); |
[email protected] | f929f2f2 | 2009-06-12 16:56:58 | [diff] [blame] | 1565 | UMA_HISTOGRAM_CLIPPED_TIMES("Net.Transaction_Latency_Total_Under_10", |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 1566 | total_duration, |
| 1567 | base::TimeDelta::FromMilliseconds(1), |
| 1568 | base::TimeDelta::FromMinutes(10), 100); |
[email protected] | 808f640 | 2009-03-30 20:02:07 | [diff] [blame] | 1569 | if (!reused_socket_) { |
[email protected] | f929f2f2 | 2009-06-12 16:56:58 | [diff] [blame] | 1570 | UMA_HISTOGRAM_CLIPPED_TIMES( |
[email protected] | 808f640 | 2009-03-30 20:02:07 | [diff] [blame] | 1571 | "Net.Transaction_Latency_Total_New_Connection_Under_10", |
[email protected] | 808f640 | 2009-03-30 20:02:07 | [diff] [blame] | 1572 | total_duration, base::TimeDelta::FromMilliseconds(1), |
| 1573 | base::TimeDelta::FromMinutes(10), 100); |
| 1574 | } |
[email protected] | 5630017 | 2008-11-06 18:42:55 | [diff] [blame] | 1575 | } |
| 1576 | |
[email protected] | 9f9f86c | 2009-03-12 22:32:42 | [diff] [blame] | 1577 | void HttpNetworkTransaction::LogBlockedTunnelResponse( |
[email protected] | af89ba6 | 2009-03-16 20:26:38 | [diff] [blame] | 1578 | int response_code) const { |
| 1579 | LOG(WARNING) << "Blocked proxy response with status " << response_code |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 1580 | << " to CONNECT request for " |
| 1581 | << GetHostAndPort(request_->url) << "."; |
[email protected] | 9f9f86c | 2009-03-12 22:32:42 | [diff] [blame] | 1582 | } |
| 1583 | |
[email protected] | ccb40e5 | 2008-09-17 20:54:40 | [diff] [blame] | 1584 | int HttpNetworkTransaction::HandleCertificateError(int error) { |
| 1585 | DCHECK(using_ssl_); |
[email protected] | d7660f1c6 | 2010-02-15 02:57:29 | [diff] [blame] | 1586 | DCHECK(IsCertificateError(error)); |
| 1587 | |
| 1588 | SSLClientSocket* ssl_socket = |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 1589 | reinterpret_cast<SSLClientSocket*>(connection_->socket()); |
[email protected] | d7660f1c6 | 2010-02-15 02:57:29 | [diff] [blame] | 1590 | ssl_socket->GetSSLInfo(&response_.ssl_info); |
| 1591 | |
| 1592 | // Add the bad certificate to the set of allowed certificates in the |
| 1593 | // SSL info object. This data structure will be consulted after calling |
| 1594 | // RestartIgnoringLastError(). And the user will be asked interactively |
| 1595 | // before RestartIgnoringLastError() is ever called. |
| 1596 | SSLConfig::CertAndStatus bad_cert; |
| 1597 | bad_cert.cert = response_.ssl_info.cert; |
| 1598 | bad_cert.cert_status = response_.ssl_info.cert_status; |
| 1599 | ssl_config_.allowed_bad_certs.push_back(bad_cert); |
[email protected] | ccb40e5 | 2008-09-17 20:54:40 | [diff] [blame] | 1600 | |
[email protected] | 37a6792 | 2010-03-05 00:16:02 | [diff] [blame] | 1601 | if (g_ignore_certificate_errors) |
| 1602 | return OK; |
| 1603 | |
[email protected] | ccb40e5 | 2008-09-17 20:54:40 | [diff] [blame] | 1604 | const int kCertFlags = LOAD_IGNORE_CERT_COMMON_NAME_INVALID | |
| 1605 | LOAD_IGNORE_CERT_DATE_INVALID | |
| 1606 | LOAD_IGNORE_CERT_AUTHORITY_INVALID | |
| 1607 | LOAD_IGNORE_CERT_WRONG_USAGE; |
| 1608 | if (request_->load_flags & kCertFlags) { |
| 1609 | switch (error) { |
| 1610 | case ERR_CERT_COMMON_NAME_INVALID: |
| 1611 | if (request_->load_flags & LOAD_IGNORE_CERT_COMMON_NAME_INVALID) |
| 1612 | error = OK; |
| 1613 | break; |
| 1614 | case ERR_CERT_DATE_INVALID: |
| 1615 | if (request_->load_flags & LOAD_IGNORE_CERT_DATE_INVALID) |
| 1616 | error = OK; |
| 1617 | break; |
| 1618 | case ERR_CERT_AUTHORITY_INVALID: |
| 1619 | if (request_->load_flags & LOAD_IGNORE_CERT_AUTHORITY_INVALID) |
| 1620 | error = OK; |
| 1621 | break; |
| 1622 | } |
| 1623 | } |
[email protected] | ccb40e5 | 2008-09-17 20:54:40 | [diff] [blame] | 1624 | return error; |
| 1625 | } |
| 1626 | |
[email protected] | 5e36396 | 2009-06-19 19:57:01 | [diff] [blame] | 1627 | int HttpNetworkTransaction::HandleCertificateRequest(int error) { |
| 1628 | // Assert that the socket did not send a client certificate. |
| 1629 | // Note: If we got a reused socket, it was created with some other |
| 1630 | // transaction's ssl_config_, so we need to disable this assertion. We can |
| 1631 | // get a certificate request on a reused socket when the server requested |
| 1632 | // renegotiation (rehandshake). |
| 1633 | // TODO(wtc): add a GetSSLParams method to SSLClientSocket so we can query |
| 1634 | // the SSL parameters it was created with and get rid of the reused_socket_ |
| 1635 | // test. |
| 1636 | DCHECK(reused_socket_ || !ssl_config_.send_client_cert); |
| 1637 | |
[email protected] | a7e4131 | 2009-12-16 23:18:14 | [diff] [blame] | 1638 | response_.cert_request_info = new SSLCertRequestInfo; |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 1639 | SSLClientSocket* ssl_socket = |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 1640 | reinterpret_cast<SSLClientSocket*>(connection_->socket()); |
[email protected] | a7e4131 | 2009-12-16 23:18:14 | [diff] [blame] | 1641 | ssl_socket->GetSSLCertRequestInfo(response_.cert_request_info); |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 1642 | |
| 1643 | // Close the connection while the user is selecting a certificate to send |
| 1644 | // to the server. |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 1645 | connection_->socket()->Disconnect(); |
| 1646 | connection_->Reset(); |
[email protected] | 5e36396 | 2009-06-19 19:57:01 | [diff] [blame] | 1647 | |
| 1648 | // If the user selected one of the certificate in client_certs for this |
| 1649 | // server before, use it automatically. |
| 1650 | X509Certificate* client_cert = session_->ssl_client_auth_cache()-> |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 1651 | Lookup(GetHostAndPort(request_->url)); |
[email protected] | 5e36396 | 2009-06-19 19:57:01 | [diff] [blame] | 1652 | if (client_cert) { |
| 1653 | const std::vector<scoped_refptr<X509Certificate> >& client_certs = |
[email protected] | a7e4131 | 2009-12-16 23:18:14 | [diff] [blame] | 1654 | response_.cert_request_info->client_certs; |
[email protected] | 5e36396 | 2009-06-19 19:57:01 | [diff] [blame] | 1655 | for (size_t i = 0; i < client_certs.size(); ++i) { |
[email protected] | bd0b743 | 2009-06-23 21:03:42 | [diff] [blame] | 1656 | if (client_cert->fingerprint().Equals(client_certs[i]->fingerprint())) { |
[email protected] | 5e36396 | 2009-06-19 19:57:01 | [diff] [blame] | 1657 | ssl_config_.client_cert = client_cert; |
| 1658 | ssl_config_.send_client_cert = true; |
| 1659 | next_state_ = STATE_INIT_CONNECTION; |
| 1660 | // Reset the other member variables. |
| 1661 | // Note: this is necessary only with SSL renegotiation. |
| 1662 | ResetStateForRestart(); |
| 1663 | return OK; |
| 1664 | } |
| 1665 | } |
| 1666 | } |
| 1667 | return error; |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 1668 | } |
| 1669 | |
[email protected] | c5949a3 | 2008-10-08 17:28:23 | [diff] [blame] | 1670 | int HttpNetworkTransaction::HandleSSLHandshakeError(int error) { |
[email protected] | 5e36396 | 2009-06-19 19:57:01 | [diff] [blame] | 1671 | if (ssl_config_.send_client_cert && |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 1672 | (error == ERR_SSL_PROTOCOL_ERROR || |
| 1673 | error == ERR_BAD_SSL_CLIENT_AUTH_CERT)) { |
[email protected] | 5e36396 | 2009-06-19 19:57:01 | [diff] [blame] | 1674 | session_->ssl_client_auth_cache()->Remove(GetHostAndPort(request_->url)); |
| 1675 | } |
| 1676 | |
[email protected] | 5a179bcc | 2008-10-13 18:10:59 | [diff] [blame] | 1677 | switch (error) { |
| 1678 | case ERR_SSL_PROTOCOL_ERROR: |
| 1679 | case ERR_SSL_VERSION_OR_CIPHER_MISMATCH: |
[email protected] | aeaca1f | 2010-04-20 22:05:21 | [diff] [blame] | 1680 | case ERR_SSL_DECOMPRESSION_FAILURE_ALERT: |
[email protected] | 0ed9468 | 2010-05-18 15:09:00 | [diff] [blame] | 1681 | case ERR_SSL_BAD_RECORD_MAC_ALERT: |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 1682 | if (ssl_config_.tls1_enabled) { |
[email protected] | 0ed9468 | 2010-05-18 15:09:00 | [diff] [blame] | 1683 | // This could be a TLS-intolerant server, an SSL 3.0 server that |
| 1684 | // chose a TLS-only cipher suite or a server with buggy DEFLATE |
| 1685 | // support. Turn off TLS 1.0, DEFLATE support and retry. |
[email protected] | aeaca1f | 2010-04-20 22:05:21 | [diff] [blame] | 1686 | g_tls_intolerant_servers->insert(GetHostAndPort(request_->url)); |
| 1687 | ResetConnectionAndRequestForResend(); |
[email protected] | 5a179bcc | 2008-10-13 18:10:59 | [diff] [blame] | 1688 | error = OK; |
| 1689 | } |
| 1690 | break; |
[email protected] | c5949a3 | 2008-10-08 17:28:23 | [diff] [blame] | 1691 | } |
[email protected] | c5949a3 | 2008-10-08 17:28:23 | [diff] [blame] | 1692 | return error; |
| 1693 | } |
| 1694 | |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 1695 | // This method determines whether it is safe to resend the request after an |
| 1696 | // IO error. It can only be called in response to request header or body |
| 1697 | // write errors or response header read errors. It should not be used in |
| 1698 | // other cases, such as a Connect error. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1699 | int HttpNetworkTransaction::HandleIOError(int error) { |
| 1700 | switch (error) { |
| 1701 | // If we try to reuse a connection that the server is in the process of |
| 1702 | // closing, we may end up successfully writing out our request (or a |
| 1703 | // portion of our request) only to find a connection error when we try to |
| 1704 | // read from (or finish writing to) the socket. |
| 1705 | case ERR_CONNECTION_RESET: |
| 1706 | case ERR_CONNECTION_CLOSED: |
| 1707 | case ERR_CONNECTION_ABORTED: |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 1708 | LogIOErrorMetrics(*connection_); |
[email protected] | a19f1c60 | 2009-08-24 21:35:28 | [diff] [blame] | 1709 | if (ShouldResendRequest(error)) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1710 | ResetConnectionAndRequestForResend(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1711 | error = OK; |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1712 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1713 | break; |
| 1714 | } |
| 1715 | return error; |
| 1716 | } |
| 1717 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1718 | void HttpNetworkTransaction::ResetStateForRestart() { |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 1719 | pending_auth_target_ = HttpAuth::AUTH_NONE; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1720 | read_buf_ = NULL; |
| 1721 | read_buf_len_ = 0; |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 1722 | http_stream_.reset(); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1723 | headers_valid_ = false; |
| 1724 | request_headers_.clear(); |
[email protected] | a7e4131 | 2009-12-16 23:18:14 | [diff] [blame] | 1725 | response_ = HttpResponseInfo(); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1726 | } |
| 1727 | |
| 1728 | HttpResponseHeaders* HttpNetworkTransaction::GetResponseHeaders() const { |
[email protected] | a7e4131 | 2009-12-16 23:18:14 | [diff] [blame] | 1729 | return response_.headers; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1730 | } |
| 1731 | |
[email protected] | a19f1c60 | 2009-08-24 21:35:28 | [diff] [blame] | 1732 | bool HttpNetworkTransaction::ShouldResendRequest(int error) const { |
[email protected] | 2a5c76b | 2008-09-25 22:15:16 | [diff] [blame] | 1733 | // NOTE: we resend a request only if we reused a keep-alive connection. |
| 1734 | // This automatically prevents an infinite resend loop because we'll run |
| 1735 | // out of the cached keep-alive connections eventually. |
| 1736 | if (establishing_tunnel_ || |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 1737 | !connection_->ShouldResendFailedRequest(error) || |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1738 | GetResponseHeaders()) { // We have received some response headers. |
[email protected] | 2a5c76b | 2008-09-25 22:15:16 | [diff] [blame] | 1739 | return false; |
| 1740 | } |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1741 | return true; |
| 1742 | } |
| 1743 | |
| 1744 | void HttpNetworkTransaction::ResetConnectionAndRequestForResend() { |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 1745 | connection_->socket()->Disconnect(); |
| 1746 | connection_->Reset(); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1747 | // We need to clear request_headers_ because it contains the real request |
| 1748 | // headers, but we may need to resend the CONNECT request first to recreate |
| 1749 | // the SSL tunnel. |
| 1750 | request_headers_.clear(); |
[email protected] | 2a5c76b | 2008-09-25 22:15:16 | [diff] [blame] | 1751 | next_state_ = STATE_INIT_CONNECTION; // Resend the request. |
[email protected] | 2a5c76b | 2008-09-25 22:15:16 | [diff] [blame] | 1752 | } |
| 1753 | |
[email protected] | 86ec30d | 2008-09-29 21:53:54 | [diff] [blame] | 1754 | int HttpNetworkTransaction::ReconsiderProxyAfterError(int error) { |
| 1755 | DCHECK(!pac_request_); |
| 1756 | |
| 1757 | // A failure to resolve the hostname or any error related to establishing a |
| 1758 | // TCP connection could be grounds for trying a new proxy configuration. |
[email protected] | 7be5131 | 2008-09-29 23:21:30 | [diff] [blame] | 1759 | // |
| 1760 | // Why do this when a hostname cannot be resolved? Some URLs only make sense |
| 1761 | // to proxy servers. The hostname in those URLs might fail to resolve if we |
| 1762 | // are still using a non-proxy config. We need to check if a proxy config |
| 1763 | // now exists that corresponds to a proxy server that could load the URL. |
| 1764 | // |
[email protected] | 86ec30d | 2008-09-29 21:53:54 | [diff] [blame] | 1765 | switch (error) { |
| 1766 | case ERR_NAME_NOT_RESOLVED: |
| 1767 | case ERR_INTERNET_DISCONNECTED: |
| 1768 | case ERR_ADDRESS_UNREACHABLE: |
| 1769 | case ERR_CONNECTION_CLOSED: |
| 1770 | case ERR_CONNECTION_RESET: |
| 1771 | case ERR_CONNECTION_REFUSED: |
| 1772 | case ERR_CONNECTION_ABORTED: |
| 1773 | case ERR_TIMED_OUT: |
| 1774 | case ERR_TUNNEL_CONNECTION_FAILED: |
[email protected] | d5a30959 | 2010-02-05 02:22:52 | [diff] [blame] | 1775 | case ERR_SOCKS_CONNECTION_FAILED: |
[email protected] | 86ec30d | 2008-09-29 21:53:54 | [diff] [blame] | 1776 | break; |
[email protected] | d5a30959 | 2010-02-05 02:22:52 | [diff] [blame] | 1777 | case ERR_SOCKS_CONNECTION_HOST_UNREACHABLE: |
| 1778 | // Remap the SOCKS-specific "host unreachable" error to a more |
| 1779 | // generic error code (this way consumers like the link doctor |
| 1780 | // know to substitute their error page). |
| 1781 | // |
| 1782 | // Note that if the host resolving was done by the SOCSK5 proxy, we can't |
| 1783 | // differentiate between a proxy-side "host not found" versus a proxy-side |
| 1784 | // "address unreachable" error, and will report both of these failures as |
| 1785 | // ERR_ADDRESS_UNREACHABLE. |
| 1786 | return ERR_ADDRESS_UNREACHABLE; |
[email protected] | 86ec30d | 2008-09-29 21:53:54 | [diff] [blame] | 1787 | default: |
| 1788 | return error; |
| 1789 | } |
| 1790 | |
[email protected] | 677c9057 | 2008-12-10 09:03:15 | [diff] [blame] | 1791 | if (request_->load_flags & LOAD_BYPASS_PROXY) { |
| 1792 | return error; |
| 1793 | } |
| 1794 | |
[email protected] | 86ec30d | 2008-09-29 21:53:54 | [diff] [blame] | 1795 | int rv = session_->proxy_service()->ReconsiderProxyAfterError( |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1796 | request_->url, &proxy_info_, &io_callback_, &pac_request_, net_log_); |
[email protected] | 86ec30d | 2008-09-29 21:53:54 | [diff] [blame] | 1797 | if (rv == OK || rv == ERR_IO_PENDING) { |
[email protected] | 9172a98 | 2009-06-06 00:30:25 | [diff] [blame] | 1798 | // If the error was during connection setup, there is no socket to |
| 1799 | // disconnect. |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 1800 | if (connection_->socket()) |
| 1801 | connection_->socket()->Disconnect(); |
| 1802 | connection_->Reset(); |
[email protected] | 86ec30d | 2008-09-29 21:53:54 | [diff] [blame] | 1803 | next_state_ = STATE_RESOLVE_PROXY_COMPLETE; |
| 1804 | } else { |
[email protected] | 6971906 | 2010-01-05 20:09:21 | [diff] [blame] | 1805 | // If ReconsiderProxyAfterError() failed synchronously, it means |
| 1806 | // there was nothing left to fall-back to, so fail the transaction |
| 1807 | // with the last connection error we got. |
| 1808 | // TODO(eroman): This is a confusing contract, make it more obvious. |
[email protected] | 86ec30d | 2008-09-29 21:53:54 | [diff] [blame] | 1809 | rv = error; |
| 1810 | } |
| 1811 | |
| 1812 | return rv; |
| 1813 | } |
| 1814 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1815 | bool HttpNetworkTransaction::ShouldApplyProxyAuth() const { |
[email protected] | 874190c | 2010-04-28 05:35:40 | [diff] [blame] | 1816 | return (!using_ssl_ && proxy_info_.is_http()) || establishing_tunnel_; |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1817 | } |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1818 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1819 | bool HttpNetworkTransaction::ShouldApplyServerAuth() const { |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 1820 | return !establishing_tunnel_ && |
| 1821 | !(request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1822 | } |
| 1823 | |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 1824 | void HttpNetworkTransaction::AddAuthorizationHeader( |
| 1825 | HttpAuth::Target target, HttpRequestHeaders* authorization_headers) const { |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1826 | DCHECK(HaveAuth(target)); |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 1827 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1828 | // Add a Authorization/Proxy-Authorization header line. |
[email protected] | ac3fa8e2 | 2010-02-05 19:13:29 | [diff] [blame] | 1829 | std::string auth_token; |
[email protected] | d7f1663 | 2010-03-29 18:02:36 | [diff] [blame] | 1830 | int rv; |
| 1831 | if (auth_identity_[target].source == |
| 1832 | HttpAuth::IDENT_SRC_DEFAULT_CREDENTIALS) { |
| 1833 | rv = auth_handler_[target]->GenerateDefaultAuthToken( |
| 1834 | request_, &proxy_info_, &auth_token); |
| 1835 | } else { |
| 1836 | rv = auth_handler_[target]->GenerateAuthToken( |
| 1837 | auth_identity_[target].username, |
| 1838 | auth_identity_[target].password, |
| 1839 | request_, &proxy_info_, &auth_token); |
| 1840 | } |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 1841 | if (rv == OK) { |
| 1842 | authorization_headers->SetHeader( |
| 1843 | HttpAuth::GetAuthorizationHeaderName(target), auth_token); |
| 1844 | } |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1845 | |
[email protected] | ac3fa8e2 | 2010-02-05 19:13:29 | [diff] [blame] | 1846 | // TODO(cbentzel): Evict username and password from cache on non-OK return? |
| 1847 | // TODO(cbentzel): Never use this scheme again if |
| 1848 | // ERR_UNSUPPORTED_AUTH_SCHEME is returned. |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1849 | } |
| 1850 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1851 | GURL HttpNetworkTransaction::AuthOrigin(HttpAuth::Target target) const { |
[email protected] | 8fdbcd2 | 2010-05-05 02:54:52 | [diff] [blame] | 1852 | GURL origin = PossiblyInvalidAuthOrigin(target); |
| 1853 | DCHECK(origin.is_valid()); |
| 1854 | return origin; |
| 1855 | } |
| 1856 | |
| 1857 | GURL HttpNetworkTransaction::PossiblyInvalidAuthOrigin( |
| 1858 | HttpAuth::Target target) const { |
| 1859 | switch (target) { |
| 1860 | case HttpAuth::AUTH_PROXY: |
| 1861 | if (!proxy_info_.proxy_server().is_valid() || |
| 1862 | proxy_info_.proxy_server().is_direct()) { |
| 1863 | return GURL(); // There is no proxy server. |
| 1864 | } |
| 1865 | return GURL("http://" + proxy_info_.proxy_server().host_and_port()); |
| 1866 | case HttpAuth::AUTH_SERVER: |
| 1867 | return request_->url.GetOrigin(); |
| 1868 | default: |
| 1869 | return GURL(); |
| 1870 | } |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1871 | } |
| 1872 | |
| 1873 | std::string HttpNetworkTransaction::AuthPath(HttpAuth::Target target) |
| 1874 | const { |
| 1875 | // Proxy authentication realms apply to all paths. So we will use |
| 1876 | // empty string in place of an absolute path. |
| 1877 | return target == HttpAuth::AUTH_PROXY ? |
| 1878 | std::string() : request_->url.path(); |
| 1879 | } |
| 1880 | |
[email protected] | 3c86adc6 | 2009-04-21 16:48:21 | [diff] [blame] | 1881 | // static |
| 1882 | std::string HttpNetworkTransaction::AuthTargetString( |
| 1883 | HttpAuth::Target target) { |
| 1884 | return target == HttpAuth::AUTH_PROXY ? "proxy" : "server"; |
| 1885 | } |
| 1886 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1887 | void HttpNetworkTransaction::InvalidateRejectedAuthFromCache( |
[email protected] | f494fae | 2009-10-15 17:00:47 | [diff] [blame] | 1888 | HttpAuth::Target target, |
| 1889 | const GURL& auth_origin) { |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1890 | DCHECK(HaveAuth(target)); |
| 1891 | |
| 1892 | // TODO(eroman): this short-circuit can be relaxed. If the realm of |
| 1893 | // the preemptively used auth entry matches the realm of the subsequent |
| 1894 | // challenge, then we can invalidate the preemptively used entry. |
| 1895 | // Otherwise as-is we may send the failed credentials one extra time. |
| 1896 | if (auth_identity_[target].source == HttpAuth::IDENT_SRC_PATH_LOOKUP) |
| 1897 | return; |
| 1898 | |
| 1899 | // Clear the cache entry for the identity we just failed on. |
| 1900 | // Note: we require the username/password to match before invalidating |
| 1901 | // since the entry in the cache may be newer than what we used last time. |
[email protected] | f494fae | 2009-10-15 17:00:47 | [diff] [blame] | 1902 | session_->auth_cache()->Remove(auth_origin, |
[email protected] | 5d0153c51 | 2009-01-12 19:08:36 | [diff] [blame] | 1903 | auth_handler_[target]->realm(), |
[email protected] | 9001c8c | 2010-05-13 16:21:40 | [diff] [blame] | 1904 | auth_handler_[target]->scheme(), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1905 | auth_identity_[target].username, |
| 1906 | auth_identity_[target].password); |
| 1907 | } |
| 1908 | |
| 1909 | bool HttpNetworkTransaction::SelectPreemptiveAuth(HttpAuth::Target target) { |
| 1910 | DCHECK(!HaveAuth(target)); |
| 1911 | |
| 1912 | // Don't do preemptive authorization if the URL contains a username/password, |
| 1913 | // since we must first be challenged in order to use the URL's identity. |
| 1914 | if (request_->url.has_username()) |
| 1915 | return false; |
| 1916 | |
| 1917 | // SelectPreemptiveAuth() is on the critical path for each request, so it |
| 1918 | // is expected to be fast. LookupByPath() is fast in the common case, since |
| 1919 | // the number of http auth cache entries is expected to be very small. |
| 1920 | // (For most users in fact, it will be 0.) |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1921 | HttpAuthCache::Entry* entry = session_->auth_cache()->LookupByPath( |
| 1922 | AuthOrigin(target), AuthPath(target)); |
[email protected] | fa82f93 | 2010-05-20 11:09:24 | [diff] [blame] | 1923 | if (!entry) |
| 1924 | return false; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1925 | |
[email protected] | fa82f93 | 2010-05-20 11:09:24 | [diff] [blame] | 1926 | // Try to create a handler using the previous auth challenge. |
| 1927 | scoped_refptr<HttpAuthHandler> handler_preemptive; |
| 1928 | int rv_create = session_->http_auth_handler_factory()-> |
| 1929 | CreatePreemptiveAuthHandlerFromString( |
| 1930 | entry->auth_challenge(), target, AuthOrigin(target), |
| 1931 | entry->IncrementNonceCount(), &handler_preemptive); |
| 1932 | if (rv_create != OK) |
| 1933 | return false; |
[email protected] | d7f1663 | 2010-03-29 18:02:36 | [diff] [blame] | 1934 | |
[email protected] | fa82f93 | 2010-05-20 11:09:24 | [diff] [blame] | 1935 | // Set the state |
| 1936 | auth_identity_[target].source = HttpAuth::IDENT_SRC_PATH_LOOKUP; |
| 1937 | auth_identity_[target].invalid = false; |
| 1938 | auth_identity_[target].username = entry->username(); |
| 1939 | auth_identity_[target].password = entry->password(); |
| 1940 | auth_handler_[target] = handler_preemptive; |
| 1941 | return true; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1942 | } |
| 1943 | |
| 1944 | bool HttpNetworkTransaction::SelectNextAuthIdentityToTry( |
[email protected] | f494fae | 2009-10-15 17:00:47 | [diff] [blame] | 1945 | HttpAuth::Target target, |
| 1946 | const GURL& auth_origin) { |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1947 | DCHECK(auth_handler_[target]); |
| 1948 | DCHECK(auth_identity_[target].invalid); |
| 1949 | |
| 1950 | // Try to use the username/password encoded into the URL first. |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1951 | if (target == HttpAuth::AUTH_SERVER && request_->url.has_username() && |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 1952 | !embedded_identity_used_) { |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1953 | auth_identity_[target].source = HttpAuth::IDENT_SRC_URL; |
| 1954 | auth_identity_[target].invalid = false; |
[email protected] | a97cca4 | 2009-08-14 01:00:29 | [diff] [blame] | 1955 | // Extract the username:password from the URL. |
[email protected] | 99d6935 | 2009-09-16 00:20:29 | [diff] [blame] | 1956 | GetIdentityFromURL(request_->url, |
[email protected] | a97cca4 | 2009-08-14 01:00:29 | [diff] [blame] | 1957 | &auth_identity_[target].username, |
| 1958 | &auth_identity_[target].password); |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 1959 | embedded_identity_used_ = true; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1960 | // TODO(eroman): If the password is blank, should we also try combining |
| 1961 | // with a password from the cache? |
| 1962 | return true; |
| 1963 | } |
| 1964 | |
| 1965 | // Check the auth cache for a realm entry. |
[email protected] | 9001c8c | 2010-05-13 16:21:40 | [diff] [blame] | 1966 | HttpAuthCache::Entry* entry = |
| 1967 | session_->auth_cache()->Lookup(auth_origin, auth_handler_[target]->realm(), |
| 1968 | auth_handler_[target]->scheme()); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1969 | |
| 1970 | if (entry) { |
[email protected] | 9001c8c | 2010-05-13 16:21:40 | [diff] [blame] | 1971 | auth_identity_[target].source = HttpAuth::IDENT_SRC_REALM_LOOKUP; |
| 1972 | auth_identity_[target].invalid = false; |
| 1973 | auth_identity_[target].username = entry->username(); |
| 1974 | auth_identity_[target].password = entry->password(); |
| 1975 | return true; |
[email protected] | d7f1663 | 2010-03-29 18:02:36 | [diff] [blame] | 1976 | } |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1977 | |
[email protected] | d7f1663 | 2010-03-29 18:02:36 | [diff] [blame] | 1978 | // Use default credentials (single sign on) if this is the first attempt |
| 1979 | // at identity. Do not allow multiple times as it will infinite loop. |
| 1980 | // We use default credentials after checking the auth cache so that if |
| 1981 | // single sign-on doesn't work, we won't try default credentials for future |
| 1982 | // transactions. |
[email protected] | b4955e7d | 2010-04-16 20:22:30 | [diff] [blame] | 1983 | if (!default_credentials_used_ && |
| 1984 | auth_handler_[target]->AllowsDefaultCredentials()) { |
[email protected] | d7f1663 | 2010-03-29 18:02:36 | [diff] [blame] | 1985 | auth_identity_[target].source = HttpAuth::IDENT_SRC_DEFAULT_CREDENTIALS; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1986 | auth_identity_[target].invalid = false; |
[email protected] | d7f1663 | 2010-03-29 18:02:36 | [diff] [blame] | 1987 | default_credentials_used_ = true; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1988 | return true; |
| 1989 | } |
[email protected] | d7f1663 | 2010-03-29 18:02:36 | [diff] [blame] | 1990 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1991 | return false; |
| 1992 | } |
| 1993 | |
[email protected] | 3c86adc6 | 2009-04-21 16:48:21 | [diff] [blame] | 1994 | std::string HttpNetworkTransaction::AuthChallengeLogMessage() const { |
| 1995 | std::string msg; |
| 1996 | std::string header_val; |
| 1997 | void* iter = NULL; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1998 | scoped_refptr<HttpResponseHeaders> headers = GetResponseHeaders(); |
| 1999 | while (headers->EnumerateHeader(&iter, "proxy-authenticate", &header_val)) { |
[email protected] | 3c86adc6 | 2009-04-21 16:48:21 | [diff] [blame] | 2000 | msg.append("\n Has header Proxy-Authenticate: "); |
| 2001 | msg.append(header_val); |
| 2002 | } |
| 2003 | |
| 2004 | iter = NULL; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 2005 | while (headers->EnumerateHeader(&iter, "www-authenticate", &header_val)) { |
[email protected] | 3c86adc6 | 2009-04-21 16:48:21 | [diff] [blame] | 2006 | msg.append("\n Has header WWW-Authenticate: "); |
| 2007 | msg.append(header_val); |
| 2008 | } |
| 2009 | |
| 2010 | // RFC 4559 requires that a proxy indicate its support of NTLM/Negotiate |
| 2011 | // authentication with a "Proxy-Support: Session-Based-Authentication" |
| 2012 | // response header. |
| 2013 | iter = NULL; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 2014 | while (headers->EnumerateHeader(&iter, "proxy-support", &header_val)) { |
[email protected] | 3c86adc6 | 2009-04-21 16:48:21 | [diff] [blame] | 2015 | msg.append("\n Has header Proxy-Support: "); |
| 2016 | msg.append(header_val); |
| 2017 | } |
| 2018 | |
| 2019 | return msg; |
| 2020 | } |
| 2021 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2022 | int HttpNetworkTransaction::HandleAuthChallenge() { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 2023 | scoped_refptr<HttpResponseHeaders> headers = GetResponseHeaders(); |
| 2024 | DCHECK(headers); |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 2025 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 2026 | int status = headers->response_code(); |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 2027 | if (status != 401 && status != 407) |
| 2028 | return OK; |
| 2029 | HttpAuth::Target target = status == 407 ? |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 2030 | HttpAuth::AUTH_PROXY : HttpAuth::AUTH_SERVER; |
[email protected] | 8fdbcd2 | 2010-05-05 02:54:52 | [diff] [blame] | 2031 | GURL auth_origin = PossiblyInvalidAuthOrigin(target); |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 2032 | |
[email protected] | 3c86adc6 | 2009-04-21 16:48:21 | [diff] [blame] | 2033 | LOG(INFO) << "The " << AuthTargetString(target) << " " |
[email protected] | f494fae | 2009-10-15 17:00:47 | [diff] [blame] | 2034 | << auth_origin << " requested auth" |
[email protected] | 3c86adc6 | 2009-04-21 16:48:21 | [diff] [blame] | 2035 | << AuthChallengeLogMessage(); |
| 2036 | |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2037 | if (target == HttpAuth::AUTH_PROXY && proxy_info_.is_direct()) |
| 2038 | return ERR_UNEXPECTED_PROXY_AUTH; |
[email protected] | 8fdbcd2 | 2010-05-05 02:54:52 | [diff] [blame] | 2039 | DCHECK(auth_origin.is_valid()); |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 2040 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2041 | // The auth we tried just failed, hence it can't be valid. Remove it from |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 2042 | // the cache so it won't be used again. |
| 2043 | // TODO(wtc): IsFinalRound is not the right condition. In a multi-round |
| 2044 | // auth sequence, the server may fail the auth in round 1 if our first |
| 2045 | // authorization header is broken. We should inspect response_.headers to |
| 2046 | // determine if the server already failed the auth or wants us to continue. |
| 2047 | // See http://crbug.com/21015. |
| 2048 | if (HaveAuth(target) && auth_handler_[target]->IsFinalRound()) { |
[email protected] | f494fae | 2009-10-15 17:00:47 | [diff] [blame] | 2049 | InvalidateRejectedAuthFromCache(target, auth_origin); |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 2050 | auth_handler_[target] = NULL; |
| 2051 | auth_identity_[target] = HttpAuth::Identity(); |
| 2052 | } |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2053 | |
| 2054 | auth_identity_[target].invalid = true; |
| 2055 | |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 2056 | if (target != HttpAuth::AUTH_SERVER || |
| 2057 | !(request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA)) { |
| 2058 | // Find the best authentication challenge that we support. |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 2059 | HttpAuth::ChooseBestChallenge(session_->http_auth_handler_factory(), |
| 2060 | headers, target, |
| 2061 | auth_origin, &auth_handler_[target]); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 2062 | } |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 2063 | |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 2064 | if (!auth_handler_[target]) { |
| 2065 | if (establishing_tunnel_) { |
[email protected] | 3c86adc6 | 2009-04-21 16:48:21 | [diff] [blame] | 2066 | LOG(ERROR) << "Can't perform auth to the " << AuthTargetString(target) |
[email protected] | f494fae | 2009-10-15 17:00:47 | [diff] [blame] | 2067 | << " " << auth_origin << " when establishing a tunnel" |
[email protected] | 3c86adc6 | 2009-04-21 16:48:21 | [diff] [blame] | 2068 | << AuthChallengeLogMessage(); |
[email protected] | 655bdba | 2009-03-13 23:35:38 | [diff] [blame] | 2069 | |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 2070 | // We are establishing a tunnel, we can't show the error page because an |
| 2071 | // active network attacker could control its contents. Instead, we just |
| 2072 | // fail to establish the tunnel. |
[email protected] | 9f9f86c | 2009-03-12 22:32:42 | [diff] [blame] | 2073 | DCHECK(target == HttpAuth::AUTH_PROXY); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 2074 | return ERR_PROXY_AUTH_REQUESTED; |
| 2075 | } |
| 2076 | // We found no supported challenge -- let the transaction continue |
| 2077 | // so we end up displaying the error page. |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 2078 | return OK; |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 2079 | } |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 2080 | |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2081 | if (auth_handler_[target]->NeedsIdentity()) { |
| 2082 | // Pick a new auth identity to try, by looking to the URL and auth cache. |
| 2083 | // If an identity to try is found, it is saved to auth_identity_[target]. |
[email protected] | f494fae | 2009-10-15 17:00:47 | [diff] [blame] | 2084 | SelectNextAuthIdentityToTry(target, auth_origin); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2085 | } else { |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 2086 | // Proceed with the existing identity or a null identity. |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2087 | // |
| 2088 | // TODO(wtc): Add a safeguard against infinite transaction restarts, if |
| 2089 | // the server keeps returning "NTLM". |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2090 | auth_identity_[target].invalid = false; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2091 | } |
| 2092 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2093 | // Make a note that we are waiting for auth. This variable is inspected |
| 2094 | // when the client calls RestartWithAuth() to pick up where we left off. |
| 2095 | pending_auth_target_ = target; |
| 2096 | |
| 2097 | if (auth_identity_[target].invalid) { |
| 2098 | // We have exhausted all identity possibilities, all we can do now is |
| 2099 | // pass the challenge information back to the client. |
[email protected] | f494fae | 2009-10-15 17:00:47 | [diff] [blame] | 2100 | PopulateAuthChallenge(target, auth_origin); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2101 | } |
[email protected] | e5ae96a | 2010-04-14 20:12:45 | [diff] [blame] | 2102 | |
| 2103 | // SPN determination (for Negotiate) requires a DNS lookup to find the |
| 2104 | // canonical name. This needs to be done asynchronously to prevent blocking |
| 2105 | // the IO thread. |
| 2106 | if (auth_handler_[target]->NeedsCanonicalName()) |
| 2107 | next_state_ = STATE_RESOLVE_CANONICAL_NAME; |
| 2108 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2109 | return OK; |
| 2110 | } |
| 2111 | |
[email protected] | f494fae | 2009-10-15 17:00:47 | [diff] [blame] | 2112 | void HttpNetworkTransaction::PopulateAuthChallenge(HttpAuth::Target target, |
| 2113 | const GURL& auth_origin) { |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2114 | // Populates response_.auth_challenge with the authentication challenge info. |
| 2115 | // This info is consumed by URLRequestHttpJob::GetAuthChallengeInfo(). |
| 2116 | |
| 2117 | AuthChallengeInfo* auth_info = new AuthChallengeInfo; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 2118 | auth_info->is_proxy = target == HttpAuth::AUTH_PROXY; |
[email protected] | f494fae | 2009-10-15 17:00:47 | [diff] [blame] | 2119 | auth_info->host_and_port = ASCIIToWide(GetHostAndPort(auth_origin)); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2120 | auth_info->scheme = ASCIIToWide(auth_handler_[target]->scheme()); |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 2121 | // TODO(eroman): decode realm according to RFC 2047. |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2122 | auth_info->realm = ASCIIToWide(auth_handler_[target]->realm()); |
[email protected] | a7e4131 | 2009-12-16 23:18:14 | [diff] [blame] | 2123 | response_.auth_challenge = auth_info; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 2124 | } |
| 2125 | |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 2126 | void HttpNetworkTransaction::MarkBrokenAlternateProtocolAndFallback() { |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 2127 | // We have to: |
| 2128 | // * Reset the endpoint to be the unmodified URL specified destination. |
| 2129 | // * Mark the endpoint as broken so we don't try again. |
| 2130 | // * Set the alternate protocol mode to kDoNotUseAlternateProtocol so we |
| 2131 | // ignore future Alternate-Protocol headers from the HostPortPair. |
| 2132 | // * Reset the connection and go back to STATE_INIT_CONNECTION. |
| 2133 | |
| 2134 | endpoint_ = HostPortPair(request_->url.HostNoBrackets(), |
| 2135 | request_->url.EffectiveIntPort()); |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 2136 | |
| 2137 | session_->mutable_alternate_protocols()->MarkBrokenAlternateProtocolFor( |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 2138 | endpoint_); |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 2139 | |
| 2140 | alternate_protocol_mode_ = kDoNotUseAlternateProtocol; |
| 2141 | if (connection_->socket()) |
| 2142 | connection_->socket()->Disconnect(); |
| 2143 | connection_->Reset(); |
| 2144 | next_state_ = STATE_INIT_CONNECTION; |
| 2145 | } |
| 2146 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 2147 | } // namespace net |