Skip to content

Commit 294e3fe

Browse files
committed
improve vfsgen to serve direct compress file
1 parent bdd9729 commit 294e3fe

File tree

30 files changed

+12584
-19
lines changed

30 files changed

+12584
-19
lines changed

go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module code.gitea.io/gitea
33
go 1.12
44

55
require (
6+
code.gitea.io/log v0.0.0-20190526010349-0560851a166a
67
github.com/BurntSushi/toml v0.3.1 // indirect
78
github.com/PuerkitoBio/goquery v0.0.0-20170324135448-ed7d758e9a34
89
github.com/RoaringBitmap/roaring v0.4.7 // indirect
@@ -102,6 +103,7 @@ require (
102103
github.com/satori/go.uuid v1.2.0
103104
github.com/sergi/go-diff v1.0.0
104105
github.com/shurcooL/httpfs v0.0.0-20190527155220-6a4d4a70508b // indirect
106+
github.com/shurcooL/httpgzip v0.0.0-20190516014818-1c7afaae1203
105107
github.com/shurcooL/sanitized_anchor_name v0.0.0-20160918041101-1dba4b3954bc // indirect
106108
github.com/shurcooL/vfsgen v0.0.0-20181202132449-6a9ea43bcacd
107109
github.com/siddontang/go-snappy v0.0.0-20140704025258-d8f7bb82a96d // indirect
@@ -118,7 +120,7 @@ require (
118120
golang.org/x/crypto v0.0.0-20190418165655-df01cb2cc480
119121
golang.org/x/net v0.0.0-20181023162649-9b4f9f5ad519
120122
golang.org/x/oauth2 v0.0.0-20181101160152-c453e0c75759
121-
golang.org/x/sys v0.0.0-20190403152447-81d4e9dc473e
123+
golang.org/x/sys v0.0.0-20190429190828-d89cdac9e872
122124
golang.org/x/text v0.3.0
123125
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
124126
gopkg.in/asn1-ber.v1 v1.0.0-20150924051756-4e86f4367175 // indirect

go.sum

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
cloud.google.com/go v0.30.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
2+
code.gitea.io/log v0.0.0-20190526010349-0560851a166a h1:c1wd1bb9rP28jMyXa0X19yrnX0Ux/zoOlv4j/fhwi8Y=
3+
code.gitea.io/log v0.0.0-20190526010349-0560851a166a/go.mod h1:YOBHMQw/14CwuwNStgQyvnzoDJEO6ARjcSdD48QRzhM=
24
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
35
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
46
github.com/PuerkitoBio/goquery v0.0.0-20170324135448-ed7d758e9a34 h1:UsHpWO0Elp6NaWVARdZHjiYwkhrspHVEGsyIKPb9OI8=
@@ -280,6 +282,8 @@ github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24 h1:pntxY8Ary0t4
280282
github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9NzErvs504Cn4c5DxATwIqPbtswREoFCre64PpcG4=
281283
github.com/shurcooL/httpfs v0.0.0-20190527155220-6a4d4a70508b h1:4kg1wyftSKxLtnPAvcRWakIPpokB9w780/KwrNLnfPA=
282284
github.com/shurcooL/httpfs v0.0.0-20190527155220-6a4d4a70508b/go.mod h1:ZY1cvUeJuFPAdZ/B6v7RHavJWZn2YPVFQ1OSXhCGOkg=
285+
github.com/shurcooL/httpgzip v0.0.0-20190516014818-1c7afaae1203 h1:JcuOigWBeic6AhwUYIBLpo9h2ugpTfoMSJb1rhsvcLY=
286+
github.com/shurcooL/httpgzip v0.0.0-20190516014818-1c7afaae1203/go.mod h1:919LwcH0M7/W4fcZ0/jy0qGght1GIhqyS/EgWGH2j5Q=
283287
github.com/shurcooL/sanitized_anchor_name v0.0.0-20160918041101-1dba4b3954bc h1:3wIrJvFb3Pf6B/2mDBnN1G5IfUVev4X5apadQlWOczE=
284288
github.com/shurcooL/sanitized_anchor_name v0.0.0-20160918041101-1dba4b3954bc/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
285289
github.com/shurcooL/vfsgen v0.0.0-20181202132449-6a9ea43bcacd h1:ug7PpSOB5RBPK1Kg6qskGBoP3Vnj/aNYFTznWvlkGo0=
@@ -346,6 +350,7 @@ golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223 h1:DH4skfRX4EBpamg7iV4ZlCpbl
346350
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
347351
golang.org/x/sys v0.0.0-20190403152447-81d4e9dc473e h1:nFYrTHrdrAOpShe27kaFHjsqYSEQ0KWqdWLu3xuZJts=
348352
golang.org/x/sys v0.0.0-20190403152447-81d4e9dc473e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
353+
golang.org/x/sys v0.0.0-20190429190828-d89cdac9e872/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
349354
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
350355
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
351356
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635 h1:2eB4G6bDQDeP69ZXbOKC00S2Kf6TIiRS+DzfKsKeQU0=

modules/public/public.go

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,21 @@
55
package public
66

77
import (
8+
"bytes"
9+
"compress/gzip"
810
"encoding/base64"
11+
"io"
912
"log"
13+
"mime"
1014
"net/http"
1115
"path"
1216
"path/filepath"
1317
"strings"
1418
"time"
1519

1620
"code.gitea.io/gitea/modules/setting"
21+
22+
"github.com/shurcooL/httpgzip"
1723
"gopkg.in/macaron.v1"
1824
)
1925

@@ -36,23 +42,6 @@ func Custom(opts *Options) macaron.Handler {
3642
return opts.staticHandler(path.Join(setting.CustomPath, "public"))
3743
}
3844

39-
// staticFileSystem implements http.FileSystem interface.
40-
type staticFileSystem struct {
41-
dir *http.Dir
42-
}
43-
44-
func newStaticFileSystem(directory string) staticFileSystem {
45-
if !filepath.IsAbs(directory) {
46-
directory = filepath.Join(macaron.Root, directory)
47-
}
48-
dir := http.Dir(directory)
49-
return staticFileSystem{&dir}
50-
}
51-
52-
func (fs staticFileSystem) Open(name string) (http.File, error) {
53-
return fs.dir.Open(name)
54-
}
55-
5645
// StaticHandler sets up a new middleware for serving static files in the
5746
func StaticHandler(dir string, opts *Options) macaron.Handler {
5847
return opts.staticHandler(dir)
@@ -73,7 +62,7 @@ func (opts *Options) staticHandler(dir string) macaron.Handler {
7362
opts.Prefix = strings.TrimRight(opts.Prefix, "/")
7463
}
7564
if opts.FileSystem == nil {
76-
opts.FileSystem = newStaticFileSystem(dir)
65+
opts.FileSystem = http.Dir(dir)
7766
}
7867

7968
return func(ctx *macaron.Context, log *log.Logger) {
@@ -145,6 +134,29 @@ func (opts *Options) handle(ctx *macaron.Context, log *log.Logger, opt *Options)
145134
}
146135
}
147136

137+
if _, ok := f.(httpgzip.NotWorthGzipCompressing); !ok {
138+
if g, ok := f.(httpgzip.GzipByter); ok {
139+
ctx.Resp.Header().Set("Content-Encoding", "gzip")
140+
rd := bytes.NewReader(g.GzipBytes())
141+
ctype := mime.TypeByExtension(filepath.Ext(fi.Name()))
142+
if ctype == "" {
143+
// read a chunk to decide between utf-8 text and binary
144+
var buf [512]byte
145+
grd, _ := gzip.NewReader(rd)
146+
n, _ := io.ReadFull(grd, buf[:])
147+
ctype = http.DetectContentType(buf[:n])
148+
_, err := rd.Seek(0, io.SeekStart) // rewind to output whole file
149+
if err != nil {
150+
log.Printf("rd.Seek error: %v\n", err)
151+
return false
152+
}
153+
}
154+
ctx.Resp.Header().Set("Content-Type", ctype)
155+
http.ServeContent(ctx.Resp, ctx.Req.Request, file, fi.ModTime(), rd)
156+
return true
157+
}
158+
}
159+
148160
http.ServeContent(ctx.Resp, ctx.Req.Request, file, fi.ModTime(), f)
149161
return true
150162
}

vendor/github.com/shurcooL/httpgzip/.travis.yml

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/shurcooL/httpgzip/LICENSE

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/shurcooL/httpgzip/README.md

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/shurcooL/httpgzip/doc.go

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)