Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 class _WebSocketMessageType { | 5 class _WebSocketMessageType { |
| 6 static final int NONE = 0; | 6 static final int NONE = 0; |
| 7 static final int BINARY = 1; | 7 static final int BINARY = 1; |
| 8 static final int TEXT = 2; | 8 static final int TEXT = 2; |
| 9 static final int CLOSE = 3; | 9 static final int CLOSE = 3; |
| 10 } | 10 } |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 56 _currentMessageType = _WebSocketMessageType.NONE; | 56 _currentMessageType = _WebSocketMessageType.NONE; |
| 57 } | 57 } |
| 58 | 58 |
| 59 /** | 59 /** |
| 60 * Process data received from the underlying communication channel. | 60 * Process data received from the underlying communication channel. |
| 61 */ | 61 */ |
| 62 void update(List<int> buffer, int offset, int count) { | 62 void update(List<int> buffer, int offset, int count) { |
| 63 int index = offset; | 63 int index = offset; |
| 64 int lastIndex = offset + count; | 64 int lastIndex = offset + count; |
| 65 try { | 65 try { |
| 66 if (_state == _State.CLOSED) { | 66 if (_state == CLOSED) { |
| 67 throw new WebSocketException("Data on closed connection"); | 67 throw new WebSocketException("Data on closed connection"); |
| 68 } | 68 } |
| 69 if (_state == _State.FAILURE) { | 69 if (_state == FAILURE) { |
| 70 throw new WebSocketException("Data on failed connection"); | 70 throw new WebSocketException("Data on failed connection"); |
| 71 } | 71 } |
| 72 while ((index < lastIndex) && _state != CLOSED && _state != FAILURE) { | 72 while ((index < lastIndex) && _state != CLOSED && _state != FAILURE) { |
| 73 int byte = buffer[index]; | 73 int byte = buffer[index]; |
| 74 switch (_state) { | 74 switch (_state) { |
| 75 case START: | 75 case START: |
| 76 _fin = (byte & 0x80) != 0; | 76 _fin = (byte & 0x80) != 0; |
| 77 _opcode = (byte & 0xF); | 77 _opcode = (byte & 0xF); |
| 78 switch (_opcode) { | 78 switch (_opcode) { |
| 79 case _WebSocketOpcode.CONTINUATION: | 79 case _WebSocketOpcode.CONTINUATION: |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 123 } | 123 } |
| 124 _state = LEN_FIRST; | 124 _state = LEN_FIRST; |
| 125 break; | 125 break; |
| 126 | 126 |
| 127 case LEN_FIRST: | 127 case LEN_FIRST: |
| 128 _masked = (byte & 0x80) != 0; | 128 _masked = (byte & 0x80) != 0; |
| 129 _len = byte & 0x7F; | 129 _len = byte & 0x7F; |
| 130 if (_len < 126) { | 130 if (_len < 126) { |
| 131 _lengthDone(); | 131 _lengthDone(); |
| 132 } else if (_len == 126) { | 132 } else if (_len == 126) { |
| 133 _ len = 0; | 133 _len = 0; |
| 134 _remainingLenBytes = 2; | 134 _remainingLenBytes = 2; |
| 135 _state = LEN_REST; | |
| 135 } else if (_len == 127) { | 136 } else if (_len == 127) { |
| 136 _ len = 0; | 137 _len = 0; |
| 137 _remainingLenBytes = 8; | 138 _remainingLenBytes = 8; |
| 139 _state = LEN_REST; | |
| 138 } | 140 } |
| 139 break; | 141 break; |
| 140 | 142 |
| 141 case LEN_REST: | 143 case LEN_REST: |
| 142 _len = _len << 8 | byte; | 144 _len = _len << 8 | byte; |
| 143 _remainingLenBytes--; | 145 _remainingLenBytes--; |
| 144 if (_remainingLenBytes == 0) { | 146 if (_remainingLenBytes == 0) { |
| 145 _lengthDone(); | 147 _lengthDone(); |
| 146 } | 148 } |
| 147 break; | 149 break; |
| 148 | 150 |
| 149 case MASK: | 151 case MASK: |
| 150 _maskingKey = _maskingKey << 8 | byte; | 152 _maskingKey = _maskingKey << 8 | byte; |
| 151 _remainingMaskingKeyBytes--; | 153 _remainingMaskingKeyBytes--; |
| 152 if (_remainingMaskingKeyBytes == 0) { | 154 if (_remainingMaskingKeyBytes == 0) { |
| 153 _maskDone(); | 155 _maskDone(); |
| 154 } | 156 } |
| 155 break; | 157 break; |
| 156 | 158 |
| 157 case PAYLOAD: | 159 case PAYLOAD: |
| 158 // The payload is not handled one byte at the time but in blocks. | 160 // The payload is not handled one byte at the time but in blocks. |
| 159 int payload; | 161 int payload; |
| 160 if (lastIndex - index >= _remainingPayloadBytes) { | 162 if (lastIndex - index <= _remainingPayloadBytes) { |
| 161 payload = lastIndex - index; | 163 payload = lastIndex - index; |
| 162 } else { | 164 } else { |
| 163 payload = _remainingPayloadBytes; | 165 payload = _remainingPayloadBytes; |
| 164 } | 166 } |
| 165 // Unmask payload if masked. | 167 // Unmask payload if masked. |
| 166 if (_masked) { | 168 if (_masked) { |
| 167 for (int i = 0; i < payload; i++) { | 169 for (int i = 0; i < payload; i++) { |
| 168 int maskingByte = | 170 int maskingByte = |
| 169 ((_maskingKey >> ((3 - _unmaskingIndex) * 8)) & 0xFF); | 171 ((_maskingKey >> ((3 - _unmaskingIndex) * 8)) & 0xFF); |
| 170 buffer[index + i] = buffer[index + i] ^ maskingByte; | 172 buffer[index + i] = buffer[index + i] ^ maskingByte; |
| 171 _unmaskingIndex = (_unmaskingIndex + 1) % 4; | 173 _unmaskingIndex = (_unmaskingIndex + 1) % 4; |
| 172 } | 174 } |
| 173 } | 175 } |
| 174 | 176 |
| 175 switch (_currentMessageType) { | 177 switch (_currentMessageType) { |
| 176 case _WebSocketMessageType.NONE: | 178 case _WebSocketMessageType.NONE: |
| 177 throw new WebSocketException("Protocol error"); | 179 throw new WebSocketException("Protocol error"); |
| 178 break; | 180 break; |
| 179 | 181 |
| 180 case _WebSocketMessageType.TEXT: | 182 case _WebSocketMessageType.TEXT: |
| 181 case _WebSocketMessageType.BINARY: | 183 case _WebSocketMessageType.BINARY: |
| 182 if (onMessageData != null) { | 184 if (onMessageData != null) { |
| 183 onMessageData(buffer, index, payload); | 185 onMessageData(buffer, index, payload); |
| 184 } | 186 } |
| 185 _remainingPayloadBytes -= payload; | 187 _remainingPayloadBytes -= payload; |
| 186 index += payload; | 188 index += payload; |
| 187 if (_fin) { | 189 if (_remainingPayloadBytes == 0) { |
| 188 _messageEnd(); | 190 _frameEnd(); |
| 189 } | 191 } |
| 190 break; | 192 break; |
| 191 | 193 |
| 192 case _WebSocketMessageType.CLOSE: | 194 case _WebSocketMessageType.CLOSE: |
| 193 // Allocate a buffer for holding the close payload if any. | 195 // Allocate a buffer for holding the close payload if any. |
| 194 if (_closePayload == null) { | 196 if (_closePayload == null) { |
| 195 _closePayload = new List<int>(); | 197 _closePayload = new List<int>(); |
| 196 } | 198 } |
| 197 _closePayload.addAll(buffer.getRange(index, payload)); | 199 _closePayload.addAll(buffer.getRange(index, payload)); |
| 198 _remainingPayloadBytes -= payload; | 200 _remainingPayloadBytes -= payload; |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 260 _startPayload(); | 262 _startPayload(); |
| 261 } | 263 } |
| 262 } | 264 } |
| 263 | 265 |
| 264 void _maskDone() { | 266 void _maskDone() { |
| 265 _remainingPayloadBytes = _len; | 267 _remainingPayloadBytes = _len; |
| 266 _startPayload(); | 268 _startPayload(); |
| 267 } | 269 } |
| 268 | 270 |
| 269 void _startPayload() { | 271 void _startPayload() { |
| 270 // Check whether there is any payload. If not indicate empty message or | 272 // Check whether there is any payload. If not indicate empty message or |
|
Mads Ager (google)
2012/04/25 10:47:46
Finish comment. :)
Søren Gjesse
2012/04/27 11:58:24
Done.
| |
| 271 if (_remainingPayloadBytes == 0) { | 273 if (_remainingPayloadBytes == 0) { |
| 272 if (_currentMessageType ==_WebSocketMessageType.CLOSE) { | 274 if (_currentMessageType ==_WebSocketMessageType.CLOSE) { |
| 273 if (onClosed != null) onClosed(null, null); | 275 if (onClosed != null) onClosed(null, null); |
| 274 } else { | 276 } else { |
| 275 _messageEnd(); | 277 _frameEnd(); |
| 276 } | 278 } |
| 277 } else { | 279 } else { |
| 278 _state = PAYLOAD; | 280 _state = PAYLOAD; |
| 279 } | 281 } |
| 280 } | 282 } |
| 281 | 283 |
| 282 void _messageEnd() { | 284 void _frameEnd() { |
| 283 if (_remainingPayloadBytes != 0) { | 285 if (_remainingPayloadBytes != 0) { |
| 284 throw new WebSocketException("Protocol error"); | 286 throw new WebSocketException("Protocol error"); |
| 285 } | 287 } |
| 286 if (onMessageEnd != null) onMessageEnd(); | 288 if (_fin) { |
| 287 _currentMessageType = _WebSocketMessageType.NONE; | 289 if (onMessageEnd != null) onMessageEnd(); |
| 290 _currentMessageType = _WebSocketMessageType.NONE; | |
| 291 } | |
| 288 _reset(); | 292 _reset(); |
| 289 } | 293 } |
| 290 | 294 |
| 291 void _reset() { | 295 void _reset() { |
| 292 _state = START; | 296 _state = START; |
| 293 _fin = null; | 297 _fin = null; |
| 294 _opcode = null; | 298 _opcode = null; |
| 295 _len = null; | 299 _len = null; |
| 296 _masked = null; | 300 _masked = null; |
| 297 _maskingKey = 0; | 301 _maskingKey = 0; |
| 298 _remainingLenBytes = null; | 302 _remainingLenBytes = null; |
| 299 _remainingMaskingKeyBytes = null; | 303 _remainingMaskingKeyBytes = null; |
| 300 _remainingPayloadBytes = null; | 304 _remainingPayloadBytes = null; |
| 301 _unmaskingIndex = 0; | 305 _unmaskingIndex = 0; |
| 302 } | 306 } |
| 303 | 307 |
| 304 void _reportError(e) { | 308 void _reportError(e) { |
| 305 // Report the error through the error callback if any. Otherwise | 309 // Report the error through the error callback if any. Otherwise |
| 306 // throw the error. | 310 // throw the error. |
| 307 if (onError != null) { | 311 if (onError != null) { |
| 308 onError(e); | 312 onError(e); |
| 309 _state = _State.FAILURE; | 313 _state = FAILURE; |
| 310 } else { | 314 } else { |
| 311 throw e; | 315 throw e; |
| 312 } | 316 } |
| 313 } | 317 } |
| 314 | 318 |
| 315 int _state; | 319 int _state; |
| 316 bool _fin; | 320 bool _fin; |
| 317 int _opcode; | 321 int _opcode; |
| 318 int _len; | 322 int _len; |
| 319 bool _masked; | 323 bool _masked; |
| (...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 582 } | 586 } |
| 583 String key = request.headers.value("Sec-WebSocket-Key"); | 587 String key = request.headers.value("Sec-WebSocket-Key"); |
| 584 if (key == null) { | 588 if (key == null) { |
| 585 return false; | 589 return false; |
| 586 } | 590 } |
| 587 return true; | 591 return true; |
| 588 } | 592 } |
| 589 | 593 |
| 590 Function _onConnection; | 594 Function _onConnection; |
| 591 } | 595 } |
| OLD | NEW |