ahmetb / go-httpbin

http://httpbin.org endpoints for your Go tests

Home Page:https://godoc.org/github.com/ahmetalpbalkan/go-httpbin

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

HEAD support

gaul opened this issue · comments

Presently go-httpbin only serves GET requests. It should also serve HEADs, but the obvious change of adding to the mux methods does not include the Content-Length header, which GET also lacks.

@andrewgaul In this scenario, we'd likely want to stick to what httpbin.org does. Last time I recall, they did not support HEAD only all endpoints. I am open to any patches with tests in this area.

Here are the output for httpbin.org and go-httpbin for the /html endpoint:

$ curl --head http://httpbin.org/html 
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 24 Jan 2017 21:13:08 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 3741
Connection: keep-alive
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
$ curl --head http://127.0.0.1:8080/html 
HTTP/1.1 404 Not Found
Content-Type: text/plain; charset=utf-8
X-Content-Type-Options: nosniff
Date: Tue, 24 Jan 2017 21:13:12 GMT
Content-Length: 19

Sounds good. Want to send a patch?

This is annoying to implement; we need to change each handler to precompute its payload so we can add the Content-Length header. For example:

https://github.com/andrewgaul/go-httpbin/tree/head

Do you know a better way to do this?

I think we can wrap the handlers with a method before registering, in which we use TeeReader to count the bytes written simultaneously and also save them in memory. Later on do the actual write to the resp.Body at once after we set Content-Length. I can take a look at this later, feel free to experiment with this idea in the meanwhile.

Please reopen; HEAD methods do not emit the required Content-Length header.

Based on my observation it does

curl -I http://127.0.0.1:8080/get
HTTP/1.1 200 OK
Date: Mon, 13 Mar 2017 04:55:13 GMT
Content-Length: 128
Content-Type: text/plain; charset=utf-8

curl --head http://127.0.0.1:8080/xml
HTTP/1.1 200 OK
Content-Type: text/xml
Date: Mon, 13 Mar 2017 04:56:33 GMT
Content-Length: 502

However there's something with some endpoints like /html, /image/png it doesn't return the Content-Length header. I suspect it has something to do with how Go (or Mux library) treats HEAD requests.

Specifically I do not observe Content-Length for /image/gif:

$ curl --head http://127.0.0.1:8080/image/gif
HTTP/1.1 200 OK
Date: Mon, 13 Mar 2017 05:19:40 GMT
Content-Type: image/gif

Do you want to open a separate issue for this?

This is indeed a golang issue.

For 100 bytes it returns the header: https://play.golang.org/p/aOr_FrwZ8R
For 1000 bytes it does not: https://play.golang.org/p/EEe7DB1Jlt

You can open a separate issue, but I don't think I know how to solve this.

The limit is actually from 2048 to 2049:

https://play.golang.org/p/ADq3jFsQ2h