From 5aa62ebfa7db8c5dc3a1a9a00bebd8331d3532d5 Mon Sep 17 00:00:00 2001 From: "kim (grufwub)" Date: Tue, 12 May 2020 14:04:16 +0100 Subject: [PATCH] improve file remap regex matching Signed-off-by: kim (grufwub) --- filesystem.go | 2 +- gophor.go | 2 +- regex.go | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/filesystem.go b/filesystem.go index aa49e2e..1ab03a7 100644 --- a/filesystem.go +++ b/filesystem.go @@ -53,7 +53,7 @@ func (fs *FileSystem) RemapRequestPath(requestPath *RequestPath) (*RequestPath, /* Create new path from template and submatches */ newPath := make([]byte, 0) - for _, submatches := range remap.Regex.FindAllStringSubmatchIndex(requestPath.Selector(), -1) { + for _, submatches := range remap.Regex.FindAllStringSubmatchIndex(requestPath.Relative(), -1) { newPath = remap.Regex.ExpandString(newPath, remap.Template, requestPath.Relative(), submatches) } diff --git a/gophor.go b/gophor.go index 58c8537..e22f33d 100644 --- a/gophor.go +++ b/gophor.go @@ -72,7 +72,6 @@ func setupServer() []*GophorListener { /* File system */ fileMonitorFreq := flag.Duration("file-monitor-freq", time.Second*60, "Change file monitor frequency.") - fileRemaps := flag.String("file-remap", "", "New-line separated list of file remappings of format: /virtual/relative/path -> /actual/relative/path") /* Cache settings */ cacheSize := flag.Int("cache-size", 50, "Change file cache size, measured in file count.") @@ -88,6 +87,7 @@ func setupServer() []*GophorListener { /* Regex */ restrictedFiles := flag.String("restrict-files", "", "New-line separated list of regex statements restricting accessible files.") + fileRemaps := flag.String("file-remap", "", "New-line separated list of file remappings of format: /virtual/relative/path -> /actual/relative/path") /* User supplied caps.txt information */ serverDescription := flag.String("description", "Gophor, a Gopher server in Go.", "Change server description in generated caps.txt.") diff --git a/regex.go b/regex.go index ecb42e7..9213ea0 100644 --- a/regex.go +++ b/regex.go @@ -11,8 +11,8 @@ const ( ) type FileRemap struct { - Regex *regexp.Regexp - Template string + Regex *regexp.Regexp + Template string } /* Compile a user supplied new line separated list of regex statements */ @@ -60,7 +60,7 @@ func compileUserRemapRegex(remaps string) []*FileRemap { } /* Try compile regex */ - regex, err := regexp.Compile(strings.TrimPrefix(split[0], "/")) + regex, err := regexp.Compile("(?m)"+strings.TrimPrefix(split[0], "/")+"$") if err != nil { log.Fatalf("Failed compiling user supplied regex: %s\n", expr) }