【新月実装】需【Clojure・ClojureScript】

e31bf0e8 :Anonymous 2016-01-27 15:11
@markdown
こんな風にringのラッパーを作って対処しました。
柔軟なのはいいけど資料が殆ど無いのはちとしんどいですね。

```clojure
(defn- percent-encode-everything
[s]
(cond
(zero? (count s))
""

(re-find #"^(%[0-9A-F]{2})+$" s)
s

(re-find #"^%[0-9A-F]{2}" s)
(let [match (re-find #"^(%[0-9A-F]{2})(.*)$" s)]
(percent-encode-everything
(str
(nth match 1)
(percent-encode-everything (nth match 2)))))

:else
(percent-encode-everything
(str
(format "%%%02X" (int (first (take 1 s))))
(apply str (drop 1 s))))))

(defn percent-decode-for-2ch-post
[s]
(timbre/debug s)
(percent-decode (percent-encode-everything s) "windows-31j"))

(defn wrap-2ch-post
[handler]
(fn [request]
(if (= (:uri request) "/test/bbs.cgi")
(handler (assoc request
:form-params
(->> (clojure.string/split (slurp (:body request)) #"&")
(map #(re-find #"^([^=]+)=(.*)$" %))
(map #(do {(keyword (nth % 1)) (percent-decode-for-2ch-post (nth % 2))}))
(apply merge))))
(handler request))))
```
Powered by shinGETsu.