Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/http/proxy_client_socket.h" | 5 #include "net/http/proxy_client_socket.h" |
| 6 | 6 |
| 7 #include <unordered_set> | |
| 8 | |
| 7 #include "base/metrics/histogram_macros.h" | 9 #include "base/metrics/histogram_macros.h" |
| 10 #include "base/strings/string_util.h" | |
| 8 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 9 #include "net/base/host_port_pair.h" | 12 #include "net/base/host_port_pair.h" |
| 10 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
| 11 #include "net/http/http_auth_controller.h" | 14 #include "net/http/http_auth_controller.h" |
| 12 #include "net/http/http_request_info.h" | 15 #include "net/http/http_request_info.h" |
| 13 #include "net/http/http_response_headers.h" | 16 #include "net/http/http_response_headers.h" |
| 14 #include "net/http/http_response_info.h" | 17 #include "net/http/http_response_info.h" |
| 15 #include "url/gurl.h" | 18 #include "url/gurl.h" |
| 16 | 19 |
| 17 namespace net { | 20 namespace net { |
| 18 | 21 |
| 19 namespace { | |
| 20 | |
| 21 void CopyHeaderValues(scoped_refptr<HttpResponseHeaders> source, | |
| 22 scoped_refptr<HttpResponseHeaders> dest, | |
| 23 const std::string& header_name) { | |
| 24 size_t iter = 0; | |
| 25 std::string header_value; | |
| 26 | |
| 27 while (source->EnumerateHeader(&iter, header_name, &header_value)) | |
| 28 dest->AddHeader(header_name + ": " + header_value); | |
| 29 } | |
| 30 | |
| 31 } // namespace | |
| 32 | |
| 33 // static | 22 // static |
| 34 void ProxyClientSocket::BuildTunnelRequest( | 23 void ProxyClientSocket::BuildTunnelRequest( |
| 35 const HostPortPair& endpoint, | 24 const HostPortPair& endpoint, |
| 36 const HttpRequestHeaders& auth_headers, | 25 const HttpRequestHeaders& auth_headers, |
| 37 const std::string& user_agent, | 26 const std::string& user_agent, |
| 38 std::string* request_line, | 27 std::string* request_line, |
| 39 HttpRequestHeaders* request_headers) { | 28 HttpRequestHeaders* request_headers) { |
| 40 // RFC 7230 Section 5.4 says a client MUST send a Host header field in all | 29 // RFC 7230 Section 5.4 says a client MUST send a Host header field in all |
| 41 // HTTP/1.1 request messages, and Host SHOULD be the first header field | 30 // HTTP/1.1 request messages, and Host SHOULD be the first header field |
| 42 // following the request-line. Add "Proxy-Connection: keep-alive" for compat | 31 // following the request-line. Add "Proxy-Connection: keep-alive" for compat |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 80 "Net.BlockedTunnelResponse.HttpProxy", | 69 "Net.BlockedTunnelResponse.HttpProxy", |
| 81 HttpUtil::MapStatusCodeForHistogram(http_status_code), | 70 HttpUtil::MapStatusCodeForHistogram(http_status_code), |
| 82 HttpUtil::GetStatusCodesForHistogram()); | 71 HttpUtil::GetStatusCodesForHistogram()); |
| 83 } | 72 } |
| 84 } | 73 } |
| 85 | 74 |
| 86 // static | 75 // static |
| 87 bool ProxyClientSocket::SanitizeProxyAuth(HttpResponseInfo* response) { | 76 bool ProxyClientSocket::SanitizeProxyAuth(HttpResponseInfo* response) { |
| 88 DCHECK(response && response->headers.get()); | 77 DCHECK(response && response->headers.get()); |
| 89 | 78 |
| 90 scoped_refptr<HttpResponseHeaders> old_headers = response->headers; | |
| 91 | |
| 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))); | |
| 95 | |
| 96 // Copy status line and all hop-by-hop headers to preserve keep-alive | 79 // Copy status line and all hop-by-hop headers to preserve keep-alive |
| 97 // behavior. | 80 // behavior. |
| 98 new_headers->ReplaceStatusLine(old_headers->GetStatusLine()); | 81 const char* kHeadersToKeep[] = { |
| 99 CopyHeaderValues(old_headers, new_headers, "connection"); | 82 "connection", "proxy-connection", "keep-alive", "trailer", |
| 100 CopyHeaderValues(old_headers, new_headers, "proxy-connection"); | 83 "transfer-encoding", "upgrade", |
| 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"); | |
| 105 | 84 |
| 106 CopyHeaderValues(old_headers, new_headers, "content-length"); | 85 "content-length", |
| 107 | 86 |
| 108 CopyHeaderValues(old_headers, new_headers, "proxy-authenticate"); | 87 "proxy-authenticate", |
| 88 }; | |
| 109 | 89 |
| 110 response->headers = new_headers; | 90 // Create a list of all present header not in |kHeadersToKeep|, and then |
| 91 // remove them. | |
| 92 size_t iter = 0; | |
| 93 std::string header_name; | |
| 94 std::string header_value; | |
| 95 std::unordered_set<std::string> headers_to_remove; | |
| 96 while (response->headers->EnumerateHeaderLines(&iter, &header_name, | |
| 97 &header_value)) { | |
| 98 bool remove = true; | |
| 99 for (const char* header : kHeadersToKeep) { | |
|
Julia Tuttle
2017/01/19 22:26:40
Might be faster to just unconditionally remove the
mmenke
2017/01/23 17:28:14
The problem with that is the same reason adding he
| |
| 100 if (base::EqualsCaseInsensitiveASCII(header, header_name)) { | |
| 101 remove = false; | |
| 102 break; | |
| 103 } | |
| 104 } | |
| 105 if (remove) | |
| 106 headers_to_remove.insert(header_name); | |
| 107 } | |
| 108 | |
| 109 response->headers->RemoveHeaders(headers_to_remove); | |
| 110 | |
| 111 return true; | 111 return true; |
| 112 } | 112 } |
| 113 | 113 |
| 114 // static | 114 // static |
| 115 bool ProxyClientSocket::SanitizeProxyRedirect(HttpResponseInfo* response) { | 115 bool ProxyClientSocket::SanitizeProxyRedirect(HttpResponseInfo* response) { |
| 116 DCHECK(response && response->headers.get()); | 116 DCHECK(response && response->headers.get()); |
| 117 | 117 |
| 118 std::string location; | 118 std::string location; |
| 119 if (!response->headers->IsRedirect(&location)) | 119 if (!response->headers->IsRedirect(&location)) |
| 120 return false; | 120 return false; |
| 121 | 121 |
| 122 // Return minimal headers; set "Content-Length: 0" to ignore response body. | 122 // Return minimal headers; set "Content-Length: 0" to ignore response body. |
| 123 std::string fake_response_headers = base::StringPrintf( | 123 std::string fake_response_headers = base::StringPrintf( |
| 124 "HTTP/1.0 302 Found\n" | 124 "HTTP/1.0 302 Found\n" |
| 125 "Location: %s\n" | 125 "Location: %s\n" |
| 126 "Content-Length: 0\n" | 126 "Content-Length: 0\n" |
| 127 "Connection: close\n" | 127 "Connection: close\n" |
| 128 "\n", | 128 "\n", |
| 129 location.c_str()); | 129 location.c_str()); |
| 130 std::string raw_headers = | 130 std::string raw_headers = |
| 131 HttpUtil::AssembleRawHeaders(fake_response_headers.data(), | 131 HttpUtil::AssembleRawHeaders(fake_response_headers.data(), |
| 132 fake_response_headers.length()); | 132 fake_response_headers.length()); |
| 133 response->headers = new HttpResponseHeaders(raw_headers); | 133 response->headers = new HttpResponseHeaders(raw_headers); |
| 134 | 134 |
| 135 return true; | 135 return true; |
| 136 } | 136 } |
| 137 | 137 |
| 138 } // namespace net | 138 } // namespace net |
| OLD | NEW |