Skip to content

Commit ac93602

Browse files
committed
Merge pull request weavejester#7 from ptaoussanis/master
Added support for scheme-relative URLs.
2 parents e22a500 + 403e555 commit ac93602

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ Clout can also match absolute routes:
3333
(request :get "http://subdomain.example.com/"))
3434
{}
3535

36+
And scheme-relative routes:
37+
38+
user=> (route-matches "//subdomain.example.com/"
39+
(request :get "http://subdomain.example.com/"))
40+
{}
41+
user=> (route-matches "//subdomain.example.com/"
42+
(request :get "https://subdomain.example.com/"))
43+
{}
44+
3645
Clout supports both keywords and wildcards. Keywords (like ":title") will
3746
match any character but the following: `/ . , ; ?`. Wildcards (*) will match
3847
anything.

src/clout/core.clj

+3-2
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@
112112
(recur results src clauses))))))
113113

114114
(defn- absolute-url?
115-
"True if the path contains an absolute URL."
115+
"True if the path contains an absolute or scheme-relative URL."
116116
[path]
117-
(boolean (re-matches #"https?://.*" path)))
117+
(boolean (re-matches #"(https?:)?//.*" path)))
118118

119119
(defn route-compile
120120
"Compile a path string using the routes syntax into a uri-matcher struct."
@@ -131,6 +131,7 @@
131131
(apply str
132132
(lex path
133133
splat "(.*?)"
134+
#"^//" "https?://"
134135
word #(str "(" (word-regex %) ")")
135136
literal #(re-escape (.group ^Matcher %)))))
136137
(remove nil?

test/clout/test/core.clj

+11-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,17 @@
7474
"http://localhost/"
7575
{:scheme :http
7676
:headers {"host" "localhost"}
77-
:uri "/"})))
77+
:uri "/"}))
78+
(is (route-matches
79+
"//localhost/"
80+
{:scheme :http
81+
:headers {"host" "localhost"}
82+
:uri "/"}))
83+
(is (route-matches
84+
"//localhost/"
85+
{:scheme :https
86+
:headers {"host" "localhost"}
87+
:uri "/"})))
7888

7989
(deftest url-port-paths
8090
(let [req (request :get "http://localhost:8080/")]

0 commit comments

Comments
 (0)