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
abeb337f
Commit
abeb337f
authored
Apr 11, 2015
by
Matthew Holt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactored rewrite middleware
parent
d0a02166
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
39 additions
and
23 deletions
+39
-23
main.go
main.go
+2
-2
middleware/rewrite/rewrite.go
middleware/rewrite/rewrite.go
+37
-21
No files found.
main.go
View file @
abeb337f
...
...
@@ -16,7 +16,7 @@ var (
func
init
()
{
flag
.
StringVar
(
&
conf
,
"conf"
,
server
.
DefaultConfigFile
,
"the configuration file to use"
)
flag
.
BoolVar
(
&
http2
,
"http2"
,
true
,
"enable HTTP/2 support"
)
//
flag temporary
until http2 merged into std lib
flag
.
BoolVar
(
&
http2
,
"http2"
,
true
,
"enable HTTP/2 support"
)
//
temporary flag
until http2 merged into std lib
}
func
main
()
{
...
...
@@ -38,7 +38,7 @@ func main() {
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
s
.
HTTP2
=
http2
s
.
HTTP2
=
http2
// TODO: This setting is temporary
wg
.
Add
(
1
)
go
func
(
s
*
server
.
Server
)
{
defer
wg
.
Done
()
...
...
middleware/rewrite/rewrite.go
View file @
abeb337f
...
...
@@ -8,42 +8,58 @@ import (
"github.com/mholt/caddy/middleware"
)
// New instantiates a new
rewrite
middleware.
// New instantiates a new
Rewrites
middleware.
func
New
(
c
middleware
.
Controller
)
(
middleware
.
Middleware
,
error
)
{
var
rewrites
[]
rewrite
rewrites
,
err
:=
parse
(
c
)
if
err
!=
nil
{
return
nil
,
err
}
return
func
(
next
middleware
.
Handler
)
middleware
.
Handler
{
return
Rewrite
{
Next
:
next
,
Rules
:
rewrites
}
},
nil
}
// Rewrite is middleware to rewrite request locations internally before being handled.
type
Rewrite
struct
{
Next
middleware
.
Handler
Rules
[]
RewriteRule
}
// ServeHTTP implements the middleware.Handler interface.
func
(
rw
Rewrite
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
for
_
,
rule
:=
range
rw
.
Rules
{
if
r
.
URL
.
Path
==
rule
.
From
{
r
.
URL
.
Path
=
rule
.
To
break
}
}
return
rw
.
Next
.
ServeHTTP
(
w
,
r
)
}
func
parse
(
c
middleware
.
Controller
)
([]
RewriteRule
,
error
)
{
var
rewrites
[]
RewriteRule
for
c
.
Next
()
{
var
rule
rewrit
e
var
rule
RewriteRul
e
if
!
c
.
NextArg
()
{
return
nil
,
c
.
ArgErr
()
return
rewrites
,
c
.
ArgErr
()
}
rule
.
From
=
c
.
Val
()
if
!
c
.
NextArg
()
{
return
nil
,
c
.
ArgErr
()
return
rewrites
,
c
.
ArgErr
()
}
rule
.
To
=
c
.
Val
()
rewrites
=
append
(
rewrites
,
rule
)
}
// TODO: Why can't we just return an http.Handler here instead?
return
func
(
next
middleware
.
Handler
)
middleware
.
Handler
{
return
middleware
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
for
_
,
rule
:=
range
rewrites
{
if
r
.
URL
.
Path
==
rule
.
From
{
r
.
URL
.
Path
=
rule
.
To
break
}
}
return
next
.
ServeHTTP
(
w
,
r
)
})
},
nil
return
rewrites
,
nil
}
// rewrite describes an internal location rewrite rule.
type
rewrite
struct
{
From
string
To
string
// RewriteRule describes an internal location rewrite rule.
type
RewriteRule
struct
{
From
,
To
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