OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_WEBSOCKETS_WEBSOCKET_BASIC_STREAM_H_ | 5 #ifndef NET_WEBSOCKETS_WEBSOCKET_BASIC_STREAM_H_ |
6 #define NET_WEBSOCKETS_WEBSOCKET_BASIC_STREAM_H_ | 6 #define NET_WEBSOCKETS_WEBSOCKET_BASIC_STREAM_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/memory/scoped_vector.h" | 13 #include "base/memory/scoped_vector.h" |
14 #include "net/websockets/websocket_frame_parser.h" | 14 #include "net/websockets/websocket_frame_parser.h" |
15 #include "net/websockets/websocket_stream.h" | 15 #include "net/websockets/websocket_stream.h" |
16 | 16 |
17 namespace net { | 17 namespace net { |
18 | 18 |
19 class ClientSocketHandle; | 19 class ClientSocketHandle; |
20 class DrainableIOBuffer; | 20 class DrainableIOBuffer; |
21 class GrowableIOBuffer; | 21 class GrowableIOBuffer; |
22 class HttpRequestHeaders; | 22 class HttpRequestHeaders; |
23 class HttpResponseInfo; | 23 class HttpResponseInfo; |
24 class IOBufferWithSize; | 24 class IOBufferWithSize; |
25 struct WebSocketFrame; | |
25 struct WebSocketFrameChunk; | 26 struct WebSocketFrameChunk; |
26 | 27 |
27 // Implementation of WebSocketStream for non-multiplexed ws:// connections (or | 28 // Implementation of WebSocketStream for non-multiplexed ws:// connections (or |
28 // the physical side of a multiplexed ws:// connection). | 29 // the physical side of a multiplexed ws:// connection). |
29 class NET_EXPORT_PRIVATE WebSocketBasicStream : public WebSocketStream { | 30 class NET_EXPORT_PRIVATE WebSocketBasicStream : public WebSocketStream { |
30 public: | 31 public: |
31 typedef WebSocketMaskingKey (*WebSocketMaskingKeyGeneratorFunction)(); | 32 typedef WebSocketMaskingKey (*WebSocketMaskingKeyGeneratorFunction)(); |
32 | 33 |
33 // This class should not normally be constructed directly; see | 34 // This class should not normally be constructed directly; see |
34 // WebSocketStream::CreateAndConnectStream. | 35 // WebSocketStream::CreateAndConnectStream. |
35 explicit WebSocketBasicStream(scoped_ptr<ClientSocketHandle> connection); | 36 explicit WebSocketBasicStream(scoped_ptr<ClientSocketHandle> connection); |
36 | 37 |
37 // The destructor has to make sure the connection is closed when we finish so | 38 // The destructor has to make sure the connection is closed when we finish so |
38 // that it does not get returned to the pool. | 39 // that it does not get returned to the pool. |
39 virtual ~WebSocketBasicStream(); | 40 virtual ~WebSocketBasicStream(); |
40 | 41 |
41 // WebSocketStream implementation. | 42 // WebSocketStream implementation. |
42 virtual int ReadFrames(ScopedVector<WebSocketFrameChunk>* frame_chunks, | 43 virtual int ReadFrames(ScopedVector<WebSocketFrame>* frames, |
43 const CompletionCallback& callback) OVERRIDE; | 44 const CompletionCallback& callback) OVERRIDE; |
44 | 45 |
45 virtual int WriteFrames(ScopedVector<WebSocketFrameChunk>* frame_chunks, | 46 virtual int WriteFrames(ScopedVector<WebSocketFrame>* frames, |
46 const CompletionCallback& callback) OVERRIDE; | 47 const CompletionCallback& callback) OVERRIDE; |
47 | 48 |
48 virtual void Close() OVERRIDE; | 49 virtual void Close() OVERRIDE; |
49 | 50 |
50 virtual std::string GetSubProtocol() const OVERRIDE; | 51 virtual std::string GetSubProtocol() const OVERRIDE; |
51 | 52 |
52 virtual std::string GetExtensions() const OVERRIDE; | 53 virtual std::string GetExtensions() const OVERRIDE; |
53 | 54 |
54 // Writes WebSocket handshake request HTTP-style to the connection. Adds | 55 // Writes WebSocket handshake request HTTP-style to the connection. Adds |
55 // "Sec-WebSocket-Key" header; this should not be included in |headers|. | 56 // "Sec-WebSocket-Key" header; this should not be included in |headers|. |
(...skipping 21 matching lines...) Expand all Loading... | |
77 int WriteEverything(const scoped_refptr<DrainableIOBuffer>& buffer, | 78 int WriteEverything(const scoped_refptr<DrainableIOBuffer>& buffer, |
78 const CompletionCallback& callback); | 79 const CompletionCallback& callback); |
79 | 80 |
80 // Wraps the |callback| to continue writing until everything has been written. | 81 // Wraps the |callback| to continue writing until everything has been written. |
81 void OnWriteComplete(const scoped_refptr<DrainableIOBuffer>& buffer, | 82 void OnWriteComplete(const scoped_refptr<DrainableIOBuffer>& buffer, |
82 const CompletionCallback& callback, | 83 const CompletionCallback& callback, |
83 int result); | 84 int result); |
84 | 85 |
85 // Attempts to parse the output of a read as WebSocket frames. On success, | 86 // Attempts to parse the output of a read as WebSocket frames. On success, |
86 // returns OK and places the frame(s) in frame_chunks. | 87 // returns OK and places the frame(s) in frame_chunks. |
87 int HandleReadResult(int result, | 88 int HandleReadResult(int result, ScopedVector<WebSocketFrame>* frames); |
88 ScopedVector<WebSocketFrameChunk>* frame_chunks); | 89 |
90 // Converts the chunks in |frame_chunks| into frames and writes them to | |
91 // |frames|. |frame_chunks| is destroyed in the process. Returns | |
92 // ERR_WS_PROTOCOL_ERROR if an invalid chunk was found. If one or more frames | |
93 // was added to |frames|, then returns OK, otherwise returns ERR_IO_PENDING. | |
94 int ConvertChunksToFrames(ScopedVector<WebSocketFrameChunk>* frame_chunks, | |
95 ScopedVector<WebSocketFrame>* frames); | |
96 | |
97 // Converts a |chunk| to a |frame|. |frame| should be NULL on entry to this | |
yhirano
2013/09/13 10:03:17
|*frame| should be Null?
| |
98 // method. If |chunk| is an incomplete control frame, then |frame| may still | |
99 // be NULL on exit. If an invalid control frame is | |
100 // found, returns ERR_WS_PROTOCOL_ERROR and the stream is no longer | |
101 // usable. Otherwise returns OK (even if frame is still NULL). | |
102 int ConvertChunkToFrame(scoped_ptr<WebSocketFrameChunk> chunk, | |
103 scoped_ptr<WebSocketFrame>* frame); | |
104 | |
105 // Add |data_buffer| to the end of |incomplete_control_frame_body_|, applying | |
106 // bounds checks. | |
107 void AddToIncompleteControlFrameBody( | |
108 const scoped_refptr<IOBufferWithSize>& data_buffer); | |
89 | 109 |
90 // Called when a read completes. Parses the result and (unless no complete | 110 // Called when a read completes. Parses the result and (unless no complete |
91 // header has been received) calls |callback|. | 111 // header has been received) calls |callback|. |
92 void OnReadComplete(ScopedVector<WebSocketFrameChunk>* frame_chunks, | 112 void OnReadComplete(ScopedVector<WebSocketFrame>* frames, |
93 const CompletionCallback& callback, | 113 const CompletionCallback& callback, |
94 int result); | 114 int result); |
95 | 115 |
96 // Storage for pending reads. All active WebSockets spend all the time with a | 116 // Storage for pending reads. All active WebSockets spend all the time with a |
97 // call to ReadFrames() pending, so there is no benefit in trying to share | 117 // call to ReadFrames() pending, so there is no benefit in trying to share |
98 // this between sockets. | 118 // this between sockets. |
99 scoped_refptr<IOBufferWithSize> read_buffer_; | 119 scoped_refptr<IOBufferWithSize> read_buffer_; |
100 | 120 |
101 // The connection, wrapped in a ClientSocketHandle so that we can prevent it | 121 // The connection, wrapped in a ClientSocketHandle so that we can prevent it |
102 // from being returned to the pool. | 122 // from being returned to the pool. |
103 scoped_ptr<ClientSocketHandle> connection_; | 123 scoped_ptr<ClientSocketHandle> connection_; |
104 | 124 |
125 // Frame header for the frame currently being received. Only non-NULL while we | |
126 // are processing the frame. If the frame arrives in multiple chunks, it can | |
127 // remain non-NULL until additional chunks arrive. If the header of the frame | |
128 // was invalid, this is set to NULL, the channel is failed, and subsequent | |
129 // chunks of the same frame will be ignored. | |
130 scoped_ptr<WebSocketFrameHeader> current_frame_header_; | |
131 | |
132 // Although it should rarely happen in practice, a control frame can arrive | |
133 // broken into chunks. This variable provides storage for a partial control | |
134 // frame until the rest arrives. It will be NULL the rest of the time. | |
135 scoped_refptr<GrowableIOBuffer> incomplete_control_frame_body_; | |
136 | |
105 // Only used during handshake. Some data may be left in this buffer after the | 137 // Only used during handshake. Some data may be left in this buffer after the |
106 // handshake, in which case it will be picked up during the first call to | 138 // handshake, in which case it will be picked up during the first call to |
107 // ReadFrames(). The type is GrowableIOBuffer for compatibility with | 139 // ReadFrames(). The type is GrowableIOBuffer for compatibility with |
108 // net::HttpStreamParser, which is used to parse the handshake. | 140 // net::HttpStreamParser, which is used to parse the handshake. |
109 scoped_refptr<GrowableIOBuffer> http_read_buffer_; | 141 scoped_refptr<GrowableIOBuffer> http_read_buffer_; |
110 | 142 |
111 // This keeps the current parse state (including any incomplete headers) and | 143 // This keeps the current parse state (including any incomplete headers) and |
112 // parses frames. | 144 // parses frames. |
113 WebSocketFrameParser parser_; | 145 WebSocketFrameParser parser_; |
114 | 146 |
115 // The negotated sub-protocol, or empty for none. | 147 // The negotated sub-protocol, or empty for none. |
116 std::string sub_protocol_; | 148 std::string sub_protocol_; |
117 | 149 |
118 // The extensions negotiated with the remote server. | 150 // The extensions negotiated with the remote server. |
119 std::string extensions_; | 151 std::string extensions_; |
120 | 152 |
121 // This can be overridden in tests to make the output deterministic. We don't | 153 // This can be overridden in tests to make the output deterministic. We don't |
122 // use a Callback here because a function pointer is faster and good enough | 154 // use a Callback here because a function pointer is faster and good enough |
123 // for our purposes. | 155 // for our purposes. |
124 WebSocketMaskingKeyGeneratorFunction generate_websocket_masking_key_; | 156 WebSocketMaskingKeyGeneratorFunction generate_websocket_masking_key_; |
125 }; | 157 }; |
126 | 158 |
127 } // namespace net | 159 } // namespace net |
128 | 160 |
129 #endif // NET_WEBSOCKETS_WEBSOCKET_BASIC_STREAM_H_ | 161 #endif // NET_WEBSOCKETS_WEBSOCKET_BASIC_STREAM_H_ |
OLD | NEW |