Skip to content

Commit 403e555

Browse files
author
Peter Taoussanis
committed
Added support for scheme-relative URLs.
* Modified core/absolute-url?. * Added core/route-compile lex rule. * Added example to README. * Added corresponding unit test. * All tests passing: new and old. Signed-off-by: Peter Taoussanis <[email protected]>
1 parent b1f93e9 commit 403e555

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
@@ -111,9 +111,9 @@
111111
(recur results src clauses))))))
112112

113113
(defn- absolute-url?
114-
"True if the path contains an absolute URL."
114+
"True if the path contains an absolute or scheme-relative URL."
115115
[path]
116-
(boolean (re-matches #"https?://.*" path)))
116+
(boolean (re-matches #"(https?:)?//.*" path)))
117117

118118
(defn route-compile
119119
"Compile a path string using the routes syntax into a uri-matcher struct."
@@ -130,6 +130,7 @@
130130
(apply str
131131
(lex path
132132
splat "(.*?)"
133+
#"^//" "https?://"
133134
word #(str "(" (word-regex %) ")")
134135
literal #(re-escape (.group %)))))
135136
(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)