1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-08 12:31:58 +00:00

New iris.DirOptions.PushTargetsRegexp

rel to: https://github.com/kataras/iris/issues/1562#issuecomment-659702891


Former-commit-id: 778cf9146b730d424040ea9e259ce6500f53b563
This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-07-18 06:10:16 +03:00
parent dab03102f3
commit 552f990358
4 changed files with 116 additions and 12 deletions

View File

@@ -11,6 +11,7 @@ import (
"os"
"path"
"path/filepath"
"regexp"
"sort"
"strings"
"time"
@@ -48,7 +49,23 @@ type DirOptions struct {
// 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`).
//
// Example:
// "/": {
// "favicon.ico",
// "js/main.js",
// "css/main.css",
// }
PushTargets map[string][]string
// PushTargetsRegexp like `PushTargets` but accepts regexp which
// is compared against all files under a directory (recursively).
// The `IndexName` should be set.
//
// Example:
// "/": regexp.MustCompile("((.*).js|(.*).css|(.*).ico)$")
// See `iris.MatchCommonAssets` too.
PushTargetsRegexp map[string]*regexp.Regexp
// When files should served under compression.
Compress bool
@@ -407,7 +424,11 @@ func FileServer(directory string, opts ...DirOptions) context.Handler {
return
}
indexFound := false
var (
indexFound bool
indexDirectory http.File
)
// use contents of index.html for directory, if present
if info.IsDir() && options.IndexName != "" {
// Note that, in contrast of the default net/http mechanism;
@@ -425,9 +446,11 @@ func FileServer(directory string, opts ...DirOptions) context.Handler {
defer fIndex.Close()
infoIndex, err := fIndex.Stat()
if err == nil {
info = infoIndex
f = fIndex
indexFound = true
indexDirectory = f
f = fIndex
info = infoIndex
}
}
}
@@ -454,6 +477,25 @@ func FileServer(directory string, opts ...DirOptions) context.Handler {
}
}
}
if regex, ok := options.PushTargetsRegexp[r.URL.Path]; ok {
if pusher, ok := ctx.ResponseWriter().(http.Pusher); ok {
for _, indexAsset := range getFilenamesRecursively(fs, indexDirectory, "") {
// it's an index file, do not pushed that.
if strings.HasSuffix("/"+indexAsset, options.IndexName) {
continue
}
// match using relative path (without the first '/' slash)
// to keep consistency between the `PushTargets` behavior
if regex.MatchString(indexAsset) {
// println("Regex Matched: " + indexAsset)
if err = pusher.Push(path.Join(r.RequestURI, indexAsset), nil); err != nil {
break
}
}
}
}
}
}
// Still a directory? (we didn't find an index.html file)
@@ -532,6 +574,38 @@ func FileServer(directory string, opts ...DirOptions) context.Handler {
return h
}
func getFilenamesRecursively(fs http.FileSystem, f http.File, parent string) []string {
defer f.Close()
info, err := f.Stat()
if err != nil {
return nil
}
var filenames []string
if info.IsDir() {
fileinfos, err := f.Readdir(-1)
if err != nil {
return nil
}
for _, fileinfo := range fileinfos {
fullname := path.Join(parent, fileinfo.Name())
ff, err := fs.Open(fullname)
if err != nil {
return nil
}
filenames = append(filenames, getFilenamesRecursively(fs, ff, fullname)...)
}
return filenames
}
filenames = append(filenames, path.Dir(path.Join(parent, info.Name())))
return filenames
}
// StripPrefix returns a handler that serves HTTP requests
// by removing the given prefix from the request URL's Path
// and invoking the handler h. StripPrefix handles a