| 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 #include "net/websockets/websocket_basic_stream.h" | 5 #include "net/websockets/websocket_basic_stream.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <limits> | 8 #include <limits> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 http_read_buffer_(http_read_buffer), | 77 http_read_buffer_(http_read_buffer), |
| 78 sub_protocol_(sub_protocol), | 78 sub_protocol_(sub_protocol), |
| 79 extensions_(extensions), | 79 extensions_(extensions), |
| 80 generate_websocket_masking_key_(&GenerateWebSocketMaskingKey) { | 80 generate_websocket_masking_key_(&GenerateWebSocketMaskingKey) { |
| 81 // http_read_buffer_ should not be set if it contains no data. | 81 // http_read_buffer_ should not be set if it contains no data. |
| 82 if (http_read_buffer_ && http_read_buffer_->offset() == 0) | 82 if (http_read_buffer_ && http_read_buffer_->offset() == 0) |
| 83 http_read_buffer_ = NULL; | 83 http_read_buffer_ = NULL; |
| 84 DCHECK(connection_->is_initialized()); | 84 DCHECK(connection_->is_initialized()); |
| 85 } | 85 } |
| 86 | 86 |
| 87 WebSocketBasicStream::~WebSocketBasicStream() { Close(); } | 87 WebSocketBasicStream::~WebSocketBasicStream() { |
| 88 Close(); |
| 89 } |
| 88 | 90 |
| 89 int WebSocketBasicStream::ReadFrames(ScopedVector<WebSocketFrame>* frames, | 91 int WebSocketBasicStream::ReadFrames(ScopedVector<WebSocketFrame>* frames, |
| 90 const CompletionCallback& callback) { | 92 const CompletionCallback& callback) { |
| 91 DCHECK(frames->empty()); | 93 DCHECK(frames->empty()); |
| 92 // If there is data left over after parsing the HTTP headers, attempt to parse | 94 // If there is data left over after parsing the HTTP headers, attempt to parse |
| 93 // it as WebSocket frames. | 95 // it as WebSocket frames. |
| 94 if (http_read_buffer_) { | 96 if (http_read_buffer_) { |
| 95 DCHECK_GE(http_read_buffer_->offset(), 0); | 97 DCHECK_GE(http_read_buffer_->offset(), 0); |
| 96 // We cannot simply copy the data into read_buffer_, as it might be too | 98 // We cannot simply copy the data into read_buffer_, as it might be too |
| 97 // large. | 99 // large. |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 remaining_size -= frame_size; | 169 remaining_size -= frame_size; |
| 168 } | 170 } |
| 169 } | 171 } |
| 170 DCHECK_EQ(0, remaining_size) << "Buffer size calculation was wrong; " | 172 DCHECK_EQ(0, remaining_size) << "Buffer size calculation was wrong; " |
| 171 << remaining_size << " bytes left over."; | 173 << remaining_size << " bytes left over."; |
| 172 scoped_refptr<DrainableIOBuffer> drainable_buffer( | 174 scoped_refptr<DrainableIOBuffer> drainable_buffer( |
| 173 new DrainableIOBuffer(combined_buffer, total_size)); | 175 new DrainableIOBuffer(combined_buffer, total_size)); |
| 174 return WriteEverything(drainable_buffer, callback); | 176 return WriteEverything(drainable_buffer, callback); |
| 175 } | 177 } |
| 176 | 178 |
| 177 void WebSocketBasicStream::Close() { connection_->socket()->Disconnect(); } | 179 void WebSocketBasicStream::Close() { |
| 180 connection_->socket()->Disconnect(); |
| 181 } |
| 178 | 182 |
| 179 std::string WebSocketBasicStream::GetSubProtocol() const { | 183 std::string WebSocketBasicStream::GetSubProtocol() const { |
| 180 return sub_protocol_; | 184 return sub_protocol_; |
| 181 } | 185 } |
| 182 | 186 |
| 183 std::string WebSocketBasicStream::GetExtensions() const { return extensions_; } | 187 std::string WebSocketBasicStream::GetExtensions() const { |
| 188 return extensions_; |
| 189 } |
| 184 | 190 |
| 185 /*static*/ | 191 /*static*/ |
| 186 scoped_ptr<WebSocketBasicStream> | 192 scoped_ptr<WebSocketBasicStream> |
| 187 WebSocketBasicStream::CreateWebSocketBasicStreamForTesting( | 193 WebSocketBasicStream::CreateWebSocketBasicStreamForTesting( |
| 188 scoped_ptr<ClientSocketHandle> connection, | 194 scoped_ptr<ClientSocketHandle> connection, |
| 189 const scoped_refptr<GrowableIOBuffer>& http_read_buffer, | 195 const scoped_refptr<GrowableIOBuffer>& http_read_buffer, |
| 190 const std::string& sub_protocol, | 196 const std::string& sub_protocol, |
| 191 const std::string& extensions, | 197 const std::string& extensions, |
| 192 WebSocketMaskingKeyGeneratorFunction key_generator_function) { | 198 WebSocketMaskingKeyGeneratorFunction key_generator_function) { |
| 193 scoped_ptr<WebSocketBasicStream> stream(new WebSocketBasicStream( | 199 scoped_ptr<WebSocketBasicStream> stream(new WebSocketBasicStream( |
| (...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 415 const CompletionCallback& callback, | 421 const CompletionCallback& callback, |
| 416 int result) { | 422 int result) { |
| 417 result = HandleReadResult(result, frames); | 423 result = HandleReadResult(result, frames); |
| 418 if (result == ERR_IO_PENDING) | 424 if (result == ERR_IO_PENDING) |
| 419 result = ReadFrames(frames, callback); | 425 result = ReadFrames(frames, callback); |
| 420 if (result != ERR_IO_PENDING) | 426 if (result != ERR_IO_PENDING) |
| 421 callback.Run(result); | 427 callback.Run(result); |
| 422 } | 428 } |
| 423 | 429 |
| 424 } // namespace net | 430 } // namespace net |
| OLD | NEW |