OLD | NEW |
---|---|
(Empty) | |
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_STREAM_H_ | |
6 #define NET_WEBSOCKETS_WEBSOCKET_BASIC_STREAM_H_ | |
7 | |
8 #include <string> | |
9 | |
10 #include "base/callback.h" | |
11 #include "base/memory/ref_counted.h" | |
tyoshino (SeeGerritForStatus)
2013/08/22 07:56:10
base/memory/scoped_ptr.h
Adam Rice
2013/08/22 08:13:04
Done.
| |
12 #include "base/memory/scoped_vector.h" | |
13 #include "base/strings/string_piece.h" | |
tyoshino (SeeGerritForStatus)
2013/08/22 07:56:10
not used?
Adam Rice
2013/08/22 08:13:04
Yes. Removed.
| |
14 #include "net/websockets/websocket_frame_parser.h" | |
15 #include "net/websockets/websocket_stream.h" | |
16 | |
17 namespace net { | |
18 | |
19 class ClientSocketHandle; | |
20 class DrainableIOBuffer; | |
21 class GrowableIOBuffer; | |
22 class HttpRequestHeaders; | |
23 class HttpResponseInfo; | |
24 class IOBufferWithSize; | |
25 struct WebSocketFrameChunk; | |
26 | |
27 // Implementation of WebSocketStream for non-multiplexed ws:// connections (or | |
28 // the physical side of a multiplexed ws:// connection). | |
29 class NET_EXPORT_PRIVATE WebSocketBasicStream : public WebSocketStream { | |
30 public: | |
31 // The type of a pointer to a function that generates a WebSocketMaskingKey. | |
32 typedef WebSocketMaskingKey (*WebSocketMaskingKeyGeneratorFunction)(); | |
33 | |
34 // This class should not normally be constructed directly; see | |
35 // WebSocketStream::CreateAndConnectStream. | |
36 explicit WebSocketBasicStream(scoped_ptr<ClientSocketHandle> connection); | |
37 | |
38 // The destructor has to make sure the connection is closed when we finish so | |
39 // that it does not get returned to the pool. | |
40 virtual ~WebSocketBasicStream(); | |
41 | |
42 // Reads one or more frame chunks. See WebSocketStream for the full | |
43 // specifications of the behaviour. | |
44 virtual int ReadFrames(ScopedVector<WebSocketFrameChunk>* frame_chunks, | |
45 const CompletionCallback& callback) OVERRIDE; | |
46 | |
47 // Writes one or more frame chunks. Either writes everything or fails. | |
48 virtual int WriteFrames(ScopedVector<WebSocketFrameChunk>* frame_chunks, | |
49 const CompletionCallback& callback) OVERRIDE; | |
50 | |
51 // Closes the stream, cancelling all pending IO operations without calling the | |
52 // callbacks. | |
53 virtual void Close() OVERRIDE; | |
54 | |
55 // Returns the optional sub-protocol that was negotiated with the server. | |
56 virtual std::string GetSubProtocol() const OVERRIDE; | |
57 | |
58 // Returns the extensions that were negotiated with the server. | |
59 virtual std::string GetExtensions() const OVERRIDE; | |
60 | |
61 // Writes WebSocket handshake request HTTP-style to the connection. Adds | |
62 // "Sec-Websocket-Key" header; this should not be included in |headers|. | |
tyoshino (SeeGerritForStatus)
2013/08/22 07:56:10
Websocket -> WebSocket
Adam Rice
2013/08/22 08:13:04
Done.
| |
63 // | |
64 // "callback" will only be called if this method returns ERR_IO_PENDING. | |
65 // | |
66 // |response_info| must remain valid until the callback from | |
67 // ReadHandshakeResponse has been called. | |
68 virtual int SendHandshakeRequest(const GURL& url, | |
69 const HttpRequestHeaders& headers, | |
70 HttpResponseInfo* response_info, | |
71 const CompletionCallback& callback) OVERRIDE; | |
72 | |
73 // Reads WebSocket handshake response from the connection. The callback | |
74 // function completes when the response headers have been completely | |
75 // received. Must be called after SendHandshakeRequest() completes (including | |
76 // the callback, if relevant). If the result was not available synchronously, | |
77 // then it will be passed to the callback. Note that the actual response info | |
78 // is passed into the HttpResponseInfo object that was passed to | |
79 // SendHandshakeResponse. | |
80 virtual int ReadHandshakeResponse( | |
81 const CompletionCallback& callback) OVERRIDE; | |
82 | |
83 //////////////////////////////////////////////////////////////////////////// | |
84 // Methods for testing only. | |
85 | |
86 // Creates a WebSocketBasicStream with the specified |connection|, | |
87 // |http_read_buffer|, |sub_protocol|, |extensions| and | |
88 // |key_generator_function|. | |
89 static scoped_ptr<WebSocketBasicStream> CreateWebSocketBasicStreamForTesting( | |
90 scoped_ptr<ClientSocketHandle> connection, | |
91 const scoped_refptr<GrowableIOBuffer>& http_read_buffer, | |
92 const std::string& sub_protocol, | |
93 const std::string& extensions, | |
94 WebSocketMaskingKeyGeneratorFunction key_generator_function); | |
95 | |
96 private: | |
97 // The semantics of WriteFrames() are such that it only returns OK or calls | |
98 // the callback when everything is written or something has failed. But | |
99 // Socket::Write() can do partial writes, so this method wraps the call in a | |
100 // loop. | |
101 int WriteEverything(const scoped_refptr<DrainableIOBuffer>& buffer, | |
102 const CompletionCallback& callback); | |
103 | |
104 // Wraps the |callback| to continue writing until everything has been written. | |
105 void WriteDone(const scoped_refptr<DrainableIOBuffer>& buffer, | |
106 const CompletionCallback& callback, | |
107 int result); | |
108 | |
109 // Called when a read completes. Parses the result and (unless no complete | |
110 // header has been received) calls |callback|. | |
111 void ReadDone(ScopedVector<WebSocketFrameChunk>* frame_chunks, | |
112 const CompletionCallback& callback, | |
113 int result); | |
114 | |
115 // Storage for pending reads. All active WebSockets spend all the time with a | |
116 // call to ReadFrames() pending, so there is no benefit in trying to share | |
117 // this between sockets. | |
118 scoped_refptr<IOBufferWithSize> read_buffer_; | |
119 | |
120 // The connection, wrapped in a ClientSocketHandle so that we can prevent it | |
121 // from being returned to the pool. | |
122 scoped_ptr<ClientSocketHandle> connection_; | |
123 | |
124 // Only used during handshake. Some data may be left in this buffer after the | |
125 // handshake, in which case it will be picked up during the first call to | |
126 // ReadFrames(). | |
127 scoped_refptr<GrowableIOBuffer> http_read_buffer_; | |
128 | |
129 // This keeps the current parse state (including any incomplete headers) and | |
130 // parses frames. | |
131 WebSocketFrameParser parser_; | |
132 | |
133 // The negotated sub-protocol, or empty for none. | |
134 std::string sub_protocol_; | |
135 | |
136 // The extensions negotiated with the remote server. | |
137 std::string extensions_; | |
138 | |
139 // This can be overridden in tests to make the output deterministic. We don't | |
140 // use a Callback here because a function pointer is faster and good enough | |
141 // for our purposes. | |
142 WebSocketMaskingKeyGeneratorFunction generate_websocket_masking_key_; | |
143 }; | |
144 | |
145 } // namespace net | |
146 | |
147 #endif // NET_WEBSOCKETS_WEBSOCKET_BASIC_STREAM_H_ | |
OLD | NEW |