Skip to content

Commit

Permalink
https://github.com/kataras/iris/issues/1562#issuecomment-659845730
Browse files Browse the repository at this point in the history
  • Loading branch information
kataras committed Jul 17, 2020
1 parent 4e3836e commit 961533f
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func newApp() *iris.Application {
AssetInfo: GzipAssetInfo,
AssetNames: GzipAssetNames,
AssetValidator: func(ctx iris.Context, name string) bool {
ctx.Header("Vary", "Accept-Encoding")
// ctx.Header("Vary", "Accept-Encoding")
ctx.Header("Content-Encoding", "gzip")
return true
},
Expand Down
11 changes: 6 additions & 5 deletions _examples/file-server/http2push-embedded-gzipped/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ import (
var opts = iris.DirOptions{
IndexName: "/index.html",
PushTargets: map[string][]string{
"/": {
"/public/favicon.ico",
"/public/js/main.js",
"/public/css/main.css",
"/": { // Relative path without route prefix.
"favicon.ico",
"js/main.js",
"css/main.css",
// ^ Relative to the index, if need absolute ones start with a slash ('/').
},
},
Compress: false, // SHOULD be set to false, files already compressed.
Expand All @@ -30,7 +31,7 @@ var opts = iris.DirOptions{
AssetNames: GzipAssetNames,
// Required for pre-compressed files:
AssetValidator: func(ctx iris.Context, _ string) bool {
ctx.Header("Vary", "Content-Encoding")
// ctx.Header("Vary", "Content-Encoding")
ctx.Header("Content-Encoding", "gzip")
return true
},
Expand Down
9 changes: 5 additions & 4 deletions _examples/file-server/http2push-embedded/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ import (
var opts = iris.DirOptions{
IndexName: "/index.html",
PushTargets: map[string][]string{
"/": {
"/public/favicon.ico",
"/public/js/main.js",
"/public/css/main.css",
"/": { // Relative path without route prefix.
"favicon.ico",
"js/main.js",
"css/main.css",
// ^ Relative to the index, if need absolute ones start with a slash ('/').
},
},
Compress: false,
Expand Down
11 changes: 6 additions & 5 deletions _examples/file-server/http2push/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ import (

var opts = iris.DirOptions{
IndexName: "/index.html",
// Optionally register files (map's absolute values) to be served
// Optionally register files (map's values) to be served
// when a specific path (map's key WITHOUT prefix) is requested
// is fired before client asks (HTTP/2 Push).
// E.g. "/" (which serves the `IndexName` if not empty).
//
// Note: Requires running server under TLS,
// that's why we use `iris.TLS` below.
PushTargets: map[string][]string{
"/": {
"/public/favicon.ico",
"/public/js/main.js",
"/public/css/main.css",
"/": { // Relative path without route prefix.
"favicon.ico",
"js/main.js",
"css/main.css",
// ^ Relative to the index, if need absolute ones start with a slash ('/').
},
},
Compress: true,
Expand Down
4 changes: 2 additions & 2 deletions context/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ var _ ResponseWriter = (*CompressResponseWriter)(nil)
// It returns the best candidate among "gzip", "defate", "br", "snappy" and "s2"
// based on the request's "Accept-Encoding" header value.
func AcquireCompressResponseWriter(w ResponseWriter, r *http.Request, level int) (*CompressResponseWriter, error) {
acceptEncoding := r.Header.Values(AcceptEncodingHeaderKey)

// acceptEncoding := r.Header.Values(AcceptEncodingHeaderKey)
acceptEncoding := r.Header[AcceptEncodingHeaderKey]
if len(acceptEncoding) == 0 {
return nil, ErrResponseNotCompressed
}
Expand Down
55 changes: 39 additions & 16 deletions core/router/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ type DirOptions struct {
// that another handler, called index handler, is auto-registered by the framework
// if end developer does not managed to handle it by hand.
IndexName string
// PushTargets optionally absolute filenames (map's value) to be served without any
// additional client's requests (HTTP/2 Push)
// when a specific path (map's key) is requested and
// it's not a directory (it's an `IndexFile`).
// PushTargets filenames (map's value) to
// be served without additional client's requests (HTTP/2 Push)
// when a specific request path (map's key WITHOUT prefix)
// is requested and it's not a directory (it's an `IndexFile`).
PushTargets map[string][]string
// When files should served under compression.
Compress bool
Expand Down Expand Up @@ -88,6 +88,14 @@ func getDirOptions(opts ...DirOptions) (options DirOptions) {
options.Attachments.Burst = 0
}

// Make sure PushTarget's paths are in the proper form.
for path, filenames := range options.PushTargets {
for idx, filename := range filenames {
filenames[idx] = filepath.ToSlash(filename)
}
options.PushTargets[path] = filenames
}

return
}

Expand Down Expand Up @@ -382,8 +390,9 @@ func FileServer(directory string, opts ...DirOptions) context.Handler {
}

h := func(ctx *context.Context) {
name := prefix(ctx.Request().URL.Path, "/")
ctx.Request().URL.Path = name
r := ctx.Request()
name := prefix(r.URL.Path, "/")
r.URL.Path = name

f, err := fs.Open(name)
if err != nil {
Expand Down Expand Up @@ -423,6 +432,30 @@ func FileServer(directory string, opts ...DirOptions) context.Handler {
}
}

if indexFound && !options.Attachments.Enable {
if indexAssets, ok := options.PushTargets[name]; ok {
if pusher, ok := ctx.ResponseWriter().(http.Pusher); ok {
for _, indexAsset := range indexAssets {
// pushOpts := &http.PushOptions{
// Method: "GET",
// Header: http.Header{
// "Vary": []string{"Accept-Encoding"},
// "Content-Encoding": []string{"gzip"},
// },
// }
if indexAsset[0] != '/' {
// it's relative path.
indexAsset = path.Join(r.RequestURI, indexAsset)
}

if err = pusher.Push(indexAsset, nil); err != nil {
break
}
}
}
}
}

// Still a directory? (we didn't find an index.html file)
if info.IsDir() {
if !options.ShowList {
Expand Down Expand Up @@ -486,16 +519,6 @@ func FileServer(directory string, opts ...DirOptions) context.Handler {

ctx.Compress(options.Compress)

if indexFound && len(options.PushTargets) > 0 && !options.Attachments.Enable {
if indexAssets, ok := options.PushTargets[name]; ok {
if pusher, ok := ctx.ResponseWriter().(http.Pusher); ok {
for _, indexAsset := range indexAssets {
pusher.Push(indexAsset, nil)
}
}
}
}

// If limit is 0 then same as ServeContent.
ctx.ServeContentWithRate(f, info.Name(), info.ModTime(), options.Attachments.Limit, options.Attachments.Burst)
if serveCode := ctx.GetStatusCode(); context.StatusCodeNotSuccessful(serveCode) {
Expand Down

0 comments on commit 961533f

Please sign in to comment.