Beispiel #1
0
        public void SetCharSequences()
        {
            var headers = new DefaultHttpHeaders();

            headers.Set(HeaderName, HttpHeadersTestUtils.HeaderValue.Three.AsList());
            AssertDefaultValues(headers, HttpHeadersTestUtils.HeaderValue.Three);
        }
Beispiel #2
0
        public void AddObjects()
        {
            var headers = new DefaultHttpHeaders();

            headers.Add(HeaderName, HttpHeadersTestUtils.HeaderValue.Three.AsList());
            AssertDefaultValues(headers, HttpHeadersTestUtils.HeaderValue.Three);
        }
 public void SetSelfIsNoOp()
 {
     HttpHeaders headers = new DefaultHttpHeaders(false);
     headers.Add((AsciiString)"name", (StringCharSequence)"value");
     headers.Set(headers);
     Assert.Equal(1, headers.Size);
 }
Beispiel #4
0
        public void GlobalSetup()
        {
            ResourceLeakDetector.Level = ResourceLeakDetector.DetectionLevel.Disabled;
            System.Environment.SetEnvironmentVariable("io.netty.buffer.checkAccessible", "false");
            System.Environment.SetEnvironmentVariable("io.netty.buffer.checkBounds", "false");
            Dictionary <HeaderExample, Dictionary <string, string> > headersSet = ExampleHeaders.GetExamples();
            Dictionary <string, string> headers = headersSet[(HeaderExample)this.HeaderSize];

            this.httpNames   = new AsciiString[headers.Count];
            this.httpValues  = new AsciiString[headers.Count];
            this.httpHeaders = new DefaultHttpHeaders(false);
            int idx = 0;

            foreach (KeyValuePair <string, string> header in headers)
            {
                string httpName = ToHttpName(header.Key);
                string value    = header.Value;
                this.httpNames[idx]  = new AsciiString(httpName);
                this.httpValues[idx] = new AsciiString(value);
                this.httpHeaders.Add(this.httpNames[idx], this.httpValues[idx]);
                idx++;
            }
            this.emptyHttpHeaders           = new DefaultHttpHeaders();
            this.emptyHttpHeadersNoValidate = new DefaultHttpHeaders(false);
        }
        public void ConnectionSpecificHeadersShouldBeRemoved()
        {
            HttpHeaders inHeaders = new DefaultHttpHeaders();

            inHeaders.Add(HttpHeaderNames.Connection, "keep-alive");
            inHeaders.Add(HttpHeaderNames.Host, "example.com");
            //@SuppressWarnings("deprecation")
            AsciiString keepAlive = HttpHeaderNames.KeepAlive;

            inHeaders.Add(keepAlive, "timeout=5, max=1000");
            //@SuppressWarnings("deprecation")
            AsciiString proxyConnection = HttpHeaderNames.ProxyConnection;

            inHeaders.Add(proxyConnection, "timeout=5, max=1000");
            inHeaders.Add(HttpHeaderNames.TransferEncoding, "chunked");
            inHeaders.Add(HttpHeaderNames.Upgrade, "h2c");

            IHttp2Headers outHeaders = new DefaultHttp2Headers();

            HttpConversionUtil.ToHttp2Headers(inHeaders, outHeaders);

            Assert.False(outHeaders.Contains(HttpHeaderNames.Connection));
            Assert.False(outHeaders.Contains(HttpHeaderNames.Host));
            Assert.False(outHeaders.Contains(keepAlive));
            Assert.False(outHeaders.Contains(proxyConnection));
            Assert.False(outHeaders.Contains(HttpHeaderNames.TransferEncoding));
            Assert.False(outHeaders.Contains(HttpHeaderNames.Upgrade));
        }
        public void StripTEHeadersAccountsForOWS()
        {
            HttpHeaders inHeaders = new DefaultHttpHeaders();

            inHeaders.Add(HttpHeaderNames.Te, " " + HttpHeaderValues.Trailers + " ");
            var output = new DefaultHttp2Headers();

            HttpConversionUtil.ToHttp2Headers(inHeaders, output);
            Assert.Same(HttpHeaderValues.Trailers, output.Get(HttpHeaderNames.Te, null));
        }
        public void StripTEHeadersAccountsForValueSimilarToTrailers()
        {
            HttpHeaders inHeaders = new DefaultHttpHeaders();

            inHeaders.Add(HttpHeaderNames.Te, HttpHeaderValues.Trailers + "foo");
            var output = new DefaultHttp2Headers();

            HttpConversionUtil.ToHttp2Headers(inHeaders, output);
            Assert.False(output.Contains(HttpHeaderNames.Te));
        }
