Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / net / websockets / websocket_basic_handshake_stream.h
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 #include "url/gurl.h"
17
18 namespace net {
19
20 class ClientSocketHandle;
21 class HttpResponseHeaders;
22 class HttpResponseInfo;
23 class HttpStreamParser;
24
25 struct WebSocketExtensionParams;
26
27 class NET_EXPORT_PRIVATE WebSocketBasicHandshakeStream
28     : public WebSocketHandshakeStreamBase {
29  public:
30   // |connect_delegate| and |failure_message| must out-live this object.
31   WebSocketBasicHandshakeStream(
32       scoped_ptr<ClientSocketHandle> connection,
33       WebSocketStream::ConnectDelegate* connect_delegate,
34       bool using_proxy,
35       std::vector<std::string> requested_sub_protocols,
36       std::vector<std::string> requested_extensions,
37       std::string* failure_message);
38
39   ~WebSocketBasicHandshakeStream() override;
40
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,
51                        int buf_len,
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;
68
69
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;
75
76   // Set the value used for the next Sec-WebSocket-Key header
77   // deterministically. The key is only used once, and then discarded.
78   // For tests only.
79   void SetWebSocketKeyForTesting(const std::string& key);
80
81  private:
82   // A wrapper for the ReadResponseHeaders callback that checks whether or not
83   // the connection has been accepted.
84   void ReadResponseHeadersCallback(const CompletionCallback& callback,
85                                    int result);
86
87   void OnFinishOpeningHandshake();
88
89   // Validates the response and sends the finished handshake event.
90   int ValidateResponse(int rv, bool* is_redirect);
91
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
96   HttpStreamParser* parser() const { return state_.parser(); }
97
98   void set_failure_message(const std::string& failure_message);
99
100   // The request URL.
101   GURL url_;
102
103   // HttpBasicState holds most of the handshake-related state.
104   HttpBasicState state_;
105
106   // Owned by another object.
107   // |connect_delegate| will live during the lifetime of this object.
108   WebSocketStream::ConnectDelegate* connect_delegate_;
109
110   // This is stored in SendRequest() for use by ReadResponseHeaders().
111   HttpResponseInfo* http_response_info_;
112
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_;
116
117   // The required value for the Sec-WebSocket-Accept header.
118   std::string handshake_challenge_response_;
119
120   // The sub-protocols we requested.
121   std::vector<std::string> requested_sub_protocols_;
122
123   // The extensions we requested.
124   std::vector<std::string> requested_extensions_;
125
126   // The sub-protocol selected by the server.
127   std::string sub_protocol_;
128
129   // The extension(s) selected by the server.
130   std::string extensions_;
131
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_;
135
136   std::string* failure_message_;
137
138   DISALLOW_COPY_AND_ASSIGN(WebSocketBasicHandshakeStream);
139 };
140
141 }  // namespace net
142
143 #endif  // NET_WEBSOCKETS_WEBSOCKET_BASIC_HANDSHAKE_STREAM_H_