websocket_handshake_stream_base.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright 2013 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. #ifndef NET_WEBSOCKETS_WEBSOCKET_HANDSHAKE_STREAM_BASE_H_
  5. #define NET_WEBSOCKETS_WEBSOCKET_HANDSHAKE_STREAM_BASE_H_
  6. // This file is included from net/http files.
  7. // Since net/http can be built without linking net/websockets code,
  8. // this file must not introduce any link-time dependencies on websockets.
  9. #include <memory>
  10. #include <set>
  11. #include <string>
  12. #include <vector>
  13. #include "base/memory/weak_ptr.h"
  14. #include "base/supports_user_data.h"
  15. #include "net/base/net_export.h"
  16. #include "net/http/http_stream.h"
  17. #include "net/websockets/websocket_deflate_parameters.h"
  18. #include "net/websockets/websocket_stream.h"
  19. namespace net {
  20. class ClientSocketHandle;
  21. class SpdySession;
  22. class HttpRequestHeaders;
  23. class HttpResponseHeaders;
  24. class WebSocketEndpointLockManager;
  25. // WebSocketHandshakeStreamBase is the base class of
  26. // WebSocketBasicHandshakeStream. net/http code uses this interface to handle
  27. // WebSocketBasicHandshakeStream when it needs to be treated differently from
  28. // HttpStreamBase.
  29. class NET_EXPORT WebSocketHandshakeStreamBase : public HttpStream {
  30. public:
  31. // These entries must match histogram Net.WebSocket.HandshakeResult2.
  32. // Do not change or reuse values.
  33. enum class HandshakeResult {
  34. // Handshake not completed via Upgrade over HTTP/1 connection.
  35. INCOMPLETE = 0,
  36. // Server responded to Upgrade request with invalid status.
  37. INVALID_STATUS = 1,
  38. // Server responded to Upgrade request with empty response.
  39. EMPTY_RESPONSE = 2,
  40. // Server responded to Upgrade request with 101 status but there was some
  41. // other network error.
  42. FAILED_SWITCHING_PROTOCOLS = 3,
  43. // Server responded to Upgrade request with invalid Upgrade header.
  44. FAILED_UPGRADE = 4,
  45. // Server responded to Upgrade request with invalid Sec-WebSocket-Accept
  46. // header.
  47. FAILED_ACCEPT = 5,
  48. // Server responded to Upgrade request with invalid Connection header.
  49. FAILED_CONNECTION = 6,
  50. // Server responded to Upgrade request with invalid Sec-WebSocket-Protocol
  51. // header.
  52. FAILED_SUBPROTO = 7,
  53. // Server responded to Upgrade request with invalid Sec-WebSocket-Extensions
  54. // header.
  55. FAILED_EXTENSIONS = 8,
  56. // Upgrade request failed due to other network error.
  57. FAILED = 9,
  58. // Connected via Upgrade over HTTP/1 connection.
  59. CONNECTED = 10,
  60. // Handshake not completed over an HTTP/2 connection.
  61. HTTP2_INCOMPLETE = 11,
  62. // Server responded to WebSocket request over an HTTP/2 connection with
  63. // invalid status code.
  64. HTTP2_INVALID_STATUS = 12,
  65. // Server responded to WebSocket request over an HTTP/2 connection with
  66. // invalid sec-websocket-protocol header.
  67. HTTP2_FAILED_SUBPROTO = 13,
  68. // Server responded to WebSocket request over an HTTP/2 connection with
  69. // invalid sec-websocket-extensions header.
  70. HTTP2_FAILED_EXTENSIONS = 14,
  71. // WebSocket request over an HTTP/2 connection failed with some other error.
  72. HTTP2_FAILED = 15,
  73. // Connected over an HTTP/2 connection.
  74. HTTP2_CONNECTED = 16,
  75. NUM_HANDSHAKE_RESULT_TYPES = 17
  76. };
  77. WebSocketHandshakeStreamBase() = default;
  78. WebSocketHandshakeStreamBase(const WebSocketHandshakeStreamBase&) = delete;
  79. WebSocketHandshakeStreamBase& operator=(const WebSocketHandshakeStreamBase&) =
  80. delete;
  81. ~WebSocketHandshakeStreamBase() override = default;
  82. // An object that stores data needed for the creation of a
  83. // WebSocketBasicHandshakeStream object. A new CreateHelper is used for each
  84. // WebSocket connection.
  85. class NET_EXPORT_PRIVATE CreateHelper : public base::SupportsUserData::Data {
  86. public:
  87. ~CreateHelper() override = default;
  88. // Create a WebSocketBasicHandshakeStream. This is called after the
  89. // underlying connection has been established but before any handshake data
  90. // has been transferred. This can be called more than once in the case that
  91. // HTTP authentication is needed.
  92. virtual std::unique_ptr<WebSocketHandshakeStreamBase> CreateBasicStream(
  93. std::unique_ptr<ClientSocketHandle> connection,
  94. bool using_proxy,
  95. WebSocketEndpointLockManager* websocket_endpoint_lock_manager) = 0;
  96. // Create a WebSocketHttp2HandshakeStream. This is called after the
  97. // underlying HTTP/2 connection has been established but before the stream
  98. // has been opened. This cannot be called more than once.
  99. virtual std::unique_ptr<WebSocketHandshakeStreamBase> CreateHttp2Stream(
  100. base::WeakPtr<SpdySession> session,
  101. std::set<std::string> dns_aliases) = 0;
  102. };
  103. // After the handshake has completed, this method creates a WebSocketStream
  104. // (of the appropriate type) from the WebSocketHandshakeStreamBase object.
  105. // The WebSocketHandshakeStreamBase object is unusable after Upgrade() has
  106. // been called.
  107. virtual std::unique_ptr<WebSocketStream> Upgrade() = 0;
  108. void SetRequestHeadersCallback(RequestHeadersCallback callback) override {}
  109. static std::string MultipleHeaderValuesMessage(
  110. const std::string& header_name);
  111. // Subclasses need to implement this method so that the resulting weak
  112. // pointers are invalidated as soon as the derived class is destroyed.
  113. virtual base::WeakPtr<WebSocketHandshakeStreamBase> GetWeakPtr() = 0;
  114. protected:
  115. // TODO(ricea): If more extensions are added, replace this with a more general
  116. // mechanism.
  117. struct WebSocketExtensionParams {
  118. bool deflate_enabled = false;
  119. WebSocketDeflateParameters deflate_parameters;
  120. };
  121. static void AddVectorHeaderIfNonEmpty(const char* name,
  122. const std::vector<std::string>& value,
  123. HttpRequestHeaders* headers);
  124. static bool ValidateSubProtocol(
  125. const HttpResponseHeaders* headers,
  126. const std::vector<std::string>& requested_sub_protocols,
  127. std::string* sub_protocol,
  128. std::string* failure_message);
  129. static bool ValidateExtensions(const HttpResponseHeaders* headers,
  130. std::string* accepted_extensions_descriptor,
  131. std::string* failure_message,
  132. WebSocketExtensionParams* params);
  133. void RecordHandshakeResult(HandshakeResult result);
  134. };
  135. } // namespace net
  136. #endif // NET_WEBSOCKETS_WEBSOCKET_HANDSHAKE_STREAM_BASE_H_