Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 读取请求头时Fasthttp错误:无效的头键“http/1.1\r\n用户代理”_Go_Fasthttp - Fatal编程技术网

Go 读取请求头时Fasthttp错误:无效的头键“http/1.1\r\n用户代理”

Go 读取请求头时Fasthttp错误:无效的头键“http/1.1\r\n用户代理”,go,fasthttp,Go,Fasthttp,我刚刚开始学习围棋,这个问题让我陷入了困境。 正在尝试使用github.com/valyala/fasthttp在测试func的本地主机上测试请求处理。 首先运行服务器,如中所示: 然后,如果我从同一个测试函数运行request func FastRequesturl字符串,它会正常工作 Fasthttp请求函数: func FastRequest(url string) error { Request := &fasthttp.Request{} Response :=

我刚刚开始学习围棋,这个问题让我陷入了困境。 正在尝试使用github.com/valyala/fasthttp在测试func的本地主机上测试请求处理。 首先运行服务器,如中所示:

然后,如果我从同一个测试函数运行request func FastRequesturl字符串,它会正常工作

Fasthttp请求函数:

func FastRequest(url string) error {
    Request := &fasthttp.Request{}
    Response := &fasthttp.Response{}
    FastHTTPClient := &fasthttp.Client{}    

    Request.SetRequestURI(url)

    for {

        err := FastHTTPClient.DoTimeout(Request, Response, time.Minute)
        switch err {
        case fasthttp.ErrTimeout, fasthttp.ErrDialTimeout:
            <-time.After(time.Minute * 2)
            continue
        case fasthttp.ErrNoFreeConns:
            <-time.After(time.Minute * 2)
            continue
        case nil:
            return nil
        default:
            if strings.Contains(err.Error(), "connection reset by peer") {
                <-time.After(time.Minute * 2)
                continue
            } else {
                return err
            }
        }
    }
}
但我真正需要测试的是从我的对象发送一个请求,它在goroutine中实现了相同的FastRequest方法。 这里我得到了一条错误信息:

 error when serving connection ":8080"<->":51325": error when reading request headers: invalid header key " http/1.1\r\nuser-Agent". Buffer size=206, contents: "GET here_is_request_url \n http/1.1\r\nuser-Agent: fasthttp\r\nHost: localhost:8080\r\n\r\n"
在FastRequest中,我没有指定任何用户代理,FastRequest的功能是相同的。只有调用函数的位置不同。是否在goroutine中调用并不重要

那么,fasthttp.RequestCtx无法解析自己的头?或者发生了什么

========================================================================== 另外,我应该补充一点,在第一种情况下,我使用了fasthttp v1.6.0,当我将其更改为1.8.0时,错误是:

error when serving connection ":8080"<->":57093": error when reading request headers: invalid header name. Buffer size=215, contents: "GET here_is_request_url\n HTTP/1.1\r\nUser-Agent: fasthttp\r\nHost: localhost:8080\r\n\r\n"
最后,问题出现在url末尾的/n中,它适用于真正的服务器,因为我的小型localhost服务器无法处理它

... contents: "GET here_is_request_url \n http/1.1\r\n ...
                                     ^^^^^^^^^^^^^
                                         WRONG! 
您在代码中使用的URL可能在末尾有一个换行符\n,因为它在HTTP版本之前包含在您的请求中,因此会打乱HTTP请求。一个真正的URL应该没有包含空格和换行符的空白


此外,HTTP版本应该是全大写的HTTP/1.1,即您的小写HTTP/1.1也是错误的。您没有展示如何创建HTTP请求,但很可能会出错。

您是否在标准库HTTP实现中尝试过此操作?用户代理不是有效的头密钥,根据http规范,它应该是用户代理。如果您正在学习Go,请不要使用fasthttp。它不适合随意使用。它只适用于需要巨大性能的安装。是的,这就是它的外观,如果是这样的话,这将是fasthttp中的一个bug。这并不是该软件包中的第一个bug。这就是为什么特别是如果您是新手,我建议您使用标准库http包。它性能稳定。众所周知,fasthttp违反了多个HTTP标准,有些是故意的。该项目的头版就写着这样的话。它几乎永远都不应该被使用。我和项目的维护者是朋友,他也这么说。@BurakSerdar:HTTP中的字段名不区分大小写。“用户代理”和“用户代理”或“用户代理”一样完全有效——它们的意思完全相同。见鬼!!是的,我增加了一个新行字符,所以如果我一直打印URL到日志,它是相同的,因为我打印了Println,只有上下文显示了这个字符。非常感谢。
... contents: "GET here_is_request_url \n http/1.1\r\n ...
                                     ^^^^^^^^^^^^^
                                         WRONG!