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
ab5087e2
Commit
ab5087e2
authored
Dec 07, 2015
by
Abiola Ibrahim
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Gzip: support for min_length.
parent
41bdd775
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
192 additions
and
16 deletions
+192
-16
caddy/setup/gzip.go
caddy/setup/gzip.go
+26
-4
caddy/setup/gzip_test.go
caddy/setup/gzip_test.go
+12
-0
middleware/gzip/gzip.go
middleware/gzip/gzip.go
+15
-5
middleware/gzip/gzip_test.go
middleware/gzip/gzip_test.go
+1
-1
middleware/gzip/request_filter.go
middleware/gzip/request_filter.go
+4
-4
middleware/gzip/request_filter_test.go
middleware/gzip/request_filter_test.go
+2
-2
middleware/gzip/response_filter.go
middleware/gzip/response_filter.go
+62
-0
middleware/gzip/response_filter_test.go
middleware/gzip/response_filter_test.go
+70
-0
No files found.
caddy/setup/gzip.go
View file @
ab5087e2
...
@@ -27,9 +27,13 @@ func gzipParse(c *Controller) ([]gzip.Config, error) {
...
@@ -27,9 +27,13 @@ func gzipParse(c *Controller) ([]gzip.Config, error) {
for
c
.
Next
()
{
for
c
.
Next
()
{
config
:=
gzip
.
Config
{}
config
:=
gzip
.
Config
{}
// Request Filters
pathFilter
:=
gzip
.
PathFilter
{
IgnoredPaths
:
make
(
gzip
.
Set
)}
pathFilter
:=
gzip
.
PathFilter
{
IgnoredPaths
:
make
(
gzip
.
Set
)}
extFilter
:=
gzip
.
ExtFilter
{
Exts
:
make
(
gzip
.
Set
)}
extFilter
:=
gzip
.
ExtFilter
{
Exts
:
make
(
gzip
.
Set
)}
// Response Filters
lengthFilter
:=
gzip
.
LengthFilter
(
0
)
// No extra args expected
// No extra args expected
if
len
(
c
.
RemainingArgs
())
>
0
{
if
len
(
c
.
RemainingArgs
())
>
0
{
return
configs
,
c
.
ArgErr
()
return
configs
,
c
.
ArgErr
()
...
@@ -68,24 +72,42 @@ func gzipParse(c *Controller) ([]gzip.Config, error) {
...
@@ -68,24 +72,42 @@ func gzipParse(c *Controller) ([]gzip.Config, error) {
}
}
level
,
_
:=
strconv
.
Atoi
(
c
.
Val
())
level
,
_
:=
strconv
.
Atoi
(
c
.
Val
())
config
.
Level
=
level
config
.
Level
=
level
case
"min_length"
:
if
!
c
.
NextArg
()
{
return
configs
,
c
.
ArgErr
()
}
length
,
err
:=
strconv
.
ParseInt
(
c
.
Val
(),
10
,
64
)
if
err
!=
nil
{
return
configs
,
err
}
else
if
length
==
0
{
return
configs
,
fmt
.
Errorf
(
`gzip: min_length must be greater than 0`
)
}
lengthFilter
=
gzip
.
LengthFilter
(
length
)
default
:
default
:
return
configs
,
c
.
ArgErr
()
return
configs
,
c
.
ArgErr
()
}
}
}
}
config
.
Filters
=
[]
gzip
.
Filter
{}
// Request Filters
config
.
RequestFilters
=
[]
gzip
.
RequestFilter
{}
// If ignored paths are specified, put in front to filter with path first
// If ignored paths are specified, put in front to filter with path first
if
len
(
pathFilter
.
IgnoredPaths
)
>
0
{
if
len
(
pathFilter
.
IgnoredPaths
)
>
0
{
config
.
Filters
=
[]
gzip
.
Filter
{
pathFilter
}
config
.
RequestFilters
=
[]
gzip
.
Request
Filter
{
pathFilter
}
}
}
// Then, if extensions are specified, use those to filter.
// Then, if extensions are specified, use those to filter.
// Otherwise, use default extensions filter.
// Otherwise, use default extensions filter.
if
len
(
extFilter
.
Exts
)
>
0
{
if
len
(
extFilter
.
Exts
)
>
0
{
config
.
Filters
=
append
(
config
.
Filters
,
extFilter
)
config
.
RequestFilters
=
append
(
config
.
Request
Filters
,
extFilter
)
}
else
{
}
else
{
config
.
Filters
=
append
(
config
.
Filters
,
gzip
.
DefaultExtFilter
())
config
.
RequestFilters
=
append
(
config
.
RequestFilters
,
gzip
.
DefaultExtFilter
())
}
// Response Filters
// If min_length is specified, use it.
if
int64
(
lengthFilter
)
!=
0
{
config
.
ResponseFilters
=
append
(
config
.
ResponseFilters
,
lengthFilter
)
}
}
configs
=
append
(
configs
,
config
)
configs
=
append
(
configs
,
config
)
...
...
caddy/setup/gzip_test.go
View file @
ab5087e2
...
@@ -73,6 +73,18 @@ func TestGzip(t *testing.T) {
...
@@ -73,6 +73,18 @@ func TestGzip(t *testing.T) {
level 1
level 1
}
}
`
,
false
},
`
,
false
},
{
`gzip { not /file
ext *
level 1
min_length ab
}
`
,
true
},
{
`gzip { not /file
ext *
level 1
min_length 1000
}
`
,
false
},
}
}
for
i
,
test
:=
range
tests
{
for
i
,
test
:=
range
tests
{
c
:=
NewTestController
(
test
.
input
)
c
:=
NewTestController
(
test
.
input
)
...
...
middleware/gzip/gzip.go
View file @
ab5087e2
...
@@ -23,8 +23,9 @@ type Gzip struct {
...
@@ -23,8 +23,9 @@ type Gzip struct {
// Config holds the configuration for Gzip middleware
// Config holds the configuration for Gzip middleware
type
Config
struct
{
type
Config
struct
{
Filters
[]
Filter
// Filters to use
RequestFilters
[]
RequestFilter
Level
int
// Compression level
ResponseFilters
[]
ResponseFilter
Level
int
// Compression level
}
}
// ServeHTTP serves a gzipped response if the client supports it.
// ServeHTTP serves a gzipped response if the client supports it.
...
@@ -36,8 +37,8 @@ func (g Gzip) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
...
@@ -36,8 +37,8 @@ func (g Gzip) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
outer
:
outer
:
for
_
,
c
:=
range
g
.
Configs
{
for
_
,
c
:=
range
g
.
Configs
{
// Check filters to determine if gzipping is permitted for this request
// Check
request
filters to determine if gzipping is permitted for this request
for
_
,
filter
:=
range
c
.
Filters
{
for
_
,
filter
:=
range
c
.
Request
Filters
{
if
!
filter
.
ShouldCompress
(
r
)
{
if
!
filter
.
ShouldCompress
(
r
)
{
continue
outer
continue
outer
}
}
...
@@ -56,8 +57,17 @@ outer:
...
@@ -56,8 +57,17 @@ outer:
defer
gzipWriter
.
Close
()
defer
gzipWriter
.
Close
()
gz
:=
gzipResponseWriter
{
Writer
:
gzipWriter
,
ResponseWriter
:
w
}
gz
:=
gzipResponseWriter
{
Writer
:
gzipWriter
,
ResponseWriter
:
w
}
var
rw
http
.
ResponseWriter
// if no response filter is used
if
len
(
c
.
ResponseFilters
)
==
0
{
rw
=
gz
}
else
{
// wrap gzip writer with ResponseFilterWriter
rw
=
NewResponseFilterWriter
(
c
.
ResponseFilters
,
gz
)
}
// Any response in forward middleware will now be compressed
// Any response in forward middleware will now be compressed
status
,
err
:=
g
.
Next
.
ServeHTTP
(
gz
,
r
)
status
,
err
:=
g
.
Next
.
ServeHTTP
(
rw
,
r
)
// If there was an error that remained unhandled, we need
// If there was an error that remained unhandled, we need
// to send something back before gzipWriter gets closed at
// to send something back before gzipWriter gets closed at
...
...
middleware/gzip/gzip_test.go
View file @
ab5087e2
...
@@ -21,7 +21,7 @@ func TestGzipHandler(t *testing.T) {
...
@@ -21,7 +21,7 @@ func TestGzipHandler(t *testing.T) {
extFilter
.
Exts
.
Add
(
e
)
extFilter
.
Exts
.
Add
(
e
)
}
}
gz
:=
Gzip
{
Configs
:
[]
Config
{
gz
:=
Gzip
{
Configs
:
[]
Config
{
{
Filters
:
[]
Filter
{
pathFilter
,
extFilter
}},
{
RequestFilters
:
[]
Request
Filter
{
pathFilter
,
extFilter
}},
}}
}}
w
:=
httptest
.
NewRecorder
()
w
:=
httptest
.
NewRecorder
()
...
...
middleware/gzip/filter.go
→
middleware/gzip/
request_
filter.go
View file @
ab5087e2
...
@@ -7,8 +7,8 @@ import (
...
@@ -7,8 +7,8 @@ import (
"github.com/mholt/caddy/middleware"
"github.com/mholt/caddy/middleware"
)
)
// Filter determines if a request should be gzipped.
//
Request
Filter determines if a request should be gzipped.
type
Filter
interface
{
type
Request
Filter
interface
{
// ShouldCompress tells if gzip compression
// ShouldCompress tells if gzip compression
// should be done on the request.
// should be done on the request.
ShouldCompress
(
*
http
.
Request
)
bool
ShouldCompress
(
*
http
.
Request
)
bool
...
@@ -26,7 +26,7 @@ func DefaultExtFilter() ExtFilter {
...
@@ -26,7 +26,7 @@ func DefaultExtFilter() ExtFilter {
return
m
return
m
}
}
// ExtFilter is Filter for file name extensions.
// ExtFilter is
Request
Filter for file name extensions.
type
ExtFilter
struct
{
type
ExtFilter
struct
{
// Exts is the file name extensions to accept
// Exts is the file name extensions to accept
Exts
Set
Exts
Set
...
@@ -43,7 +43,7 @@ func (e ExtFilter) ShouldCompress(r *http.Request) bool {
...
@@ -43,7 +43,7 @@ func (e ExtFilter) ShouldCompress(r *http.Request) bool {
return
e
.
Exts
.
Contains
(
ExtWildCard
)
||
e
.
Exts
.
Contains
(
ext
)
return
e
.
Exts
.
Contains
(
ExtWildCard
)
||
e
.
Exts
.
Contains
(
ext
)
}
}
// PathFilter is Filter for request path.
// PathFilter is
Request
Filter for request path.
type
PathFilter
struct
{
type
PathFilter
struct
{
// IgnoredPaths is the paths to ignore
// IgnoredPaths is the paths to ignore
IgnoredPaths
Set
IgnoredPaths
Set
...
...
middleware/gzip/filter_test.go
→
middleware/gzip/
request_
filter_test.go
View file @
ab5087e2
...
@@ -47,7 +47,7 @@ func TestSet(t *testing.T) {
...
@@ -47,7 +47,7 @@ func TestSet(t *testing.T) {
}
}
func
TestExtFilter
(
t
*
testing
.
T
)
{
func
TestExtFilter
(
t
*
testing
.
T
)
{
var
filter
Filter
=
ExtFilter
{
make
(
Set
)}
var
filter
Request
Filter
=
ExtFilter
{
make
(
Set
)}
for
_
,
e
:=
range
[]
string
{
".txt"
,
".html"
,
".css"
,
".md"
}
{
for
_
,
e
:=
range
[]
string
{
".txt"
,
".html"
,
".css"
,
".md"
}
{
filter
.
(
ExtFilter
)
.
Exts
.
Add
(
e
)
filter
.
(
ExtFilter
)
.
Exts
.
Add
(
e
)
}
}
...
@@ -86,7 +86,7 @@ func TestPathFilter(t *testing.T) {
...
@@ -86,7 +86,7 @@ func TestPathFilter(t *testing.T) {
paths
:=
[]
string
{
paths
:=
[]
string
{
"/a"
,
"/b"
,
"/c"
,
"/de"
,
"/a"
,
"/b"
,
"/c"
,
"/de"
,
}
}
var
filter
Filter
=
PathFilter
{
make
(
Set
)}
var
filter
Request
Filter
=
PathFilter
{
make
(
Set
)}
for
_
,
p
:=
range
paths
{
for
_
,
p
:=
range
paths
{
filter
.
(
PathFilter
)
.
IgnoredPaths
.
Add
(
p
)
filter
.
(
PathFilter
)
.
IgnoredPaths
.
Add
(
p
)
}
}
...
...
middleware/gzip/response_filter.go
0 → 100644
View file @
ab5087e2
package
gzip
import
(
"net/http"
"strconv"
)
// ResponseFilter determines if the response should be gzipped.
type
ResponseFilter
interface
{
ShouldCompress
(
http
.
ResponseWriter
)
bool
}
// LengthFilter is ResponseFilter for minimum content length.
type
LengthFilter
int64
// ShouldCompress returns if content length is greater than or
// equals to minimum length.
func
(
l
LengthFilter
)
ShouldCompress
(
w
http
.
ResponseWriter
)
bool
{
contentLength
:=
(
w
.
Header
()
.
Get
(
"Content-Length"
))
length
,
err
:=
strconv
.
ParseInt
(
contentLength
,
10
,
64
)
if
err
!=
nil
||
length
==
0
{
return
false
}
return
l
!=
0
&&
int64
(
l
)
<=
length
}
// ResponseFilterWriter validates ResponseFilters. It writes
// gzip compressed data if ResponseFilters are satisfied or
// uncompressed data otherwise.
type
ResponseFilterWriter
struct
{
filters
[]
ResponseFilter
validated
bool
shouldCompress
bool
gzipResponseWriter
}
// NewResponseFilterWriter creates and initializes a new ResponseFilterWriter.
func
NewResponseFilterWriter
(
filters
[]
ResponseFilter
,
gz
gzipResponseWriter
)
*
ResponseFilterWriter
{
return
&
ResponseFilterWriter
{
filters
:
filters
,
gzipResponseWriter
:
gz
}
}
// Write wraps underlying Write method and compresses if filters
// are satisfied
func
(
r
*
ResponseFilterWriter
)
Write
(
b
[]
byte
)
(
int
,
error
)
{
// One time validation to determine if compression should
// be used or not.
if
!
r
.
validated
{
r
.
shouldCompress
=
true
for
_
,
filter
:=
range
r
.
filters
{
if
!
filter
.
ShouldCompress
(
r
)
{
r
.
shouldCompress
=
false
break
}
}
r
.
validated
=
true
}
if
r
.
shouldCompress
{
return
r
.
gzipResponseWriter
.
Write
(
b
)
}
return
r
.
ResponseWriter
.
Write
(
b
)
}
middleware/gzip/response_filter_test.go
0 → 100644
View file @
ab5087e2
package
gzip
import
(
"compress/gzip"
"fmt"
"net/http/httptest"
"testing"
)
func
TestLengthFilter
(
t
*
testing
.
T
)
{
var
filters
[]
ResponseFilter
=
[]
ResponseFilter
{
LengthFilter
(
100
),
LengthFilter
(
1000
),
LengthFilter
(
0
),
}
var
tests
=
[]
struct
{
length
int64
shouldCompress
[
3
]
bool
}{
{
20
,
[
3
]
bool
{
false
,
false
,
false
}},
{
50
,
[
3
]
bool
{
false
,
false
,
false
}},
{
100
,
[
3
]
bool
{
true
,
false
,
false
}},
{
500
,
[
3
]
bool
{
true
,
false
,
false
}},
{
1000
,
[
3
]
bool
{
true
,
true
,
false
}},
{
1500
,
[
3
]
bool
{
true
,
true
,
false
}},
}
for
i
,
ts
:=
range
tests
{
for
j
,
filter
:=
range
filters
{
r
:=
httptest
.
NewRecorder
()
r
.
Header
()
.
Set
(
"Content-Length"
,
fmt
.
Sprint
(
ts
.
length
))
if
filter
.
ShouldCompress
(
r
)
!=
ts
.
shouldCompress
[
j
]
{
t
.
Errorf
(
"Test %v: Expected %v found %v"
,
i
,
ts
.
shouldCompress
[
j
],
filter
.
ShouldCompress
(
r
))
}
}
}
}
func
TestResponseFilterWriter
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
body
string
shouldCompress
bool
}{
{
"Hello
\t\t\t\n
"
,
false
},
{
"Hello the
\t\t\t
world is
\n\n\n
great"
,
true
},
{
"Hello
\t\t\n
from gzip"
,
true
},
{
"Hello gzip
\n
"
,
false
},
}
filters
:=
[]
ResponseFilter
{
LengthFilter
(
15
),
}
for
i
,
ts
:=
range
tests
{
w
:=
httptest
.
NewRecorder
()
w
.
Header
()
.
Set
(
"Content-Length"
,
fmt
.
Sprint
(
len
(
ts
.
body
)))
gz
:=
gzipResponseWriter
{
gzip
.
NewWriter
(
w
),
w
}
rw
:=
NewResponseFilterWriter
(
filters
,
gz
)
rw
.
Write
([]
byte
(
ts
.
body
))
resp
:=
w
.
Body
.
String
()
if
!
ts
.
shouldCompress
{
if
resp
!=
ts
.
body
{
t
.
Errorf
(
"Test %v: No compression expected, found %v"
,
i
,
resp
)
}
}
else
{
if
resp
==
ts
.
body
{
t
.
Errorf
(
"Test %v: Compression expected, found %v"
,
i
,
resp
)
}
}
}
}
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