我剛開(kāi)始學(xué)習(xí)圍棋,這個(gè)問(wèn)題讓我卡住了。嘗試使用 github.com/valyala/fasthttp 在測(cè)試 func 中測(cè)試本地主機(jī)上的請(qǐng)求處理。首先在https://github.com/valyala/fasthttp/blob/master/server_example_test.go中運(yùn)行服務(wù)器:ln, err := net.Listen("tcp", ":8080")if err != nil { log.Fatalf("error in net.Listen: %s", err)}requestHandler := func(ctx *fasthttp.RequestCtx) { fmt.Println(ctx, "Requested path is")}if err := fasthttp.Serve(ln, requestHandler); err != nil { log.Fatalf("error in Serve: %s", err)}那么如果我從同一個(gè)測(cè)試函數(shù)運(yùn)行請(qǐng)求函數(shù)(FastRequest(url string))它工作正常......Fasthttp請(qǐng)求函數(shù):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 } } }}但我真正需要測(cè)試的是從我的對(duì)象發(fā)送一個(gè)請(qǐng)求,它在 goroutine 中實(shí)現(xiàn)了相同的 FastRequest 方法。在這里我收到了這個(gè)錯(cuò)誤信息: 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 中,我沒(méi)有指定任何用戶代理,F(xiàn)astRequest() 函數(shù)是相同的。只是函數(shù)調(diào)用的地方不同。是否在 goroutine 中調(diào)用并不重要。那么,fasthttp.RequestCtx 無(wú)法解析自己的頭部?或者發(fā)生了什么?
1 回答

慕妹3146593
TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超9個(gè)贊
... contents: "GET here_is_request_url \n http/1.1\r\n ...
^^^^^^^^^^^^^
WRONG!
您在代碼中使用的 URL 可能仍然在末尾有一個(gè)換行符 ( \n),因?yàn)樗?HTTP 版本之前的請(qǐng)求中,因此會(huì)混淆 HTTP 請(qǐng)求。真正的 URL 不應(yīng)包含空格,其中包括空格和換行符。
此外,HTTP 版本應(yīng)該是全大寫(xiě)的HTTP/1.1,即你的小寫(xiě)http/1.1也是錯(cuò)誤的。您沒(méi)有展示如何創(chuàng)建 HTTP 請(qǐng)求,但它很可能搞砸了。
- 1 回答
- 0 關(guān)注
- 373 瀏覽
添加回答
舉報(bào)
0/150
提交
取消