Skip to content

Commit bfe7409

Browse files
jafingerhutstuarthalloway
authored andcommitted
CLJ-1177: Treat URLs with consecutive %HH escapes as UTF-8 encoded bytes
Signed-off-by: Stuart Halloway <[email protected]>
1 parent 39aa021 commit bfe7409

File tree

2 files changed

+8
-11
lines changed
  • src/clj/clojure/java
  • test/clojure/test_clojure/java

2 files changed

+8
-11
lines changed

src/clj/clojure/java/io.clj

+7-11
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
StringReader ByteArrayInputStream
2020
BufferedInputStream BufferedOutputStream
2121
CharArrayReader Closeable)
22-
(java.net URI URL MalformedURLException Socket)))
22+
(java.net URI URL MalformedURLException Socket URLDecoder URLEncoder)))
2323

2424
(def
2525
^{:doc "Type object for a Java primitive byte array."
@@ -37,6 +37,10 @@
3737
(^{:tag java.io.File, :added "1.2"} as-file [x] "Coerce argument to a file.")
3838
(^{:tag java.net.URL, :added "1.2"} as-url [x] "Coerce argument to a URL."))
3939

40+
(defn- escaped-utf8-urlstring->str [s]
41+
(-> (clojure.string/replace s "+" (URLEncoder/encode "+" "UTF-8"))
42+
(URLDecoder/decode "UTF-8")))
43+
4044
(extend-protocol Coercions
4145
nil
4246
(as-file [_] nil)
@@ -54,16 +58,8 @@
5458
(as-url [u] u)
5559
(as-file [u]
5660
(if (= "file" (.getProtocol u))
57-
(as-file
58-
(clojure.string/replace
59-
(.replace (.getFile u) \/ File/separatorChar)
60-
#"%.."
61-
(fn [escape]
62-
(-> escape
63-
(.substring 1 3)
64-
(Integer/parseInt 16)
65-
(char)
66-
(str)))))
61+
(as-file (escaped-utf8-urlstring->str
62+
(.replace (.getFile u) \/ File/separatorChar)))
6763
(throw (IllegalArgumentException. (str "Not a file: " u)))))
6864

6965
URI

test/clojure/test_clojure/java/io.clj

+1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126
(File. "bar+baz") (URL. "file:bar+baz")
127127
(File. "bar baz qux") (URL. "file:bar%20baz%20qux")
128128
(File. "quux") (URI. "file:quux")
129+
(File. "abcíd/foo.txt") (URL. "file:abc%c3%add/foo.txt")
129130
nil nil))
130131

131132
(deftest test-resources-with-spaces

0 commit comments

Comments
 (0)