|
3 | 3 | (:require [clojure.string :as str]) |
4 | 4 | (:import java.util.Map |
5 | 5 | clojure.lang.MapEntry |
| 6 | + java.nio.charset.Charset |
6 | 7 | [java.net URLEncoder URLDecoder] |
7 | 8 | [java.util Base64 StringTokenizer])) |
8 | 9 |
|
|
28 | 29 | `(double-escape ~x) |
29 | 30 | x)) |
30 | 31 |
|
| 32 | +(def ^:private ^Charset utf-8 (Charset/forName "UTF-8")) |
| 33 | + |
| 34 | +(defn- ^Charset to-charset [s] |
| 35 | + (if (nil? s) utf-8 (if (string? s) (Charset/forName s) s))) |
| 36 | + |
31 | 37 | (defn percent-encode |
32 | 38 | "Percent-encode every character in the given string using either the specified |
33 | 39 | encoding, or UTF-8 by default." |
34 | 40 | ([unencoded] |
35 | | - (percent-encode unencoded "UTF-8")) |
36 | | - ([^String unencoded ^String encoding] |
37 | | - (->> (.getBytes unencoded encoding) |
| 41 | + (percent-encode unencoded utf-8)) |
| 42 | + ([^String unencoded encoding] |
| 43 | + (->> (.getBytes unencoded (to-charset encoding)) |
38 | 44 | (map (partial format "%%%02X")) |
39 | 45 | (str/join)))) |
40 | 46 |
|
|
53 | 59 | "Decode every percent-encoded character in the given string using the |
54 | 60 | specified encoding, or UTF-8 by default." |
55 | 61 | ([encoded] |
56 | | - (percent-decode encoded "UTF-8")) |
57 | | - ([^String encoded ^String encoding] |
58 | | - (str/replace encoded |
59 | | - #"(?:%[A-Fa-f0-9]{2})+" |
60 | | - (fn [chars] |
61 | | - (-> (parse-bytes chars) |
62 | | - (String. encoding) |
63 | | - (fix-string-replace-bug)))))) |
| 62 | + (percent-decode encoded utf-8)) |
| 63 | + ([^String encoded encoding] |
| 64 | + (let [encoding (to-charset encoding)] |
| 65 | + (str/replace encoded |
| 66 | + #"(?:%[A-Fa-f0-9]{2})+" |
| 67 | + (fn [chars] |
| 68 | + (-> (parse-bytes chars) |
| 69 | + (String. encoding) |
| 70 | + (fix-string-replace-bug))))))) |
64 | 71 |
|
65 | 72 | (defn url-encode |
66 | 73 | "Returns the url-encoded version of the given string, using either a specified |
67 | 74 | encoding or UTF-8 by default." |
68 | 75 | ([unencoded] |
69 | | - (url-encode unencoded "UTF-8")) |
| 76 | + (url-encode unencoded utf-8)) |
70 | 77 | ([unencoded encoding] |
71 | | - (str/replace |
72 | | - unencoded |
73 | | - #"[^A-Za-z0-9_~.+-]+" |
74 | | - #(double-escape (percent-encode % encoding))))) |
| 78 | + (let [encoding (to-charset encoding)] |
| 79 | + (str/replace |
| 80 | + unencoded |
| 81 | + #"[^A-Za-z0-9_~.+-]+" |
| 82 | + #(double-escape (percent-encode % encoding)))))) |
75 | 83 |
|
76 | 84 | (defn ^String url-decode |
77 | 85 | "Returns the url-decoded version of the given string, using either a specified |
78 | 86 | encoding or UTF-8 by default. If the encoding is invalid, nil is returned." |
79 | 87 | ([encoded] |
80 | | - (url-decode encoded "UTF-8")) |
| 88 | + (url-decode encoded utf-8)) |
81 | 89 | ([encoded encoding] |
82 | | - (percent-decode encoded encoding))) |
| 90 | + (percent-decode encoded (to-charset encoding)))) |
83 | 91 |
|
84 | 92 | (defn base64-encode |
85 | 93 | "Encode an array of bytes into a base64 encoded string." |
|
97 | 105 | (extend-protocol FormEncodeable |
98 | 106 | String |
99 | 107 | (form-encode* [^String unencoded ^String encoding] |
100 | | - (URLEncoder/encode unencoded encoding)) |
| 108 | + (URLEncoder/encode unencoded (to-charset encoding))) |
101 | 109 | Map |
102 | 110 | (form-encode* [params encoding] |
103 | 111 | (letfn [(encode [x] (form-encode* x encoding)) |
|
121 | 129 | URL query strings and POST request bodies, using the specified encoding. |
122 | 130 | If the encoding is not specified, it defaults to UTF-8" |
123 | 131 | ([x] |
124 | | - (form-encode x "UTF-8")) |
| 132 | + (form-encode x utf-8)) |
125 | 133 | ([x encoding] |
126 | 134 | (form-encode* x encoding))) |
127 | 135 |
|
|
132 | 140 | "Decode the supplied www-form-urlencoded string using the specified encoding, |
133 | 141 | or UTF-8 by default." |
134 | 142 | ([encoded] |
135 | | - (form-decode-str encoded "UTF-8")) |
| 143 | + (form-decode-str encoded utf-8)) |
136 | 144 | ([^String encoded encoding] |
137 | 145 | (if (form-encoded-chars? encoded) |
138 | 146 | (try |
139 | | - (let [^String encoding (or encoding "UTF-8")] |
140 | | - (URLDecoder/decode encoded encoding)) |
| 147 | + (URLDecoder/decode encoded (to-charset encoding)) |
141 | 148 | (catch Exception _ nil)) |
142 | 149 | encoded))) |
143 | 150 |
|
|
162 | 169 | or UTF-8 by default. If the encoded value is a string, a string is returned. |
163 | 170 | If the encoded value is a map of parameters, a map is returned." |
164 | 171 | ([encoded] |
165 | | - (form-decode encoded "UTF-8")) |
| 172 | + (form-decode encoded utf-8)) |
166 | 173 | ([^String encoded encoding] |
167 | 174 | (if-not (.contains encoded "=") |
168 | 175 | (form-decode-str encoded encoding) |
|
0 commit comments