Beispiel #8
0
        public DefaultHttpHeaders HttpPut()
        {
            var headers = new DefaultHttpHeaders(false);

            for (int i = 0; i < this.httpNames.Length; i++)
            {
                headers.Add(this.httpNames[i], this.httpValues[i]);
            }
            return(headers);
        }
        public void StripTEHeadersCsvSeparatedExcludingTrailers()
        {
            HttpHeaders inHeaders = new DefaultHttpHeaders();

            inHeaders.Add(HttpHeaderNames.Te, HttpHeaderValues.Gzip + "," + HttpHeaderValues.Trailers);
            var output = new DefaultHttp2Headers();

            HttpConversionUtil.ToHttp2Headers(inHeaders, output);
            Assert.Same(HttpHeaderValues.Trailers, output.Get(HttpHeaderNames.Te, null));
        }
        public void StripTEHeaders()
        {
            HttpHeaders inHeaders = new DefaultHttpHeaders();

            inHeaders.Add(HttpHeaderNames.Te, HttpHeaderValues.Gzip);
            var output = new DefaultHttp2Headers();

            HttpConversionUtil.ToHttp2Headers(inHeaders, output);
            Assert.True(output.IsEmpty);
        }
        public void StripConnectionHeadersAndNominees()
        {
            HttpHeaders inHeaders = new DefaultHttpHeaders();

            inHeaders.Add(HttpHeaderNames.Connection, "foo");
            inHeaders.Add((AsciiString)"foo", "bar");
            var output = new DefaultHttp2Headers();

            HttpConversionUtil.ToHttp2Headers(inHeaders, output);
            Assert.True(output.IsEmpty);
        }
        public void IsWebsocketUpgrade()
        {
            HttpHeaders headers = new DefaultHttpHeaders();

            Assert.False(WebSocketExtensionUtil.IsWebsocketUpgrade(headers));

            headers.Add(HttpHeaderNames.Upgrade, HttpHeaderValues.Websocket);
            Assert.False(WebSocketExtensionUtil.IsWebsocketUpgrade(headers));

            headers.Add(HttpHeaderNames.Connection, "Keep-Alive, Upgrade");
            Assert.True(WebSocketExtensionUtil.IsWebsocketUpgrade(headers));
        }
