summaryrefslogtreecommitdiff
path: root/vendor/github.com/playwright-community/playwright-go/glob.go
blob: 77deba33ff64002ec57ebccdd24b65a22c7a1c0a (plain)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package playwright

import (
	"net/url"
	"regexp"
	"strconv"
	"strings"
)

var escapedChars = map[rune]bool{
	'$':  true,
	'^':  true,
	'+':  true,
	'.':  true,
	'*':  true,
	'(':  true,
	')':  true,
	'|':  true,
	'\\': true,
	'?':  true,
	'{':  true,
	'}':  true,
	'[':  true,
	']':  true,
}

func globMustToRegex(glob string) *regexp.Regexp {
	tokens := []string{"^"}
	inGroup := false

	for i := 0; i < len(glob); i++ {
		c := rune(glob[i])
		if c == '\\' && i+1 < len(glob) {
			char := rune(glob[i+1])
			if _, ok := escapedChars[char]; ok {
				tokens = append(tokens, "\\"+string(char))
			} else {
				tokens = append(tokens, string(char))
			}
			i++
		} else if c == '*' {
			beforeDeep := rune(0)
			if i > 0 {
				beforeDeep = rune(glob[i-1])
			}
			starCount := 1
			for i+1 < len(glob) && glob[i+1] == '*' {
				starCount++
				i++
			}
			afterDeep := rune(0)
			if i+1 < len(glob) {
				afterDeep = rune(glob[i+1])
			}
			isDeep := starCount > 1 && (beforeDeep == '/' || beforeDeep == 0) && (afterDeep == '/' || afterDeep == 0)
			if isDeep {
				tokens = append(tokens, "((?:[^/]*(?:/|$))*)")
				i++
			} else {
				tokens = append(tokens, "([^/]*)")
			}
		} else {
			switch c {
			case '{':
				inGroup = true
				tokens = append(tokens, "(")
			case '}':
				inGroup = false
				tokens = append(tokens, ")")
			case ',':
				if inGroup {
					tokens = append(tokens, "|")
				} else {
					tokens = append(tokens, "\\"+string(c))
				}
			default:
				if _, ok := escapedChars[c]; ok {
					tokens = append(tokens, "\\"+string(c))
				} else {
					tokens = append(tokens, string(c))
				}
			}
		}
	}

	tokens = append(tokens, "$")
	return regexp.MustCompile(strings.Join(tokens, ""))
}

func resolveGlobToRegex(baseURL *string, glob string, isWebSocketUrl bool) *regexp.Regexp {
	if isWebSocketUrl {
		baseURL = toWebSocketBaseURL(baseURL)
	}
	glob = resolveGlobBase(baseURL, glob)
	return globMustToRegex(glob)
}

func resolveGlobBase(baseURL *string, match string) string {
	if strings.HasPrefix(match, "*") {
		return match
	}

	tokenMap := make(map[string]string)
	mapToken := func(original string, replacement string) string {
		if len(original) == 0 {
			return ""
		}
		tokenMap[replacement] = original
		return replacement
	}
	// Escaped `\\?` behaves the same as `?` in our glob patterns.
	match = strings.ReplaceAll(match, `\\?`, "?")
	// Glob symbols may be escaped in the URL and some of them such as ? affect resolution,
	// so we replace them with safe components first.
	relativePath := strings.Split(match, "/")
	for i, token := range relativePath {
		if token == "." || token == ".." || token == "" {
			continue
		}
		// Handle special case of http*://, note that the new schema has to be
		// a web schema so that slashes are properly inserted after domain.
		if i == 0 && strings.HasSuffix(token, ":") {
			relativePath[i] = mapToken(token, "http:")
		} else {
			questionIndex := strings.Index(token, "?")
			if questionIndex == -1 {
				relativePath[i] = mapToken(token, "$_"+strconv.Itoa(i)+"_$")
			} else {
				newPrefix := mapToken(token[:questionIndex], "$_"+strconv.Itoa(i)+"_$")
				newSuffix := mapToken(token[questionIndex:], "?$"+strconv.Itoa(i)+"_$")
				relativePath[i] = newPrefix + newSuffix
			}
		}
	}
	resolved := constructURLBasedOnBaseURL(baseURL, strings.Join(relativePath, "/"))
	for token, original := range tokenMap {
		resolved = strings.ReplaceAll(resolved, token, original)
	}
	return resolved
}

func constructURLBasedOnBaseURL(baseURL *string, givenURL string) string {
	u, err := url.Parse(givenURL)
	if err != nil {
		return givenURL
	}
	if baseURL != nil {
		base, err := url.Parse(*baseURL)
		if err != nil {
			return givenURL
		}
		u = base.ResolveReference(u)
	}
	if u.Path == "" { // In Node.js, new URL('http://localhost') returns 'http://localhost/'.
		u.Path = "/"
	}
	return u.String()
}

func toWebSocketBaseURL(baseURL *string) *string {
	if baseURL == nil {
		return nil
	}

	// Allow http(s) baseURL to match ws(s) urls.
	re := regexp.MustCompile(`(?m)^http(s?://)`)
	wsBaseURL := re.ReplaceAllString(*baseURL, "ws$1")

	return &wsBaseURL
}