OLD | NEW |
1 // | 1 // |
2 // Copyright 2012 Square Inc. | 2 // Copyright 2012 Square Inc. |
3 // | 3 // |
4 // Licensed under the Apache License, Version 2.0 (the "License"); | 4 // Licensed under the Apache License, Version 2.0 (the "License"); |
5 // you may not use this file except in compliance with the License. | 5 // you may not use this file except in compliance with the License. |
6 // You may obtain a copy of the License at | 6 // You may obtain a copy of the License at |
7 // | 7 // |
8 // http://www.apache.org/licenses/LICENSE-2.0 | 8 // http://www.apache.org/licenses/LICENSE-2.0 |
9 // | 9 // |
10 // Unless required by applicable law or agreed to in writing, software | 10 // Unless required by applicable law or agreed to in writing, software |
(...skipping 486 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
497 | 497 |
498 - (void)didConnect | 498 - (void)didConnect |
499 { | 499 { |
500 SRFastLog(@"Connected"); | 500 SRFastLog(@"Connected"); |
501 CFHTTPMessageRef request = CFHTTPMessageCreateRequest(NULL, CFSTR("GET"), (_
_bridge CFURLRef)_url, kCFHTTPVersion1_1); | 501 CFHTTPMessageRef request = CFHTTPMessageCreateRequest(NULL, CFSTR("GET"), (_
_bridge CFURLRef)_url, kCFHTTPVersion1_1); |
502 | 502 |
503 // Set host first so it defaults | 503 // Set host first so it defaults |
504 CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Host"), (__bridge CFStringR
ef)(_url.port ? [NSString stringWithFormat:@"%@:%@", _url.host, _url.port] : _ur
l.host)); | 504 CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Host"), (__bridge CFStringR
ef)(_url.port ? [NSString stringWithFormat:@"%@:%@", _url.host, _url.port] : _ur
l.host)); |
505 | 505 |
506 NSMutableData *keyBytes = [[NSMutableData alloc] initWithLength:16]; | 506 NSMutableData *keyBytes = [[NSMutableData alloc] initWithLength:16]; |
507 SecRandomCopyBytes(kSecRandomDefault, keyBytes.length, keyBytes.mutableBytes
); | 507 assert(!SecRandomCopyBytes(kSecRandomDefault, keyBytes.length, keyBytes.muta
bleBytes)); |
508 | 508 |
509 if ([keyBytes respondsToSelector:@selector(base64EncodedStringWithOptions:)]
) { | 509 if ([keyBytes respondsToSelector:@selector(base64EncodedStringWithOptions:)]
) { |
510 _secKey = [keyBytes base64EncodedStringWithOptions:0]; | 510 _secKey = [keyBytes base64EncodedStringWithOptions:0]; |
511 } else { | 511 } else { |
512 _secKey = [keyBytes base64Encoding]; | 512 _secKey = [keyBytes base64Encoding]; |
513 } | 513 } |
514 | 514 |
515 assert([_secKey length] == 24); | 515 assert([_secKey length] == 24); |
516 | 516 |
517 CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Upgrade"), CFSTR("websocket
")); | 517 CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Upgrade"), CFSTR("websocket
")); |
518 CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Connection"), CFSTR("Upgrad
e")); | 518 CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Connection"), CFSTR("Upgrad
e")); |
519 CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Sec-WebSocket-Key"), (__bri
dge CFStringRef)_secKey); | 519 CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Sec-WebSocket-Key"), (__bri
dge CFStringRef)_secKey); |
520 CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Sec-WebSocket-Version"), (_
_bridge CFStringRef)[NSString stringWithFormat:@"%ld", (long)_webSocketVersion])
; | 520 CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Sec-WebSocket-Version"), (_
_bridge CFStringRef)[NSString stringWithFormat:@"%ld", (long)_webSocketVersion])
; |
521 | 521 |
522 CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Origin"), (__bridge CFStrin
gRef)_url.SR_origin); | 522 CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Origin"), (__bridge CFStrin
gRef)_url.SR_origin); |
523 | 523 |
524 if (_requestedProtocols) { | 524 if (_requestedProtocols) { |
525 CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Sec-WebSocket-Protocol"
), (__bridge CFStringRef)[_requestedProtocols componentsJoinedByString:@", "]); | 525 CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Sec-WebSocket-Protocol"
), (__bridge CFStringRef)[_requestedProtocols componentsJoinedByString:@", "]); |
526 } | 526 } |
527 | 527 |
528 [_urlRequest.allHTTPHeaderFields enumerateKeysAndObjectsUsingBlock:^(id key,
id obj, BOOL *stop) { | 528 [_urlRequest.allHTTPHeaderFields enumerateKeysAndObjectsUsingBlock:^(id key,
id obj, BOOL *stop) { |
529 CFHTTPMessageSetHeaderFieldValue(request, (__bridge CFStringRef)key, (__
bridge CFStringRef)obj); | 529 CFHTTPMessageSetHeaderFieldValue(request, (__bridge CFStringRef)key, (__
bridge CFStringRef)obj); |
530 }]; | 530 }]; |
531 | 531 |
532 NSData *message = CFBridgingRelease(CFHTTPMessageCopySerializedMessage(reque
st)); | 532 NSData *message = CFBridgingRelease(CFHTTPMessageCopySerializedMessage(reque
st)); |
533 | 533 |
534 CFRelease(request); | 534 CFRelease(request); |
535 | 535 |
536 [self _writeData:message]; | 536 [self _writeData:message]; |
537 [self _readHTTPHeader]; | 537 [self _readHTTPHeader]; |
538 } | 538 } |
539 | 539 |
540 - (void)_initializeStreams; | 540 - (void)_initializeStreams; |
541 { | 541 { |
(...skipping 812 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1354 frame_buffer_size += sizeof(uint64_t); | 1354 frame_buffer_size += sizeof(uint64_t); |
1355 } | 1355 } |
1356 | 1356 |
1357 if (!useMask) { | 1357 if (!useMask) { |
1358 for (size_t i = 0; i < payloadLength; i++) { | 1358 for (size_t i = 0; i < payloadLength; i++) { |
1359 frame_buffer[frame_buffer_size] = unmasked_payload[i]; | 1359 frame_buffer[frame_buffer_size] = unmasked_payload[i]; |
1360 frame_buffer_size += 1; | 1360 frame_buffer_size += 1; |
1361 } | 1361 } |
1362 } else { | 1362 } else { |
1363 uint8_t *mask_key = frame_buffer + frame_buffer_size; | 1363 uint8_t *mask_key = frame_buffer + frame_buffer_size; |
1364 SecRandomCopyBytes(kSecRandomDefault, sizeof(uint32_t), (uint8_t *)mask_
key); | 1364 assert(!SecRandomCopyBytes(kSecRandomDefault, sizeof(uint32_t), (uint8_t
*)mask_key)); |
1365 frame_buffer_size += sizeof(uint32_t); | 1365 frame_buffer_size += sizeof(uint32_t); |
1366 | 1366 |
1367 // TODO: could probably optimize this with SIMD | 1367 // TODO: could probably optimize this with SIMD |
1368 for (size_t i = 0; i < payloadLength; i++) { | 1368 for (size_t i = 0; i < payloadLength; i++) { |
1369 frame_buffer[frame_buffer_size] = unmasked_payload[i] ^ mask_key[i %
sizeof(uint32_t)]; | 1369 frame_buffer[frame_buffer_size] = unmasked_payload[i] ^ mask_key[i %
sizeof(uint32_t)]; |
1370 frame_buffer_size += 1; | 1370 frame_buffer_size += 1; |
1371 } | 1371 } |
1372 } | 1372 } |
1373 | 1373 |
1374 assert(frame_buffer_size <= [frame length]); | 1374 assert(frame_buffer_size <= [frame length]); |
(...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1752 } | 1752 } |
1753 } | 1753 } |
1754 | 1754 |
1755 - (NSRunLoop *)runLoop; | 1755 - (NSRunLoop *)runLoop; |
1756 { | 1756 { |
1757 dispatch_group_wait(_waitGroup, DISPATCH_TIME_FOREVER); | 1757 dispatch_group_wait(_waitGroup, DISPATCH_TIME_FOREVER); |
1758 return _runLoop; | 1758 return _runLoop; |
1759 } | 1759 } |
1760 | 1760 |
1761 @end | 1761 @end |
OLD | NEW |