|
31 | 31 | (defn percent-encode |
32 | 32 | "Percent-encode every character in the given string using either the specified |
33 | 33 | encoding, or UTF-8 by default." |
34 | | - [^String unencoded & [^String encoding]] |
35 | | - (->> (.getBytes unencoded (or encoding "UTF-8")) |
36 | | - (map (partial format "%%%02X")) |
37 | | - (str/join))) |
| 34 | + ([unencoded] |
| 35 | + (percent-encode unencoded "UTF-8")) |
| 36 | + ([^String unencoded ^String encoding] |
| 37 | + (->> (.getBytes unencoded encoding) |
| 38 | + (map (partial format "%%%02X")) |
| 39 | + (str/join)))) |
38 | 40 |
|
39 | 41 | (defn- parse-bytes [encoded-bytes] |
40 | 42 | (->> (re-seq #"%[A-Za-z0-9]{2}" encoded-bytes) |
|
45 | 47 | (defn percent-decode |
46 | 48 | "Decode every percent-encoded character in the given string using the |
47 | 49 | specified encoding, or UTF-8 by default." |
48 | | - [^String encoded & [^String encoding]] |
49 | | - (str/replace encoded |
50 | | - #"(?:%[A-Za-z0-9]{2})+" |
51 | | - (fn [chars] |
52 | | - (-> ^bytes (parse-bytes chars) |
53 | | - (String. (or encoding "UTF-8")) |
54 | | - (fix-string-replace-bug))))) |
| 50 | + ([encoded] |
| 51 | + (percent-decode encoded "UTF-8")) |
| 52 | + ([^String encoded ^String encoding] |
| 53 | + (str/replace encoded |
| 54 | + #"(?:%[A-Za-z0-9]{2})+" |
| 55 | + (fn [chars] |
| 56 | + (-> ^bytes (parse-bytes chars) |
| 57 | + (String. encoding) |
| 58 | + (fix-string-replace-bug)))))) |
55 | 59 |
|
56 | 60 | (defn url-encode |
57 | 61 | "Returns the url-encoded version of the given string, using either a specified |
58 | 62 | encoding or UTF-8 by default." |
59 | | - [unencoded & [encoding]] |
60 | | - (str/replace |
| 63 | + ([unencoded] |
| 64 | + (url-encode unencoded "UTF-8")) |
| 65 | + ([unencoded encoding] |
| 66 | + (str/replace |
61 | 67 | unencoded |
62 | 68 | #"[^A-Za-z0-9_~.+-]+" |
63 | | - #(double-escape (percent-encode % encoding)))) |
| 69 | + #(double-escape (percent-encode % encoding))))) |
64 | 70 |
|
65 | 71 | (defn ^String url-decode |
66 | 72 | "Returns the url-decoded version of the given string, using either a specified |
67 | 73 | encoding or UTF-8 by default. If the encoding is invalid, nil is returned." |
68 | | - [encoded & [encoding]] |
69 | | - (percent-decode encoded encoding)) |
| 74 | + ([encoded] |
| 75 | + (url-decode encoded "UTF-8")) |
| 76 | + ([encoded encoding] |
| 77 | + (percent-decode encoded encoding))) |
70 | 78 |
|
71 | 79 | (defn base64-encode |
72 | 80 | "Encode an array of bytes into a base64 encoded string." |
|
106 | 114 | "Encode the supplied value into www-form-urlencoded format, often used in |
107 | 115 | URL query strings and POST request bodies, using the specified encoding. |
108 | 116 | If the encoding is not specified, it defaults to UTF-8" |
109 | | - [x & [encoding]] |
110 | | - (form-encode* x (or encoding "UTF-8"))) |
| 117 | + ([x] |
| 118 | + (form-encode x "UTF-8")) |
| 119 | + ([x encoding] |
| 120 | + (form-encode* x encoding))) |
111 | 121 |
|
112 | 122 | (defn form-decode-str |
113 | 123 | "Decode the supplied www-form-urlencoded string using the specified encoding, |
114 | 124 | or UTF-8 by default." |
115 | | - [^String encoded & [encoding]] |
116 | | - (try |
117 | | - (URLDecoder/decode encoded (or encoding "UTF-8")) |
118 | | - (catch Exception _ nil))) |
| 125 | + ([encoded] |
| 126 | + (form-decode-str encoded "UTF-8")) |
| 127 | + ([^String encoded encoding] |
| 128 | + (try |
| 129 | + (URLDecoder/decode encoded encoding) |
| 130 | + (catch Exception _ nil)))) |
119 | 131 |
|
120 | 132 | (defn form-decode |
121 | 133 | "Decode the supplied www-form-urlencoded string using the specified encoding, |
122 | 134 | or UTF-8 by default. If the encoded value is a string, a string is returned. |
123 | 135 | If the encoded value is a map of parameters, a map is returned." |
124 | | - [^String encoded & [encoding]] |
125 | | - (if-not (.contains encoded "=") |
126 | | - (form-decode-str encoded encoding) |
127 | | - (reduce |
128 | | - (fn [m param] |
129 | | - (if-let [[k v] (str/split param #"=" 2)] |
130 | | - (assoc-conj m (form-decode-str k encoding) (form-decode-str v encoding)) |
131 | | - m)) |
132 | | - {} |
133 | | - (str/split encoded #"&")))) |
| 136 | + ([encoded] |
| 137 | + (form-decode encoded "UTF-8")) |
| 138 | + ([^String encoded encoding] |
| 139 | + (if-not (.contains encoded "=") |
| 140 | + (form-decode-str encoded encoding) |
| 141 | + (reduce |
| 142 | + (fn [m param] |
| 143 | + (if-let [[k v] (str/split param #"=" 2)] |
| 144 | + (assoc-conj m (form-decode-str k encoding) (form-decode-str v encoding)) |
| 145 | + m)) |
| 146 | + {} |
| 147 | + (str/split encoded #"&"))))) |
0 commit comments