Roll src/third_party/WebKit a3b4a2e:7441784 (svn 202551:202552)
[chromium-blink-merge.git] / net / websockets / websocket_basic_handshake_stream.h
blob9352d307b0023a371dfcb3985e80b7778e77835a
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_
8 #include <stdint.h>
10 #include <string>
11 #include <vector>
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "net/base/net_export.h"
16 #include "net/http/http_basic_state.h"
17 #include "net/websockets/websocket_handshake_stream_base.h"
18 #include "url/gurl.h"
20 namespace net {
22 class ClientSocketHandle;
23 class HttpResponseHeaders;
24 class HttpResponseInfo;
25 class HttpStreamParser;
27 struct WebSocketExtensionParams;
29 class NET_EXPORT_PRIVATE WebSocketBasicHandshakeStream
30 : public WebSocketHandshakeStreamBase {
31 public:
32 // |connect_delegate| and |failure_message| must out-live this object.
33 WebSocketBasicHandshakeStream(
34 scoped_ptr<ClientSocketHandle> connection,
35 WebSocketStream::ConnectDelegate* connect_delegate,
36 bool using_proxy,
37 std::vector<std::string> requested_sub_protocols,
38 std::vector<std::string> requested_extensions,
39 std::string* failure_message);
41 ~WebSocketBasicHandshakeStream() override;
43 // HttpStreamBase methods
44 int InitializeStream(const HttpRequestInfo* request_info,
45 RequestPriority priority,
46 const BoundNetLog& net_log,
47 const CompletionCallback& callback) override;
48 int SendRequest(const HttpRequestHeaders& request_headers,
49 HttpResponseInfo* response,
50 const CompletionCallback& callback) override;
51 int ReadResponseHeaders(const CompletionCallback& callback) override;
52 int ReadResponseBody(IOBuffer* buf,
53 int buf_len,
54 const CompletionCallback& callback) override;
55 void Close(bool not_reusable) override;
56 bool IsResponseBodyComplete() const override;
57 bool IsConnectionReused() const override;
58 void SetConnectionReused() override;
59 bool CanReuseConnection() const override;
60 int64 GetTotalReceivedBytes() const override;
61 int64_t GetTotalSentBytes() const override;
62 bool GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const override;
63 void GetSSLInfo(SSLInfo* ssl_info) override;
64 void GetSSLCertRequestInfo(SSLCertRequestInfo* cert_request_info) override;
65 void Drain(HttpNetworkSession* session) override;
66 void SetPriority(RequestPriority priority) override;
67 UploadProgress GetUploadProgress() const override;
68 HttpStream* RenewStreamForAuth() override;
71 // This is called from the top level once correct handshake response headers
72 // have been received. It creates an appropriate subclass of WebSocketStream
73 // depending on what extensions were negotiated. This object is unusable after
74 // Upgrade() has been called and should be disposed of as soon as possible.
75 scoped_ptr<WebSocketStream> Upgrade() override;
77 // Set the value used for the next Sec-WebSocket-Key header
78 // deterministically. The key is only used once, and then discarded.
79 // For tests only.
80 void SetWebSocketKeyForTesting(const std::string& key);
82 private:
83 // A wrapper for the ReadResponseHeaders callback that checks whether or not
84 // the connection has been accepted.
85 void ReadResponseHeadersCallback(const CompletionCallback& callback,
86 int result);
88 void OnFinishOpeningHandshake();
90 // Validates the response and sends the finished handshake event.
91 int ValidateResponse(int rv);
93 // Check that the headers are well-formed for a 101 response, and returns
94 // OK if they are, otherwise returns ERR_INVALID_RESPONSE.
95 int ValidateUpgradeResponse(const HttpResponseHeaders* headers);
97 HttpStreamParser* parser() const { return state_.parser(); }
99 void set_failure_message(const std::string& failure_message);
101 // The request URL.
102 GURL url_;
104 // HttpBasicState holds most of the handshake-related state.
105 HttpBasicState state_;
107 // Owned by another object.
108 // |connect_delegate| will live during the lifetime of this object.
109 WebSocketStream::ConnectDelegate* connect_delegate_;
111 // This is stored in SendRequest() for use by ReadResponseHeaders().
112 HttpResponseInfo* http_response_info_;
114 // The key to be sent in the next Sec-WebSocket-Key header. Usually NULL (the
115 // key is generated on the fly).
116 scoped_ptr<std::string> handshake_challenge_for_testing_;
118 // The required value for the Sec-WebSocket-Accept header.
119 std::string handshake_challenge_response_;
121 // The sub-protocols we requested.
122 std::vector<std::string> requested_sub_protocols_;
124 // The extensions we requested.
125 std::vector<std::string> requested_extensions_;
127 // The sub-protocol selected by the server.
128 std::string sub_protocol_;
130 // The extension(s) selected by the server.
131 std::string extensions_;
133 // The extension parameters. The class is defined in the implementation file
134 // to avoid including extension-related header files here.
135 scoped_ptr<WebSocketExtensionParams> extension_params_;
137 std::string* failure_message_;
139 DISALLOW_COPY_AND_ASSIGN(WebSocketBasicHandshakeStream);
142 } // namespace net
144 #endif // NET_WEBSOCKETS_WEBSOCKET_BASIC_HANDSHAKE_STREAM_H_