Join GitHub today
GitHub is home to over 36 million developers working together to host and review code, manage projects, and build software together.Sign up
net/http: ServeContent for a directory unexpectedly closes the socket #10196
Using http's ServeContent, a GET on a directory opened by os.Open unexpectedly closes the socket. The Content-Length header contains a non-zero size but then not a single byte is written in the HTTP body.
This is because a directory opened with os.Open seeks to a positive size but fails when read. I report this as an issue for net/http, instead os, since I am not sure if this is allowed behaviour for a directory. For HTTP, reporting a non-zero Content-Length with an empty body during GET in my understanding isn't.
How to reproduce this issue
GET a directory served by http.ServeContent on a real filesystem:
Same for Go 1.4.1 and 1.4.2 on OS X and Linux.
Demo that a directory seeks to a positive size but fails when read
seekdir.go seeks a directory, rewinds and tries to read:
How to fix this
Unfortunately I couldn't come up with a good solution. In my understanding, http.ServeContent is fine.
I had hoped that inspecting the return values of CopyN in fs.go line 257 for zero or error would help. But at this point, one can't rewrite the Content-Length header.
If a directory should remain seekable but not readable, a workaround in ServeContent could be to try to read the first n bytes in a buffer before calling WriteHeader.