• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
5 #ifndef NET_WEBSOCKETS_WEBSOCKET_BASIC_HANDSHAKE_STREAM_H_
6 #define NET_WEBSOCKETS_WEBSOCKET_BASIC_HANDSHAKE_STREAM_H_
7 
8 #include <string>
9 #include <vector>
10 
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"
16 
17 namespace net {
18 
19 class ClientSocketHandle;
20 class HttpResponseHeaders;
21 class HttpResponseInfo;
22 class HttpStreamParser;
23 
24 class NET_EXPORT_PRIVATE WebSocketBasicHandshakeStream
25     : public WebSocketHandshakeStreamBase {
26  public:
27   WebSocketBasicHandshakeStream(
28       scoped_ptr<ClientSocketHandle> connection,
29       bool using_proxy,
30       std::vector<std::string> requested_sub_protocols,
31       std::vector<std::string> requested_extensions);
32 
33   virtual ~WebSocketBasicHandshakeStream();
34 
35   // HttpStreamBase methods
36   virtual int InitializeStream(const HttpRequestInfo* request_info,
37                                RequestPriority priority,
38                                const BoundNetLog& net_log,
39                                const CompletionCallback& callback) OVERRIDE;
40   virtual int SendRequest(const HttpRequestHeaders& request_headers,
41                           HttpResponseInfo* response,
42                           const CompletionCallback& callback) OVERRIDE;
43   virtual int ReadResponseHeaders(const CompletionCallback& callback) OVERRIDE;
44   virtual const HttpResponseInfo* GetResponseInfo() const OVERRIDE;
45   virtual int ReadResponseBody(IOBuffer* buf,
46                                int buf_len,
47                                const CompletionCallback& callback) OVERRIDE;
48   virtual void Close(bool not_reusable) OVERRIDE;
49   virtual bool IsResponseBodyComplete() const OVERRIDE;
50   virtual bool CanFindEndOfResponse() const OVERRIDE;
51   virtual bool IsConnectionReused() const OVERRIDE;
52   virtual void SetConnectionReused() OVERRIDE;
53   virtual bool IsConnectionReusable() const OVERRIDE;
54   virtual int64 GetTotalReceivedBytes() const OVERRIDE;
55   virtual bool GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const
56       OVERRIDE;
57   virtual void GetSSLInfo(SSLInfo* ssl_info) OVERRIDE;
58   virtual void GetSSLCertRequestInfo(
59       SSLCertRequestInfo* cert_request_info) OVERRIDE;
60   virtual bool IsSpdyHttpStream() const OVERRIDE;
61   virtual void Drain(HttpNetworkSession* session) OVERRIDE;
62   virtual void SetPriority(RequestPriority priority) OVERRIDE;
63 
64   // This is called from the top level once correct handshake response headers
65   // have been received. It creates an appropriate subclass of WebSocketStream
66   // depending on what extensions were negotiated. This object is unusable after
67   // Upgrade() has been called and should be disposed of as soon as possible.
68   virtual scoped_ptr<WebSocketStream> Upgrade() OVERRIDE;
69 
70   // Set the value used for the next Sec-WebSocket-Key header
71   // deterministically. The key is only used once, and then discarded.
72   // For tests only.
73   void SetWebSocketKeyForTesting(const std::string& key);
74 
75  private:
76   // A wrapper for the ReadResponseHeaders callback that checks whether or not
77   // the connection has been accepted.
78   void ReadResponseHeadersCallback(const CompletionCallback& callback,
79                                    int result);
80 
81   // Validates the response from the server and returns OK or
82   // ERR_INVALID_RESPONSE.
83   int ValidateResponse();
84 
85   // Check that the headers are well-formed for a 101 response, and returns
86   // OK if they are, otherwise returns ERR_INVALID_RESPONSE.
87   int ValidateUpgradeResponse(
88       const scoped_refptr<HttpResponseHeaders>& headers);
89 
parser()90   HttpStreamParser* parser() const { return state_.parser(); }
91 
92   // HttpBasicState holds most of the handshake-related state.
93   HttpBasicState state_;
94 
95   // This is stored in SendRequest() for use by ReadResponseHeaders().
96   HttpResponseInfo* http_response_info_;
97 
98   // The key to be sent in the next Sec-WebSocket-Key header. Usually NULL (the
99   // key is generated on the fly).
100   scoped_ptr<std::string> handshake_challenge_for_testing_;
101 
102   // The required value for the Sec-WebSocket-Accept header.
103   std::string handshake_challenge_response_;
104 
105   // The sub-protocols we requested.
106   std::vector<std::string> requested_sub_protocols_;
107 
108   // The extensions we requested.
109   std::vector<std::string> requested_extensions_;
110 
111   // The sub-protocol selected by the server.
112   std::string sub_protocol_;
113 
114   // The extension(s) selected by the server.
115   std::string extensions_;
116 
117   DISALLOW_COPY_AND_ASSIGN(WebSocketBasicHandshakeStream);
118 };
119 
120 }  // namespace net
121 
122 #endif  // NET_WEBSOCKETS_WEBSOCKET_BASIC_HANDSHAKE_STREAM_H_
123