0
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-23 22:27:38 -05:00

Renamed extensionless to extension, Extensionless to Ext

This commit is contained in:
Matthew Holt 2015-04-02 21:59:45 -06:00
parent 3e6f5de92f
commit db2cd9e941
2 changed files with 25 additions and 19 deletions

View file

@ -4,7 +4,7 @@ import (
"github.com/mholt/caddy/middleware" "github.com/mholt/caddy/middleware"
"github.com/mholt/caddy/middleware/browse" "github.com/mholt/caddy/middleware/browse"
"github.com/mholt/caddy/middleware/errors" "github.com/mholt/caddy/middleware/errors"
"github.com/mholt/caddy/middleware/extensionless" "github.com/mholt/caddy/middleware/extension"
"github.com/mholt/caddy/middleware/fastcgi" "github.com/mholt/caddy/middleware/fastcgi"
"github.com/mholt/caddy/middleware/gzip" "github.com/mholt/caddy/middleware/gzip"
"github.com/mholt/caddy/middleware/headers" "github.com/mholt/caddy/middleware/headers"
@ -43,7 +43,7 @@ func init() {
register("header", headers.New) register("header", headers.New)
register("rewrite", rewrite.New) register("rewrite", rewrite.New)
register("redir", redirect.New) register("redir", redirect.New)
register("ext", extensionless.New) register("ext", extension.New)
register("proxy", proxy.New) register("proxy", proxy.New)
register("fastcgi", fastcgi.New) register("fastcgi", fastcgi.New)
register("websocket", websockets.New) register("websocket", websockets.New)

View file

@ -1,8 +1,9 @@
// Package extensionless is middleware for clean URLs. A root path is // Package extension is middleware for clean URLs. The root path
// passed in as well as possible extensions to add, internally, // of the site is passed in as well as possible extensions to try
// to paths requested. The first path+ext that matches a resource // internally for paths requested that don't match an existing
// that exists will be used. // resource. The first path+ext combination that matches a valid
package extensionless // file will be used.
package extension
import ( import (
"net/http" "net/http"
@ -13,14 +14,6 @@ import (
"github.com/mholt/caddy/middleware" "github.com/mholt/caddy/middleware"
) )
// Extensionless is an http.Handler that can assume an extension from clean URLs.
// It tries extensions in the order listed in Extensions.
type Extensionless struct {
Next middleware.HandlerFunc
Root string
Extensions []string
}
// New creates a new instance of middleware that assumes extensions // New creates a new instance of middleware that assumes extensions
// so the site can use cleaner, extensionless URLs // so the site can use cleaner, extensionless URLs
func New(c middleware.Controller) (middleware.Middleware, error) { func New(c middleware.Controller) (middleware.Middleware, error) {
@ -32,7 +25,7 @@ func New(c middleware.Controller) (middleware.Middleware, error) {
} }
return func(next middleware.HandlerFunc) middleware.HandlerFunc { return func(next middleware.HandlerFunc) middleware.HandlerFunc {
return Extensionless{ return Ext{
Next: next, Next: next,
Extensions: extensions, Extensions: extensions,
Root: root, Root: root,
@ -40,8 +33,21 @@ func New(c middleware.Controller) (middleware.Middleware, error) {
}, nil }, nil
} }
// ServeHTTP implements the http.Handler interface. // Ext can assume an extension from clean URLs.
func (e Extensionless) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) { // It tries extensions in the order listed in Extensions.
type Ext struct {
// Next handler in the chain
Next middleware.HandlerFunc
// Path to ther root of the site
Root string
// List of extensions to try
Extensions []string
}
// ServeHTTP implements the middleware.Handler interface.
func (e Ext) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
urlpath := strings.TrimSuffix(r.URL.Path, "/") urlpath := strings.TrimSuffix(r.URL.Path, "/")
if path.Ext(urlpath) == "" { if path.Ext(urlpath) == "" {
for _, ext := range e.Extensions { for _, ext := range e.Extensions {
@ -54,7 +60,7 @@ func (e Extensionless) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, e
return e.Next(w, r) return e.Next(w, r)
} }
// parse sets up an instance of Extensionless middleware // parse sets up an instance of extension middleware
// from a middleware controller and returns a list of extensions. // from a middleware controller and returns a list of extensions.
func parse(c middleware.Controller) ([]string, error) { func parse(c middleware.Controller) ([]string, error) {
var extensions []string var extensions []string