Skip to content

Commit 4270285

Browse files
committed
Revert "Started implementing WebSocket protocol draft-hixie-thewebsocketprotocol-76"
This reverts commit 98ef436.
1 parent 797cb50 commit 4270285

File tree

1 file changed

+11
-66
lines changed

1 file changed

+11
-66
lines changed

WebSocket.m

Lines changed: 11 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,6 @@
1818
WebSocketTagMessage = 1
1919
};
2020

21-
long MAX_INT = 4294967295;
22-
23-
#define HANDSHAKE_REQUEST \
24-
@"GET %@ HTTP/1.1\r\n" \
25-
"Upgrade: WebSocket\r\n" \
26-
"Connection: Upgrade\r\n" \
27-
"Sec-WebSocket-Protocol: sample\r\n" \
28-
"Sec-WebSocket-Key1: %@\r\n" \
29-
"Sec-WebSocket-Key2: %@\r\n" \
30-
"Host: %@\r\n" \
31-
"Origin: %@\r\n" \
32-
"\r\n" \
33-
"%@"
34-
3521
@implementation WebSocket
3622

3723
@synthesize delegate, url, origin, connected, runLoopModes;
@@ -94,33 +80,6 @@ -(void)_readNextMessage {
9480
[socket readDataToData:[NSData dataWithBytes:"\xFF" length:1] withTimeout:-1 tag:WebSocketTagMessage];
9581
}
9682

97-
-(NSString *)_makeKey {
98-
int i, spaces;
99-
long num, prod;
100-
unichar letter;
101-
102-
spaces = (arc4random() % 12) + 1;
103-
num = arc4random() % (MAX_INT/spaces);
104-
prod = spaces * num;
105-
106-
NSMutableString *key = [NSMutableString stringWithFormat:@"%ld", prod];
107-
108-
for (i=0; i<12; i++) {
109-
110-
if ((arc4random() % 2) == 0)
111-
letter = (arc4random() % (64 - 33 + 1)) + 33;
112-
else
113-
letter = (arc4random() % (126 - 58 + 1)) + 58;
114-
115-
[key insertString:[[[NSString alloc] initWithCharacters:&letter length:1] autorelease] atIndex:(arc4random() % 11)];
116-
}
117-
118-
for (i=0; i<spaces; i++)
119-
[key insertString:@" " atIndex:((arc4random() % 22)+1)];
120-
121-
return key;
122-
}
123-
12483
#pragma mark Public interface
12584

12685
-(void)close {
@@ -164,30 +123,19 @@ - (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UIn
164123
if (url.query) {
165124
requestPath = [requestPath stringByAppendingFormat:@"?%@", url.query];
166125
}
167-
168-
NSString *key1 = [self _makeKey];
169-
NSString *key2 = [self _makeKey];
170-
NSMutableString *key3 = [NSMutableString string];
171-
172-
for (int i=0; i<8; i++) {
173-
unichar letter = arc4random() % 126;
174-
[key3 appendString:[[[NSString alloc] initWithCharacters:&letter length:1] autorelease]];
175-
}
176-
177-
NSString *request = [NSString stringWithFormat:HANDSHAKE_REQUEST,
178-
requestPath,
179-
key1,
180-
key2,
181-
url.host,
182-
requestOrigin,
183-
key3];
184-
185-
[socket writeData:[request dataUsingEncoding:NSASCIIStringEncoding] withTimeout:-1 tag:WebSocketTagHandshake];
126+
NSString* getRequest = [NSString stringWithFormat:@"GET %@ HTTP/1.1\r\n"
127+
"Upgrade: WebSocket\r\n"
128+
"Connection: Upgrade\r\n"
129+
"Host: %@\r\n"
130+
"Origin: %@\r\n"
131+
"\r\n",
132+
requestPath,url.host,requestOrigin];
133+
[socket writeData:[getRequest dataUsingEncoding:NSASCIIStringEncoding] withTimeout:-1 tag:WebSocketTagHandshake];
186134
}
187135

188136
-(void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag {
189137
if (tag == WebSocketTagHandshake) {
190-
[sock readDataWithTimeout:-1 tag:WebSocketTagHandshake];
138+
[sock readDataToData:[@"\r\n\r\n" dataUsingEncoding:NSASCIIStringEncoding] withTimeout:-1 tag:WebSocketTagHandshake];
191139
} else if (tag == WebSocketTagMessage) {
192140
[self _dispatchMessageSent];
193141
}
@@ -196,13 +144,10 @@ -(void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag {
196144
-(void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag {
197145
if (tag == WebSocketTagHandshake) {
198146
NSString* response = [[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding] autorelease];
199-
// TODO: Better way for matching WebSocket Protocol Handshake response
200-
if ([response hasPrefix:@"HTTP/1.1 101 WebSocket Protocol Handshake\r\nUpgrade: WebSocket\r\nConnection: Upgrade\r\n"]) {
201-
// TODO: Verify key
202-
//NSRange r = [response rangeOfString:@"\r\n\r\n"];
203-
//NSString *key = [response substringFromIndex:r.location+r.length];
147+
if ([response hasPrefix:@"HTTP/1.1 101 Web Socket Protocol Handshake\r\nUpgrade: WebSocket\r\nConnection: Upgrade\r\n"]) {
204148
connected = YES;
205149
[self _dispatchOpened];
150+
206151
[self _readNextMessage];
207152
} else {
208153
[self _dispatchFailure:[NSNumber numberWithInt:WebSocketErrorHandshakeFailed]];

0 commit comments

Comments
 (0)