Runtime: HttpListener cannot recognize Websocket request from firefox

Created on 10 Oct 2017  路  4Comments  路  Source: dotnet/runtime

When establishing websocket connection, firefox sends such Connection header: Connection: keep-alive, Upgrade and System.Net.HttpListener can't recognize the Upgrade

It can be tested with inserting this test case

[InlineData("Connection: keep-alive, Upgrade\r\nUpgrade: websocket", true)]

to the IsWebSocketRequest_GetProperty_ReturnsExpected test of HttpListenerRequestTests.cs

area-System.Net bug

Most helpful comment

I ran into this issue today.

The spec says that the Connection header need only _include_ the token "Upgrade", but the HttpListenerRequest checks for string equality.

Here's a workaround:

        /// <summary>
        /// Workaround https://github.com/dotnet/corefx/issues/24550.
        /// </summary>
        private static void WorkaroundFixHeaders(HttpListenerRequest request)
        {
            if (string.IsNullOrEmpty(request.Headers["Connection"]))
            {
                return;
            }

            // There are more token seperators possible (see RFC 2616 Section 2.2) but these are a reasonable subset.
            Regex splitter = new Regex(@"(\s|[,;])+");
            // This copies the values to a new array.
            string[] connectionValues = request.Headers.GetValues("Connection");
            // Removes the header.
            request.Headers.Remove("Connection");

            var allTokens = from connection in connectionValues
                   from token in splitter.Split(connection)
                   select token;

            // Adds each individual token back.
            allTokens.ToList().ForEach(value => request.Headers.Add("Connection", value));
        }

All 4 comments

Which OS? Windows? Linux? Both?

Both

I ran into this issue today.

The spec says that the Connection header need only _include_ the token "Upgrade", but the HttpListenerRequest checks for string equality.

Here's a workaround:

        /// <summary>
        /// Workaround https://github.com/dotnet/corefx/issues/24550.
        /// </summary>
        private static void WorkaroundFixHeaders(HttpListenerRequest request)
        {
            if (string.IsNullOrEmpty(request.Headers["Connection"]))
            {
                return;
            }

            // There are more token seperators possible (see RFC 2616 Section 2.2) but these are a reasonable subset.
            Regex splitter = new Regex(@"(\s|[,;])+");
            // This copies the values to a new array.
            string[] connectionValues = request.Headers.GetValues("Connection");
            // Removes the header.
            request.Headers.Remove("Connection");

            var allTokens = from connection in connectionValues
                   from token in splitter.Split(connection)
                   select token;

            // Adds each individual token back.
            allTokens.ToList().ForEach(value => request.Headers.Add("Connection", value));
        }

Not able to repro, probably has already been fixed here.

Please re-open this bug if the problem still exists.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sahithreddyk picture sahithreddyk  路  3Comments

matty-hall picture matty-hall  路  3Comments

chunseoklee picture chunseoklee  路  3Comments

btecu picture btecu  路  3Comments

bencz picture bencz  路  3Comments