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
Łukasz Nowak
caddy
Commits
2a0cfb60
Commit
2a0cfb60
authored
Apr 08, 2015
by
Matthew Holt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Bug fix for default error handling with gzip
parent
d33256f1
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
2 deletions
+21
-2
middleware/gzip/gzip.go
middleware/gzip/gzip.go
+21
-2
No files found.
middleware/gzip/gzip.go
View file @
2a0cfb60
...
@@ -4,6 +4,7 @@ package gzip
...
@@ -4,6 +4,7 @@ package gzip
import
(
import
(
"compress/gzip"
"compress/gzip"
"fmt"
"io"
"io"
"net/http"
"net/http"
"strings"
"strings"
...
@@ -11,7 +12,10 @@ import (
...
@@ -11,7 +12,10 @@ import (
"github.com/mholt/caddy/middleware"
"github.com/mholt/caddy/middleware"
)
)
// Gzip is a middleware type which gzips HTTP responses.
// Gzip is a middleware type which gzips HTTP responses. It is
// imperative that any handler which writes to a gzipped response
// specifies the Content-Type, otherwise some clients will assume
// application/x-gzip and try to download a file.
type
Gzip
struct
{
type
Gzip
struct
{
Next
middleware
.
Handler
Next
middleware
.
Handler
}
}
...
@@ -28,11 +32,26 @@ func (g Gzip) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
...
@@ -28,11 +32,26 @@ func (g Gzip) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
if
!
strings
.
Contains
(
r
.
Header
.
Get
(
"Accept-Encoding"
),
"gzip"
)
{
if
!
strings
.
Contains
(
r
.
Header
.
Get
(
"Accept-Encoding"
),
"gzip"
)
{
return
g
.
Next
.
ServeHTTP
(
w
,
r
)
return
g
.
Next
.
ServeHTTP
(
w
,
r
)
}
}
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
}
return
g
.
Next
.
ServeHTTP
(
gz
,
r
)
// Any response in forward middleware will now be compressed
status
,
err
:=
g
.
Next
.
ServeHTTP
(
gz
,
r
)
// If there was an error that remained unhandled, we need
// to send something back before gzipWriter gets closed at
// the return of this method!
if
status
>=
400
{
gz
.
Header
()
.
Set
(
"Content-Type"
,
"text/plain"
)
// very necessary
gz
.
WriteHeader
(
status
)
fmt
.
Fprintf
(
gz
,
"%d %s"
,
status
,
http
.
StatusText
(
status
))
return
0
,
err
}
else
{
return
status
,
err
}
}
}
// gzipResponeWriter wraps the underlying Write method
// gzipResponeWriter wraps the underlying Write method
...
...
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