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_STREAM_H_
6 #define NET_WEBSOCKETS_WEBSOCKET_BASIC_STREAM_H_
10 #include "base/callback.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/scoped_vector.h"
14 #include "net/websockets/websocket_frame_parser.h"
15 #include "net/websockets/websocket_stream.h"
19 class ClientSocketHandle;
20 class DrainableIOBuffer;
21 class GrowableIOBuffer;
22 class IOBufferWithSize;
23 struct WebSocketFrame;
24 struct WebSocketFrameChunk;
26 // Implementation of WebSocketStream for non-multiplexed ws:// connections (or
27 // the physical side of a multiplexed ws:// connection).
28 class NET_EXPORT_PRIVATE WebSocketBasicStream : public WebSocketStream {
30 typedef WebSocketMaskingKey (*WebSocketMaskingKeyGeneratorFunction)();
32 // This class should not normally be constructed directly; see
33 // WebSocketStream::CreateAndConnectStream.
34 explicit WebSocketBasicStream(scoped_ptr<ClientSocketHandle> connection);
36 // The destructor has to make sure the connection is closed when we finish so
37 // that it does not get returned to the pool.
38 virtual ~WebSocketBasicStream();
40 // WebSocketStream implementation.
41 virtual int ReadFrames(ScopedVector<WebSocketFrame>* frames,
42 const CompletionCallback& callback) OVERRIDE;
44 virtual int WriteFrames(ScopedVector<WebSocketFrame>* frames,
45 const CompletionCallback& callback) OVERRIDE;
47 virtual void Close() OVERRIDE;
49 virtual std::string GetSubProtocol() const OVERRIDE;
51 virtual std::string GetExtensions() const OVERRIDE;
53 ////////////////////////////////////////////////////////////////////////////
54 // Methods for testing only.
56 static scoped_ptr<WebSocketBasicStream> CreateWebSocketBasicStreamForTesting(
57 scoped_ptr<ClientSocketHandle> connection,
58 const scoped_refptr<GrowableIOBuffer>& http_read_buffer,
59 const std::string& sub_protocol,
60 const std::string& extensions,
61 WebSocketMaskingKeyGeneratorFunction key_generator_function);
64 // Returns OK or calls |callback| when the |buffer| is fully drained or
65 // something has failed.
66 int WriteEverything(const scoped_refptr<DrainableIOBuffer>& buffer,
67 const CompletionCallback& callback);
69 // Wraps the |callback| to continue writing until everything has been written.
70 void OnWriteComplete(const scoped_refptr<DrainableIOBuffer>& buffer,
71 const CompletionCallback& callback,
74 // Attempts to parse the output of a read as WebSocket frames. On success,
75 // returns OK and places the frame(s) in |frames|.
76 int HandleReadResult(int result, ScopedVector<WebSocketFrame>* frames);
78 // Converts the chunks in |frame_chunks| into frames and writes them to
79 // |frames|. |frame_chunks| is destroyed in the process. Returns
80 // ERR_WS_PROTOCOL_ERROR if an invalid chunk was found. If one or more frames
81 // was added to |frames|, then returns OK, otherwise returns ERR_IO_PENDING.
82 int ConvertChunksToFrames(ScopedVector<WebSocketFrameChunk>* frame_chunks,
83 ScopedVector<WebSocketFrame>* frames);
85 // Converts a |chunk| to a |frame|. |*frame| should be NULL on entry to this
86 // method. If |chunk| is an incomplete control frame, or an empty middle
87 // frame, then |*frame| may still be NULL on exit. If an invalid control frame
88 // is found, returns ERR_WS_PROTOCOL_ERROR and the stream is no longer
89 // usable. Otherwise returns OK (even if frame is still NULL).
90 int ConvertChunkToFrame(scoped_ptr<WebSocketFrameChunk> chunk,
91 scoped_ptr<WebSocketFrame>* frame);
93 // Creates a frame based on the value of |is_final_chunk|, |data| and
94 // |current_frame_header_|. Clears |current_frame_header_| if |is_final_chunk|
95 // is true. |data| may be NULL if the frame has an empty payload. A frame in
96 // the middle of a message with no data is not useful; in this case the
97 // returned frame will be NULL. Otherwise, |current_frame_header_->opcode| is
98 // set to Continuation after use if it was Text or Binary, in accordance with
99 // WebSocket RFC6455 section 5.4.
100 scoped_ptr<WebSocketFrame> CreateFrame(
102 const scoped_refptr<IOBufferWithSize>& data);
104 // Adds |data_buffer| to the end of |incomplete_control_frame_body_|, applying
106 void AddToIncompleteControlFrameBody(
107 const scoped_refptr<IOBufferWithSize>& data_buffer);
109 // Called when a read completes. Parses the result and (unless no complete
110 // header has been received) calls |callback|.
111 void OnReadComplete(ScopedVector<WebSocketFrame>* frames,
112 const CompletionCallback& callback,
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_;
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_;
124 // Frame header for the frame currently being received. Only non-NULL while we
125 // are processing the frame. If the frame arrives in multiple chunks, it can
126 // remain non-NULL until additional chunks arrive. If the header of the frame
127 // was invalid, this is set to NULL, the channel is failed, and subsequent
128 // chunks of the same frame will be ignored.
129 scoped_ptr<WebSocketFrameHeader> current_frame_header_;
131 // Although it should rarely happen in practice, a control frame can arrive
132 // broken into chunks. This variable provides storage for a partial control
133 // frame until the rest arrives. It will be NULL the rest of the time.
134 scoped_refptr<GrowableIOBuffer> incomplete_control_frame_body_;
136 // Only used during handshake. Some data may be left in this buffer after the
137 // handshake, in which case it will be picked up during the first call to
138 // ReadFrames(). The type is GrowableIOBuffer for compatibility with
139 // net::HttpStreamParser, which is used to parse the handshake.
140 scoped_refptr<GrowableIOBuffer> http_read_buffer_;
142 // This keeps the current parse state (including any incomplete headers) and
144 WebSocketFrameParser parser_;
146 // The negotated sub-protocol, or empty for none.
147 std::string sub_protocol_;
149 // The extensions negotiated with the remote server.
150 std::string extensions_;
152 // This can be overridden in tests to make the output deterministic. We don't
153 // use a Callback here because a function pointer is faster and good enough
155 WebSocketMaskingKeyGeneratorFunction generate_websocket_masking_key_;
160 #endif // NET_WEBSOCKETS_WEBSOCKET_BASIC_STREAM_H_