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" |
| 8 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| 9 #include "net/base/host_port_pair.h" | 11 #include "net/base/host_port_pair.h" |
| 10 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
| 11 #include "net/http/http_auth_controller.h" | 13 #include "net/http/http_auth_controller.h" |
| 12 #include "net/http/http_request_info.h" | 14 #include "net/http/http_request_info.h" |
| 13 #include "net/http/http_response_headers.h" | 15 #include "net/http/http_response_headers.h" |
| 14 #include "net/http/http_response_info.h" | 16 #include "net/http/http_response_info.h" |
| 15 #include "url/gurl.h" | 17 #include "url/gurl.h" |
| 16 | 18 |
| 17 namespace net { | 19 namespace net { |
| 18 | 20 |
| 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 | 21 // static |
| 34 void ProxyClientSocket::BuildTunnelRequest( | 22 void ProxyClientSocket::BuildTunnelRequest( |
| 35 const HostPortPair& endpoint, | 23 const HostPortPair& endpoint, |
| 36 const HttpRequestHeaders& auth_headers, | 24 const HttpRequestHeaders& auth_headers, |
| 37 const std::string& user_agent, | 25 const std::string& user_agent, |
| 38 std::string* request_line, | 26 std::string* request_line, |
| 39 HttpRequestHeaders* request_headers) { | 27 HttpRequestHeaders* request_headers) { |
| 40 // RFC 7230 Section 5.4 says a client MUST send a Host header field in all | 28 // 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 | 29 // 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 | 30 // 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", | 68 "Net.BlockedTunnelResponse.HttpProxy", |
| 81 HttpUtil::MapStatusCodeForHistogram(http_status_code), | 69 HttpUtil::MapStatusCodeForHistogram(http_status_code), |
| 82 HttpUtil::GetStatusCodesForHistogram()); | 70 HttpUtil::GetStatusCodesForHistogram()); |
| 83 } | 71 } |
| 84 } | 72 } |
| 85 | 73 |
| 86 // static | 74 // static |
| 87 bool ProxyClientSocket::SanitizeProxyAuth(HttpResponseInfo* response) { | 75 bool ProxyClientSocket::SanitizeProxyAuth(HttpResponseInfo* response) { |
| 88 DCHECK(response && response->headers.get()); | 76 DCHECK(response && response->headers.get()); |
| 89 | 77 |
| 90 scoped_refptr<HttpResponseHeaders> old_headers = response->headers; | 78 response->headers->ReplaceStatusLine( |
| 91 | 79 "HTTP/1.1 407 Proxy Authentication Required"); |
| 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 | 80 |
| 96 // Copy status line and all hop-by-hop headers to preserve keep-alive | 81 // Copy status line and all hop-by-hop headers to preserve keep-alive |
| 97 // behavior. | 82 // behavior. |
| 98 new_headers->ReplaceStatusLine(old_headers->GetStatusLine()); | 83 // Note that these must be lower case. |
| 99 CopyHeaderValues(old_headers, new_headers, "connection"); | 84 const char* kHeadersToKeep[] = { |
| 100 CopyHeaderValues(old_headers, new_headers, "proxy-connection"); | 85 "connection", "proxy-connection", "keep-alive", "trailer", |
| 101 CopyHeaderValues(old_headers, new_headers, "keep-alive"); | 86 "transfer-encoding", "upgrade", |
| 102 CopyHeaderValues(old_headers, new_headers, "trailer"); | |
| 103 CopyHeaderValues(old_headers, new_headers, "transfer-encoding"); | |
| 104 CopyHeaderValues(old_headers, new_headers, "upgrade"); | |
| 105 | 87 |
| 106 CopyHeaderValues(old_headers, new_headers, "content-length"); | 88 "content-length", |
| 107 | 89 |
| 108 CopyHeaderValues(old_headers, new_headers, "proxy-authenticate"); | 90 "proxy-authenticate", |
| 91 }; | |
| 109 | 92 |
| 110 response->headers = new_headers; | 93 size_t iter = 0; |
| 94 std::string header_name; | |
| 95 std::string header_value; | |
| 96 std::unordered_set<std::string> headers_to_remove; | |
| 97 while (response->headers->EnumerateHeaderLines(&iter, &header_name, | |
| 98 &header_value)) { | |
|
mmenke
2017/01/19 18:51:55
I went with this approach instead of adding a "Rem
| |
| 99 bool remove = true; | |
| 100 for (const char* header : kHeadersToKeep) { | |
| 101 if (header == header_name) { | |
| 102 remove = false; | |
| 103 break; | |
| 104 } | |
| 105 } | |
| 106 if (remove) | |
| 107 headers_to_remove.insert(header_name); | |
| 108 } | |
| 109 | |
| 110 response->headers->RemoveHeaders(headers_to_remove); | |
| 111 | |
| 111 return true; | 112 return true; |
| 112 } | 113 } |
| 113 | 114 |
| 114 // static | 115 // static |
| 115 bool ProxyClientSocket::SanitizeProxyRedirect(HttpResponseInfo* response) { | 116 bool ProxyClientSocket::SanitizeProxyRedirect(HttpResponseInfo* response) { |
| 116 DCHECK(response && response->headers.get()); | 117 DCHECK(response && response->headers.get()); |
| 117 | 118 |
| 118 std::string location; | 119 std::string location; |
| 119 if (!response->headers->IsRedirect(&location)) | 120 if (!response->headers->IsRedirect(&location)) |
| 120 return false; | 121 return false; |
| 121 | 122 |
| 122 // Return minimal headers; set "Content-Length: 0" to ignore response body. | 123 // Return minimal headers; set "Content-Length: 0" to ignore response body. |
| 123 std::string fake_response_headers = base::StringPrintf( | 124 std::string fake_response_headers = base::StringPrintf( |
| 124 "HTTP/1.0 302 Found\n" | 125 "HTTP/1.0 302 Found\n" |
| 125 "Location: %s\n" | 126 "Location: %s\n" |
| 126 "Content-Length: 0\n" | 127 "Content-Length: 0\n" |
| 127 "Connection: close\n" | 128 "Connection: close\n" |
| 128 "\n", | 129 "\n", |
| 129 location.c_str()); | 130 location.c_str()); |
| 130 std::string raw_headers = | 131 std::string raw_headers = |
| 131 HttpUtil::AssembleRawHeaders(fake_response_headers.data(), | 132 HttpUtil::AssembleRawHeaders(fake_response_headers.data(), |
| 132 fake_response_headers.length()); | 133 fake_response_headers.length()); |
| 133 response->headers = new HttpResponseHeaders(raw_headers); | 134 response->headers = new HttpResponseHeaders(raw_headers); |
| 134 | 135 |
| 135 return true; | 136 return true; |
| 136 } | 137 } |
| 137 | 138 |
| 138 } // namespace net | 139 } // namespace net |
| OLD | NEW |