Beispiel #13
0
        public void SetUncombinedHeaders()
        {
            CombinedHttpHeaders headers = NewCombinedHttpHeaders();

            headers.Add(HeaderName, "a");
            var otherHeaders = new DefaultHttpHeaders();

            otherHeaders.Add(HeaderName, "b");
            otherHeaders.Add(HeaderName, "c");
            headers.Set(otherHeaders);
            Assert.Equal("b,c", headers.Get(HeaderName, null)?.ToString());
        }
        public void TestDuplicateHandshakeResponseHeaders()
        {
            WebSocketServerHandshaker serverHandshaker = NewHandshaker("ws://example.com/chat",
                                                                       "chat", WebSocketDecoderConfig.Default);
            IFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.Http11, HttpMethod.Get, "/chat");

            request.Headers
            .Set(HttpHeaderNames.Host, "example.com")
            .Set(HttpHeaderNames.Origin, "example.com")
            .Set(HttpHeaderNames.Upgrade, HttpHeaderValues.Websocket)
            .Set(HttpHeaderNames.Connection, HttpHeaderValues.Upgrade)
            .Set(HttpHeaderNames.SecWebsocketKey, "dGhlIHNhbXBsZSBub25jZQ==")
            .Set(HttpHeaderNames.SecWebsocketOrigin, "http://example.com")
            .Set(HttpHeaderNames.SecWebsocketProtocol, "chat, superchat")
            .Set(HttpHeaderNames.SecWebsocketVersion, WebSocketVersion().ToHttpHeaderValue());
            HttpHeaders customResponseHeaders = new DefaultHttpHeaders();

            // set duplicate required headers and one custom
            customResponseHeaders
            .Set(HttpHeaderNames.Connection, HttpHeaderValues.Upgrade)
            .Set(HttpHeaderNames.Upgrade, HttpHeaderValues.Websocket)
            .Set(AsciiString.Of("custom"), AsciiString.Of("header"));

            if (WebSocketVersion() != Http.WebSockets.WebSocketVersion.V00)
            {
                customResponseHeaders.Set(HttpHeaderNames.SecWebsocketAccept, "12345");
            }

            IFullHttpResponse response = null;

            try
            {
                response = serverHandshaker.NewHandshakeResponse(request, customResponseHeaders);
                HttpHeaders responseHeaders = response.Headers;

                Assert.Equal(1, responseHeaders.GetAll(HttpHeaderNames.Connection).Count);
                Assert.Equal(1, responseHeaders.GetAll(HttpHeaderNames.Upgrade).Count);
                Assert.True(responseHeaders.ContainsValue(AsciiString.Of("custom"), AsciiString.Of("header"), true));
                if (WebSocketVersion() != Http.WebSockets.WebSocketVersion.V00)
                {
                    Assert.False(responseHeaders.ContainsValue(HttpHeaderNames.SecWebsocketAccept, AsciiString.Of("12345"), false));
                }
            }
            finally
            {
                request.Release();
                if (response != null)
                {
                    response.Release();
                }
            }
        }
        public void GetOperations()
        {
            HttpHeaders headers = new DefaultHttpHeaders();
            headers.Add(HttpHeadersTestUtils.Of("Foo"), HttpHeadersTestUtils.Of("1"));
            headers.Add(HttpHeadersTestUtils.Of("Foo"), HttpHeadersTestUtils.Of("2"));

            Assert.Equal("1", headers.Get(HttpHeadersTestUtils.Of("Foo"), null));

            IList<ICharSequence> values = headers.GetAll(HttpHeadersTestUtils.Of("Foo"));
            Assert.Equal(2, values.Count);
            Assert.Equal("1", values[0].ToString());
            Assert.Equal("2", values[1].ToString());
        }
        public void StripConnectionNomineesWithCsv()
        {
            HttpHeaders inHeaders = new DefaultHttpHeaders();

            inHeaders.Add(HttpHeaderNames.Connection, "foo,  bar");
            inHeaders.Add((AsciiString)"foo", "baz");
            inHeaders.Add((AsciiString)"bar", "qux");
            inHeaders.Add((AsciiString)"hello", "world");
            var output = new DefaultHttp2Headers();

            HttpConversionUtil.ToHttp2Headers(inHeaders, output);
            Assert.Equal(1, output.Size);
            Assert.Same("world", output.Get((AsciiString)"hello", null).ToString());
        }
        public void AddHttp2ToHttpHeadersCombinesCookies()
        {
            var inHeaders = new DefaultHttp2Headers();

            inHeaders.Add((AsciiString)"yes", (AsciiString)"no");
            inHeaders.Add(HttpHeaderNames.Cookie, (AsciiString)"foo=bar");
            inHeaders.Add(HttpHeaderNames.Cookie, (AsciiString)"bax=baz");

            HttpHeaders outHeaders = new DefaultHttpHeaders();

            HttpConversionUtil.AddHttp2ToHttpHeaders(5, inHeaders, outHeaders, HttpVersion.Http11, false, false);
            Assert.Equal("no", outHeaders.Get((AsciiString)"yes", null));
            Assert.Equal("foo=bar; bax=baz", outHeaders.Get(HttpHeaderNames.Cookie, null));
        }
Beispiel #18
0
        public void SetOriginFromCustomHeaders()
        {
            HttpHeaders customHeaders            = new DefaultHttpHeaders().Set(GetOriginHeaderName(), "http://example.com");
            WebSocketClientHandshaker handshaker = NewHandshaker(new Uri("ws://server.example.com/chat"), null,
                                                                 customHeaders, false);
            IFullHttpRequest request = handshaker.NewHandshakeRequest();

            try
            {
                Assert.Equal("http://example.com", request.Headers.Get(GetOriginHeaderName(), null));
            }
            finally
            {
                request.Release();
            }
        }
