1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-20 03:17:04 +00:00
Former-commit-id: 60d9b0d77693895fdfbebe83712e9cc1ee3f8f26
This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-07-17 12:03:20 +03:00
parent baeff3e80b
commit 0f79728c88
6 changed files with 59 additions and 33 deletions

View File

@@ -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
@@ -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
}
@@ -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 {
@@ -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 {
@@ -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) {