Fix UnlockEndpointIsDelayed again.
[chromium-blink-merge.git] / net / http / proxy_client_socket.cc
blob3c539c6895e295a98f9645070e4258bbf78b5d73
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/http/proxy_client_socket.h"
7 #include "base/metrics/histogram.h"
8 #include "base/strings/stringprintf.h"
9 #include "net/base/host_port_pair.h"
10 #include "net/base/net_errors.h"
11 #include "net/base/net_util.h"
12 #include "net/http/http_auth_controller.h"
13 #include "net/http/http_request_info.h"
14 #include "net/http/http_response_headers.h"
15 #include "net/http/http_response_info.h"
16 #include "url/gurl.h"
18 namespace net {
20 namespace {
22 void CopyHeaderValues(scoped_refptr<HttpResponseHeaders> source,
23 scoped_refptr<HttpResponseHeaders> dest,
24 const std::string& header_name) {
25 void* iter = NULL;
26 std::string header_value;
28 while (source->EnumerateHeader(&iter, header_name, &header_value))
29 dest->AddHeader(header_name + ": " + header_value);
32 } // namespace
34 // static
35 void ProxyClientSocket::BuildTunnelRequest(
36 const HttpRequestInfo& request_info,
37 const HttpRequestHeaders& auth_headers,
38 const HostPortPair& endpoint,
39 std::string* request_line,
40 HttpRequestHeaders* request_headers) {
41 // RFC 2616 Section 9 says the Host request-header field MUST accompany all
42 // HTTP/1.1 requests. Add "Proxy-Connection: keep-alive" for compat with
43 // HTTP/1.0 proxies such as Squid (required for NTLM authentication).
44 *request_line = base::StringPrintf(
45 "CONNECT %s HTTP/1.1\r\n", endpoint.ToString().c_str());
46 request_headers->SetHeader(HttpRequestHeaders::kHost,
47 GetHostAndOptionalPort(request_info.url));
48 request_headers->SetHeader(HttpRequestHeaders::kProxyConnection,
49 "keep-alive");
51 std::string user_agent;
52 if (request_info.extra_headers.GetHeader(HttpRequestHeaders::kUserAgent,
53 &user_agent))
54 request_headers->SetHeader(HttpRequestHeaders::kUserAgent, user_agent);
56 request_headers->MergeFrom(auth_headers);
59 // static
60 int ProxyClientSocket::HandleProxyAuthChallenge(HttpAuthController* auth,
61 HttpResponseInfo* response,
62 const BoundNetLog& net_log) {
63 DCHECK(response->headers.get());
64 int rv = auth->HandleAuthChallenge(response->headers, false, true, net_log);
65 response->auth_challenge = auth->auth_info();
66 if (rv == OK)
67 return ERR_PROXY_AUTH_REQUESTED;
68 return rv;
71 // static
72 void ProxyClientSocket::LogBlockedTunnelResponse(int http_status_code,
73 const GURL& url,
74 bool is_https_proxy) {
75 if (is_https_proxy) {
76 UMA_HISTOGRAM_CUSTOM_ENUMERATION(
77 "Net.BlockedTunnelResponse.HttpsProxy",
78 HttpUtil::MapStatusCodeForHistogram(http_status_code),
79 HttpUtil::GetStatusCodesForHistogram());
80 } else {
81 UMA_HISTOGRAM_CUSTOM_ENUMERATION(
82 "Net.BlockedTunnelResponse.HttpProxy",
83 HttpUtil::MapStatusCodeForHistogram(http_status_code),
84 HttpUtil::GetStatusCodesForHistogram());
88 // static
89 bool ProxyClientSocket::SanitizeProxyAuth(HttpResponseInfo* response) {
90 DCHECK(response && response->headers.get());
92 scoped_refptr<HttpResponseHeaders> old_headers = response->headers;
94 const char kHeaders[] = "HTTP/1.1 407 Proxy Authentication Required\n\n";
95 scoped_refptr<HttpResponseHeaders> new_headers = new HttpResponseHeaders(
96 HttpUtil::AssembleRawHeaders(kHeaders, arraysize(kHeaders)));
98 new_headers->ReplaceStatusLine(old_headers->GetStatusLine());
99 CopyHeaderValues(old_headers, new_headers, "Connection");
100 CopyHeaderValues(old_headers, new_headers, "Proxy-Authenticate");
102 response->headers = new_headers;
103 return true;
106 // static
107 bool ProxyClientSocket::SanitizeProxyRedirect(HttpResponseInfo* response) {
108 DCHECK(response && response->headers.get());
110 std::string location;
111 if (!response->headers->IsRedirect(&location))
112 return false;
114 // Return minimal headers; set "Content-Length: 0" to ignore response body.
115 std::string fake_response_headers = base::StringPrintf(
116 "HTTP/1.0 302 Found\n"
117 "Location: %s\n"
118 "Content-Length: 0\n"
119 "Connection: close\n"
120 "\n",
121 location.c_str());
122 std::string raw_headers =
123 HttpUtil::AssembleRawHeaders(fake_response_headers.data(),
124 fake_response_headers.length());
125 response->headers = new HttpResponseHeaders(raw_headers);
127 return true;
130 } // namespace net