protected internal override IFullHttpRequest NewHandshakeRequest()
        {
            Uri wsUrl = Uri;

            // Get 16 bit nonce and base 64 encode it
            byte[] nonce = WebSocketUtil.RandomBytes(16);
            string key   = WebSocketUtil.Base64String(nonce);

            string acceptSeed = key + MagicGuid;

            byte[] sha1 = WebSocketUtil.Sha1(Encoding.ASCII.GetBytes(acceptSeed));
            _expectedChallengeResponseString = new AsciiString(WebSocketUtil.Base64String(sha1));

#if DEBUG
            if (Logger.DebugEnabled)
            {
                Logger.WebSocketVersion07ClientHandshakeKey(key, _expectedChallengeResponseString);
            }
#endif

            // Format request
            var request = new DefaultFullHttpRequest(HttpVersion.Http11, HttpMethod.Get, UpgradeUrl(wsUrl),
                                                     Unpooled.Empty);
            HttpHeaders headers = request.Headers;

            if (CustomHeaders is object)
            {
                _ = headers.Add(CustomHeaders);
                if (!headers.Contains(HttpHeaderNames.Host))
                {
                    // Only add HOST header if customHeaders did not contain it.
                    //
                    // See https://github.com/netty/netty/issues/10101
                    _ = headers.Set(HttpHeaderNames.Host, WebsocketHostValue(wsUrl));
                }
            }
            else
            {
                _ = headers.Set(HttpHeaderNames.Host, WebsocketHostValue(wsUrl));
            }

            _ = headers.Set(HttpHeaderNames.Upgrade, HttpHeaderValues.Websocket)
                .Set(HttpHeaderNames.Connection, HttpHeaderValues.Upgrade)
                .Set(HttpHeaderNames.SecWebsocketKey, key);

            if (!headers.Contains(HttpHeaderNames.SecWebsocketOrigin))
            {
                _ = headers.Set(HttpHeaderNames.SecWebsocketOrigin, WebsocketOriginValue(wsUrl));
            }

            string expectedSubprotocol = ExpectedSubprotocol;
            if (!string.IsNullOrEmpty(expectedSubprotocol))
            {
                _ = headers.Set(HttpHeaderNames.SecWebsocketProtocol, expectedSubprotocol);
            }

            _ = headers.Set(HttpHeaderNames.SecWebsocketVersion, Version.ToString());

            return(request);
        }
Example #2
0
        protected override IFullHttpRequest NewHandshakeRequest()
        {
            // Get path
            Uri    wsUrl = this.Uri;
            string path  = RawPath(wsUrl);

            // Get 16 bit nonce and base 64 encode it
            byte[] nonce = WebSocketUtil.RandomBytes(16);
            string key   = WebSocketUtil.Base64String(nonce);

            string acceptSeed = key + MagicGuid;

            byte[] sha1 = WebSocketUtil.Sha1(Encoding.ASCII.GetBytes(acceptSeed));
            this.expectedChallengeResponseString = new AsciiString(WebSocketUtil.Base64String(sha1));

            if (Logger.DebugEnabled)
            {
                Logger.Debug("WebSocket version 07 client handshake key: {}, expected response: {}",
                             key, this.expectedChallengeResponseString);
            }

            // Format request
            var         request = new DefaultFullHttpRequest(HttpVersion.Http11, HttpMethod.Get, path);
            HttpHeaders headers = request.Headers;

            headers.Add(HttpHeaderNames.Upgrade, HttpHeaderValues.Websocket)
            .Add(HttpHeaderNames.Connection, HttpHeaderValues.Upgrade)
            .Add(HttpHeaderNames.SecWebsocketKey, key)
            .Add(HttpHeaderNames.Host, WebsocketHostValue(wsUrl))
            .Add(HttpHeaderNames.SecWebsocketOrigin, WebsocketOriginValue(wsUrl));

            string expectedSubprotocol = this.ExpectedSubprotocol;

            if (string.IsNullOrEmpty(expectedSubprotocol))
            {
                headers.Add(HttpHeaderNames.SecWebsocketProtocol, expectedSubprotocol);
            }

            headers.Add(HttpHeaderNames.SecWebsocketVersion, "7");

            if (this.CustomHeaders != null)
            {
                headers.Add(this.CustomHeaders);
            }
            return(request);
        }
        protected internal override unsafe IFullHttpRequest NewHandshakeRequest()
        {
            // Make keys
            int spaces1 = WebSocketUtil.RandomNumber(1, 12);
            int spaces2 = WebSocketUtil.RandomNumber(1, 12);

            int max1 = int.MaxValue / spaces1;
            int max2 = int.MaxValue / spaces2;

            int number1 = WebSocketUtil.RandomNumber(0, max1);
            int number2 = WebSocketUtil.RandomNumber(0, max2);

            int product1 = number1 * spaces1;
            int product2 = number2 * spaces2;

            string key1 = Convert.ToString(product1);
            string key2 = Convert.ToString(product2);

            key1 = InsertRandomCharacters(key1);
            key2 = InsertRandomCharacters(key2);

            key1 = InsertSpaces(key1, spaces1);
            key2 = InsertSpaces(key2, spaces2);

            byte[] key3      = WebSocketUtil.RandomBytes(8);
            var    challenge = new byte[16];

            fixed(byte *bytes = challenge)
            {
                Unsafe.WriteUnaligned(bytes, number1);
                Unsafe.WriteUnaligned(bytes + 4, number2);
                PlatformDependent.CopyMemory(key3, 0, bytes + 8, 8);
            }

            this.expectedChallengeResponseBytes = Unpooled.WrappedBuffer(WebSocketUtil.Md5(challenge));

            // Get path
            Uri    wsUrl = this.Uri;
            string path  = RawPath(wsUrl);

            // Format request
            var         request = new DefaultFullHttpRequest(HttpVersion.Http11, HttpMethod.Get, path);
            HttpHeaders headers = request.Headers;

            headers.Add(HttpHeaderNames.Upgrade, Websocket)
            .Add(HttpHeaderNames.Connection, HttpHeaderValues.Upgrade)
            .Add(HttpHeaderNames.Host, WebsocketHostValue(wsUrl))
            .Add(HttpHeaderNames.Origin, WebsocketOriginValue(wsUrl))
            .Add(HttpHeaderNames.SecWebsocketKey1, key1)
            .Add(HttpHeaderNames.SecWebsocketKey2, key2);

            string expectedSubprotocol = this.ExpectedSubprotocol;

            if (!string.IsNullOrEmpty(expectedSubprotocol))
            {
                headers.Add(HttpHeaderNames.SecWebsocketProtocol, expectedSubprotocol);
            }

            if (this.CustomHeaders != null)
            {
                headers.Add(this.CustomHeaders);
            }

            // Set Content-Length to workaround some known defect.
            // See also: http://www.ietf.org/mail-archive/web/hybi/current/msg02149.html
            headers.Set(HttpHeaderNames.ContentLength, key3.Length);
            request.Content.WriteBytes(key3);
            return(request);
        }