[Cronet] Delay StartNetLog and StopNetLog until native request context is initialized
[chromium-blink-merge.git] / net / http / proxy_client_socket.cc
blob3c0d12d5cb2ada770a0e8114c867a44c1fe059fd
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 // Copy status line and all hop-by-hop headers to preserve keep-alive
99 // behavior.
100 new_headers->ReplaceStatusLine(old_headers->GetStatusLine());
101 CopyHeaderValues(old_headers, new_headers, "connection");
102 CopyHeaderValues(old_headers, new_headers, "proxy-connection");
103 CopyHeaderValues(old_headers, new_headers, "keep-alive");
104 CopyHeaderValues(old_headers, new_headers, "trailer");
105 CopyHeaderValues(old_headers, new_headers, "transfer-encoding");
106 CopyHeaderValues(old_headers, new_headers, "upgrade");
108 CopyHeaderValues(old_headers, new_headers, "content-length");
110 CopyHeaderValues(old_headers, new_headers, "proxy-authenticate");
112 response->headers = new_headers;
113 return true;
116 // static
117 bool ProxyClientSocket::SanitizeProxyRedirect(HttpResponseInfo* response) {
118 DCHECK(response && response->headers.get());
120 std::string location;
121 if (!response->headers->IsRedirect(&location))
122 return false;
124 // Return minimal headers; set "Content-Length: 0" to ignore response body.
125 std::string fake_response_headers = base::StringPrintf(
126 "HTTP/1.0 302 Found\n"
127 "Location: %s\n"
128 "Content-Length: 0\n"
129 "Connection: close\n"
130 "\n",
131 location.c_str());
132 std::string raw_headers =
133 HttpUtil::AssembleRawHeaders(fake_response_headers.data(),
134 fake_response_headers.length());
135 response->headers = new HttpResponseHeaders(raw_headers);
137 return true;
140 } // namespace net