2015-01-30 00:14:31 -05:00
|
|
|
// Package middleware provides some types and functions common among middleware.
|
2015-01-13 14:43:45 -05:00
|
|
|
package middleware
|
|
|
|
|
2015-05-05 16:50:42 -05:00
|
|
|
import (
|
|
|
|
"net/http"
|
2015-10-13 18:49:53 -05:00
|
|
|
"path"
|
2015-10-29 11:34:47 -05:00
|
|
|
"time"
|
2015-05-05 16:50:42 -05:00
|
|
|
)
|
2015-01-13 14:43:45 -05:00
|
|
|
|
2015-01-19 01:11:21 -05:00
|
|
|
type (
|
|
|
|
// Middleware is the middle layer which represents the traditional
|
2015-04-03 00:30:54 -05:00
|
|
|
// idea of middleware: it chains one Handler to the next by being
|
|
|
|
// passed the next Handler in the chain.
|
|
|
|
Middleware func(Handler) Handler
|
2015-03-28 17:37:37 -05:00
|
|
|
|
2016-03-02 13:33:40 -05:00
|
|
|
// Handler is like http.Handler except ServeHTTP may return a status
|
|
|
|
// code and/or error.
|
2015-03-28 17:37:37 -05:00
|
|
|
//
|
2016-03-02 13:33:40 -05:00
|
|
|
// If ServeHTTP writes to the response body, it should return a status
|
|
|
|
// code of 0. This signals to other handlers above it that the response
|
|
|
|
// body is already written, and that they should not write to it also.
|
2015-03-29 23:01:42 -05:00
|
|
|
//
|
2016-03-02 13:33:40 -05:00
|
|
|
// If ServeHTTP encounters an error, it should return the error value
|
|
|
|
// so it can be logged by designated error-handling middleware.
|
2015-03-29 23:01:42 -05:00
|
|
|
//
|
2016-03-02 13:33:40 -05:00
|
|
|
// If writing a response after calling another ServeHTTP method, the
|
|
|
|
// returned status code SHOULD be used when writing the response.
|
|
|
|
//
|
|
|
|
// If handling errors after calling another ServeHTTP method, the
|
|
|
|
// returned error value SHOULD be logged or handled accordingly.
|
|
|
|
//
|
|
|
|
// Otherwise, return values should be propagated down the middleware
|
|
|
|
// chain by returning them unchanged.
|
2015-03-28 17:37:37 -05:00
|
|
|
Handler interface {
|
|
|
|
ServeHTTP(http.ResponseWriter, *http.Request) (int, error)
|
|
|
|
}
|
2015-01-19 01:11:21 -05:00
|
|
|
|
2015-05-04 14:40:07 -05:00
|
|
|
// HandlerFunc is a convenience type like http.HandlerFunc, except
|
|
|
|
// ServeHTTP returns a status code and an error. See Handler
|
|
|
|
// documentation for more information.
|
|
|
|
HandlerFunc func(http.ResponseWriter, *http.Request) (int, error)
|
2015-01-19 01:11:21 -05:00
|
|
|
)
|
2015-04-03 00:30:54 -05:00
|
|
|
|
|
|
|
// ServeHTTP implements the Handler interface.
|
|
|
|
func (f HandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
|
|
|
return f(w, r)
|
|
|
|
}
|
2015-05-05 16:50:42 -05:00
|
|
|
|
|
|
|
// IndexFile looks for a file in /root/fpath/indexFile for each string
|
|
|
|
// in indexFiles. If an index file is found, it returns the root-relative
|
|
|
|
// path to the file and true. If no index file is found, empty string
|
|
|
|
// and false is returned. fpath must end in a forward slash '/'
|
|
|
|
// otherwise no index files will be tried (directory paths must end
|
|
|
|
// in a forward slash according to HTTP).
|
2015-10-13 18:49:53 -05:00
|
|
|
//
|
|
|
|
// All paths passed into and returned from this function use '/' as the
|
|
|
|
// path separator, just like URLs. IndexFle handles path manipulation
|
|
|
|
// internally for systems that use different path separators.
|
2015-05-05 16:50:42 -05:00
|
|
|
func IndexFile(root http.FileSystem, fpath string, indexFiles []string) (string, bool) {
|
|
|
|
if fpath[len(fpath)-1] != '/' || root == nil {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
for _, indexFile := range indexFiles {
|
2015-10-13 18:49:53 -05:00
|
|
|
// func (http.FileSystem).Open wants all paths separated by "/",
|
|
|
|
// regardless of operating system convention, so use
|
|
|
|
// path.Join instead of filepath.Join
|
|
|
|
fp := path.Join(fpath, indexFile)
|
2015-05-05 16:50:42 -05:00
|
|
|
f, err := root.Open(fp)
|
|
|
|
if err == nil {
|
|
|
|
f.Close()
|
|
|
|
return fp, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", false
|
|
|
|
}
|
2015-10-29 11:34:47 -05:00
|
|
|
|
|
|
|
// SetLastModifiedHeader checks if the provided modTime is valid and if it is sets it
|
|
|
|
// as a Last-Modified header to the ResponseWriter. If the modTime is in the future
|
|
|
|
// the current time is used instead.
|
|
|
|
func SetLastModifiedHeader(w http.ResponseWriter, modTime time.Time) {
|
|
|
|
if modTime.IsZero() || modTime.Equal(time.Unix(0, 0)) {
|
|
|
|
// the time does not appear to be valid. Don't put it in the response
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// RFC 2616 - Section 14.29 - Last-Modified:
|
|
|
|
// An origin server MUST NOT send a Last-Modified date which is later than the
|
|
|
|
// server's time of message origination. In such cases, where the resource's last
|
|
|
|
// modification would indicate some time in the future, the server MUST replace
|
|
|
|
// that date with the message origination date.
|
|
|
|
now := currentTime()
|
|
|
|
if modTime.After(now) {
|
|
|
|
modTime = now
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Last-Modified", modTime.UTC().Format(http.TimeFormat))
|
|
|
|
}
|
|
|
|
|
2016-02-14 02:10:57 -05:00
|
|
|
// currentTime, as it is defined here, returns time.Now().
|
|
|
|
// It's defined as a variable for mocking time in tests.
|
2015-10-29 11:34:47 -05:00
|
|
|
var currentTime = func() time.Time {
|
|
|
|
return time.Now()
|
|
|
|
}
|