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 WebSocketBasicHandshakeStream(
31 scoped_ptr
<ClientSocketHandle
> connection
,
32 WebSocketStream::ConnectDelegate
* connect_delegate
,
34 std::vector
<std::string
> requested_sub_protocols
,
35 std::vector
<std::string
> requested_extensions
);
37 virtual ~WebSocketBasicHandshakeStream();
39 // HttpStreamBase methods
40 virtual int InitializeStream(const HttpRequestInfo
* request_info
,
41 RequestPriority priority
,
42 const BoundNetLog
& net_log
,
43 const CompletionCallback
& callback
) OVERRIDE
;
44 virtual int SendRequest(const HttpRequestHeaders
& request_headers
,
45 HttpResponseInfo
* response
,
46 const CompletionCallback
& callback
) OVERRIDE
;
47 virtual int ReadResponseHeaders(const CompletionCallback
& callback
) OVERRIDE
;
48 virtual const HttpResponseInfo
* GetResponseInfo() const OVERRIDE
;
49 virtual int ReadResponseBody(IOBuffer
* buf
,
51 const CompletionCallback
& callback
) OVERRIDE
;
52 virtual void Close(bool not_reusable
) OVERRIDE
;
53 virtual bool IsResponseBodyComplete() const OVERRIDE
;
54 virtual bool CanFindEndOfResponse() const OVERRIDE
;
55 virtual bool IsConnectionReused() const OVERRIDE
;
56 virtual void SetConnectionReused() OVERRIDE
;
57 virtual bool IsConnectionReusable() const OVERRIDE
;
58 virtual int64
GetTotalReceivedBytes() const OVERRIDE
;
59 virtual bool GetLoadTimingInfo(LoadTimingInfo
* load_timing_info
) const
61 virtual void GetSSLInfo(SSLInfo
* ssl_info
) OVERRIDE
;
62 virtual void GetSSLCertRequestInfo(
63 SSLCertRequestInfo
* cert_request_info
) OVERRIDE
;
64 virtual bool IsSpdyHttpStream() const OVERRIDE
;
65 virtual void Drain(HttpNetworkSession
* session
) OVERRIDE
;
66 virtual void SetPriority(RequestPriority priority
) OVERRIDE
;
68 // This is called from the top level once correct handshake response headers
69 // have been received. It creates an appropriate subclass of WebSocketStream
70 // depending on what extensions were negotiated. This object is unusable after
71 // Upgrade() has been called and should be disposed of as soon as possible.
72 virtual scoped_ptr
<WebSocketStream
> Upgrade() OVERRIDE
;
74 // Set the value used for the next Sec-WebSocket-Key header
75 // deterministically. The key is only used once, and then discarded.
77 void SetWebSocketKeyForTesting(const std::string
& key
);
79 virtual std::string
GetFailureMessage() const OVERRIDE
;
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(); }
101 // HttpBasicState holds most of the handshake-related state.
102 HttpBasicState state_
;
104 // Owned by another object.
105 // |connect_delegate| will live during the lifetime of this object.
106 WebSocketStream::ConnectDelegate
* connect_delegate_
;
108 // This is stored in SendRequest() for use by ReadResponseHeaders().
109 HttpResponseInfo
* http_response_info_
;
111 // The key to be sent in the next Sec-WebSocket-Key header. Usually NULL (the
112 // key is generated on the fly).
113 scoped_ptr
<std::string
> handshake_challenge_for_testing_
;
115 // The required value for the Sec-WebSocket-Accept header.
116 std::string handshake_challenge_response_
;
118 // The sub-protocols we requested.
119 std::vector
<std::string
> requested_sub_protocols_
;
121 // The extensions we requested.
122 std::vector
<std::string
> requested_extensions_
;
124 // The sub-protocol selected by the server.
125 std::string sub_protocol_
;
127 // The extension(s) selected by the server.
128 std::string extensions_
;
130 // The extension parameters. The class is defined in the implementation file
131 // to avoid including extension-related header files here.
132 scoped_ptr
<WebSocketExtensionParams
> extension_params_
;
134 std::string failure_message_
;
136 DISALLOW_COPY_AND_ASSIGN(WebSocketBasicHandshakeStream
);
141 #endif // NET_WEBSOCKETS_WEBSOCKET_BASIC_HANDSHAKE_STREAM_H_