Commit db2cd9e9 authored by Matthew Holt's avatar Matthew Holt

Renamed extensionless to extension, Extensionless to Ext

parent 3e6f5de9
...@@ -4,7 +4,7 @@ import ( ...@@ -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() { ...@@ -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)
......
// 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 ( ...@@ -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) { ...@@ -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) { ...@@ -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 ...@@ -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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment