http.ListenAndServe处理函数在端口80上执行两次 [英] http.ListenAndServe handler function executed twice on port 80
问题描述
如果我在端口8080上运行以下简单的http服务器代码,一切都按预期运行。如果我在端口80上运行相同的代码,只需更改端口,处理函数会在每次请求时执行两次。为什么以及如何解决它?
// httptest项目main.go
包裹主
进口(
净/ http
记录
fmt
html
)
var count int
func defaultHandler(w http.ResponseWriter,r * http.Request){
count ++
fmt.Fprintf(w,Hello,%q count =%d, html.EscapeString(r.URL.Path),count)
fmt.Println(count,r.RemoteAddr)
}
func main(){
http .HandleFunc(/,defaultHandler)
log.Fatal(http.ListenAndServe(:8080,nil))
}
如果我在浏览器中输入localhost:8080,我会得到一个响应,计数从1开始,每增加一个新请求就增加1。
如果我将代码更改为端口80,并在浏览器中输入localhost或localhost:80,则会得到第一个响应,计数从1开始,但增加了2与每个以下请求。与此同时,控制台输出的print语句被执行两次。
终端控制台在端口80上运行时有3个请求:
> go run main.go
1 [:: 1]:51335
2 [:: 1]:51335
3 [:: 1]:51335
4 [:: 1]:51335
5 [:: 1]:51335
6 [:: 1]:51335
浏览器中的响应是 Hello,/count = 1
, Hello,/count = 3
和 Hello,/count = 5
。
我一直在使用Go版本go1.9.2 windows / amd64和最新的Google Chrome浏览器在Windows 10上本地运行此程序。但是,我在远程Linux服务器上的一个简单Web应用程序中检测到该问题,其代码已使用go版本go1.9.1 linux / amd64进行编译。
>我刚刚在我的电脑上用Fiddler打开了它
浏览器发出2个请求
GET / HTTP / 1.1
GET / favicon。 ico HTTP / 1.1
favicon的请求也会被defaultHandler处理,这会导致count增量
我也尝试过使用firefox,它不会发送另一个favicon的请求。
If I run the following simple http server code on port 8080 everything works as expected. If I run the same code on port 80, by just changing the port, the handler function is executed twice with each request. Why, and how to fix it?
// httptest project main.go
package main
import (
"net/http"
"log"
"fmt"
"html"
)
var count int
func defaultHandler(w http.ResponseWriter, r *http.Request) {
count++
fmt.Fprintf(w, "Hello, %q count=%d", html.EscapeString(r.URL.Path), count)
fmt.Println(count,r.RemoteAddr)
}
func main() {
http.HandleFunc("/", defaultHandler)
log.Fatal(http.ListenAndServe(":8080", nil))
}
If I enter localhost:8080 in the browser, I get a response with a count starting at 1 and increased by 1 with each new request.
If I change the code to port 80 and enter just localhost or localhost:80 in the browser, I get a first response with a count starting at 1 but increased by two with each following request. At the same time the print statement for the console output is executed twice.
Terminal console when running on port 80 with 3 requests:
>go run main.go
1 [::1]:51335
2 [::1]:51335
3 [::1]:51335
4 [::1]:51335
5 [::1]:51335
6 [::1]:51335
The responses in the browser are Hello, "/" count=1
, Hello, "/" count=3
and Hello, "/" count=5
.
I've been running this locally on Windows 10 with Go version go1.9.2 windows/amd64 and the latest Google Chrome Browser.
However, I detected the issue in a simple web application on a remote Linux server where the code has been compiled with go version go1.9.1 linux/amd64.
i just tried it on my pc with Fiddler open
I noticed when navigating to the url using Google Chrome, the browser makes 2 request
GET / HTTP/1.1
GET /favicon.ico HTTP/1.1
the request for the favicon also gets handled by the defaultHandler, which causes the count to increment
I also tried with firefox and it doesn't send another request for the favicon
这篇关于http.ListenAndServe处理函数在端口80上执行两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!