ServeContent replies to the request using the content in the provided ReadSeeker. The main benefit of ServeContent over io.Copy is that it handles Range requests properly, sets the MIME type, and handles If-Modified-Since requests.
If the response's Content-Type header is not set, ServeContent first tries to deduce the type from name's file extension and, if that fails, falls back to reading the first block of the content and passing it to DetectContentType. The name is otherwise unused; in particular it can be empty and is never sent in the response.
If modtime is not the zero time or Unix epoch, ServeContent includes it in a Last-Modified header in the response. If the request includes an If-Modified-Since header, ServeContent uses modtime to decide whether the content needs to be sent at all.
The content's Seek method must work: ServeContent uses a seek to the end of the content to determine its size.
If the caller has set w's ETag header, ServeContent uses it to handle requests using If-Range and If-None-Match.
Note that *os.File implements the io.ReadSeeker interface.
w.Header().Set("Content-Type", p.contentType) } ServeContent(w, r, p.name, p.modtime, p.content) })) defer ts.Close()
ServeContent is referenced in 522 repositoriesgithub.com/golang/go
- 1 reference in src/net/http/fs_test.go
- 2 references in src/github.com/coreos/etcd/mod/dashboard/dashboard.go
- 1 reference in src/github.com/TheRealBill/redskull/handlers/static.go
- 1 reference in src/github.com/cloudflare/redoctober/redoctober.go
- 1 reference in src/github.com/gogits/gogs/modules/middleware/context.go
- 1 reference in src/github.com/gogits/gogs/modules/middleware/static.go
- 5 references in app/internal/gzipfileserver/gzipfileserver.go