Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(150)

Side by Side Diff: net/websockets/websocket_frame.h

Issue 10384180: Add functions used for building WebSocket frame data. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Don't take std::vector as arguments. Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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|.
87 //
88 // WebSocket frame format is defined at:
89 // <http://tools.ietf.org/html/rfc6455#section-5.2>. This function writes
90 // everything but payload data in a WebSocket frame to |buffer|.
91 //
92 // If |header->masked| is true, |masking_key| must point to a valid
93 // WebSocketMaskingKey object containing the masking key for that frame
94 // (possibly generated by GenerateWebSocketMaskingKey() function below).
95 // Otherwise, |masking_key| must be NULL.
96 //
97 // |buffer| should have enough size to contain the frame header. Size of a
98 // frame header varies from 2 bytes to 14 bytes depending on the payload length
99 // and maskedness. If the size of |buffer| is insufficient, this function
100 // returns false without writing any data to |buffer|.
101 NET_EXPORT_PRIVATE bool WriteWebSocketFrameHeader(
102 const WebSocketFrameHeader& header,
103 const WebSocketMaskingKey* masking_key,
104 char* buffer,
105 size_t buffer_size);
mmenke 2012/06/06 23:56:28 I believe what Will was asking for was more along
Yuta Kitamura 2012/06/07 08:33:30 Yeah, the number of bytes is definitely necessary.
106
107 // Generates a masking key suitable for use in a new WebSocket frame.
108 NET_EXPORT_PRIVATE WebSocketMaskingKey GenerateWebSocketMaskingKey();
109
110 // Masks WebSocket frame payload.
111 //
112 // A client must mask every WebSocket frame by XOR'ing the frame payload
113 // with four-byte random data (masking key). This function applies the masking
114 // to the given payload data.
115 //
116 // This function masks |data| with |masking_key|, assuming |data| is partial
117 // data starting from |frame_offset| bytes from the beginning of the payload
118 // data.
119 //
120 // Since frame masking is a reversible operation, this function can also be
121 // used for unmasking a WebSocket frame.
122 NET_EXPORT_PRIVATE void MaskWebSocketFramePayload(
123 const WebSocketMaskingKey& masking_key,
124 uint64 frame_offset,
125 char* data,
126 size_t data_size);
127
78 } // namespace net 128 } // namespace net
79 129
80 #endif // NET_WEBSOCKETS_WEBSOCKET_FRAME_H_ 130 #endif // NET_WEBSOCKETS_WEBSOCKET_FRAME_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698