proxy_client_socket.cc 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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.
  4. #include "net/http/proxy_client_socket.h"
  5. #include <unordered_set>
  6. #include "base/metrics/histogram_macros.h"
  7. #include "base/strings/string_util.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/http/http_auth_controller.h"
  12. #include "net/http/http_request_info.h"
  13. #include "net/http/http_response_headers.h"
  14. #include "net/http/http_response_info.h"
  15. #include "url/gurl.h"
  16. namespace net {
  17. void ProxyClientSocket::SetStreamPriority(RequestPriority priority) {}
  18. // static
  19. void ProxyClientSocket::BuildTunnelRequest(
  20. const HostPortPair& endpoint,
  21. const HttpRequestHeaders& extra_headers,
  22. const std::string& user_agent,
  23. std::string* request_line,
  24. HttpRequestHeaders* request_headers) {
  25. // RFC 7230 Section 5.4 says a client MUST send a Host header field in all
  26. // HTTP/1.1 request messages, and Host SHOULD be the first header field
  27. // following the request-line. Add "Proxy-Connection: keep-alive" for compat
  28. // with HTTP/1.0 proxies such as Squid (required for NTLM authentication).
  29. std::string host_and_port = endpoint.ToString();
  30. *request_line =
  31. base::StringPrintf("CONNECT %s HTTP/1.1\r\n", host_and_port.c_str());
  32. request_headers->SetHeader(HttpRequestHeaders::kHost, host_and_port);
  33. request_headers->SetHeader(HttpRequestHeaders::kProxyConnection,
  34. "keep-alive");
  35. if (!user_agent.empty())
  36. request_headers->SetHeader(HttpRequestHeaders::kUserAgent, user_agent);
  37. request_headers->MergeFrom(extra_headers);
  38. }
  39. // static
  40. int ProxyClientSocket::HandleProxyAuthChallenge(
  41. HttpAuthController* auth,
  42. HttpResponseInfo* response,
  43. const NetLogWithSource& net_log) {
  44. DCHECK(response->headers.get());
  45. int rv = auth->HandleAuthChallenge(response->headers, response->ssl_info,
  46. false, true, net_log);
  47. auth->TakeAuthInfo(&response->auth_challenge);
  48. if (rv == OK)
  49. return ERR_PROXY_AUTH_REQUESTED;
  50. return rv;
  51. }
  52. // static
  53. void ProxyClientSocket::SanitizeProxyAuth(HttpResponseInfo& response) {
  54. DCHECK(response.headers);
  55. // Copy status line and all hop-by-hop headers to preserve keep-alive
  56. // behavior.
  57. const char* kHeadersToKeep[] = {
  58. "connection", "proxy-connection", "keep-alive", "trailer",
  59. "transfer-encoding", "upgrade",
  60. "content-length",
  61. "proxy-authenticate",
  62. };
  63. // Create a list of all present header not in |kHeadersToKeep|, and then
  64. // remove them.
  65. size_t iter = 0;
  66. std::string header_name;
  67. std::string header_value;
  68. std::unordered_set<std::string> headers_to_remove;
  69. while (response.headers->EnumerateHeaderLines(&iter, &header_name,
  70. &header_value)) {
  71. bool remove = true;
  72. for (const char* header : kHeadersToKeep) {
  73. if (base::EqualsCaseInsensitiveASCII(header, header_name)) {
  74. remove = false;
  75. break;
  76. }
  77. }
  78. if (remove)
  79. headers_to_remove.insert(header_name);
  80. }
  81. response.headers->RemoveHeaders(headers_to_remove);
  82. }
  83. } // namespace net