forked from imgproxy/imgproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_path.go
More file actions
34 lines (27 loc) · 876 Bytes
/
fix_path.go
File metadata and controls
34 lines (27 loc) · 876 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package main
import (
"fmt"
"regexp"
"strings"
)
var fixPathRe = regexp.MustCompile(`^(\S+)\:/([^/])`)
func fixPath(path string) string {
// Cut the path at the `/plain/` segment to process those parts separately
options, plainURL, hasPlain := strings.Cut(path, "/plain/")
// Some proxies/CDNs may encode `:` in options as `%3A`, so we need to unescape it first
path = strings.ReplaceAll(options, "%3A", ":")
if hasPlain {
// Some proxies/CDNs may "normalize" URLs by replacing `scheme://` with `scheme:/`
// in the plain URL part, so we need to fix it back.
if match := fixPathRe.FindStringSubmatch(plainURL); match != nil {
repl := fmt.Sprintf("%s://", match[1])
if match[1] == "local" {
repl += "/"
}
repl += match[2]
plainURL = strings.Replace(plainURL, match[0], repl, 1)
}
return path + "/plain/" + plainURL
}
return path
}