| 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 | 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 "net/base/net_export.h" | 13 #include "net/base/net_export.h" |
| 14 | 14 |
| 15 namespace net { | 15 namespace net { |
| 16 | 16 |
| 17 class IOBufferWithSize; | 17 class IOBufferWithSize; |
| 18 | 18 |
| 19 // Represents a WebSocket frame header. | 19 // Represents a WebSocket frame header. |
| 20 // | 20 // |
| 21 // Members of this class correspond to each element in WebSocket frame header | 21 // Members of this class correspond to each element in WebSocket frame header |
| 22 // (see http://tools.ietf.org/html/rfc6455#section-5.2). | 22 // (see http://tools.ietf.org/html/rfc6455#section-5.2). |
| 23 struct NET_EXPORT WebSocketFrameHeader { | 23 struct NET_EXPORT WebSocketFrameHeader { |
| 24 typedef int OpCode; | 24 typedef int OpCode; |
| 25 static const OpCode kOpCodeContinuation; | |
| 26 static const OpCode kOpCodeText; | |
| 27 static const OpCode kOpCodeBinary; | |
| 28 static const OpCode kOpCodeDataUnused; | |
| 29 static const OpCode kOpCodeClose; | |
| 30 static const OpCode kOpCodePing; | |
| 31 static const OpCode kOpCodePong; | |
| 32 static const OpCode kOpCodeControlUnused; | |
| 33 | 25 |
| 34 // Efficiently determine whether a given opcode is one of the data opcodes | 26 // Originally these constants were static const int, but to make it possible |
| 35 // known to this implementation. | 27 // to use them in a switch statement they were changed to an enum. |
| 36 static bool IsKnownDataOpCode(OpCode opCode) { | 28 enum OpCodeEnum { |
| 37 return (opCode & ~3) == 0 && opCode != 3; | 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. |
| 41 static bool IsKnownDataOpCode(OpCode opcode) { |
| 42 return opcode == kOpCodeContinuation || opcode == kOpCodeText || |
| 43 opcode == kOpCodeBinary; |
| 38 } | 44 } |
| 39 | 45 |
| 40 // Efficiently determine whether a given opcode is one of the control opcodes | 46 // Return true if |opcode| is one of the control opcodes known to this |
| 41 // known to this implementation. | 47 // implementation. |
| 42 static bool IsKnownControlOpCode(OpCode opCode) { | 48 static bool IsKnownControlOpCode(OpCode opcode) { |
| 43 return (opCode & ~3) == 8 && opCode != 0x0B; | 49 return opcode == kOpCodeClose || opcode == kOpCodePing || |
| 50 opcode == kOpCodePong; |
| 44 } | 51 } |
| 45 | 52 |
| 46 // These values must be a compile-time constant. "enum hack" is used here | 53 // These values must be a compile-time constant. "enum hack" is used here |
| 47 // to make MSVC happy. | 54 // to make MSVC happy. |
| 48 enum { | 55 enum { |
| 49 kBaseHeaderSize = 2, | 56 kBaseHeaderSize = 2, |
| 50 kMaximumExtendedLengthSize = 8, | 57 kMaximumExtendedLengthSize = 8, |
| 51 kMaskingKeyLength = 4 | 58 kMaskingKeyLength = 4 |
| 52 }; | 59 }; |
| 53 | 60 |
| 54 // Constructor to avoid a lot of repetitive initialisation. | 61 // Constructor to avoid a lot of repetitive initialisation. |
| 55 explicit WebSocketFrameHeader(OpCode opCode) | 62 explicit WebSocketFrameHeader(OpCode opCode) |
| 56 : final(false), | 63 : final(false), |
| 57 reserved1(false), | 64 reserved1(false), |
| 58 reserved2(false), | 65 reserved2(false), |
| 59 reserved3(false), | 66 reserved3(false), |
| 60 opcode(opCode), | 67 opcode(opCode), |
| 61 masked(false), | 68 masked(false), |
| 62 payload_length(0) {} | 69 payload_length(0) {} |
| 63 | 70 |
| 64 // Create a clone of this object on the heap. | 71 // Create a clone of this object on the heap. |
| 65 scoped_ptr<WebSocketFrameHeader> Clone(); | 72 scoped_ptr<WebSocketFrameHeader> Clone(); |
| 66 | 73 |
| 67 // Members below correspond to each item in WebSocket frame header. | 74 // Members below correspond to each item in WebSocket frame header. |
| 68 // See <http://tools.ietf.org/html/rfc6455#section-5.2> for details. | 75 // See <http://tools.ietf.org/html/rfc6455#section-5.2> for details. |
| 69 bool final; | 76 bool final; |
| 70 bool reserved1; | 77 bool reserved1; |
| 71 bool reserved2; | 78 bool reserved2; |
| 72 bool reserved3; | 79 bool reserved3; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 }; | 119 }; |
| 113 | 120 |
| 114 // Contains four-byte data representing "masking key" of WebSocket frames. | 121 // Contains four-byte data representing "masking key" of WebSocket frames. |
| 115 struct WebSocketMaskingKey { | 122 struct WebSocketMaskingKey { |
| 116 char key[WebSocketFrameHeader::kMaskingKeyLength]; | 123 char key[WebSocketFrameHeader::kMaskingKeyLength]; |
| 117 }; | 124 }; |
| 118 | 125 |
| 119 // Returns the size of WebSocket frame header. The size of WebSocket frame | 126 // Returns the size of WebSocket frame header. The size of WebSocket frame |
| 120 // header varies from 2 bytes to 14 bytes depending on the payload length | 127 // header varies from 2 bytes to 14 bytes depending on the payload length |
| 121 // and maskedness. | 128 // and maskedness. |
| 122 NET_EXPORT int GetWebSocketFrameHeaderSize( | 129 NET_EXPORT int GetWebSocketFrameHeaderSize(const WebSocketFrameHeader& header); |
| 123 const WebSocketFrameHeader& header); | |
| 124 | 130 |
| 125 // Writes wire format of a WebSocket frame header into |output|, and returns | 131 // Writes wire format of a WebSocket frame header into |output|, and returns |
| 126 // the number of bytes written. | 132 // the number of bytes written. |
| 127 // | 133 // |
| 128 // WebSocket frame format is defined at: | 134 // WebSocket frame format is defined at: |
| 129 // <http://tools.ietf.org/html/rfc6455#section-5.2>. This function writes | 135 // <http://tools.ietf.org/html/rfc6455#section-5.2>. This function writes |
| 130 // everything but payload data in a WebSocket frame to |buffer|. | 136 // everything but payload data in a WebSocket frame to |buffer|. |
| 131 // | 137 // |
| 132 // If |header->masked| is true, |masking_key| must point to a valid | 138 // If |header->masked| is true, |masking_key| must point to a valid |
| 133 // WebSocketMaskingKey object containing the masking key for that frame | 139 // WebSocketMaskingKey object containing the masking key for that frame |
| 134 // (possibly generated by GenerateWebSocketMaskingKey() function below). | 140 // (possibly generated by GenerateWebSocketMaskingKey() function below). |
| 135 // Otherwise, |masking_key| must be NULL. | 141 // Otherwise, |masking_key| must be NULL. |
| 136 // | 142 // |
| 137 // |buffer| should have enough size to contain the frame header. | 143 // |buffer| should have enough size to contain the frame header. |
| 138 // GetWebSocketFrameHeaderSize() can be used to know the size of header | 144 // GetWebSocketFrameHeaderSize() can be used to know the size of header |
| 139 // beforehand. If the size of |buffer| is insufficient, this function returns | 145 // beforehand. If the size of |buffer| is insufficient, this function returns |
| 140 // ERR_INVALID_ARGUMENT and does not write any data to |buffer|. | 146 // ERR_INVALID_ARGUMENT and does not write any data to |buffer|. |
| 141 NET_EXPORT int WriteWebSocketFrameHeader( | 147 NET_EXPORT int WriteWebSocketFrameHeader(const WebSocketFrameHeader& header, |
| 142 const WebSocketFrameHeader& header, | 148 const WebSocketMaskingKey* masking_key, |
| 143 const WebSocketMaskingKey* masking_key, | 149 char* buffer, |
| 144 char* buffer, | 150 int buffer_size); |
| 145 int buffer_size); | |
| 146 | 151 |
| 147 // Generates a masking key suitable for use in a new WebSocket frame. | 152 // Generates a masking key suitable for use in a new WebSocket frame. |
| 148 NET_EXPORT WebSocketMaskingKey GenerateWebSocketMaskingKey(); | 153 NET_EXPORT WebSocketMaskingKey GenerateWebSocketMaskingKey(); |
| 149 | 154 |
| 150 // Masks WebSocket frame payload. | 155 // Masks WebSocket frame payload. |
| 151 // | 156 // |
| 152 // A client must mask every WebSocket frame by XOR'ing the frame payload | 157 // A client must mask every WebSocket frame by XOR'ing the frame payload |
| 153 // with four-byte random data (masking key). This function applies the masking | 158 // with four-byte random data (masking key). This function applies the masking |
| 154 // to the given payload data. | 159 // to the given payload data. |
| 155 // | 160 // |
| 156 // This function masks |data| with |masking_key|, assuming |data| is partial | 161 // This function masks |data| with |masking_key|, assuming |data| is partial |
| 157 // data starting from |frame_offset| bytes from the beginning of the payload | 162 // data starting from |frame_offset| bytes from the beginning of the payload |
| 158 // data. | 163 // data. |
| 159 // | 164 // |
| 160 // Since frame masking is a reversible operation, this function can also be | 165 // Since frame masking is a reversible operation, this function can also be |
| 161 // used for unmasking a WebSocket frame. | 166 // used for unmasking a WebSocket frame. |
| 162 NET_EXPORT void MaskWebSocketFramePayload( | 167 NET_EXPORT void MaskWebSocketFramePayload( |
| 163 const WebSocketMaskingKey& masking_key, | 168 const WebSocketMaskingKey& masking_key, |
| 164 uint64 frame_offset, | 169 uint64 frame_offset, |
| 165 char* data, | 170 char* data, |
| 166 int data_size); | 171 int data_size); |
| 167 | 172 |
| 168 } // namespace net | 173 } // namespace net |
| 169 | 174 |
| 170 #endif // NET_WEBSOCKETS_WEBSOCKET_FRAME_H_ | 175 #endif // NET_WEBSOCKETS_WEBSOCKET_FRAME_H_ |
| OLD | NEW |