Skip to content
Snippets Groups Projects

Support for statically compressed gzip content-encoding

Merged username-removed-849905 requested to merge (removed):master into master
All threads resolved!
+ 42
1
@@ -21,8 +21,40 @@ type domain struct {
certificateError error
}
func acceptsGZip(r *http.Request) bool {
if r.Header.Get("Range") == "" {
// Note that Accept-Encoding is never tokenized by the Request
acceptedEncodings := strings.Split(r.Header.Get("Accept-Encoding"), ",")
if len(acceptedEncodings) > 0 {
for _, v := range acceptedEncodings {
encoding := strings.Trim(v, " \t")
if strings.ToLower(encoding) == "gzip" {
return true
}
}
}
}
return false
}
func (d *domain) serveFile(w http.ResponseWriter, r *http.Request, fullPath string) error {
// Open and serve content of file
if acceptsGZip(r) {
_, err := os.Stat(fullPath + ".gz")
if err == nil {
// Set the content type based on the non-gzipped extension
_, haveType := w.Header()["Content-Type"]
if !haveType {
ctype := mime.TypeByExtension(filepath.Ext(fullPath))
w.Header().Set("Content-Type", ctype)
}
// Serve up the gzipped version
fullPath += ".gz"
w.Header().Set("Content-Encoding", "gzip")
}
}
file, err := os.Open(fullPath)
if err != nil {
return err
@@ -41,6 +73,15 @@ func (d *domain) serveFile(w http.ResponseWriter, r *http.Request, fullPath stri
func (d *domain) serveCustomFile(w http.ResponseWriter, r *http.Request, code int, fullPath string) error {
// Open and serve content of file
ext := filepath.Ext(fullPath)
if acceptsGZip(r) {
_, err := os.Stat(fullPath + ".gz")
if err == nil {
// Serve up the gzipped version
fullPath += ".gz"
w.Header().Set("Content-Encoding", "gzip")
}
}
file, err := os.Open(fullPath)
if err != nil {
return err
@@ -57,7 +98,7 @@ func (d *domain) serveCustomFile(w http.ResponseWriter, r *http.Request, code in
// Serve the file
_, haveType := w.Header()["Content-Type"]
if !haveType {
ctype := mime.TypeByExtension(filepath.Ext(fullPath))
ctype := mime.TypeByExtension(ext)
w.Header().Set("Content-Type", ctype)
}
w.Header().Set("Content-Length", strconv.FormatInt(fi.Size(), 10))
Loading