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_H_ | 5 #ifndef NET_WEBSOCKETS_WEBSOCKET_FRAME_H_ |
6 #define NET_WEBSOCKETS_WEBSOCKET_FRAME_H_ | 6 #define NET_WEBSOCKETS_WEBSOCKET_FRAME_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 // series of chunk objects of a WebSocket frame. | 53 // series of chunk objects of a WebSocket frame. |
54 // | 54 // |
55 // Frame dissection is necessary to handle WebSocket frame stream containing | 55 // Frame dissection is necessary to handle WebSocket frame stream containing |
56 // abritrarily large frames in the browser process. Because the server may send | 56 // abritrarily large frames in the browser process. Because the server may send |
57 // a huge frame that doesn't fit in the memory, we cannot store the entire | 57 // a huge frame that doesn't fit in the memory, we cannot store the entire |
58 // payload data in the memory. | 58 // payload data in the memory. |
59 // | 59 // |
60 // Users of this struct should treat WebSocket frames as a data stream; it's | 60 // Users of this struct should treat WebSocket frames as a data stream; it's |
61 // important to keep the frame data flowing, especially in the browser process. | 61 // important to keep the frame data flowing, especially in the browser process. |
62 // Users should not let the data stuck somewhere in the pipeline. | 62 // Users should not let the data stuck somewhere in the pipeline. |
| 63 // |
| 64 // This struct is used for reading WebSocket frame data (created by |
| 65 // WebSocketFrameParser). To construct WebSocket frames, use functions below. |
63 struct NET_EXPORT_PRIVATE WebSocketFrameChunk { | 66 struct NET_EXPORT_PRIVATE WebSocketFrameChunk { |
64 WebSocketFrameChunk(); | 67 WebSocketFrameChunk(); |
65 ~WebSocketFrameChunk(); | 68 ~WebSocketFrameChunk(); |
66 | 69 |
67 // Non-null |header| is provided only if this chunk is the first part of | 70 // Non-null |header| is provided only if this chunk is the first part of |
68 // a series of chunks. | 71 // a series of chunks. |
69 scoped_ptr<WebSocketFrameHeader> header; | 72 scoped_ptr<WebSocketFrameHeader> header; |
70 | 73 |
71 // Indicates this part is the last chunk of a frame. | 74 // Indicates this part is the last chunk of a frame. |
72 bool final_chunk; | 75 bool final_chunk; |
73 | 76 |
74 // |data| is always unmasked even if the frame is masked. | 77 // |data| is always unmasked even if the frame is masked. |
75 std::vector<char> data; | 78 std::vector<char> data; |
76 }; | 79 }; |
77 | 80 |
| 81 // Contains four-byte data representing "masking key" of WebSocket frames. |
| 82 struct WebSocketMaskingKey { |
| 83 char key[WebSocketFrameHeader::kMaskingKeyLength]; |
| 84 }; |
| 85 |
| 86 // Writes wire format of a WebSocket frame header into |output|, and returns |
| 87 // the number of bytes written. |
| 88 // |
| 89 // WebSocket frame format is defined at: |
| 90 // <http://tools.ietf.org/html/rfc6455#section-5.2>. This function writes |
| 91 // everything but payload data in a WebSocket frame to |buffer|. |
| 92 // |
| 93 // If |header->masked| is true, |masking_key| must point to a valid |
| 94 // WebSocketMaskingKey object containing the masking key for that frame |
| 95 // (possibly generated by GenerateWebSocketMaskingKey() function below). |
| 96 // Otherwise, |masking_key| must be NULL. |
| 97 // |
| 98 // |buffer| should have enough size to contain the frame header. Size of a |
| 99 // frame header varies from 2 bytes to 14 bytes depending on the payload length |
| 100 // and maskedness. If the size of |buffer| is insufficient, this function |
| 101 // returns ERR_INVALID_ARGUMENT and does not write any data to |buffer|. |
| 102 NET_EXPORT_PRIVATE int WriteWebSocketFrameHeader( |
| 103 const WebSocketFrameHeader& header, |
| 104 const WebSocketMaskingKey* masking_key, |
| 105 char* buffer, |
| 106 int buffer_size); |
| 107 |
| 108 // Generates a masking key suitable for use in a new WebSocket frame. |
| 109 NET_EXPORT_PRIVATE WebSocketMaskingKey GenerateWebSocketMaskingKey(); |
| 110 |
| 111 // Masks WebSocket frame payload. |
| 112 // |
| 113 // A client must mask every WebSocket frame by XOR'ing the frame payload |
| 114 // with four-byte random data (masking key). This function applies the masking |
| 115 // to the given payload data. |
| 116 // |
| 117 // This function masks |data| with |masking_key|, assuming |data| is partial |
| 118 // data starting from |frame_offset| bytes from the beginning of the payload |
| 119 // data. |
| 120 // |
| 121 // Since frame masking is a reversible operation, this function can also be |
| 122 // used for unmasking a WebSocket frame. |
| 123 NET_EXPORT_PRIVATE void MaskWebSocketFramePayload( |
| 124 const WebSocketMaskingKey& masking_key, |
| 125 uint64 frame_offset, |
| 126 char* data, |
| 127 int data_size); |
| 128 |
78 } // namespace net | 129 } // namespace net |
79 | 130 |
80 #endif // NET_WEBSOCKETS_WEBSOCKET_FRAME_H_ | 131 #endif // NET_WEBSOCKETS_WEBSOCKET_FRAME_H_ |
OLD | NEW |