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