Skip to content

Commit a5dd780

Browse files
committed
url: fix parsing of relative urls in string->url/literal
Also, explicitly check for relative Location urls.
1 parent f3f9da6 commit a5dd780

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

http-easy-lib/http-easy/private/session.rkt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -282,12 +282,15 @@
282282
(string->url/literal location))
283283
(cond
284284
[(url-host location-url) location-url]
285-
[else (struct-copy
286-
url/literal location-url
287-
[scheme #:parent url (url-scheme orig)]
288-
[user #:parent url (url-user orig)]
289-
[host #:parent url (url-host orig)]
290-
[port #:parent url (url-port orig)])]))
285+
[(not (url-path-absolute? location-url))
286+
(error 'ensure-absolute-url "Location destination is relative")]
287+
[else
288+
(struct-copy
289+
url/literal location-url
290+
[scheme #:parent url (url-scheme orig)]
291+
[user #:parent url (url-user orig)]
292+
[host #:parent url (url-host orig)]
293+
[port #:parent url (url-port orig)])]))
291294

292295
(define (same-origin? a b)
293296
(and

http-easy-lib/http-easy/private/url.rkt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
(url/literal scheme user host port abs? path query fragment)))
5656

5757
(define (url/literal->string u)
58-
(match-define (url scheme user host port _ path query fragment) u)
58+
(match-define (url scheme user host port abs? path query fragment) u)
5959
(call-with-output-string
6060
(lambda (out)
6161
(when scheme
@@ -82,17 +82,19 @@
8282
(write-string "//" out)]
8383
[else
8484
(void)])
85-
(let loop ([path-components path])
86-
(unless (null? path-components)
87-
(write-char #\/ out)
85+
(unless (null? path)
86+
(when abs? (write-char #\/ out))
87+
(let loop ([path-components path])
8888
(match-define (path/param path params)
8989
(car path-components))
9090
(write-string
9191
(maybe-percent-encode
9292
(string-append path (string-join params ";"))
9393
uri-path-segment-encode)
9494
out)
95-
(loop (cdr path-components))))
95+
(unless (null? (cdr path-components))
96+
(write-char #\/ out)
97+
(loop (cdr path-components)))))
9698
(unless (null? query)
9799
(write-char #\? out)
98100
(for* ([pair (in-list query)]

http-easy-test/net/http-easy/private/url.rkt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@
6565
("http://[email protected]:5100" . "http://[email protected]:5100")
6666
("http://example.com/a/b/c" . "http://example.com/a/b/c")
6767
("http://example.com/a%2Bb.mp3" . "http://example.com/a%2Bb.mp3")
68-
("http://example.com/a%2Bb.mp3?c=d+e" . "http://example.com/a%2Bb.mp3?c=d%2Be")))
68+
("http://example.com/a%2Bb.mp3?c=d+e" . "http://example.com/a%2Bb.mp3?c=d%2Be")
69+
("a/b/c" . "a/b/c")
70+
("/a/b/c" . "/a/b/c")))
6971

7072
(for* ([pair (in-list tests)]
7173
[s (in-value (car pair))]

0 commit comments

Comments
 (0)