Beispiel #19
0
        public void KeysShouldBeCaseInsensitiveInHeadersEquals()
        {
            var headers1 = new DefaultHttpHeaders();

            headers1.Add(HttpHeadersTestUtils.Of("name1"), new[] { "value1", "value2", "value3" });
            headers1.Add(HttpHeadersTestUtils.Of("nAmE2"), HttpHeadersTestUtils.Of("value4"));

            var headers2 = new DefaultHttpHeaders();

            headers2.Add(HttpHeadersTestUtils.Of("naMe1"), new[] { "value1", "value2", "value3" });
            headers2.Add(HttpHeadersTestUtils.Of("NAME2"), HttpHeadersTestUtils.Of("value4"));

            Assert.True(Equals(headers1, headers2));
            Assert.True(Equals(headers2, headers1));
            Assert.Equal(headers1.GetHashCode(), headers2.GetHashCode());
        }
Beispiel #20
0
        public void GetOperations()
        {
            HttpHeaders headers = new DefaultHttpHeaders();

            headers.Add(new AsciiString("Foo"), new AsciiString("1"));
            headers.Add(new AsciiString("Foo"), new AsciiString("2"));

            Assert.True(headers.TryGet(new AsciiString("Foo"), out ICharSequence value));
            Assert.Equal("1", value.ToString());

            IList <ICharSequence> values = headers.GetAll(new AsciiString("Foo"));

            Assert.NotNull(values);
            Assert.Equal(2, values.Count);
            Assert.Equal("1", values[0].ToString());
            Assert.Equal("2", values[1].ToString());
        }
Beispiel #21
0
        public void AsciiStringKeyRetrievedAsString()
        {
            var headers = new DefaultHttpHeaders(false);

            // Test adding AsciiString key and retrieving it using a String key
            const string CacheControl = "no-cache";

            headers.Add(HttpHeaderNames.CacheControl, CacheControl);

            headers.TryGetAsString(HttpHeaderNames.CacheControl, out string value);
            Assert.NotNull(value);
            Assert.Equal(CacheControl, value);

            ICharSequence value2 = headers.Get(HttpHeaderNames.CacheControl, null);

            Assert.NotNull(value2);
            Assert.Equal(CacheControl, value2);
        }
Beispiel #22
0
        public void StringKeyRetrievedAsAsciiString()
        {
            var headers = new DefaultHttpHeaders(false);

            // Test adding String key and retrieving it using a AsciiString key
            const string Connection = "keep-alive";

            headers.Add(HttpHeadersTestUtils.Of("Connection"), Connection);

            // Passes
            headers.TryGetAsString(HttpHeaderNames.Connection, out string value);
            Assert.NotNull(value);
            Assert.Equal(Connection, value);

            // Passes
            ICharSequence value2 = headers.Get(HttpHeaderNames.Connection, null);

            Assert.NotNull(value2);
            Assert.Equal(Connection, value2);
        }
Beispiel #23
0
        public void KeysShouldBeCaseInsensitive()
        {
            var headers = new DefaultHttpHeaders();

            headers.Add(HttpHeadersTestUtils.Of("Name"), HttpHeadersTestUtils.Of("value1"));
            headers.Add(HttpHeadersTestUtils.Of("name"), HttpHeadersTestUtils.Of("value2"));
            headers.Add(HttpHeadersTestUtils.Of("NAME"), HttpHeadersTestUtils.Of("value3"));
            Assert.Equal(3, headers.Size);

            var values = new List <ICharSequence>
            {
                HttpHeadersTestUtils.Of("value1"),
                HttpHeadersTestUtils.Of("value2"),
                HttpHeadersTestUtils.Of("value3")
            };

            Assert.Equal(values, headers.GetAll(HttpHeadersTestUtils.Of("NAME")));
            Assert.Equal(values, headers.GetAll(HttpHeadersTestUtils.Of("name")));
            Assert.Equal(values, headers.GetAll(HttpHeadersTestUtils.Of("Name")));
            Assert.Equal(values, headers.GetAll(HttpHeadersTestUtils.Of("nAmE")));
        }
Beispiel #24
0
        public HttpHeaders PreflightResponseHeaders()
        {
            if (this.preflightHeaders.Count == 0)
            {
                return(EmptyHttpHeaders.Default);
            }
            HttpHeaders headers = new DefaultHttpHeaders();

            foreach (KeyValuePair <AsciiString, ICallable <object> > entry in this.preflightHeaders)
            {
                object value = GetValue(entry.Value);
                if (value is IEnumerable <object> values)
                {
                    headers.Add(entry.Key, values);
                }
                else
                {
                    headers.Add(entry.Key, value);
                }
            }
            return(headers);
        }
