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" |
| 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" |
| 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 typedef WebSocketMaskingKey (*WebSocketMaskingKeyGeneratorFunction)(); |
| 32 |
| 33 // This class should not normally be constructed directly; see |
| 34 // WebSocketStream::CreateAndConnectStream. |
| 35 explicit WebSocketBasicStream(scoped_ptr<ClientSocketHandle> connection); |
| 36 |
| 37 // 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 virtual ~WebSocketBasicStream(); |
| 40 |
| 41 // WebSocketStream implementation. |
| 42 virtual int ReadFrames(ScopedVector<WebSocketFrameChunk>* frame_chunks, |
| 43 const CompletionCallback& callback) OVERRIDE; |
| 44 |
| 45 virtual int WriteFrames(ScopedVector<WebSocketFrameChunk>* frame_chunks, |
| 46 const CompletionCallback& callback) OVERRIDE; |
| 47 |
| 48 virtual void Close() OVERRIDE; |
| 49 |
| 50 virtual std::string GetSubProtocol() const OVERRIDE; |
| 51 |
| 52 virtual std::string GetExtensions() const OVERRIDE; |
| 53 |
| 54 // Writes WebSocket handshake request HTTP-style to the connection. Adds |
| 55 // "Sec-WebSocket-Key" header; this should not be included in |headers|. |
| 56 virtual int SendHandshakeRequest(const GURL& url, |
| 57 const HttpRequestHeaders& headers, |
| 58 HttpResponseInfo* response_info, |
| 59 const CompletionCallback& callback) OVERRIDE; |
| 60 |
| 61 virtual int ReadHandshakeResponse( |
| 62 const CompletionCallback& callback) OVERRIDE; |
| 63 |
| 64 //////////////////////////////////////////////////////////////////////////// |
| 65 // Methods for testing only. |
| 66 |
| 67 static scoped_ptr<WebSocketBasicStream> CreateWebSocketBasicStreamForTesting( |
| 68 scoped_ptr<ClientSocketHandle> connection, |
| 69 const scoped_refptr<GrowableIOBuffer>& http_read_buffer, |
| 70 const std::string& sub_protocol, |
| 71 const std::string& extensions, |
| 72 WebSocketMaskingKeyGeneratorFunction key_generator_function); |
| 73 |
| 74 private: |
| 75 // Returns OK or calls |callback| when the |buffer| is fully drained or |
| 76 // something has failed. |
| 77 int WriteEverything(const scoped_refptr<DrainableIOBuffer>& buffer, |
| 78 const CompletionCallback& callback); |
| 79 |
| 80 // Wraps the |callback| to continue writing until everything has been written. |
| 81 void OnWriteComplete(const scoped_refptr<DrainableIOBuffer>& buffer, |
| 82 const CompletionCallback& callback, |
| 83 int result); |
| 84 |
| 85 // 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 int HandleReadResult(int result, |
| 88 ScopedVector<WebSocketFrameChunk>* frame_chunks); |
| 89 |
| 90 // Called when a read completes. Parses the result and (unless no complete |
| 91 // header has been received) calls |callback|. |
| 92 void OnReadComplete(ScopedVector<WebSocketFrameChunk>* frame_chunks, |
| 93 const CompletionCallback& callback, |
| 94 int result); |
| 95 |
| 96 // 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 |
| 98 // this between sockets. |
| 99 scoped_refptr<IOBufferWithSize> read_buffer_; |
| 100 |
| 101 // The connection, wrapped in a ClientSocketHandle so that we can prevent it |
| 102 // from being returned to the pool. |
| 103 scoped_ptr<ClientSocketHandle> connection_; |
| 104 |
| 105 // 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 |
| 107 // ReadFrames(). The type is GrowableIOBuffer for compatibility with |
| 108 // net::HttpStreamParser, which is used to parse the handshake. |
| 109 scoped_refptr<GrowableIOBuffer> http_read_buffer_; |
| 110 |
| 111 // This keeps the current parse state (including any incomplete headers) and |
| 112 // parses frames. |
| 113 WebSocketFrameParser parser_; |
| 114 |
| 115 // The negotated sub-protocol, or empty for none. |
| 116 std::string sub_protocol_; |
| 117 |
| 118 // The extensions negotiated with the remote server. |
| 119 std::string extensions_; |
| 120 |
| 121 // 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 |
| 123 // for our purposes. |
| 124 WebSocketMaskingKeyGeneratorFunction generate_websocket_masking_key_; |
| 125 }; |
| 126 |
| 127 } // namespace net |
| 128 |
| 129 #endif // NET_WEBSOCKETS_WEBSOCKET_BASIC_STREAM_H_ |
OLD | NEW |