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
Hide 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 (
// New creates a new gzip middleware instance.
func
New
(
c
middleware
.
Controller
)
(
middleware
.
Middleware
,
error
)
{
return
func
(
next
http
.
HandlerFunc
)
http
.
HandlerFunc
{
gz
:=
Gzip
{
n
ext
:
next
}
gz
:=
Gzip
{
N
ext
:
next
}
return
gz
.
ServeHTTP
},
nil
}
// Gzip is a http.Handler middleware type which gzips HTTP responses.
type
Gzip
struct
{
n
ext
http
.
HandlerFunc
N
ext
http
.
HandlerFunc
// TODO: Compression level, other settings
}
// 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"
)
{
g
.
n
ext
(
w
,
r
)
g
.
N
ext
(
w
,
r
)
return
}
w
.
Header
()
.
Set
(
"Content-Encoding"
,
"gzip"
)
gzipWriter
:=
gzip
.
NewWriter
(
w
)
defer
gzipWriter
.
Close
()
gz
:=
gzipResponseWriter
{
Writer
:
gzipWriter
,
ResponseWriter
:
w
}
g
.
n
ext
(
gz
,
r
)
g
.
N
ext
(
gz
,
r
)
}
// gzipResponeWriter wraps the underlying Write method
...
...
middleware/headers/headers.go
View file @
dcc67863
...
...
@@ -9,33 +9,33 @@ import (
// Headers is middleware that adds headers to the responses
// for requests matching a certain path.
type
Headers
struct
{
n
ext
http
.
HandlerFunc
rules
[]
headers
N
ext
http
.
HandlerFunc
Rules
[]
HeaderRule
}
// ServeHTTP implements the http.Handler interface and serves the requests,
// adding headers to the response according to the configured rules.
func
(
h
*
Headers
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
for
_
,
rule
:=
range
h
.
r
ules
{
func
(
h
Headers
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
for
_
,
rule
:=
range
h
.
R
ules
{
if
middleware
.
Path
(
r
.
URL
.
Path
)
.
Matches
(
rule
.
Url
)
{
for
_
,
header
:=
range
rule
.
Headers
{
w
.
Header
()
.
Set
(
header
.
Name
,
header
.
Value
)
}
}
}
h
.
n
ext
(
w
,
r
)
h
.
N
ext
(
w
,
r
)
}
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??
headers
struct
{
HeaderRule
struct
{
Url
string
Headers
[]
h
eader
Headers
[]
H
eader
}
// Header represents a single HTTP header, simply a name and value.
h
eader
struct
{
H
eader
struct
{
Name
string
Value
string
}
...
...
middleware/headers/new.go
View file @
dcc67863
...
...
@@ -16,8 +16,8 @@ func New(c middleware.Controller) (middleware.Middleware, error) {
return
func
(
next
http
.
HandlerFunc
)
http
.
HandlerFunc
{
head
:=
Headers
{
n
ext
:
next
,
r
ules
:
rules
,
N
ext
:
next
,
R
ules
:
rules
,
}
return
head
.
ServeHTTP
},
nil
...
...
middleware/headers/parse.go
View file @
dcc67863
...
...
@@ -2,11 +2,11 @@ package headers
import
"github.com/mholt/caddy/middleware"
func
parse
(
c
middleware
.
Controller
)
([]
headers
,
error
)
{
var
rules
[]
headers
func
parse
(
c
middleware
.
Controller
)
([]
HeaderRule
,
error
)
{
var
rules
[]
HeaderRule
for
c
.
NextLine
()
{
var
head
headers
var
head
HeaderRule
var
isNewPattern
bool
if
!
c
.
NextArg
()
{
...
...
@@ -29,7 +29,7 @@ func parse(c middleware.Controller) ([]headers, error) {
}
for
c
.
NextBlock
()
{
h
:=
h
eader
{
Name
:
c
.
Val
()}
h
:=
H
eader
{
Name
:
c
.
Val
()}
if
c
.
NextArg
()
{
h
.
Value
=
c
.
Val
()
...
...
@@ -38,7 +38,7 @@ func parse(c middleware.Controller) ([]headers, error) {
head
.
Headers
=
append
(
head
.
Headers
,
h
)
}
if
c
.
NextArg
()
{
h
:=
h
eader
{
Name
:
c
.
Val
()}
h
:=
H
eader
{
Name
:
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