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: add ServeContent #2039

Closed
bradfitz opened this issue Jul 5, 2011 · 18 comments
Closed

net/http: add ServeContent #2039

bradfitz opened this issue Jul 5, 2011 · 18 comments

Comments

@bradfitz
Copy link
Contributor

bradfitz commented Jul 5, 2011

Because http.ServeFile doesn't work with a vfs (yet?) I was trying to use
http.FileServer with a vfs and faking my the http Request.URL.Path to make FileServer
serve the virtual file I wanted.

My input URL was /ui/ and I was faking the request to the VFS for /index.html, because
that's the file I wanted to serve.

The FileServer then redirected me to "/", because it was trying to strip the
index.html from the URL.

That's a bit weird. All other filenames work fine.

I guess I can't use the filename index.html.
@rsc
Copy link
Contributor

rsc commented Jul 11, 2011

Comment 1:

Nope.  I tried very hard to make each file be
served under a unique URL.  Since / serves index.html,
/index.html cannot.
FileServer is a convenience function.  It's not
meant to be all things to all people.
Russ

@bradfitz
Copy link
Contributor Author

Comment 2:

The issue remains that http.FileServer is the only path I have to get to the meaty
http.serveFile (unexported, taking a VFS) since http.ServeFile only uses the host
filesystem.
So it's not that convenient.  I'd happily use something else if I could.

@adg
Copy link
Contributor

adg commented Jul 11, 2011

Comment 3:

So the issue is that there should be a public ServeFile that uses an http.FileSystem?

@bradfitz
Copy link
Contributor Author

Comment 4:

That was the original CL but we hit a complication with relative filenames.

@bradfitz
Copy link
Contributor Author

Comment 5:

From Andy Balholm:
"""
Thanks, Brad. I tried it out in my server, and it works.
But now it exposes another problem: redirects.
The fileHandler redirects requests for /index.html to / . But since it's running under
StripPrefix, the user really requested /static/index.html (in this case), and he should
be redirected to /static/ .
It's easy enough to fix on my site, since I was redirecting / to /static/index.html
already. I just redirected / to /static/ instead. (I really ought to eliminate the
/static directory entirely, but that's another issue.) But I'm puzzled about how to fix
it in the general case. Ideally, it would be nice to be able to fix redirects issued
even by user code (not just by FileServer), but how would you do that?
"""

@rsc
Copy link
Contributor

rsc commented Jul 18, 2011

Comment 6:

i would much rather expose serveFile somehow
than try to make FileServer all things to all people.

@andybalholm
Copy link
Contributor

Comment 7:

Maybe the old FileServer could be reinstated as SimpleFileServer or something. It seemed
to work fine for what I was doing.

@rsc
Copy link
Contributor

rsc commented Dec 9, 2011

Comment 8:

Labels changed: added priority-later, removed priority-medium.

@rsc
Copy link
Contributor

rsc commented Dec 12, 2011

Comment 9:

Labels changed: added priority-go1.

@bradfitz
Copy link
Contributor Author

Comment 10:

We currently have:
   236  // ServeFile replies to the request with the contents of the named file or directory.
   237  func ServeFile(w ResponseWriter, r *Request, name string) {
   238      dir, file := filepath.Split(name)
   239      serveFile(w, r, Dir(dir), file, false)
   240  }
... where the FileSystem (Dir(dir)) cannot be controlled.  This is the only path to
serveFile.
Can we export "serveFile" with some new non-conflicting name?

@robpike
Copy link
Contributor

robpike commented Jan 13, 2012

Comment 11:

Owner changed to builder@golang.org.

@adg
Copy link
Contributor

adg commented Jan 15, 2012

Comment 12:

http://golang.org/cl/5541059/

Status changed to Started.

@rsc
Copy link
Contributor

rsc commented Jan 24, 2012

Comment 13:

Might well be Status: Unfortunate.

@rsc
Copy link
Contributor

rsc commented Jan 30, 2012

Comment 14:

Labels changed: added go1-must.

@rsc
Copy link
Contributor

rsc commented Feb 6, 2012

Comment 15:

I think I could get behind
// ServeContent replies to the request using the content of the reader r.
// The main benefit of ServeContent over io.Copy is that it handles Range
// requests properly.  Note that *os.File implements the io.ReadSeeker interface.
func ServeContent(w ResponseWriter, req *Request, r io.ReadSeeker)
This does not have to be done before Go 1.

Labels changed: added priority-later, removed priority-go1, go1-must.

Status changed to Accepted.

@bradfitz
Copy link
Contributor Author

bradfitz commented Feb 9, 2012

Comment 16:

Owner changed to @bradfitz.

Status changed to Started.

@bradfitz
Copy link
Contributor Author

bradfitz commented Feb 9, 2012

Comment 17:

http://golang.org/cl/5643067/

@bradfitz
Copy link
Contributor Author

bradfitz commented Feb 9, 2012

Comment 18:

This issue was closed by revision 4539d1f.

Status changed to Fixed.

@bradfitz bradfitz self-assigned this Feb 9, 2012
@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

6 participants