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: ServeFile possible bug in list directory #9745

zTrix opened this issue Jan 31, 2015 · 1 comment


None yet
3 participants
Copy link

commented Jan 31, 2015

$ go version
go version go1.4.1 darwin/amd64

I don't know if this is a bug, or designed to be like this.

I implemented a http server using official http package.

I served a static directory using http.ServeFile(w, r, fp), if fp is a directory, the go server returns a very simple directory listing html page, which is good.

The problem is that the relative path is wrong when the url does not ends with slash(/).

For example, in webroot/a dir, we have file named b and directory named c

$ curl localhost:8080/a
<a href="b">b</a>
<a href="c/">c/</a>

So that if you click b, it will point to "localhost:8080/b", not "localhost:8080/a/b"

But if I get localhost:8080/a/, then everything works.

I checked the code, and it's caused by dirList func, line 86.

I just wonder, is it designed to be so or a bug? Does a 302 redirection to http://domain/dir/ make more sense?


This comment has been minimized.

Copy link

commented Feb 1, 2015

If you look at the source, comparing ServeFile with FileServer, you
will see that the redirection is intentionally disabled.

I think it's because ServeFile is a lower-level routine and if you want
to use the directory listing feature, you should handle the redirection
in your code. And if you just want to serve some files from the file
system, FileServer is a better choice.

Also, the docs says that ServeFile serves the content of the file or
directory, but that doesn't include any redirection, so the docs and
code agrees. Closing as working as intended.

@minux minux closed this Feb 1, 2015

@mikioh mikioh changed the title http.ServeFile possible bug in list directory net/http: ServeFile possible bug in list directory Feb 1, 2015

@golang golang locked and limited conversation to collaborators Jun 25, 2016

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.