From ed70f9067467c61400ecd48dcb3a69d6dc95fc5b Mon Sep 17 00:00:00 2001 From: Mad Ness Date: Thu, 6 Jul 2017 16:04:02 +0300 Subject: [PATCH 1/2] Handle IPv6 host in Request Headers In case of IPv6 wrap the ip in brackets for "Host" and "Origin" headers Without the brackets server responds with 400-Bad Request. --- SocketRocket/Internal/Utilities/SRHTTPConnectMessage.m | 4 +++- SocketRocket/Internal/Utilities/SRURLUtilities.m | 8 +++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/SocketRocket/Internal/Utilities/SRHTTPConnectMessage.m b/SocketRocket/Internal/Utilities/SRHTTPConnectMessage.m index a111a6d62..ce7422f22 100644 --- a/SocketRocket/Internal/Utilities/SRHTTPConnectMessage.m +++ b/SocketRocket/Internal/Utilities/SRHTTPConnectMessage.m @@ -16,7 +16,9 @@ static NSString *_SRHTTPConnectMessageHost(NSURL *url) { NSString *host = url.host; - if (url.port) { + if ([host containsString:@":"]) { + host = [NSString stringWithFormat:@"[%@]:%@", host, url.port]; + } else { host = [host stringByAppendingFormat:@":%@", url.port]; } return host; diff --git a/SocketRocket/Internal/Utilities/SRURLUtilities.m b/SocketRocket/Internal/Utilities/SRURLUtilities.m index fbb942961..1d7e45378 100644 --- a/SocketRocket/Internal/Utilities/SRURLUtilities.m +++ b/SocketRocket/Internal/Utilities/SRURLUtilities.m @@ -23,7 +23,13 @@ } else if ([scheme isEqualToString:@"ws"]) { scheme = @"http"; } - [origin appendFormat:@"%@://%@", scheme, url.host]; + + NSString * host = url.host; + if ([host containsString:@":"]) { + host = [NSString stringWithFormat:@"[%@]", host]; + } + + [origin appendFormat:@"%@://%@", scheme, host]; NSNumber *port = url.port; BOOL portIsDefault = (!port || From 17e89d949d261d182d480992f9af05091ade39e7 Mon Sep 17 00:00:00 2001 From: Mad Ness Date: Thu, 6 Jul 2017 20:34:11 +0300 Subject: [PATCH 2/2] Missed the port url check --- SocketRocket/Internal/Utilities/SRHTTPConnectMessage.m | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/SocketRocket/Internal/Utilities/SRHTTPConnectMessage.m b/SocketRocket/Internal/Utilities/SRHTTPConnectMessage.m index ce7422f22..a22f3a078 100644 --- a/SocketRocket/Internal/Utilities/SRHTTPConnectMessage.m +++ b/SocketRocket/Internal/Utilities/SRHTTPConnectMessage.m @@ -16,10 +16,12 @@ static NSString *_SRHTTPConnectMessageHost(NSURL *url) { NSString *host = url.host; - if ([host containsString:@":"]) { - host = [NSString stringWithFormat:@"[%@]:%@", host, url.port]; - } else { - host = [host stringByAppendingFormat:@":%@", url.port]; + if (url.port) { + if ([host containsString:@":"]) { + host = [NSString stringWithFormat:@"[%@]:%@", host, url.port]; + } else { + host = [host stringByAppendingFormat:@":%@", url.port]; + } } return host; }