• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 The Chromium Authors
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_FRAME_H_
6 #define NET_WEBSOCKETS_WEBSOCKET_FRAME_H_
7 
8 #include <stdint.h>
9 
10 #include <memory>
11 #include <vector>
12 
13 #include "base/containers/span.h"
14 #include "base/memory/scoped_refptr.h"
15 #include "net/base/net_export.h"
16 
17 namespace net {
18 
19 // Represents a WebSocket frame header.
20 //
21 // Members of this class correspond to each element in WebSocket frame header
22 // (see http://tools.ietf.org/html/rfc6455#section-5.2).
23 struct NET_EXPORT WebSocketFrameHeader {
24   typedef int OpCode;
25 
26   // Originally these constants were static const int, but to make it possible
27   // to use them in a switch statement they were changed to an enum.
28   enum OpCodeEnum {
29     kOpCodeContinuation = 0x0,
30     kOpCodeText = 0x1,
31     kOpCodeBinary = 0x2,
32     kOpCodeDataUnused = 0x3,
33     kOpCodeClose = 0x8,
34     kOpCodePing = 0x9,
35     kOpCodePong = 0xA,
36     kOpCodeControlUnused = 0xB,
37   };
38 
39   // Return true if |opcode| is one of the data opcodes known to this
40   // implementation.
IsKnownDataOpCodeWebSocketFrameHeader41   static bool IsKnownDataOpCode(OpCode opcode) {
42     return opcode == kOpCodeContinuation || opcode == kOpCodeText ||
43            opcode == kOpCodeBinary;
44   }
45 
46   // Return true if |opcode| is one of the control opcodes known to this
47   // implementation.
IsKnownControlOpCodeWebSocketFrameHeader48   static bool IsKnownControlOpCode(OpCode opcode) {
49     return opcode == kOpCodeClose || opcode == kOpCodePing ||
50            opcode == kOpCodePong;
51   }
52 
53   // These values must be compile-time constants.
54   static constexpr size_t kBaseHeaderSize = 2;
55   static constexpr size_t kMaximumExtendedLengthSize = 8;
56   static constexpr size_t kMaskingKeyLength = 4;
57 
58   // Contains four-byte data representing "masking key" of WebSocket frames.
59   struct WebSocketMaskingKey {
60     char key[WebSocketFrameHeader::kMaskingKeyLength];
61   };
62 
63   // Constructor to avoid a lot of repetitive initialisation.
WebSocketFrameHeaderWebSocketFrameHeader64   explicit WebSocketFrameHeader(OpCode opCode) : opcode(opCode) {}
65 
66   WebSocketFrameHeader(const WebSocketFrameHeader&) = delete;
67   WebSocketFrameHeader& operator=(const WebSocketFrameHeader&) = delete;
68 
69   // Create a clone of this object on the heap.
70   std::unique_ptr<WebSocketFrameHeader> Clone() const;
71 
72   // Overwrite this object with the fields from |source|.
73   void CopyFrom(const WebSocketFrameHeader& source);
74 
75   // Members below correspond to each item in WebSocket frame header.
76   // See <http://tools.ietf.org/html/rfc6455#section-5.2> for details.
77   bool final = false;
78   bool reserved1 = false;
79   bool reserved2 = false;
80   bool reserved3 = false;
81   OpCode opcode;
82   bool masked = false;
83   WebSocketMaskingKey masking_key = {};
84   uint64_t payload_length = 0;
85 };
86 
87 // Contains an entire WebSocket frame including payload. This is used by APIs
88 // that are not concerned about retaining the original frame boundaries (because
89 // frames may need to be split in order for the data to fit in memory).
90 struct NET_EXPORT_PRIVATE WebSocketFrame {
91   // A frame must always have an opcode, so this parameter is compulsory.
92   explicit WebSocketFrame(WebSocketFrameHeader::OpCode opcode);
93 
94   WebSocketFrame(const WebSocketFrame&) = delete;
95   WebSocketFrame& operator=(const WebSocketFrame&) = delete;
96 
97   ~WebSocketFrame();
98 
99   // |header| is always present.
100   WebSocketFrameHeader header;
101 
102   // |payload| is always unmasked even if the frame is masked. The size of
103   // |payload| is given by |header.payload_length|.
104   // The lifetime of |payload| is not defined by WebSocketFrameChunk. It is the
105   // responsibility of the creator to ensure it remains valid for the lifetime
106   // of this object. This should be documented in the code that creates this
107   // object.
108   // TODO(crbug.com/1001915): Find more better way to clarify the life cycle.
109   const char* payload = nullptr;
110 };
111 
112 // Structure describing one chunk of a WebSocket frame.
113 //
114 // The payload of a WebSocket frame may be divided into multiple chunks.
115 // You need to look at |final_chunk| member variable to detect the end of a
116 // series of chunk objects of a WebSocket frame.
117 //
118 // Frame dissection is necessary to handle frames that are too large to store in
119 // the browser memory without losing information about the frame boundaries. In
120 // practice, most code does not need to worry about the original frame
121 // boundaries and can use the WebSocketFrame type declared above.
122 //
123 // Users of this struct should treat WebSocket frames as a data stream; it's
124 // important to keep the frame data flowing, especially in the browser process.
125 // Users should not let the data stuck somewhere in the pipeline.
126 //
127 // This struct is used for reading WebSocket frame data (created by
128 // WebSocketFrameParser). To construct WebSocket frames, use functions below.
129 struct NET_EXPORT WebSocketFrameChunk {
130   WebSocketFrameChunk();
131 
132   WebSocketFrameChunk(const WebSocketFrameChunk&) = delete;
133   WebSocketFrameChunk& operator=(const WebSocketFrameChunk&) = delete;
134 
135   ~WebSocketFrameChunk();
136 
137   // Non-null |header| is provided only if this chunk is the first part of
138   // a series of chunks.
139   std::unique_ptr<WebSocketFrameHeader> header;
140 
141   // Indicates this part is the last chunk of a frame.
142   bool final_chunk = false;
143 
144   // |payload| is always unmasked even if the frame is masked. |payload| might
145   // be empty in the first chunk.
146   // The lifetime of |payload| is not defined by WebSocketFrameChunk. It is the
147   // responsibility of the creator to ensure it remains valid for the lifetime
148   // of this object. This should be documented in the code that creates this
149   // object.
150   // TODO(crbug.com/1001915): Find more better way to clarify the life cycle.
151   base::span<const char> payload;
152 };
153 
154 using WebSocketMaskingKey = WebSocketFrameHeader::WebSocketMaskingKey;
155 
156 // Returns the size of WebSocket frame header. The size of WebSocket frame
157 // header varies from 2 bytes to 14 bytes depending on the payload length
158 // and maskedness.
159 NET_EXPORT int GetWebSocketFrameHeaderSize(const WebSocketFrameHeader& header);
160 
161 // Writes wire format of a WebSocket frame header into |output|, and returns
162 // the number of bytes written.
163 //
164 // WebSocket frame format is defined at:
165 // <http://tools.ietf.org/html/rfc6455#section-5.2>. This function writes
166 // everything but payload data in a WebSocket frame to |buffer|.
167 //
168 // If |header->masked| is true, |masking_key| must point to a valid
169 // WebSocketMaskingKey object containing the masking key for that frame
170 // (possibly generated by GenerateWebSocketMaskingKey() function below).
171 // Otherwise, |masking_key| must be NULL.
172 //
173 // |buffer| should have enough size to contain the frame header.
174 // GetWebSocketFrameHeaderSize() can be used to know the size of header
175 // beforehand. If the size of |buffer| is insufficient, this function returns
176 // ERR_INVALID_ARGUMENT and does not write any data to |buffer|.
177 NET_EXPORT int WriteWebSocketFrameHeader(const WebSocketFrameHeader& header,
178                                          const WebSocketMaskingKey* masking_key,
179                                          char* buffer,
180                                          int buffer_size);
181 
182 // Generates a masking key suitable for use in a new WebSocket frame.
183 NET_EXPORT WebSocketMaskingKey GenerateWebSocketMaskingKey();
184 
185 // Masks WebSocket frame payload.
186 //
187 // A client must mask every WebSocket frame by XOR'ing the frame payload
188 // with four-byte random data (masking key). This function applies the masking
189 // to the given payload data.
190 //
191 // This function masks |data| with |masking_key|, assuming |data| is partial
192 // data starting from |frame_offset| bytes from the beginning of the payload
193 // data.
194 //
195 // Since frame masking is a reversible operation, this function can also be
196 // used for unmasking a WebSocket frame.
197 NET_EXPORT void MaskWebSocketFramePayload(
198     const WebSocketMaskingKey& masking_key,
199     uint64_t frame_offset,
200     char* data,
201     int data_size);
202 
203 }  // namespace net
204 
205 #endif  // NET_WEBSOCKETS_WEBSOCKET_FRAME_H_
206