@@ -3,6 +3,7 @@ package url
3
3
import (
4
4
"math"
5
5
"net/url"
6
+ "path"
6
7
"reflect"
7
8
"strconv"
8
9
"strings"
@@ -131,6 +132,14 @@ func isSpecialProtocol(protocol string) bool {
131
132
return false
132
133
}
133
134
135
+ func isSpecialNetProtocol (protocol string ) bool {
136
+ switch protocol {
137
+ case "https" , "http" , "ftp" , "wss" , "ws" :
138
+ return true
139
+ }
140
+ return false
141
+ }
142
+
134
143
func clearURLPort (u * url.URL ) {
135
144
u .Host = u .Hostname ()
136
145
}
@@ -167,6 +176,9 @@ func (m *urlModule) parseURL(s string, isBase bool) *url.URL {
167
176
if isBase && ! u .IsAbs () {
168
177
panic (m .newInvalidURLError (URLNotAbsolute , s ))
169
178
}
179
+ if isSpecialNetProtocol (u .Scheme ) && u .Host == "" && u .Path == "" {
180
+ panic (m .newInvalidURLError (InvalidURL , s ))
181
+ }
170
182
if portStr := u .Port (); portStr != "" {
171
183
if port , err := strconv .Atoi (portStr ); err != nil || isDefaultURLPort (u .Scheme , port ) {
172
184
u .Host = u .Hostname () // Clear port
@@ -182,12 +194,19 @@ func fixRawQuery(u *url.URL) {
182
194
}
183
195
}
184
196
197
+ func cleanPath (p , proto string ) string {
198
+ if ! strings .HasPrefix (p , "/" ) && (isSpecialProtocol (proto ) || p != "" ) {
199
+ p = "/" + p
200
+ }
201
+ if p != "" {
202
+ return path .Clean (p )
203
+ }
204
+ return ""
205
+ }
206
+
185
207
func (m * urlModule ) fixURL (u * url.URL ) {
186
- switch u .Scheme {
187
- case "https" , "http" , "ftp" , "wss" , "ws" :
188
- if u .Path == "" {
189
- u .Path = "/"
190
- }
208
+ u .Path = cleanPath (u .Path , u .Scheme )
209
+ if isSpecialNetProtocol (u .Scheme ) {
191
210
hostname := u .Hostname ()
192
211
lh := strings .ToLower (hostname )
193
212
ch , err := idna .Punycode .ToASCII (lh )
@@ -262,16 +281,7 @@ func (m *urlModule) createURLPrototype() *goja.Object {
262
281
m .defineURLAccessorProp (p , "pathname" , func (u * nodeURL ) interface {} {
263
282
return u .url .EscapedPath ()
264
283
}, func (u * nodeURL , arg goja.Value ) {
265
- p := arg .String ()
266
- if _ , err := url .Parse (p ); err == nil {
267
- switch u .url .Scheme {
268
- case "https" , "http" , "ftp" , "ws" , "wss" :
269
- if ! strings .HasPrefix (p , "/" ) {
270
- p = "/" + p
271
- }
272
- }
273
- u .url .Path = p
274
- }
284
+ u .url .Path = cleanPath (arg .String (), u .url .Scheme )
275
285
})
276
286
277
287
// origin
0 commit comments