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"
22 void CopyHeaderValues(scoped_refptr
<HttpResponseHeaders
> source
,
23 scoped_refptr
<HttpResponseHeaders
> dest
,
24 const std::string
& header_name
) {
26 std::string header_value
;
28 while (source
->EnumerateHeader(&iter
, header_name
, &header_value
))
29 dest
->AddHeader(header_name
+ ": " + header_value
);
35 void ProxyClientSocket::BuildTunnelRequest(
36 const HostPortPair
& endpoint
,
37 const HttpRequestHeaders
& auth_headers
,
38 const std::string
& user_agent
,
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 std::string host_and_port
= endpoint
.ToString();
46 base::StringPrintf("CONNECT %s HTTP/1.1\r\n", host_and_port
.c_str());
47 request_headers
->SetHeader(HttpRequestHeaders::kHost
, endpoint
.port() == 443
50 request_headers
->SetHeader(HttpRequestHeaders::kProxyConnection
,
52 if (!user_agent
.empty())
53 request_headers
->SetHeader(HttpRequestHeaders::kUserAgent
, user_agent
);
55 request_headers
->MergeFrom(auth_headers
);
59 int ProxyClientSocket::HandleProxyAuthChallenge(HttpAuthController
* auth
,
60 HttpResponseInfo
* response
,
61 const BoundNetLog
& net_log
) {
62 DCHECK(response
->headers
.get());
63 int rv
= auth
->HandleAuthChallenge(response
->headers
, false, true, net_log
);
64 response
->auth_challenge
= auth
->auth_info();
66 return ERR_PROXY_AUTH_REQUESTED
;
71 void ProxyClientSocket::LogBlockedTunnelResponse(int http_status_code
,
72 bool is_https_proxy
) {
74 UMA_HISTOGRAM_CUSTOM_ENUMERATION(
75 "Net.BlockedTunnelResponse.HttpsProxy",
76 HttpUtil::MapStatusCodeForHistogram(http_status_code
),
77 HttpUtil::GetStatusCodesForHistogram());
79 UMA_HISTOGRAM_CUSTOM_ENUMERATION(
80 "Net.BlockedTunnelResponse.HttpProxy",
81 HttpUtil::MapStatusCodeForHistogram(http_status_code
),
82 HttpUtil::GetStatusCodesForHistogram());
87 bool ProxyClientSocket::SanitizeProxyAuth(HttpResponseInfo
* response
) {
88 DCHECK(response
&& response
->headers
.get());
90 scoped_refptr
<HttpResponseHeaders
> old_headers
= response
->headers
;
92 const char kHeaders
[] = "HTTP/1.1 407 Proxy Authentication Required\n\n";
93 scoped_refptr
<HttpResponseHeaders
> new_headers
= new HttpResponseHeaders(
94 HttpUtil::AssembleRawHeaders(kHeaders
, arraysize(kHeaders
)));
96 // Copy status line and all hop-by-hop headers to preserve keep-alive
98 new_headers
->ReplaceStatusLine(old_headers
->GetStatusLine());
99 CopyHeaderValues(old_headers
, new_headers
, "connection");
100 CopyHeaderValues(old_headers
, new_headers
, "proxy-connection");
101 CopyHeaderValues(old_headers
, new_headers
, "keep-alive");
102 CopyHeaderValues(old_headers
, new_headers
, "trailer");
103 CopyHeaderValues(old_headers
, new_headers
, "transfer-encoding");
104 CopyHeaderValues(old_headers
, new_headers
, "upgrade");
106 CopyHeaderValues(old_headers
, new_headers
, "content-length");
108 CopyHeaderValues(old_headers
, new_headers
, "proxy-authenticate");
110 response
->headers
= new_headers
;
115 bool ProxyClientSocket::SanitizeProxyRedirect(HttpResponseInfo
* response
) {
116 DCHECK(response
&& response
->headers
.get());
118 std::string location
;
119 if (!response
->headers
->IsRedirect(&location
))
122 // Return minimal headers; set "Content-Length: 0" to ignore response body.
123 std::string fake_response_headers
= base::StringPrintf(
124 "HTTP/1.0 302 Found\n"
126 "Content-Length: 0\n"
127 "Connection: close\n"
130 std::string raw_headers
=
131 HttpUtil::AssembleRawHeaders(fake_response_headers
.data(),
132 fake_response_headers
.length());
133 response
->headers
= new HttpResponseHeaders(raw_headers
);