Make Ctrl + T from apps focus the new tab's omnibox on Linux and ChromeOS.
[chromium-blink-merge.git] / net / http / proxy_client_socket.cc
blobab072d106cdd8f4374eddb73edb36e67456d92ad
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 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();
45 *request_line =
46 base::StringPrintf("CONNECT %s HTTP/1.1\r\n", host_and_port.c_str());
47 request_headers->SetHeader(HttpRequestHeaders::kHost, endpoint.port() == 443
48 ? endpoint.host()
49 : host_and_port);
50 request_headers->SetHeader(HttpRequestHeaders::kProxyConnection,
51 "keep-alive");
52 if (!user_agent.empty())
53 request_headers->SetHeader(HttpRequestHeaders::kUserAgent, user_agent);
55 request_headers->MergeFrom(auth_headers);
58 // static
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();
65 if (rv == OK)
66 return ERR_PROXY_AUTH_REQUESTED;
67 return rv;
70 // static
71 void ProxyClientSocket::LogBlockedTunnelResponse(int http_status_code,
72 bool is_https_proxy) {
73 if (is_https_proxy) {
74 UMA_HISTOGRAM_CUSTOM_ENUMERATION(
75 "Net.BlockedTunnelResponse.HttpsProxy",
76 HttpUtil::MapStatusCodeForHistogram(http_status_code),
77 HttpUtil::GetStatusCodesForHistogram());
78 } else {
79 UMA_HISTOGRAM_CUSTOM_ENUMERATION(
80 "Net.BlockedTunnelResponse.HttpProxy",
81 HttpUtil::MapStatusCodeForHistogram(http_status_code),
82 HttpUtil::GetStatusCodesForHistogram());
86 // static
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
97 // behavior.
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;
111 return true;
114 // static
115 bool ProxyClientSocket::SanitizeProxyRedirect(HttpResponseInfo* response) {
116 DCHECK(response && response->headers.get());
118 std::string location;
119 if (!response->headers->IsRedirect(&location))
120 return false;
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"
125 "Location: %s\n"
126 "Content-Length: 0\n"
127 "Connection: close\n"
128 "\n",
129 location.c_str());
130 std::string raw_headers =
131 HttpUtil::AssembleRawHeaders(fake_response_headers.data(),
132 fake_response_headers.length());
133 response->headers = new HttpResponseHeaders(raw_headers);
135 return true;
138 } // namespace net