Skip to content

net/http: 204 and 100 response handling problems #6685

Closed
@bradfitz

Description

@bradfitz
(Mirroring bug here from report elsewhere)

Problem (what you see vs. what you expected your program to do):
Inconsistent (and by some measure incorrect handling) of 204 responses in Go http server.

Steps to reproduce:
Run  the following trivial server:
package main

import (
       "net/http"
)

type Hello struct{}

func (h Hello) ServeHTTP(w http.ResponseWriter, r *http.Request) {
       w.Header().Set("Content-Length", "4")
       w.Header().Set("Content-Type", "test/plain")
       w.WriteHeader(204)
       w.Write([]byte("data"))
}

func main() {
       var h Hello
       http.ListenAndServe("localhost:4000", h)
}

Telnet session output:
> telnet localhost 4000
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET / HTTP/1.1

HTTP/1.1 204 No Content
Content-Length: 4
Content-Type: test/plain
Date: Tue, 29 Oct 2013 20:13:15 GMT

data

-- 204 must have no body, as per HTTP specification and here it is passed on to the
client.

Now, if we change the above code to return 304, instead of 204 we get:
>telnet localhost 4000
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET / HTTP/1.1

HTTP/1.1 304 Not Modified
Date: Tue, 29 Oct 2013 20:12:43 GMT



As you can see in case of 304 Go HTTP server not only trimmed body but also removed
headers.
It should do the same with 204 responses.

Same happens with 100 responses, which must not that any body as well, but the body is
being returned.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions