websocket_basic_handshake_stream.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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_BASIC_HANDSHAKE_STREAM_H_
  5. #define NET_WEBSOCKETS_WEBSOCKET_BASIC_HANDSHAKE_STREAM_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <set>
  9. #include <string>
  10. #include <vector>
  11. #include "base/memory/raw_ptr.h"
  12. #include "base/strings/string_piece.h"
  13. #include "net/base/completion_once_callback.h"
  14. #include "net/base/net_export.h"
  15. #include "net/http/http_basic_state.h"
  16. #include "net/log/net_log_with_source.h"
  17. #include "net/websockets/websocket_handshake_stream_base.h"
  18. #include "third_party/abseil-cpp/absl/types/optional.h"
  19. #include "url/gurl.h"
  20. namespace net {
  21. class ClientSocketHandle;
  22. class HttpResponseHeaders;
  23. class HttpResponseInfo;
  24. class HttpStreamParser;
  25. class WebSocketEndpointLockManager;
  26. struct WebSocketExtensionParams;
  27. class WebSocketStreamRequestAPI;
  28. class NET_EXPORT_PRIVATE WebSocketBasicHandshakeStream final
  29. : public WebSocketHandshakeStreamBase {
  30. public:
  31. // |connect_delegate| and |failure_message| must out-live this object.
  32. WebSocketBasicHandshakeStream(
  33. std::unique_ptr<ClientSocketHandle> connection,
  34. WebSocketStream::ConnectDelegate* connect_delegate,
  35. bool using_proxy,
  36. std::vector<std::string> requested_sub_protocols,
  37. std::vector<std::string> requested_extensions,
  38. WebSocketStreamRequestAPI* request,
  39. WebSocketEndpointLockManager* websocket_endpoint_lock_manager);
  40. WebSocketBasicHandshakeStream(const WebSocketBasicHandshakeStream&) = delete;
  41. WebSocketBasicHandshakeStream& operator=(
  42. const WebSocketBasicHandshakeStream&) = delete;
  43. ~WebSocketBasicHandshakeStream() override;
  44. // HttpStreamBase methods
  45. void RegisterRequest(const HttpRequestInfo* request_info) override;
  46. int InitializeStream(bool can_send_early,
  47. RequestPriority priority,
  48. const NetLogWithSource& net_log,
  49. CompletionOnceCallback callback) override;
  50. int SendRequest(const HttpRequestHeaders& request_headers,
  51. HttpResponseInfo* response,
  52. CompletionOnceCallback callback) override;
  53. int ReadResponseHeaders(CompletionOnceCallback callback) override;
  54. int ReadResponseBody(IOBuffer* buf,
  55. int buf_len,
  56. CompletionOnceCallback callback) override;
  57. void Close(bool not_reusable) override;
  58. bool IsResponseBodyComplete() const override;
  59. bool IsConnectionReused() const override;
  60. void SetConnectionReused() override;
  61. bool CanReuseConnection() const override;
  62. int64_t GetTotalReceivedBytes() const override;
  63. int64_t GetTotalSentBytes() const override;
  64. bool GetAlternativeService(
  65. AlternativeService* alternative_service) const override;
  66. bool GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const override;
  67. void GetSSLInfo(SSLInfo* ssl_info) override;
  68. void GetSSLCertRequestInfo(SSLCertRequestInfo* cert_request_info) override;
  69. int GetRemoteEndpoint(IPEndPoint* endpoint) override;
  70. void Drain(HttpNetworkSession* session) override;
  71. void SetPriority(RequestPriority priority) override;
  72. void PopulateNetErrorDetails(NetErrorDetails* details) override;
  73. std::unique_ptr<HttpStream> RenewStreamForAuth() override;
  74. const std::set<std::string>& GetDnsAliases() const override;
  75. base::StringPiece GetAcceptChViaAlps() const override;
  76. // This is called from the top level once correct handshake response headers
  77. // have been received. It creates an appropriate subclass of WebSocketStream
  78. // depending on what extensions were negotiated. This object is unusable after
  79. // Upgrade() has been called and should be disposed of as soon as possible.
  80. std::unique_ptr<WebSocketStream> Upgrade() override;
  81. base::WeakPtr<WebSocketHandshakeStreamBase> GetWeakPtr() override;
  82. // Set the value used for the next Sec-WebSocket-Key header
  83. // deterministically. The key is only used once, and then discarded.
  84. // For tests only.
  85. void SetWebSocketKeyForTesting(const std::string& key);
  86. private:
  87. // A wrapper for the ReadResponseHeaders callback that checks whether or not
  88. // the connection has been accepted.
  89. void ReadResponseHeadersCallback(CompletionOnceCallback callback, int result);
  90. // Validates the response and sends the finished handshake event.
  91. int ValidateResponse(int rv);
  92. // Check that the headers are well-formed for a 101 response, and returns
  93. // OK if they are, otherwise returns ERR_INVALID_RESPONSE.
  94. int ValidateUpgradeResponse(const HttpResponseHeaders* headers);
  95. void OnFailure(const std::string& message,
  96. int net_error,
  97. absl::optional<int> response_code);
  98. HttpStreamParser* parser() const { return state_.parser(); }
  99. HandshakeResult result_ = HandshakeResult::INCOMPLETE;
  100. // The request URL.
  101. GURL url_;
  102. // HttpBasicState holds most of the handshake-related state.
  103. HttpBasicState state_;
  104. // Owned by another object.
  105. // |connect_delegate| will live during the lifetime of this object.
  106. const raw_ptr<WebSocketStream::ConnectDelegate> connect_delegate_;
  107. // This is stored in SendRequest() for use by ReadResponseHeaders().
  108. raw_ptr<HttpResponseInfo> http_response_info_ = nullptr;
  109. // The key to be sent in the next Sec-WebSocket-Key header. Usually NULL (the
  110. // key is generated on the fly).
  111. absl::optional<std::string> handshake_challenge_for_testing_;
  112. // The required value for the Sec-WebSocket-Accept header.
  113. std::string handshake_challenge_response_;
  114. // The sub-protocols we requested.
  115. std::vector<std::string> requested_sub_protocols_;
  116. // The extensions we requested.
  117. std::vector<std::string> requested_extensions_;
  118. // The sub-protocol selected by the server.
  119. std::string sub_protocol_;
  120. // The extension(s) selected by the server.
  121. std::string extensions_;
  122. // The extension parameters. The class is defined in the implementation file
  123. // to avoid including extension-related header files here.
  124. std::unique_ptr<WebSocketExtensionParams> extension_params_;
  125. const raw_ptr<WebSocketStreamRequestAPI> stream_request_;
  126. const raw_ptr<WebSocketEndpointLockManager> websocket_endpoint_lock_manager_;
  127. NetLogWithSource net_log_;
  128. // The request to send.
  129. // Set to null before the response body is read. This is to allow |this| to
  130. // be shared for reading and to possibly outlive request_info_'s owner.
  131. // Setting to null happens after headers are completely read or upload data
  132. // stream is uploaded, whichever is later.
  133. raw_ptr<const HttpRequestInfo> request_info_;
  134. base::WeakPtrFactory<WebSocketBasicHandshakeStream> weak_ptr_factory_{this};
  135. };
  136. } // namespace net
  137. #endif // NET_WEBSOCKETS_WEBSOCKET_BASIC_HANDSHAKE_STREAM_H_