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
dcc67863
Commit
dcc67863
authored
Jan 29, 2015
by
Matthew Holt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Experimenting to make middleware more independent
parent
ac7f50b4
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
21 additions
and
21 deletions
+21
-21
middleware/gzip/gzip.go
middleware/gzip/gzip.go
+5
-5
middleware/headers/headers.go
middleware/headers/headers.go
+9
-9
middleware/headers/new.go
middleware/headers/new.go
+2
-2
middleware/headers/parse.go
middleware/headers/parse.go
+5
-5
No files found.
middleware/gzip/gzip.go
View file @
dcc67863
...
@@ -12,28 +12,28 @@ import (
...
@@ -12,28 +12,28 @@ import (
// New creates a new gzip middleware instance.
// New creates a new gzip middleware instance.
func
New
(
c
middleware
.
Controller
)
(
middleware
.
Middleware
,
error
)
{
func
New
(
c
middleware
.
Controller
)
(
middleware
.
Middleware
,
error
)
{
return
func
(
next
http
.
HandlerFunc
)
http
.
HandlerFunc
{
return
func
(
next
http
.
HandlerFunc
)
http
.
HandlerFunc
{
gz
:=
Gzip
{
n
ext
:
next
}
gz
:=
Gzip
{
N
ext
:
next
}
return
gz
.
ServeHTTP
return
gz
.
ServeHTTP
},
nil
},
nil
}
}
// Gzip is a http.Handler middleware type which gzips HTTP responses.
// Gzip is a http.Handler middleware type which gzips HTTP responses.
type
Gzip
struct
{
type
Gzip
struct
{
n
ext
http
.
HandlerFunc
N
ext
http
.
HandlerFunc
// TODO: Compression level, other settings
// TODO: Compression level, other settings
}
}
// ServeHTTP serves a gzipped response if the client supports it.
// ServeHTTP serves a gzipped response if the client supports it.
func
(
g
*
Gzip
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
func
(
g
Gzip
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
if
!
strings
.
Contains
(
r
.
Header
.
Get
(
"Accept-Encoding"
),
"gzip"
)
{
if
!
strings
.
Contains
(
r
.
Header
.
Get
(
"Accept-Encoding"
),
"gzip"
)
{
g
.
n
ext
(
w
,
r
)
g
.
N
ext
(
w
,
r
)
return
return
}
}
w
.
Header
()
.
Set
(
"Content-Encoding"
,
"gzip"
)
w
.
Header
()
.
Set
(
"Content-Encoding"
,
"gzip"
)
gzipWriter
:=
gzip
.
NewWriter
(
w
)
gzipWriter
:=
gzip
.
NewWriter
(
w
)
defer
gzipWriter
.
Close
()
defer
gzipWriter
.
Close
()
gz
:=
gzipResponseWriter
{
Writer
:
gzipWriter
,
ResponseWriter
:
w
}
gz
:=
gzipResponseWriter
{
Writer
:
gzipWriter
,
ResponseWriter
:
w
}
g
.
n
ext
(
gz
,
r
)
g
.
N
ext
(
gz
,
r
)
}
}
// gzipResponeWriter wraps the underlying Write method
// gzipResponeWriter wraps the underlying Write method
...
...
middleware/headers/headers.go
View file @
dcc67863
...
@@ -9,33 +9,33 @@ import (
...
@@ -9,33 +9,33 @@ import (
// Headers is middleware that adds headers to the responses
// Headers is middleware that adds headers to the responses
// for requests matching a certain path.
// for requests matching a certain path.
type
Headers
struct
{
type
Headers
struct
{
n
ext
http
.
HandlerFunc
N
ext
http
.
HandlerFunc
rules
[]
headers
Rules
[]
HeaderRule
}
}
// ServeHTTP implements the http.Handler interface and serves the requests,
// ServeHTTP implements the http.Handler interface and serves the requests,
// adding headers to the response according to the configured rules.
// adding headers to the response according to the configured rules.
func
(
h
*
Headers
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
func
(
h
Headers
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
for
_
,
rule
:=
range
h
.
r
ules
{
for
_
,
rule
:=
range
h
.
R
ules
{
if
middleware
.
Path
(
r
.
URL
.
Path
)
.
Matches
(
rule
.
Url
)
{
if
middleware
.
Path
(
r
.
URL
.
Path
)
.
Matches
(
rule
.
Url
)
{
for
_
,
header
:=
range
rule
.
Headers
{
for
_
,
header
:=
range
rule
.
Headers
{
w
.
Header
()
.
Set
(
header
.
Name
,
header
.
Value
)
w
.
Header
()
.
Set
(
header
.
Name
,
header
.
Value
)
}
}
}
}
}
}
h
.
n
ext
(
w
,
r
)
h
.
N
ext
(
w
,
r
)
}
}
type
(
type
(
// Header
s
groups a slice of HTTP headers by a URL pattern.
// Header
Rule
groups a slice of HTTP headers by a URL pattern.
// TODO: use http.Header type instead??
// TODO: use http.Header type instead??
headers
struct
{
HeaderRule
struct
{
Url
string
Url
string
Headers
[]
h
eader
Headers
[]
H
eader
}
}
// Header represents a single HTTP header, simply a name and value.
// Header represents a single HTTP header, simply a name and value.
h
eader
struct
{
H
eader
struct
{
Name
string
Name
string
Value
string
Value
string
}
}
...
...
middleware/headers/new.go
View file @
dcc67863
...
@@ -16,8 +16,8 @@ func New(c middleware.Controller) (middleware.Middleware, error) {
...
@@ -16,8 +16,8 @@ func New(c middleware.Controller) (middleware.Middleware, error) {
return
func
(
next
http
.
HandlerFunc
)
http
.
HandlerFunc
{
return
func
(
next
http
.
HandlerFunc
)
http
.
HandlerFunc
{
head
:=
Headers
{
head
:=
Headers
{
n
ext
:
next
,
N
ext
:
next
,
r
ules
:
rules
,
R
ules
:
rules
,
}
}
return
head
.
ServeHTTP
return
head
.
ServeHTTP
},
nil
},
nil
...
...
middleware/headers/parse.go
View file @
dcc67863
...
@@ -2,11 +2,11 @@ package headers
...
@@ -2,11 +2,11 @@ package headers
import
"github.com/mholt/caddy/middleware"
import
"github.com/mholt/caddy/middleware"
func
parse
(
c
middleware
.
Controller
)
([]
headers
,
error
)
{
func
parse
(
c
middleware
.
Controller
)
([]
HeaderRule
,
error
)
{
var
rules
[]
headers
var
rules
[]
HeaderRule
for
c
.
NextLine
()
{
for
c
.
NextLine
()
{
var
head
headers
var
head
HeaderRule
var
isNewPattern
bool
var
isNewPattern
bool
if
!
c
.
NextArg
()
{
if
!
c
.
NextArg
()
{
...
@@ -29,7 +29,7 @@ func parse(c middleware.Controller) ([]headers, error) {
...
@@ -29,7 +29,7 @@ func parse(c middleware.Controller) ([]headers, error) {
}
}
for
c
.
NextBlock
()
{
for
c
.
NextBlock
()
{
h
:=
h
eader
{
Name
:
c
.
Val
()}
h
:=
H
eader
{
Name
:
c
.
Val
()}
if
c
.
NextArg
()
{
if
c
.
NextArg
()
{
h
.
Value
=
c
.
Val
()
h
.
Value
=
c
.
Val
()
...
@@ -38,7 +38,7 @@ func parse(c middleware.Controller) ([]headers, error) {
...
@@ -38,7 +38,7 @@ func parse(c middleware.Controller) ([]headers, error) {
head
.
Headers
=
append
(
head
.
Headers
,
h
)
head
.
Headers
=
append
(
head
.
Headers
,
h
)
}
}
if
c
.
NextArg
()
{
if
c
.
NextArg
()
{
h
:=
h
eader
{
Name
:
c
.
Val
()}
h
:=
H
eader
{
Name
:
c
.
Val
()}
h
.
Value
=
c
.
Val
()
h
.
Value
=
c
.
Val
()
...
...
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