PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/net/websockets/websocket_handshake_stream_base.h

http://github.com/chromium/chromium
C Header | 157 lines | 80 code | 23 blank | 54 comment | 0 complexity | aa5893f02153a8e7b8442f35d55b5ff4 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.0, BSD-2-Clause, LGPL-2.1, MPL-2.0, 0BSD, EPL-1.0, MPL-2.0-no-copyleft-exception, GPL-2.0, BitTorrent-1.0, CPL-1.0, LGPL-3.0, Unlicense, BSD-3-Clause, CC0-1.0, JSON, MIT, GPL-3.0, CC-BY-SA-3.0, AGPL-1.0
  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 <string>
  11. #include <vector>
  12. #include "base/macros.h"
  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() override = default;
  79. // An object that stores data needed for the creation of a
  80. // WebSocketBasicHandshakeStream object. A new CreateHelper is used for each
  81. // WebSocket connection.
  82. class NET_EXPORT_PRIVATE CreateHelper : public base::SupportsUserData::Data {
  83. public:
  84. ~CreateHelper() override {}
  85. // Create a WebSocketBasicHandshakeStream. This is called after the
  86. // underlying connection has been established but before any handshake data
  87. // has been transferred. This can be called more than once in the case that
  88. // HTTP authentication is needed.
  89. virtual std::unique_ptr<WebSocketHandshakeStreamBase> CreateBasicStream(
  90. std::unique_ptr<ClientSocketHandle> connection,
  91. bool using_proxy,
  92. WebSocketEndpointLockManager* websocket_endpoint_lock_manager) = 0;
  93. // Create a WebSocketHttp2HandshakeStream. This is called after the
  94. // underlying HTTP/2 connection has been established but before the stream
  95. // has been opened. This cannot be called more than once.
  96. virtual std::unique_ptr<WebSocketHandshakeStreamBase> CreateHttp2Stream(
  97. base::WeakPtr<SpdySession> session) = 0;
  98. };
  99. // After the handshake has completed, this method creates a WebSocketStream
  100. // (of the appropriate type) from the WebSocketHandshakeStreamBase object.
  101. // The WebSocketHandshakeStreamBase object is unusable after Upgrade() has
  102. // been called.
  103. virtual std::unique_ptr<WebSocketStream> Upgrade() = 0;
  104. void SetRequestHeadersCallback(RequestHeadersCallback callback) override {}
  105. static std::string MultipleHeaderValuesMessage(
  106. const std::string& header_name);
  107. // Subclasses need to implement this method so that the resulting weak
  108. // pointers are invalidated as soon as the derived class is destroyed.
  109. virtual base::WeakPtr<WebSocketHandshakeStreamBase> GetWeakPtr() = 0;
  110. protected:
  111. // TODO(ricea): If more extensions are added, replace this with a more general
  112. // mechanism.
  113. struct WebSocketExtensionParams {
  114. bool deflate_enabled = false;
  115. WebSocketDeflateParameters deflate_parameters;
  116. };
  117. static void AddVectorHeaderIfNonEmpty(const char* name,
  118. const std::vector<std::string>& value,
  119. HttpRequestHeaders* headers);
  120. static bool ValidateSubProtocol(
  121. const HttpResponseHeaders* headers,
  122. const std::vector<std::string>& requested_sub_protocols,
  123. std::string* sub_protocol,
  124. std::string* failure_message);
  125. static bool ValidateExtensions(const HttpResponseHeaders* headers,
  126. std::string* accepted_extensions_descriptor,
  127. std::string* failure_message,
  128. WebSocketExtensionParams* params);
  129. void RecordHandshakeResult(HandshakeResult result);
  130. private:
  131. DISALLOW_COPY_AND_ASSIGN(WebSocketHandshakeStreamBase);
  132. };
  133. } // namespace net
  134. #endif // NET_WEBSOCKETS_WEBSOCKET_HANDSHAKE_STREAM_BASE_H_