Skip to content

Commit

Permalink
Merge pull request #146 from mk/http-basic-auth
Browse files Browse the repository at this point in the history
Add support for http basic auth, closes #36
  • Loading branch information
dfed committed Oct 16, 2015
2 parents fd84880 + 6e0ca9b commit b47e74e
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions SocketRocket/SRWebSocket.m
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ @implementation SRWebSocket {
NSString *_closeReason;

NSString *_secKey;
NSString *_basicAuthorizationString;

BOOL _pinnedCertFound;

Expand Down Expand Up @@ -519,6 +520,22 @@ - (void)didConnect;
}
}

// set header for http basic auth
if (_url.user.length && _url.password.length) {
NSData *userAndPassword = [[NSString stringWithFormat:@"%@:%@", _url.user, _url.password] dataUsingEncoding:NSUTF8StringEncoding];
NSString *userAndPasswordBase64Encoded;
if ([keyBytes respondsToSelector:@selector(base64EncodedStringWithOptions:)]) {
userAndPasswordBase64Encoded = [userAndPassword base64EncodedStringWithOptions:0];
} else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
userAndPasswordBase64Encoded = [userAndPassword base64Encoding];
#pragma clang diagnostic pop
}
_basicAuthorizationString = [NSString stringWithFormat:@"Basic %@", userAndPasswordBase64Encoded];
CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Authorization"), (__bridge CFStringRef)_basicAuthorizationString);
}

CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Upgrade"), CFSTR("websocket"));
CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Connection"), CFSTR("Upgrade"));
CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Sec-WebSocket-Key"), (__bridge CFStringRef)_secKey);
Expand Down

0 comments on commit b47e74e

Please sign in to comment.