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 "googleurl/src/gurl.h"
10 #include "net/base/host_port_pair.h"
11 #include "net/base/net_errors.h"
12 #include "net/base/net_util.h"
13 #include "net/http/http_auth_controller.h"
14 #include "net/http/http_request_info.h"
15 #include "net/http/http_response_headers.h"
16 #include "net/http/http_response_info.h"
21 void ProxyClientSocket::BuildTunnelRequest(
22 const HttpRequestInfo
& request_info
,
23 const HttpRequestHeaders
& auth_headers
,
24 const HostPortPair
& endpoint
,
25 std::string
* request_line
,
26 HttpRequestHeaders
* request_headers
) {
27 // RFC 2616 Section 9 says the Host request-header field MUST accompany all
28 // HTTP/1.1 requests. Add "Proxy-Connection: keep-alive" for compat with
29 // HTTP/1.0 proxies such as Squid (required for NTLM authentication).
30 *request_line
= base::StringPrintf(
31 "CONNECT %s HTTP/1.1\r\n", endpoint
.ToString().c_str());
32 request_headers
->SetHeader(HttpRequestHeaders::kHost
,
33 GetHostAndOptionalPort(request_info
.url
));
34 request_headers
->SetHeader(HttpRequestHeaders::kProxyConnection
,
37 std::string user_agent
;
38 if (request_info
.extra_headers
.GetHeader(HttpRequestHeaders::kUserAgent
,
40 request_headers
->SetHeader(HttpRequestHeaders::kUserAgent
, user_agent
);
42 request_headers
->MergeFrom(auth_headers
);
46 int ProxyClientSocket::HandleProxyAuthChallenge(HttpAuthController
* auth
,
47 HttpResponseInfo
* response
,
48 const BoundNetLog
& net_log
) {
49 DCHECK(response
->headers
.get());
50 int rv
= auth
->HandleAuthChallenge(response
->headers
, false, true, net_log
);
51 response
->auth_challenge
= auth
->auth_info();
53 return ERR_PROXY_AUTH_REQUESTED
;
58 void ProxyClientSocket::LogBlockedTunnelResponse(int http_status_code
,
60 bool is_https_proxy
) {
62 UMA_HISTOGRAM_CUSTOM_ENUMERATION(
63 "Net.BlockedTunnelResponse.HttpsProxy",
64 HttpUtil::MapStatusCodeForHistogram(http_status_code
),
65 HttpUtil::GetStatusCodesForHistogram());
67 UMA_HISTOGRAM_CUSTOM_ENUMERATION(
68 "Net.BlockedTunnelResponse.HttpProxy",
69 HttpUtil::MapStatusCodeForHistogram(http_status_code
),
70 HttpUtil::GetStatusCodesForHistogram());
75 bool ProxyClientSocket::SanitizeProxyRedirect(HttpResponseInfo
* response
,
77 DCHECK(response
&& response
->headers
.get());
80 if (!response
->headers
->IsRedirect(&location
))
83 // Return minimal headers; set "Content-length: 0" to ignore response body.
84 std::string fake_response_headers
=
85 base::StringPrintf("HTTP/1.0 302 Found\n"
91 std::string raw_headers
=
92 HttpUtil::AssembleRawHeaders(fake_response_headers
.data(),
93 fake_response_headers
.length());
94 response
->headers
= new HttpResponseHeaders(raw_headers
);