Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#309 Adjust :uri to comply with RFC2396 #501

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/clj_http/client.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
[clojure.string :as str]
[clojure.walk :refer [keywordize-keys prewalk]])
(:import (java.io InputStream File ByteArrayOutputStream ByteArrayInputStream)
(java.net URL UnknownHostException)
(java.net URI URL UnknownHostException)
(java.nio.charset StandardCharsets)
(org.apache.hc.core5.http ContentType)
(org.apache.hc.core5.http.io.entity BufferedHttpEntity ByteArrayEntity
Expand Down Expand Up @@ -173,6 +173,14 @@
(str/replace #"[^a-zA-Z0-9\.\-\_\~\!\$\&\'\(\)\*\+\,\;\=\:\@\/\%\?]"
util/url-encode))))

(defn get-url-encoded-uri
[^String url]
(-> url url-encode-illegal-characters URI. str))

(defn get-url-encoded-path
[^java.net.URL url]
(-> url .getPath url-encode-illegal-characters))

(defn parse-url
"Parse a URL string into a map of interesting parts."
[url]
Expand All @@ -181,7 +189,8 @@
:server-name (.getHost url-parsed)
:server-port (when-pos (.getPort url-parsed))
:url url
:uri (url-encode-illegal-characters (.getPath url-parsed))
:uri (get-url-encoded-uri url)
:path (get-url-encoded-path url-parsed)
:user-info (if-let [user-info (.getUserInfo url-parsed)]
(util/url-decode user-info))
:query-string (url-encode-illegal-characters (.getQuery url-parsed))}))
Expand All @@ -190,14 +199,14 @@
"Takes a map of url-parts and generates a string representation.
WARNING: does not do any sort of encoding! Don't use this for strict RFC
following!"
[{:keys [scheme server-name server-port uri user-info query-string]}]
[{:keys [scheme server-name server-port uri path user-info query-string]}]
(str (name scheme) "://"
(if (seq user-info)
(str user-info "@" server-name)
server-name)
(when server-port
(str ":" server-port))
uri
path
(when (seq query-string)
(str "?" query-string))))

Expand Down
16 changes: 8 additions & 8 deletions test/clj_http/test/client_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@
(is (= :http (:scheme resp)))
(is (= "google.com" (:server-name resp)))
(is (= 8080 (:server-port resp)))
(is (= "/baz%20foo" (:uri resp)))
(is (= "/baz%20foo" (:path resp)))
(is (= "bar=bat%20bit?" (:query-string resp)))))

(deftest apply-on-url
Expand All @@ -653,7 +653,7 @@
(is (= :http (:scheme @resp)))
(is (= "google.com" (:server-name @resp)))
(is (= 8080 (:server-port @resp)))
(is (= "/baz%20foo" (:uri @resp)))
(is (= "/baz%20foo" (:path @resp)))
(is (= "bar=bat%20bit?" (:query-string @resp)))
(is (not (realized? exception)))))

Expand Down Expand Up @@ -1144,11 +1144,11 @@
(deftest test-url-encode-path
(is (= (client/url-encode-illegal-characters "?foo bar+baz[]75")
"?foo%20bar+baz%5B%5D75"))
(is (= {:uri (str "/:@-._~!$&'()*+,="
";"
":@-._~!$&'()*+,"
"="
":@-._~!$&'()*+,==")
(is (= {:path (str "/:@-._~!$&'()*+,="
";"
":@-._~!$&'()*+,"
"="
":@-._~!$&'()*+,==")
:query-string (str "/?:@-._~!$'()*+,;"
"="
"/?:@-._~!$'()*+,;==")}
Expand All @@ -1157,7 +1157,7 @@
(str "http://example.com/:@-._~!$&'()*+,=;:@-._~!$&'()*+"
",=:@-._~!$&'()*+,==?/?:@-._~!$'()*+,;=/?:@-._~!$'("
")*+,;==#/?:@-._~!$&'()*+,;="))
[:uri :query-string])))
[:path :query-string])))
(let [all-chars (apply str (map char (range 256)))
all-legal (client/url-encode-illegal-characters all-chars)]
(is (= all-legal
Expand Down