Beispiel #25
0
        public void DuplicateWebsocketHandshakeHeaders()
        {
            var uri = new Uri("ws://localhost:9999/foo");

            var inputHeaders     = new DefaultHttpHeaders();
            var bogusSubProtocol = "bogusSubProtocol";
            var bogusHeaderValue = "bogusHeaderValue";

            // add values for the headers that are reserved for use in the websockets handshake
            foreach (var header in GetHandshakeRequiredHeaderNames())
            {
                if (!HttpHeaderNames.Host.Equals(header))
                {
                    inputHeaders.Add(header, bogusHeaderValue);
                }
            }
            inputHeaders.Add(GetProtocolHeaderName(), bogusSubProtocol);

            var realSubProtocol = "realSubProtocol";
            var handshaker      = NewHandshaker(uri, realSubProtocol, inputHeaders, false);
            var request         = handshaker.NewHandshakeRequest();
            var outputHeaders   = request.Headers;

            // the header values passed in originally have been replaced with values generated by the Handshaker
            foreach (var header in GetHandshakeRequiredHeaderNames())
            {
                Assert.Equal(1, outputHeaders.GetAll(header).Count);
                Assert.NotEqual(bogusHeaderValue, outputHeaders.GetAsString(header));
            }

            // the subprotocol header value is that of the subprotocol string passed into the Handshaker
            Assert.Equal(1, outputHeaders.GetAll(GetProtocolHeaderName()).Count);
            Assert.Equal(realSubProtocol, outputHeaders.GetAsString(GetProtocolHeaderName()));

            request.Release();
        }
        /// <summary>
        /// Upgrade the channel to WebSocket.
        /// </summary>
        private void upgrade(ExtendedNettyFullAddress address)
        {
            /*
             * ========================================= Note =================================================
             * Operations on the channel must happen in the thread associated with the channel.
             * Otherwise subtle bugs can appear.
             * For example the method WebSocketClientHandshaker.finishHandshake can return before
             * the method WebSocketClientHandshaker.handshake returns leaving the channel pipeline in a mess.
             * ================================================================================================
             */

            chnl.EventLoop.Execute(() =>
            {
                /*
                 * If the eventLoop is overloaded, when this task is executed the channel can be broken
                 * (for example because the server has closed it).
                 * So the first thing to do is to check if the channel is healthy.
                 */
                if (chnl.Active)
                {
                    /* set cookies and extra headers */
                    string cookies = address.Cookies;
                    IDictionary <string, string> extraHeaders = address.ExtraHeaders;
                    DefaultHttpHeaders customHeaders          = new DefaultHttpHeaders();
                    if (extraHeaders != null)
                    {
                        foreach (KeyValuePair <string, string> entry in extraHeaders)
                        {
                            customHeaders.Add(new AsciiString(entry.Key), entry.Value);
                        }
                    }
                    if (!string.ReferenceEquals(cookies, null) && cookies.Length > 0)
                    {
                        customHeaders.Set(HttpHeaderNames.Cookie, cookies);
                    }
                    /* build url */
                    NettyFullAddress remoteAddress = address.Address;
                    string scheme = remoteAddress.Secure ? "wss" : "ws";
                    string host   = remoteAddress.Host;
                    string url;

                    int port = remoteAddress.Port;
                    if (host.Equals("::1"))
                    {
                        url = scheme + "://localhost:" + port + "/lightstreamer";
                    }
                    else
                    {
                        url = scheme + "://" + host + ":" + port + "/lightstreamer";
                    }

                    Uri uri            = LsUtils.uri(url);
                    string subprotocol = Constants.TLCP_VERSION + ".lightstreamer.com";
                    /* build pipeline */

                    WebSocketHandshakeHandler wsHandshakeHandler = new WebSocketHandshakeHandler(uri, subprotocol, customHeaders, this);

                    PipelineUtils.populateWSPipelineForHandshake(chnl, wsHandshakeHandler);

                    /* WS handshake */
                    futureTask = wsHandshakeHandler.handshake(chnl);
                }
                else
                {
                    futureTask = Task.Factory.StartNew(() => Thread.Sleep(2));
                }
            });

            return;
        }
 public void AddSelf()
 {
     HttpHeaders headers = new DefaultHttpHeaders(false);
     Assert.Throws<ArgumentException>(() => headers.Add(headers));
 }