mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-23 22:27:38 -05:00
d5371aff22
* httpserver/all: Clean up and standardize request URL handling The HTTP server now always creates a context value on the request which is a copy of the request's URL struct. It should not be modified by middlewares, but it is safe to get the value out of the request and make changes to it locally-scoped. Thus, the value in the context always stores the original request URL information as it was received. Any rewrites that happen will be to the request's URL field directly. The HTTP server no longer cleans /sanitizes the request URL. It made too many strong assumptions and ended up making a lot of middleware more complicated, including upstream proxying (and fastcgi). To alleviate this complexity, we no longer change the request URL. Middlewares are responsible to access the disk safely by using http.Dir or, if not actually opening files, they can use httpserver.SafePath(). I'm hoping this will address issues with #1624, #1584, #1582, and others. * staticfiles: Fix test on Windows @abiosoft: I still can't figure out exactly what this is for. 😅 * Use (potentially) changed URL for browse redirects, as before * Use filepath.ToSlash, clean up a couple proxy test cases * Oops, fix variable name
44 lines
1.1 KiB
Go
44 lines
1.1 KiB
Go
// Package extensions contains middleware for clean URLs.
|
|
//
|
|
// The root path of the site is passed in as well as possible extensions
|
|
// to try internally for paths requested that don't match an existing
|
|
// resource. The first path+ext combination that matches a valid file
|
|
// will be used.
|
|
package extensions
|
|
|
|
import (
|
|
"net/http"
|
|
"os"
|
|
"path"
|
|
"strings"
|
|
|
|
"github.com/mholt/caddy/caddyhttp/httpserver"
|
|
)
|
|
|
|
// Ext can assume an extension from clean URLs.
|
|
// It tries extensions in the order listed in Extensions.
|
|
type Ext struct {
|
|
// Next handler in the chain
|
|
Next httpserver.Handler
|
|
|
|
// Path to site root
|
|
Root string
|
|
|
|
// List of extensions to try
|
|
Extensions []string
|
|
}
|
|
|
|
// ServeHTTP implements the httpserver.Handler interface.
|
|
func (e Ext) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
|
urlpath := strings.TrimSuffix(r.URL.Path, "/")
|
|
if len(r.URL.Path) > 0 && path.Ext(urlpath) == "" && r.URL.Path[len(r.URL.Path)-1] != '/' {
|
|
for _, ext := range e.Extensions {
|
|
_, err := os.Stat(httpserver.SafePath(e.Root, urlpath) + ext)
|
|
if err == nil {
|
|
r.URL.Path = urlpath + ext
|
|
break
|
|
}
|
|
}
|
|
}
|
|
return e.Next.ServeHTTP(w, r)
|
|
}
|