aboutsummaryrefslogtreecommitdiffstats
path: root/modules/public
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2020-12-24 12:25:17 +0800
committerGitHub <noreply@github.com>2020-12-24 12:25:17 +0800
commit19ae6439b0956a578100b50cb09b1cbd40a01942 (patch)
tree48ad57f2d5fdb5c2d00735585c07089f795477d3 /modules/public
parent87a0396719dc12a19e9876bd4e5ba6ea008072d8 (diff)
downloadgitea-19ae6439b0956a578100b50cb09b1cbd40a01942.tar.gz
gitea-19ae6439b0956a578100b50cb09b1cbd40a01942.zip
Improve vfsgen to not unzip bindata files but send to browser directly (#7109)
* Don't unzip files from bindata but send to browser directly * remove dependent for httpgzip * Add tests for parseAcceptEncoding * Update docs for ENABLE_GZIP * Fix bug * Fix bug Co-authored-by: zeripath <art27@cantab.net>
Diffstat (limited to 'modules/public')
-rw-r--r--modules/public/dynamic.go12
-rw-r--r--modules/public/public.go12
-rw-r--r--modules/public/public_test.go40
-rw-r--r--modules/public/static.go40
4 files changed, 102 insertions, 2 deletions
diff --git a/modules/public/dynamic.go b/modules/public/dynamic.go
index f1a4dbb1a3..f634c598a3 100644
--- a/modules/public/dynamic.go
+++ b/modules/public/dynamic.go
@@ -6,9 +6,19 @@
package public
-import "net/http"
+import (
+ "io"
+ "net/http"
+ "os"
+ "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, fi os.FileInfo, modtime time.Time, content io.ReadSeeker) {
+ http.ServeContent(w, req, fi.Name(), modtime, content)
+}
diff --git a/modules/public/public.go b/modules/public/public.go
index fc933637d8..c8148e6db3 100644
--- a/modules/public/public.go
+++ b/modules/public/public.go
@@ -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], ",") {
+ types[strings.TrimSpace(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
@@ -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, fi, fi.ModTime(), f)
return true
}
diff --git a/modules/public/public_test.go b/modules/public/public_test.go
new file mode 100644
index 0000000000..cf8dced431
--- /dev/null
+++ b/modules/public/public_test.go
@@ -0,0 +1,40 @@
+// Copyright 2020 The Gitea Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package public
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestParseAcceptEncoding(t *testing.T) {
+ var kases = []struct {
+ Header string
+ Expected map[string]bool
+ }{
+ {
+ Header: "deflate, gzip;q=1.0, *;q=0.5",
+ Expected: map[string]bool{
+ "deflate": true,
+ "gzip": true,
+ },
+ },
+ {
+ Header: " gzip, deflate, br",
+ Expected: map[string]bool{
+ "deflate": true,
+ "gzip": true,
+ "br": true,
+ },
+ },
+ }
+
+ for _, kase := range kases {
+ t.Run(kase.Header, func(t *testing.T) {
+ assert.EqualValues(t, kase.Expected, parseAcceptEncoding(kase.Header))
+ })
+ }
+}
diff --git a/modules/public/static.go b/modules/public/static.go
index 8da10567ea..c4dd7a1eca 100644
--- a/modules/public/static.go
+++ b/modules/public/static.go
@@ -7,8 +7,17 @@
package public
import (
+ "bytes"
+ "compress/gzip"
+ "io"
"io/ioutil"
+ "mime"
"net/http"
+ "os"
+ "path/filepath"
+ "time"
+
+ "code.gitea.io/gitea/modules/log"
)
// Static implements the macaron static handler for serving assets.
@@ -49,3 +58,34 @@ func AssetIsDir(name string) (bool, error) {
}
}
}
+
+// ServeContent serve http content
+func ServeContent(w http.ResponseWriter, req *http.Request, fi os.FileInfo, modtime time.Time, content io.ReadSeeker) {
+ encodings := parseAcceptEncoding(req.Header.Get("Accept-Encoding"))
+ if encodings["gzip"] {
+ if cf, ok := fi.(*vfsgen۰CompressedFileInfo); ok {
+ rd := bytes.NewReader(cf.GzipBytes())
+ 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.Error("rd.Seek error: %v", err)
+ http.Error(w, http.StatusText(500), 500)
+ return
+ }
+ }
+ w.Header().Set("Content-Type", ctype)
+ http.ServeContent(w, req, fi.Name(), modtime, rd)
+ return
+ }
+ }
+
+ http.ServeContent(w, req, fi.Name(), modtime, content)
+ return
+}