OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_FRAME_PARSER_H_ | 5 #ifndef NET_WEBSOCKETS_WEBSOCKET_FRAME_PARSER_H_ |
6 #define NET_WEBSOCKETS_WEBSOCKET_FRAME_PARSER_H_ | 6 #define NET_WEBSOCKETS_WEBSOCKET_FRAME_PARSER_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.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/base/net_export.h" | 14 #include "net/base/net_export.h" |
| 15 #include "net/websockets/websocket_errors.h" |
15 #include "net/websockets/websocket_frame.h" | 16 #include "net/websockets/websocket_frame.h" |
16 | 17 |
17 namespace net { | 18 namespace net { |
18 | 19 |
19 // Parses WebSocket frames from byte stream. | 20 // Parses WebSocket frames from byte stream. |
20 // | 21 // |
21 // Specification of WebSocket frame format is available at | 22 // Specification of WebSocket frame format is available at |
22 // <http://tools.ietf.org/html/rfc6455#section-5>. | 23 // <http://tools.ietf.org/html/rfc6455#section-5>. |
23 | 24 |
24 class NET_EXPORT_PRIVATE WebSocketFrameParser { | 25 class NET_EXPORT_PRIVATE WebSocketFrameParser { |
25 public: | 26 public: |
26 WebSocketFrameParser(); | 27 WebSocketFrameParser(); |
27 ~WebSocketFrameParser(); | 28 ~WebSocketFrameParser(); |
28 | 29 |
29 // Decodes the given byte stream and stores parsed WebSocket frames in | 30 // Decodes the given byte stream and stores parsed WebSocket frames in |
30 // |frame_chunks|. | 31 // |frame_chunks|. |
31 // | 32 // |
32 // If the parser encounters invalid payload length format, Decode() fails | 33 // If the parser encounters invalid payload length format, Decode() fails |
33 // and returns false. Once Decode() has failed, the parser refuses to decode | 34 // and returns false. Once Decode() has failed, the parser refuses to decode |
34 // any more data and future invocations of Decode() will simply return false. | 35 // any more data and future invocations of Decode() will simply return false. |
35 // | 36 // |
36 // Payload data of parsed WebSocket frames may be incomplete; see comments in | 37 // Payload data of parsed WebSocket frames may be incomplete; see comments in |
37 // websocket_frame.h for more details. | 38 // websocket_frame.h for more details. |
38 bool Decode(const char* data, | 39 bool Decode(const char* data, |
39 size_t length, | 40 size_t length, |
40 ScopedVector<WebSocketFrameChunk>* frame_chunks); | 41 ScopedVector<WebSocketFrameChunk>* frame_chunks); |
41 | 42 |
42 // Returns true if the parser has ever failed to decode a WebSocket frame. | 43 // Returns WEB_SOCKET_OK if the parser has not failed to decode WebSocket |
43 // TODO(yutak): Provide human-readable description of failure. | 44 // frames. Otherwise returns WebSocketError which is defined in |
44 bool failed() const { return failed_; } | 45 // websocket_errors.h. We can convert net::WebSocketError to net::Error by |
| 46 // using WebSocketErrorToNetError(). |
| 47 WebSocketError websocket_error() const { return websocket_error_; } |
45 | 48 |
46 private: | 49 private: |
47 // Tries to decode a frame header from |current_read_pos_|. | 50 // Tries to decode a frame header from |current_read_pos_|. |
48 // If successful, this function updates |current_read_pos_|, | 51 // If successful, this function updates |current_read_pos_|, |
49 // |current_frame_header_|, and |masking_key_| (if available). | 52 // |current_frame_header_|, and |masking_key_| (if available). |
50 // This function may set |failed_| to true if it observes a corrupt frame. | 53 // This function may set |failed_| to true if it observes a corrupt frame. |
51 // If there is not enough data in the remaining buffer to parse a frame | 54 // If there is not enough data in the remaining buffer to parse a frame |
52 // header, this function returns without doing anything. | 55 // header, this function returns without doing anything. |
53 void DecodeFrameHeader(); | 56 void DecodeFrameHeader(); |
54 | 57 |
(...skipping 12 matching lines...) Expand all Loading... |
67 size_t current_read_pos_; | 70 size_t current_read_pos_; |
68 | 71 |
69 // Frame header and masking key of the current frame. | 72 // Frame header and masking key of the current frame. |
70 // |masking_key_| is filled with zeros if the current frame is not masked. | 73 // |masking_key_| is filled with zeros if the current frame is not masked. |
71 scoped_ptr<WebSocketFrameHeader> current_frame_header_; | 74 scoped_ptr<WebSocketFrameHeader> current_frame_header_; |
72 char masking_key_[WebSocketFrameHeader::kMaskingKeyLength]; | 75 char masking_key_[WebSocketFrameHeader::kMaskingKeyLength]; |
73 | 76 |
74 // Amount of payload data read so far for the current frame. | 77 // Amount of payload data read so far for the current frame. |
75 uint64 frame_offset_; | 78 uint64 frame_offset_; |
76 | 79 |
77 bool failed_; | 80 WebSocketError websocket_error_; |
78 | 81 |
79 DISALLOW_COPY_AND_ASSIGN(WebSocketFrameParser); | 82 DISALLOW_COPY_AND_ASSIGN(WebSocketFrameParser); |
80 }; | 83 }; |
81 | 84 |
82 } // namespace net | 85 } // namespace net |
83 | 86 |
84 #endif // NET_WEBSOCKETS_WEBSOCKET_FRAME_PARSER_H_ | 87 #endif // NET_WEBSOCKETS_WEBSOCKET_FRAME_PARSER_H_ |
OLD | NEW |