From 03512fb7214abcc2de1d9ccc85c9f0fd48c14506 Mon Sep 17 00:00:00 2001 From: Ewan Mellor Date: Fri, 3 Jun 2016 15:07:10 -0700 Subject: [PATCH] Fix the Origin: header used in WebSocket requests. Summary: RFC 6454 section 7 defines the Origin: header syntax, and it's a scheme, host, and optional port, not a URL. Section 6 defines serialization of the header, including omission of the port. Therefore, we need to omit the trailing slash in all cases, and omit the port if it matches the default port for the protocol. Closes https://github.com/facebook/react-native/pull/7920 Differential Revision: D3387619 fbshipit-source-id: 552756e63ad41463af357a5073fae56c96e58958 --- Libraries/WebSocket/RCTSRWebSocket.m | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Libraries/WebSocket/RCTSRWebSocket.m b/Libraries/WebSocket/RCTSRWebSocket.m index 54ebbc552..e5ed0adb9 100644 --- a/Libraries/WebSocket/RCTSRWebSocket.m +++ b/Libraries/WebSocket/RCTSRWebSocket.m @@ -1565,10 +1565,14 @@ static const size_t RCTSRFrameHeaderOverhead = 32; scheme = @"http"; } - if (self.port) { - return [NSString stringWithFormat:@"%@://%@:%@/", scheme, self.host, self.port]; + int defaultPort = ([scheme isEqualToString:@"https"] ? 443 : + [scheme isEqualToString:@"http"] ? 80 : + -1); + int port = self.port.intValue; + if (port > 0 && port != defaultPort) { + return [NSString stringWithFormat:@"%@://%@:%d", scheme, self.host, port]; } else { - return [NSString stringWithFormat:@"%@://%@/", scheme, self.host]; + return [NSString stringWithFormat:@"%@://%@", scheme, self.host]; } }