Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Resolve relative to root context better #60

Merged
merged 2 commits into from

2 participants

@jcrossley3

When the base-url is "/" (the root context), resolving "/bar" relative to it results in "//bar", which causes the browser to interpret "bar" as a hostname. :(

@weavejester
Owner

Looks good.

@weavejester weavejester merged commit 7ab1bb6 into weavejester:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 20 additions and 1 deletion.
  1. +4 −1 src/hiccup/util.clj
  2. +16 −0 test/hiccup/test/util.clj
View
5 src/hiccup/util.clj
@@ -26,7 +26,10 @@
(if (or (.getHost u)
(not (-> (.getPath u) (.startsWith "/"))))
(str u)
- (str *base-url* u)))
+ (let [base (str *base-url*)]
+ (if (.endsWith base "/")
+ (str (subs base 0 (dec (count base))) u)
+ (str base u)))))
Object
(to-str [x] (str x))
nil
View
16 test/hiccup/test/util.clj
@@ -30,6 +30,22 @@
(is (= (to-str (to-uri "https://example.com/bar")) "https://example.com/bar"))
(is (= (to-str (to-uri "bar")) "bar"))
(is (= (to-str (to-uri "../bar")) "../bar"))
+ (is (= (to-str (to-uri "//example.com/bar")) "//example.com/bar"))))
+ (testing "with base URL for root context"
+ (with-base-url "/"
+ (is (= (to-str (to-uri "/bar")) "/bar"))
+ (is (= (to-str (to-uri "http://example.com")) "http://example.com"))
+ (is (= (to-str (to-uri "https://example.com/bar")) "https://example.com/bar"))
+ (is (= (to-str (to-uri "bar")) "bar"))
+ (is (= (to-str (to-uri "../bar")) "../bar"))
+ (is (= (to-str (to-uri "//example.com/bar")) "//example.com/bar"))))
+ (testing "with base URL containing trailing slash"
+ (with-base-url "/foo/"
+ (is (= (to-str (to-uri "/bar")) "/foo/bar"))
+ (is (= (to-str (to-uri "http://example.com")) "http://example.com"))
+ (is (= (to-str (to-uri "https://example.com/bar")) "https://example.com/bar"))
+ (is (= (to-str (to-uri "bar")) "bar"))
+ (is (= (to-str (to-uri "../bar")) "../bar"))
(is (= (to-str (to-uri "//example.com/bar")) "//example.com/bar")))))
(deftest test-url-encode
Something went wrong with that request. Please try again.