Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion modules/public/dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,18 @@

package public

import "net/http"
import (
"io"
"net/http"
"time"
)

// Static implements the macaron static handler for serving assets.
func Static(opts *Options) func(next http.Handler) http.Handler {
return opts.staticHandler(opts.Directory)
}

// ServeContent serve http content
func ServeContent(w http.ResponseWriter, req *http.Request, name string, modtime time.Time, content io.ReadSeeker) {
http.ServeContent(w, req, name, modtime, content)
}
12 changes: 11 additions & 1 deletion modules/public/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ func (opts *Options) staticHandler(dir string) func(next http.Handler) http.Hand
}
}

// parseAcceptEncoding parse Accept-Encoding: deflate, gzip;q=1.0, *;q=0.5 as compress methods
func parseAcceptEncoding(val string) map[string]bool {
parts := strings.Split(val, ";")
var types = make(map[string]bool)
for _, v := range strings.Split(parts[0], ",") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about whitespace? E.g. Accept-Encoding: gzip, deflate, br? A test would be nice.

types[v] = true
}
return types
}

func (opts *Options) handle(w http.ResponseWriter, req *http.Request, opt *Options) bool {
if req.Method != "GET" && req.Method != "HEAD" {
return false
Expand Down Expand Up @@ -157,6 +167,6 @@ func (opts *Options) handle(w http.ResponseWriter, req *http.Request, opt *Optio
return true
}

http.ServeContent(w, req, file, fi.ModTime(), f)
ServeContent(w, req, file, fi.ModTime(), f)
return true
}
35 changes: 35 additions & 0 deletions modules/public/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@
package public

import (
"compress/gzip"
"io"
"io/ioutil"
"log"
"mime"
"net/http"
"path/filepath"
"time"
)

// Static implements the macaron static handler for serving assets.
Expand Down Expand Up @@ -49,3 +55,32 @@ func AssetIsDir(name string) (bool, error) {
}
}
}

// ServeContent serve http content
func ServeContent(w http.ResponseWriter, req *http.Request, name string, modtime time.Time, content io.ReadSeeker) {
encodings := parseAcceptEncoding(req.Header.Get("Accept-Encoding"))
if encodings["gzip"] {
if rd, ok := f.(*vfsgen۰CompressedFile); ok {
w.Header().Set("Content-Encoding", "gzip")
ctype := mime.TypeByExtension(filepath.Ext(fi.Name()))
if ctype == "" {
// read a chunk to decide between utf-8 text and binary
var buf [512]byte
grd, _ := gzip.NewReader(rd)
n, _ := io.ReadFull(grd, buf[:])
ctype = http.DetectContentType(buf[:n])
_, err := rd.Seek(0, io.SeekStart) // rewind to output whole file
if err != nil {
log.Printf("rd.Seek error: %v\n", err)
return false
}
}
w.Header().Set("Content-Type", ctype)
http.ServeContent(w, req, file, fi.ModTime(), rd)
return true
}
}

http.ServeContent(w, req, file, fi.ModTime(), f)
return true
}