Skip to content

Commit

Permalink
Added processing of a simple GET responses
Browse files Browse the repository at this point in the history
  • Loading branch information
vityevato committed Nov 5, 2017
1 parent 2096647 commit 65f5f77
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 14 deletions.
1 change: 1 addition & 0 deletions PocketSocket/PSWebSocketServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
- (void)server:(PSWebSocketServer *)server webSocketDidFlushOutput:(PSWebSocket *)webSocket;
- (BOOL)server:(PSWebSocketServer *)server acceptWebSocketWithRequest:(NSURLRequest *)request;
- (BOOL)server:(PSWebSocketServer *)server acceptWebSocketWithRequest:(NSURLRequest *)request address:(NSData *)address trust:(SecTrustRef)trust response:(NSHTTPURLResponse **)response;
- (NSHTTPURLResponse *)server:(PSWebSocketServer *)server responseOnSimpleGetRequest:(NSURLRequest *)request address:(NSData *)address trust:(SecTrustRef)trust responseBody:(NSData **)responseBody;
@end

@interface PSWebSocketServer : NSObject
Expand Down
82 changes: 68 additions & 14 deletions PocketSocket/PSWebSocketServer.m
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ - (void)disconnectGracefully:(BOOL)silent {
}

for(PSWebSocketServerConnection *connection in _connections.allObjects) {
[self disconnectConnectionGracefully:connection statusCode:500 description:@"Service Going Away" headers: nil];
[self disconnectConnectionGracefully:connection statusCode:500 description:@"Service Going Away" httpVersion:nil headers:nil body:nil];
}
for(PSWebSocket *webSocket in _webSockets.allObjects) {
[webSocket close];
Expand Down Expand Up @@ -267,9 +267,9 @@ - (void)accept:(CFSocketNativeHandle)handle {
CFReadStreamSetProperty(readStream, kCFStreamPropertySSLSettings, (__bridge CFDictionaryRef)opts);
CFWriteStreamSetProperty(writeStream, kCFStreamPropertySSLSettings, (__bridge CFDictionaryRef)opts);

SSLContextRef context = (SSLContextRef)CFWriteStreamCopyProperty(writeStream, kCFStreamPropertySSLContext);
SSLSetClientSideAuthenticate(context, kTryAuthenticate);
CFRelease(context);
// SSLContextRef context = (SSLContextRef)CFWriteStreamCopyProperty(writeStream, kCFStreamPropertySSLContext);
// SSLSetClientSideAuthenticate(context, kTryAuthenticate);
// CFRelease(context);
}

// create connection
Expand Down Expand Up @@ -354,24 +354,45 @@ - (void)detatchConnection:(PSWebSocketServerConnection *)connection {
connection.inputStream.delegate = nil;
connection.outputStream.delegate = nil;
}
- (void)disconnectConnectionGracefully:(PSWebSocketServerConnection *)connection
response:(NSHTTPURLResponse *)response
body:(NSData *)body {
[self disconnectConnectionGracefully:connection
statusCode:(response.statusCode ?: 403)
description:nil
httpVersion:nil
headers:response.allHeaderFields
body:body];
}
- (void)disconnectConnectionGracefully:(PSWebSocketServerConnection *)connection
statusCode:(NSInteger)statusCode
description:(NSString *)description
headers:(NSDictionary*)headers
httpVersion:(NSString *)httpVersion
headers:(NSDictionary *)headers
body:(NSData *)body
{
if(connection.readyState >= PSWebSocketServerConnectionReadyStateClosing) {
return;
}
connection.readyState = PSWebSocketServerConnectionReadyStateClosing;
if (!description)
description = [NSHTTPURLResponse localizedStringForStatusCode:statusCode];
CFHTTPMessageRef msg = CFHTTPMessageCreateResponse(kCFAllocatorDefault, statusCode, (__bridge CFStringRef)description, kCFHTTPVersion1_1);
if (!httpVersion) {
httpVersion = (NSString *)kCFHTTPVersion1_1;
}
CFHTTPMessageRef msg = CFHTTPMessageCreateResponse(kCFAllocatorDefault, statusCode, (__bridge CFStringRef)description, (__bridge CFStringRef)httpVersion);
for (NSString* name in headers) {
CFHTTPMessageSetHeaderFieldValue(msg, (__bridge CFStringRef)name,
(__bridge CFStringRef)headers[name]);
}
CFHTTPMessageSetHeaderFieldValue(msg, CFSTR("Connection"), CFSTR("Close"));
CFHTTPMessageSetHeaderFieldValue(msg, CFSTR("Content-Length"), CFSTR("0"));
if (body) {
CFHTTPMessageSetBody(msg, (__bridge CFDataRef)body);
// CFHTTPMessageSetHeaderFieldValue(msg, CFSTR("Content-Length"), CFSTR("0"));
}
else {
CFHTTPMessageSetHeaderFieldValue(msg, CFSTR("Content-Length"), CFSTR("0"));
}
NSData *data = CFBridgingRelease(CFHTTPMessageCopySerializedMessage(msg));
CFRelease(msg);
[connection.outputBuffer appendData:data];
Expand Down Expand Up @@ -455,10 +476,18 @@ - (void)pumpInput {

if(![PSWebSocket isWebSocketRequest:request]) {


[self disconnectConnectionGracefully:connection
statusCode:501 description:@"WebSockets only, please"
headers:nil];
NSData *body = nil;
NSHTTPURLResponse *response = [self responseFromDelegateForConnection:connection
request:request
responseBody:&body];
if (response) {
[self disconnectConnectionGracefully:connection response:response body:body];
}
else {
[self disconnectConnectionGracefully:connection
statusCode:501 description:@"WebSockets only, please"
httpVersion:nil headers:nil body:nil];
}
CFRelease(msg);
continue;
}
Expand All @@ -470,9 +499,8 @@ - (void)pumpInput {
request:request
response:&response]) {
[self disconnectConnectionGracefully:connection
statusCode:(response.statusCode ?: 403)
description:nil
headers:response.allHeaderFields];
response:response
body:nil];
CFRelease(msg);
continue;
}
Expand Down Expand Up @@ -660,6 +688,32 @@ - (BOOL)askDelegateShouldAcceptConnection:(PSWebSocketServerConnection *)connect
*outResponse = response;
return accept;
}
- (NSHTTPURLResponse *)responseFromDelegateForConnection:(PSWebSocketServerConnection *)connection
request: (NSURLRequest *)request
responseBody:(NSData **)responseBody {
__block NSHTTPURLResponse *response = nil;
__block NSData *body = nil;
[self executeDelegateAndWait:^{
if([_delegate respondsToSelector:@selector(server:responseOnSimpleGetRequest:address:trust:responseBody:)]) {
NSData* address = PSPeerAddressOfInputStream(connection.inputStream);
SecTrustRef trust = (SecTrustRef)CFReadStreamCopyProperty(
(__bridge CFReadStreamRef)connection.inputStream,
kCFStreamPropertySSLPeerTrust);
response = [_delegate server:self
responseOnSimpleGetRequest:request
address:address
trust:trust
responseBody:&body];
if(trust) {
CFRelease(trust);
}
}
}];
if (responseBody) {
*responseBody = body;
}
return response;
}

#pragma mark - Queueing

Expand Down

0 comments on commit 65f5f77

Please sign in to comment.