Skip to content

Commit

Permalink
PushTargetRegexp: simplify code
Browse files Browse the repository at this point in the history
rel to: #1562 (comment)


Former-commit-id: da04ae0543e9b743cd4989ded5983ae15316a879
  • Loading branch information
kataras committed Jul 18, 2020
1 parent e16abc9 commit fb8e677
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 49 deletions.
5 changes: 5 additions & 0 deletions _examples/file-server/http2push-embedded-gzipped/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ var opts = iris.DirOptions{
// ^ Relative to the index, if need absolute ones start with a slash ('/').
},
},
// OR:
// PushTargetsRegexp: map[string]*regexp.Regexp{
// "/": iris.MatchCommonAssets,
// "/app2/app2app3": iris.MatchCommonAssets,
// },
Compress: false, // SHOULD be set to false, files already compressed.
ShowList: true,
Asset: GzipAsset,
Expand Down
4 changes: 3 additions & 1 deletion _examples/file-server/http2push/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ var opts = iris.DirOptions{
// Match all js, css and ico files
// from all files (recursively).
// "/": regexp.MustCompile("((.*).js|(.*).css|(.*).ico)$"),
"/": iris.MatchCommonAssets,
// OR:
"/": iris.MatchCommonAssets,
"/app2/app2app3": iris.MatchCommonAssets,
},
Compress: true,
ShowList: true,
Expand Down
68 changes: 20 additions & 48 deletions core/router/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,16 @@ type embeddedFileSystem struct {

var _ http.FileSystem = (*embeddedFileSystem)(nil)

// Open implements FileSystem using os.Open, opening files for reading rooted
// and relative to the virtual directory.
func (fs *embeddedFileSystem) Open(name string) (http.File, error) {
if name != "/" {
// http://localhost:8080/app2/app2app3/dirs/
// = http://localhost:8080/app2/app2app3/dirs
name = strings.TrimSuffix(name, "/")
}

name = path.Join(fs.vdir, path.Clean("/"+name))
if d, ok := fs.dirNames[name]; ok {
return d, nil
}
Expand Down Expand Up @@ -278,13 +281,11 @@ func FileServer(directory string, opts ...DirOptions) context.Handler {
// skip any unnecessary the end-dev or the 3rd party tool may set.
var names []string
for _, name := range options.AssetNames() {
// i.e: name = static/css/main.css (including the directory, see `embeddedFileSystem.vdir`)

if !strings.HasPrefix(name, directory) {
continue
}

names = append(names, strings.TrimPrefix(name, directory))
names = append(names, filepath.ToSlash(name))
}

// Update the options.AssetNames with
Expand All @@ -298,12 +299,17 @@ func FileServer(directory string, opts ...DirOptions) context.Handler {
panic("FileServer: zero embedded files")
}

asset := func(name string) ([]byte, error) {
return options.Asset(directory + name)
}

assetInfo := func(name string) (os.FileInfo, error) {
return options.AssetInfo(directory + name)
assetInfo := options.AssetInfo
// make .Name() infos like http.Dir (base names instead of full names).
options.AssetInfo = func(name string) (os.FileInfo, error) {
info, err := assetInfo(name)
if err != nil {
return nil, err
}
return &embeddedBaseFileInfo{
baseName: path.Base(info.Name()),
FileInfo: info,
}, nil
}

dirNames := make(map[string]*embeddedDir)
Expand All @@ -326,7 +332,7 @@ func FileServer(directory string, opts ...DirOptions) context.Handler {
dirNames[dirName] = d
}

info, err := assetInfo(name)
info, err := options.AssetInfo(name)
if err != nil {
panic(fmt.Sprintf("FileServer: report as bug: file info: %s not found in: %s", name, dirName))
}
Expand All @@ -345,8 +351,8 @@ func FileServer(directory string, opts ...DirOptions) context.Handler {
vdir: directory,
dirNames: dirNames,

asset: asset,
assetInfo: assetInfo,
asset: options.Asset,
assetInfo: options.AssetInfo,
}
}
// Let it for now.
Expand Down Expand Up @@ -485,43 +491,9 @@ func FileServer(directory string, opts ...DirOptions) context.Handler {
}

if regex, ok := options.PushTargetsRegexp[r.URL.Path]; ok {
// TODO(@kataras): Fix: on physical directory the push targets regexp
// will work on root indexes but NOT
// at subindex(sub directory that contain an index file and assets).
if pusher, ok := ctx.ResponseWriter().(http.Pusher); ok {

var (
prefixURL string
indexAssets []string
)

// Use of the AssetNames (static list of filenames (no dirs)),
// improves performance vs searching on files each time.
if options.AssetNames != nil {
// This is required on embedded, and we can use it
// because info.Name returns the full name,
// http.Dir does not though.
prefixDir := strings.TrimPrefix(path.Dir(info.Name()), directory)
if prefixDir == "" || prefixDir == "." {
prefixDir = "/"
}
prefixURL = strings.TrimSuffix(r.RequestURI, prefixDir)
// currentDirname := strings.TrimPrefix(r.RequestURI, prefixURL)

for _, assetName := range options.AssetNames() {
// The file server may contain more than one directory with an index file
// so we must use the files under THIS index directory one.
if strings.HasPrefix(assetName, prefixDir) {
assetName = strings.TrimPrefix(assetName, prefixURL)
indexAssets = append(indexAssets, assetName)
}
}
} else {
prefixURL = r.RequestURI
indexAssets = getFilenamesRecursively(fs, indexDirectory, "")
}

for _, indexAsset := range indexAssets {
prefixURL := strings.TrimSuffix(r.RequestURI, name)
for _, indexAsset := range getFilenamesRecursively(fs, indexDirectory, name) {
// it's an index file, do not pushed that.
if strings.HasSuffix(prefix(indexAsset, "/"), options.IndexName) {
continue
Expand Down

0 comments on commit fb8e677

Please sign in to comment.