Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net/http: allow removal of Content-Type and Connection headers in response #5950

Closed
josharian opened this issue Jul 24, 2013 · 11 comments
Closed

Comments

@josharian
Copy link
Contributor

I am working in a severely bandwidth-limited environment and am trying to strip down the
http responses generated by net/http. I would like to prevent the Content-Type and
Connection headers from being inserted into the http response. (The Connection header
should be safe to remove at least for HTTP/1.0 requests.) It does not currently seem
possible.

I would ideally like this to remove the Content-Type and Connection headers (it
currently works for removing the Date header):

    header := resp.Header()
    header["Content-Type"] = nil
    header["Connection"] = nil


As a bonus bug:

    header["Content-Type"] = []string{""}

causes duplicate Content-Type headers in the response:

HTTP/1.0 200 OK
Content-Type: 
Content-Type: text/plain; charset=utf-8
Content-Length: 0
Connection: close


I would be happy to write a patch for this, if so desired.


Which version are you using?  (run 'go version')

1.1.1
@adg
Copy link
Contributor

adg commented Jul 24, 2013

Comment 1:

You might consider hijacking the connection and writing the HTTP response yourself,
although that would defeat keepalive.
http://golang.org/pkg/net/http/#Hijacker
Would you mind filing a separate issue about the bug?

@davecheney
Copy link
Contributor

Comment 2:

Connection: close should only be sent if you are sending a HTTP/1.1 response and have
asked to close the connection after the response has been sent. It should not be sent
for HTTP/1.0 responses, or HTTP/1.1 responses that do not request a close action.

@josharian
Copy link
Contributor Author

Comment 3:

If I understand correctly, if I hijack the connection, then I need to take care of
composing and emitted well-formed HTTP, no? I can't just use net/http's Request.Write
(because it'll emit these headers), but the implementation of Request.Write relies on a
good deal of unexported net/http, which means borrowing it will be non-trivial.
I suppose I could do something ugly like inspect and modify the output of Request.Write,
but that feels ugly and fragile.
It is definitely a minor issue, but it seems reasonable for other auto-added headers to
behave like the Date header in this regard. Evidence that the Date header behavior is
right is that I didn't consult documentation -- I just tried it, and it worked exactly
as I expected.
Filed issue https://golang.org/issue/5953 about the double
Content-Type headers.

@adg
Copy link
Contributor

adg commented Jul 25, 2013

Comment 4:

I think you can use Request.Write, as the Content-Type header is added by the
ResponseWriter's content sniffing code. Hijacking should bypass that.

@josharian
Copy link
Contributor Author

Comment 5:

Dave: Good point, thanks. Connection: close is being returned for HTTP/1.0 requests.
Filed https://golang.org/issue/5955 for that.

@adg
Copy link
Contributor

adg commented Jul 25, 2013

Comment 6:

Confirmed: http://play.golang.org/p/zYXrsJVDDW

@josharian
Copy link
Contributor Author

Comment 7:

We both meant Response.Write (oops), but you're correct; I just confirmed here as well.
It's a bit ugly, but hijacking it is! Thanks for the workaround, much appreciated.

@adg
Copy link
Contributor

adg commented Jul 25, 2013

Comment 8:

I'm pretty sure I meant Request.Write... I used it in my example code.
I'm going to mark this issue as WontFix because it's extremely rare that someone should
not want to write a Content-Type header, and the Hijacker provides a means of doing so.
Happy for others to argue otherwise.

Status changed to WontFix.

@adg
Copy link
Contributor

adg commented Jul 25, 2013

Comment 9:

But yeah, Response.Write is what you actually want. :-)

@josharian
Copy link
Contributor Author

Comment 10:

As a side effect of fixing https://golang.org/issue/5953, setting
header["Content-Type"] = nil
now does what I originally expected. Given that there's a separate issue filed about the
Connection header, I'd say this can now be closed as fixed. :)

@rsc
Copy link
Contributor

rsc commented Aug 2, 2013

Comment 11:

Status changed to Fixed.

@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants