Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
caddy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
caddy
Commits
db2cd9e9
Commit
db2cd9e9
authored
Apr 02, 2015
by
Matthew Holt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Renamed extensionless to extension, Extensionless to Ext
parent
3e6f5de9
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
25 additions
and
19 deletions
+25
-19
config/middleware.go
config/middleware.go
+2
-2
middleware/extension/ext.go
middleware/extension/ext.go
+23
-17
No files found.
config/middleware.go
View file @
db2cd9e9
...
@@ -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/extension
less
"
"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"
,
extension
less
.
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
)
...
...
middleware/extension
less/extensionless
.go
→
middleware/extension
/ext
.go
View file @
db2cd9e9
// 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
Ext
ensionless
{
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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment