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.
5 #ifndef NET_WEBSOCKETS_WEBSOCKET_BASIC_HANDSHAKE_STREAM_H_
6 #define NET_WEBSOCKETS_WEBSOCKET_BASIC_HANDSHAKE_STREAM_H_
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "net/base/net_export.h"
14 #include "net/http/http_basic_state.h"
15 #include "net/websockets/websocket_handshake_stream_base.h"
20 class ClientSocketHandle
;
21 class HttpResponseHeaders
;
22 class HttpResponseInfo
;
23 class HttpStreamParser
;
25 struct WebSocketExtensionParams
;
27 class NET_EXPORT_PRIVATE WebSocketBasicHandshakeStream
28 : public WebSocketHandshakeStreamBase
{
30 // |connect_delegate| and |failure_message| must out-live this object.
31 WebSocketBasicHandshakeStream(
32 scoped_ptr
<ClientSocketHandle
> connection
,
33 WebSocketStream::ConnectDelegate
* connect_delegate
,
35 std::vector
<std::string
> requested_sub_protocols
,
36 std::vector
<std::string
> requested_extensions
,
37 std::string
* failure_message
);
39 ~WebSocketBasicHandshakeStream() override
;
41 // HttpStreamBase methods
42 int InitializeStream(const HttpRequestInfo
* request_info
,
43 RequestPriority priority
,
44 const BoundNetLog
& net_log
,
45 const CompletionCallback
& callback
) override
;
46 int SendRequest(const HttpRequestHeaders
& request_headers
,
47 HttpResponseInfo
* response
,
48 const CompletionCallback
& callback
) override
;
49 int ReadResponseHeaders(const CompletionCallback
& callback
) override
;
50 int ReadResponseBody(IOBuffer
* buf
,
52 const CompletionCallback
& callback
) override
;
53 void Close(bool not_reusable
) override
;
54 bool IsResponseBodyComplete() const override
;
55 bool CanFindEndOfResponse() const override
;
56 bool IsConnectionReused() const override
;
57 void SetConnectionReused() override
;
58 bool IsConnectionReusable() const override
;
59 int64
GetTotalReceivedBytes() const override
;
60 bool GetLoadTimingInfo(LoadTimingInfo
* load_timing_info
) const override
;
61 void GetSSLInfo(SSLInfo
* ssl_info
) override
;
62 void GetSSLCertRequestInfo(SSLCertRequestInfo
* cert_request_info
) override
;
63 bool IsSpdyHttpStream() const override
;
64 void Drain(HttpNetworkSession
* session
) override
;
65 void SetPriority(RequestPriority priority
) override
;
66 UploadProgress
GetUploadProgress() const override
;
67 HttpStream
* RenewStreamForAuth() override
;
70 // This is called from the top level once correct handshake response headers
71 // have been received. It creates an appropriate subclass of WebSocketStream
72 // depending on what extensions were negotiated. This object is unusable after
73 // Upgrade() has been called and should be disposed of as soon as possible.
74 scoped_ptr
<WebSocketStream
> Upgrade() override
;
76 // Set the value used for the next Sec-WebSocket-Key header
77 // deterministically. The key is only used once, and then discarded.
79 void SetWebSocketKeyForTesting(const std::string
& key
);
82 // A wrapper for the ReadResponseHeaders callback that checks whether or not
83 // the connection has been accepted.
84 void ReadResponseHeadersCallback(const CompletionCallback
& callback
,
87 void OnFinishOpeningHandshake();
89 // Validates the response and sends the finished handshake event.
90 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
);
96 HttpStreamParser
* parser() const { return state_
.parser(); }
98 void set_failure_message(const std::string
& failure_message
);
103 // HttpBasicState holds most of the handshake-related state.
104 HttpBasicState state_
;
106 // Owned by another object.
107 // |connect_delegate| will live during the lifetime of this object.
108 WebSocketStream::ConnectDelegate
* connect_delegate_
;
110 // This is stored in SendRequest() for use by ReadResponseHeaders().
111 HttpResponseInfo
* http_response_info_
;
113 // The key to be sent in the next Sec-WebSocket-Key header. Usually NULL (the
114 // key is generated on the fly).
115 scoped_ptr
<std::string
> handshake_challenge_for_testing_
;
117 // The required value for the Sec-WebSocket-Accept header.
118 std::string handshake_challenge_response_
;
120 // The sub-protocols we requested.
121 std::vector
<std::string
> requested_sub_protocols_
;
123 // The extensions we requested.
124 std::vector
<std::string
> requested_extensions_
;
126 // The sub-protocol selected by the server.
127 std::string sub_protocol_
;
129 // The extension(s) selected by the server.
130 std::string extensions_
;
132 // The extension parameters. The class is defined in the implementation file
133 // to avoid including extension-related header files here.
134 scoped_ptr
<WebSocketExtensionParams
> extension_params_
;
136 std::string
* failure_message_
;
138 DISALLOW_COPY_AND_ASSIGN(WebSocketBasicHandshakeStream
);
143 #endif // NET_WEBSOCKETS_WEBSOCKET_BASIC_HANDSHAKE_STREAM_H_