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
b5579ca9
Commit
b5579ca9
authored
Jul 03, 2015
by
Matthew Holt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
gzip: Remove mimes
parent
32ef35b9
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
2 additions
and
129 deletions
+2
-129
config/setup/gzip.go
config/setup/gzip.go
+1
-16
config/setup/gzip_test.go
config/setup/gzip_test.go
+0
-12
middleware/gzip/filter.go
middleware/gzip/filter.go
+1
-35
middleware/gzip/filter_test.go
middleware/gzip/filter_test.go
+0
-26
middleware/gzip/gzip_test.go
middleware/gzip/gzip_test.go
+0
-40
No files found.
config/setup/gzip.go
View file @
b5579ca9
...
...
@@ -28,7 +28,6 @@ func gzipParse(c *Controller) ([]gzip.Config, error) {
config
:=
gzip
.
Config
{}
pathFilter
:=
gzip
.
PathFilter
{
make
(
gzip
.
Set
)}
mimeFilter
:=
gzip
.
MIMEFilter
{
make
(
gzip
.
Set
)}
extFilter
:=
gzip
.
ExtFilter
{
make
(
gzip
.
Set
)}
// No extra args expected
...
...
@@ -38,17 +37,6 @@ func gzipParse(c *Controller) ([]gzip.Config, error) {
for
c
.
NextBlock
()
{
switch
c
.
Val
()
{
case
"mimes"
:
mimes
:=
c
.
RemainingArgs
()
if
len
(
mimes
)
==
0
{
return
configs
,
c
.
ArgErr
()
}
for
_
,
m
:=
range
mimes
{
if
!
gzip
.
ValidMIME
(
m
)
{
return
configs
,
fmt
.
Errorf
(
"gzip: invalid MIME %v"
,
m
)
}
mimeFilter
.
Types
.
Add
(
m
)
}
case
"ext"
:
exts
:=
c
.
RemainingArgs
()
if
len
(
exts
)
==
0
{
...
...
@@ -92,13 +80,10 @@ func gzipParse(c *Controller) ([]gzip.Config, error) {
config
.
Filters
=
[]
gzip
.
Filter
{
pathFilter
}
}
// If extensions specified, use it over MIME types (if any).
// Otherwise, if specified, use MIME types.
// Then, if extensions are specified, use those to filter.
// Otherwise, use default extensions filter.
if
len
(
extFilter
.
Exts
)
>
0
{
config
.
Filters
=
append
(
config
.
Filters
,
extFilter
)
}
else
if
len
(
mimeFilter
.
Types
)
>
0
{
config
.
Filters
=
append
(
config
.
Filters
,
mimeFilter
)
}
else
{
config
.
Filters
=
append
(
config
.
Filters
,
gzip
.
DefaultExtFilter
())
}
...
...
config/setup/gzip_test.go
View file @
b5579ca9
...
...
@@ -59,25 +59,13 @@ func TestGzip(t *testing.T) {
level 3
}
`
,
false
},
{
`gzip { mimes text/html
}`
,
false
},
{
`gzip { mimes text/html application/json
}`
,
false
},
{
`gzip { mimes text/html application/
}`
,
true
},
{
`gzip { mimes text/html /json
}`
,
true
},
{
`gzip { mimes /json text/html
}`
,
true
},
{
`gzip { not /file
ext .html
level 1
mimes text/html text/plain
}
gzip { not /file1
ext .htm
level 3
mimes text/html text/css
}
`
,
false
},
}
...
...
middleware/gzip/filter.go
View file @
b5579ca9
...
...
@@ -3,7 +3,6 @@ package gzip
import
(
"net/http"
"path"
"strings"
"github.com/mholt/caddy/middleware"
)
...
...
@@ -16,7 +15,7 @@ type Filter interface {
}
// defaultExtensions is the list of default extensions for which to enable gzipping.
var
defaultExtensions
=
[]
string
{
""
,
".txt"
,
".htm
l"
,
".css"
,
".json"
,
".js
"
,
".md"
,
".xml"
}
var
defaultExtensions
=
[]
string
{
""
,
".txt"
,
".htm
"
,
".html"
,
".css"
,
".php"
,
".js"
,
".json
"
,
".md"
,
".xml"
}
// DefaultExtFilter creates an ExtFilter with default extensions.
func
DefaultExtFilter
()
ExtFilter
{
...
...
@@ -59,39 +58,6 @@ func (p PathFilter) ShouldCompress(r *http.Request) bool {
})
}
// MIMEFilter is Filter for request content types.
type
MIMEFilter
struct
{
// Types is the MIME types to accept.
Types
Set
}
// defaultMIMETypes is the list of default MIME types to use.
var
defaultMIMETypes
=
[]
string
{
"text/plain"
,
"text/html"
,
"text/css"
,
"application/json"
,
"application/javascript"
,
"text/x-markdown"
,
"text/xml"
,
"application/xml"
,
}
// DefaultMIMEFilter creates a MIMEFilter with default types.
func
DefaultMIMEFilter
()
MIMEFilter
{
m
:=
MIMEFilter
{
Types
:
make
(
Set
)}
for
_
,
mime
:=
range
defaultMIMETypes
{
m
.
Types
.
Add
(
mime
)
}
return
m
}
// ShouldCompress checks if the content type of the request
// matches any of the registered ones. It returns true if
// found and false otherwise.
func
(
m
MIMEFilter
)
ShouldCompress
(
r
*
http
.
Request
)
bool
{
return
m
.
Types
.
Contains
(
r
.
Header
.
Get
(
"Accept"
))
}
func
ValidMIME
(
mime
string
)
bool
{
s
:=
strings
.
Split
(
mime
,
"/"
)
return
len
(
s
)
==
2
&&
strings
.
TrimSpace
(
s
[
0
])
!=
""
&&
strings
.
TrimSpace
(
s
[
1
])
!=
""
}
// Set stores distinct strings.
type
Set
map
[
string
]
struct
{}
...
...
middleware/gzip/filter_test.go
View file @
b5579ca9
...
...
@@ -100,32 +100,6 @@ func TestPathFilter(t *testing.T) {
}
}
func
TestMIMEFilter
(
t
*
testing
.
T
)
{
var
filter
Filter
=
DefaultMIMEFilter
()
_
=
filter
.
(
MIMEFilter
)
var
mimes
=
[]
string
{
"text/html"
,
"text/css"
,
"application/json"
,
}
for
i
,
m
:=
range
mimes
{
r
:=
urlRequest
(
"file"
+
m
)
r
.
Header
.
Set
(
"Accept"
,
m
)
if
!
filter
.
ShouldCompress
(
r
)
{
t
.
Errorf
(
"Test %v: Should be valid filter"
,
i
)
}
}
mimes
=
[]
string
{
"image/jpeg"
,
"image/png"
,
}
filter
=
DefaultMIMEFilter
()
for
i
,
m
:=
range
mimes
{
r
:=
urlRequest
(
"file"
+
m
)
r
.
Header
.
Set
(
"Content-Type"
,
m
)
if
filter
.
ShouldCompress
(
r
)
{
t
.
Errorf
(
"Test %v: Should not be valid filter"
,
i
)
}
}
}
func
urlRequest
(
url
string
)
*
http
.
Request
{
r
,
_
:=
http
.
NewRequest
(
"GET"
,
url
,
nil
)
return
r
...
...
middleware/gzip/gzip_test.go
View file @
b5579ca9
...
...
@@ -76,46 +76,6 @@ func TestGzipHandler(t *testing.T) {
t
.
Error
(
err
)
}
}
gz
.
Configs
[
0
]
.
Filters
[
1
]
=
DefaultMIMEFilter
()
w
=
httptest
.
NewRecorder
()
gz
.
Next
=
nextFunc
(
true
)
var
mimes
=
[]
string
{
"text/html"
,
"text/css"
,
"application/json"
,
}
for
_
,
m
:=
range
mimes
{
url
:=
"/file"
r
,
err
:=
http
.
NewRequest
(
"GET"
,
url
,
nil
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
r
.
Header
.
Set
(
"Accept"
,
m
)
r
.
Header
.
Set
(
"Accept-Encoding"
,
"gzip"
)
_
,
err
=
gz
.
ServeHTTP
(
w
,
r
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
}
w
=
httptest
.
NewRecorder
()
gz
.
Next
=
nextFunc
(
false
)
mimes
=
[]
string
{
"image/jpeg"
,
"image/png"
,
}
for
_
,
m
:=
range
mimes
{
url
:=
"/file"
r
,
err
:=
http
.
NewRequest
(
"GET"
,
url
,
nil
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
r
.
Header
.
Set
(
"Content-Type"
,
m
)
r
.
Header
.
Set
(
"Accept-Encoding"
,
"gzip"
)
_
,
err
=
gz
.
ServeHTTP
(
w
,
r
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
}
}
func
nextFunc
(
shouldGzip
bool
)
middleware
.
Handler
{
...
...
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