【问题标题】:Fragmented GET request when using websocket in Firefox在 Firefox 中使用 websocket 时 GET 请求碎片化
【发布时间】:2014-06-19 11:27:22
【问题描述】:

我目前正在用 C# 开发一个小型 websocket 服务器来处理来自浏览器的连接。

我主要使用来自 Mozilla 和 Microsoft 的代码(分别为 herethere)。不幸的是,当我尝试从浏览器连接到我的基本服务器时(脚本来自 websocket.org),GET 请求似乎是碎片化的......

我想了解为什么 GET 请求分为两部分。让我向您展示我的代码以及从中得到的输出。

代码:

while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
    {
        data = Encoding.UTF8.GetString(bytes,0,i);
        Console.WriteLine("Received: {0}", data);
    }

输出:

Received: GET / HTTP/1.1
Host: 127.0.0.1
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate

Received: 
Sec-WebSocket-Version: 13
Origin: null
Sec-WebSocket-Key: 2QYy54zGPPKAkNyPgFjkbw==
Connection: keep-alive, Upgrade
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket

我还尝试在 Chrome 上查看它会有多么不同,通过在浏览器中运行相同的代码和相同的脚本,我得到以下输出:

Received: GET / HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: 127.0.0.1
Origin: null
Pragma: no-cache
Cache-Control: no-cache
Sec-WebSocket-Key: mp5oLqe/YaQAxRksoVZWKg==
Sec-WebSocket-Version: 13
Sec-WebSocket-Extensions: permessage-deflate; client_
Received: max_window_bits, x-webkit-deflate-frame
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36

我在 Chrome 的输出中没有看到任何问题,所以我真的不明白为什么在使用 firefox 时请求会被分割...... 我可以将请求的两个部分合并在一起,但如果我正确理解了 RFC6455,任何错误的请求都应该被服务器丢弃。

有什么建议吗?

非常感谢。

【问题讨论】:

标签: c# google-chrome firefox websocket handshake


【解决方案1】:

这完全没问题。也许 Firefox 以某种方式(较慢)发送信息,您必须读取两次网络缓冲区,但这没关系。似乎它首先发送了永远不会改变的公共信息,最后发送了关于 websocket 的信息(每次都必须生成),可能是为了减少“第一个字节的时间”。

对于HTTP协议,header在有空行时结束,所以只要你没有得到一个并且网络缓冲区包含信息(readed!=0),你可以继续读取header。

通常,您在进行网络编程时会经常发现这种情况。您必须继续阅读,直到网络流显示 readed=0。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-15
    • 2019-10-29
    • 1970-01-01
    • 1970-01-01
    • 2011-04-26
    • 1970-01-01
    • 2018-08-25
    相关资源
    最近更新 更多