Open
Description
What version of Go are you using (go version
)?
$ go version go version go1.19 linux/amd64
Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (go env
)?
go env
Output
$ go env GO111MODULE="on" GOARCH="amd64" GOBIN="" GOCACHE="/home/work/.cache/go-build" GOENV="/home/work/.config/go/env" GOEXE="" GOEXPERIMENT="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOINSECURE="" GOMODCACHE="/home/work/gopath/pkg/mod" GONOPROXY="" GONOSUMDB="*" GOOS="linux" GOPATH="/home/work/gopath" GOPRIVATE="" GOPROXY="" GOROOT="/home/work/gopath/src/go-env/go1-19-linux-amd64" GOSUMDB="sum.golang.org" GOTMPDIR="" GOTOOLDIR="/home/work/gopath/src/go-env/go1-19-linux-amd64/pkg/tool/linux_amd64" GOVCS="" GOVERSION="go1.19" GCCGO="gccgo" GOAMD64="v1" AR="ar" CC="/opt/compiler/gcc-8.2/bin/gcc" CXX="/opt/compiler/gcc-8.2/bin/g++" CGO_ENABLED="1" GOMOD="/dev/null" GOWORK="" CGO_CFLAGS="-g -O2" CGO_CPPFLAGS="" CGO_CXXFLAGS="-g -O2" CGO_FFLAGS="-g -O2" CGO_LDFLAGS="-g -O2" PKG_CONFIG="pkg-config" GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3130045008=/tmp/go-build -gno-record-gcc-switches"
What did you do?
package main
import (
"context"
"fmt"
"log"
"net"
"net/http"
"os"
"time"
)
type handler struct {
}
func (h *handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
w.Write([]byte("Hello World"))
}
func newServer(port int) *http.Server {
s := &http.Server{
Addr: fmt.Sprintf(":%d", port),
Handler: &handler{},
ReadTimeout: 100 * time.Millisecond,
WriteTimeout: 100 * time.Millisecond,
MaxHeaderBytes: 1 << 20,
ErrorLog: log.New(os.Stderr, "", log.LstdFlags),
}
return s
}
func send(port int) {
conn, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", port))
if err != nil {
fmt.Println(err)
return
}
n, e := conn.Write([]byte("GET / HTTP/1.1\r\n"))
fmt.Println("send first line", n, e)
n, e = conn.Write([]byte("Host: www.google.com\r\n"))
fmt.Println("send host", n, e)
n, e = conn.Write([]byte("Host1"))
fmt.Println("send part header", n, e)
// more than ReadTimeout
time.Sleep(time.Millisecond * 110)
buf := make([]byte, 8192)
n, e = conn.Read(buf)
fmt.Println("read resp", n, e)
fmt.Println("print resp", "`"+string(buf)+"`")
time.Sleep(time.Millisecond * 200)
fmt.Println("conn close", conn.Close())
}
func main() {
port := 12101
svr := newServer(port)
go func() {
fmt.Println(svr.ListenAndServe())
}()
// wait server start
time.Sleep(time.Second * 1)
go send(port)
time.Sleep(time.Second * 1)
fmt.Println("server shutdown", svr.Shutdown(context.Background()))
time.Sleep(time.Second * 1)
}
What did you expect to see?
send first line 16 <nil>
send host 22 <nil>
send part header 5 <nil>
read resp 0 EOF
print resp ``
conn close <nil>
http: Server closed
server shutdown <nil>
and log read tcp 127.0.0.1:12101->127.0.0.1:xxx: i/o timeout
What did you see instead?
send first line 16 <nil>
send host 22 <nil>
send part header 5 <nil>
read resp 103 <nil>
print resp `HTTP/1.1 400 Bad Request
Content-Type: text/plain; charset=utf-8
Connection: close
400 Bad Request`
conn close <nil>
http: Server closed
server shutdown <nil>
Because 'bufio. ReadLine()' dropped the error, causing http.server to return '400 Bad Request'