| 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 20 matching lines...) Expand all Loading... |
| 76 // something has failed. | 77 // something has failed. |
| 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 |frames|. |
| 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 |
| 98 // method. If |chunk| is an incomplete control frame, or an empty non-final |
| 99 // frame, then |*frame| may still be NULL on exit. If an invalid control frame |
| 100 // is 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 // Create a frame based on the value of |is_final_chunk|, |data| and |
| 106 // |current_frame_header_|. Clears |current_frame_header_| if |is_final_chunk| |
| 107 // is true. |data| may be NULL if the frame has an empty payload. A frame with |
| 108 // no data and the "final" flag not set is not useful; in this case the |
| 109 // returned frame will be NULL. Otherwise, |current_frame_header_->opcode| is |
| 110 // set to Continuation after use if it was Text or Binary, in accordance with |
| 111 // WebSocket RFC6455 section 5.4. |
| 112 scoped_ptr<WebSocketFrame> CreateFrame( |
| 113 bool is_final_chunk, |
| 114 const scoped_refptr<IOBufferWithSize>& data); |
| 115 |
| 116 // Add |data_buffer| to the end of |incomplete_control_frame_body_|, applying |
| 117 // bounds checks. |
| 118 void AddToIncompleteControlFrameBody( |
| 119 const scoped_refptr<IOBufferWithSize>& data_buffer); |
| 89 | 120 |
| 90 // Called when a read completes. Parses the result and (unless no complete | 121 // Called when a read completes. Parses the result and (unless no complete |
| 91 // header has been received) calls |callback|. | 122 // header has been received) calls |callback|. |
| 92 void OnReadComplete(ScopedVector<WebSocketFrameChunk>* frame_chunks, | 123 void OnReadComplete(ScopedVector<WebSocketFrame>* frames, |
| 93 const CompletionCallback& callback, | 124 const CompletionCallback& callback, |
| 94 int result); | 125 int result); |
| 95 | 126 |
| 96 // Storage for pending reads. All active WebSockets spend all the time with a | 127 // 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 | 128 // call to ReadFrames() pending, so there is no benefit in trying to share |
| 98 // this between sockets. | 129 // this between sockets. |
| 99 scoped_refptr<IOBufferWithSize> read_buffer_; | 130 scoped_refptr<IOBufferWithSize> read_buffer_; |
| 100 | 131 |
| 101 // The connection, wrapped in a ClientSocketHandle so that we can prevent it | 132 // The connection, wrapped in a ClientSocketHandle so that we can prevent it |
| 102 // from being returned to the pool. | 133 // from being returned to the pool. |
| 103 scoped_ptr<ClientSocketHandle> connection_; | 134 scoped_ptr<ClientSocketHandle> connection_; |
| 104 | 135 |
| 136 // Frame header for the frame currently being received. Only non-NULL while we |
| 137 // are processing the frame. If the frame arrives in multiple chunks, it can |
| 138 // remain non-NULL until additional chunks arrive. If the header of the frame |
| 139 // was invalid, this is set to NULL, the channel is failed, and subsequent |
| 140 // chunks of the same frame will be ignored. |
| 141 scoped_ptr<WebSocketFrameHeader> current_frame_header_; |
| 142 |
| 143 // Although it should rarely happen in practice, a control frame can arrive |
| 144 // broken into chunks. This variable provides storage for a partial control |
| 145 // frame until the rest arrives. It will be NULL the rest of the time. |
| 146 scoped_refptr<GrowableIOBuffer> incomplete_control_frame_body_; |
| 147 |
| 105 // Only used during handshake. Some data may be left in this buffer after the | 148 // 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 | 149 // handshake, in which case it will be picked up during the first call to |
| 107 // ReadFrames(). The type is GrowableIOBuffer for compatibility with | 150 // ReadFrames(). The type is GrowableIOBuffer for compatibility with |
| 108 // net::HttpStreamParser, which is used to parse the handshake. | 151 // net::HttpStreamParser, which is used to parse the handshake. |
| 109 scoped_refptr<GrowableIOBuffer> http_read_buffer_; | 152 scoped_refptr<GrowableIOBuffer> http_read_buffer_; |
| 110 | 153 |
| 111 // This keeps the current parse state (including any incomplete headers) and | 154 // This keeps the current parse state (including any incomplete headers) and |
| 112 // parses frames. | 155 // parses frames. |
| 113 WebSocketFrameParser parser_; | 156 WebSocketFrameParser parser_; |
| 114 | 157 |
| 115 // The negotated sub-protocol, or empty for none. | 158 // The negotated sub-protocol, or empty for none. |
| 116 std::string sub_protocol_; | 159 std::string sub_protocol_; |
| 117 | 160 |
| 118 // The extensions negotiated with the remote server. | 161 // The extensions negotiated with the remote server. |
| 119 std::string extensions_; | 162 std::string extensions_; |
| 120 | 163 |
| 121 // This can be overridden in tests to make the output deterministic. We don't | 164 // 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 | 165 // use a Callback here because a function pointer is faster and good enough |
| 123 // for our purposes. | 166 // for our purposes. |
| 124 WebSocketMaskingKeyGeneratorFunction generate_websocket_masking_key_; | 167 WebSocketMaskingKeyGeneratorFunction generate_websocket_masking_key_; |
| 125 }; | 168 }; |
| 126 | 169 |
| 127 } // namespace net | 170 } // namespace net |
| 128 | 171 |
| 129 #endif // NET_WEBSOCKETS_WEBSOCKET_BASIC_STREAM_H_ | 172 #endif // NET_WEBSOCKETS_WEBSOCKET_BASIC_STREAM_H_ |
| OLD | NEW |