Skip to content

net/http: TimeoutHandler: 200 OK should be sent if not specified #15948

Closed
@h12w

Description

@h12w

What version of Go are you using (go version)?

1.6.2 and 1.4.3

What operating system and processor architecture are you using (go env)?

linux, amd64

What did you do?

Minimal example server (main.go):

package main

import (
    "log"
    "net/http"
    "time"
)

func main() {
    log.Fatal(http.ListenAndServe(":9999", http.TimeoutHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    }), time.Second, "timeout")))
}

Then:

go run main.go
curl -i "http://127.0.0.1:9999"

What did you expect to see?

I expect 200 OK because it is consistent with the default behavior without the TimeoutHandler.

HTTP/1.1 200 OK
.......

What did you see instead?

HTTP/1.1 0 status code 0
......

Solution

In func (h *timeoutHandler) ServeHTTP in net/http/server.go, change the code from:

w.WriteHeader(tw.code)

To:

if tw.code == 0 {
    tw.code = StatusOK
}
w.WriteHeader(tw.code)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions