Compojure handler friend / authenticate吃POST请求的主体

时间:2015-04-25 03:36:47

标签: clojure http-post ring compojure undertow

如何从compojure安全地获取:body InputStream的内容?

See related but different question for background.

我尝试使用compojure handler/site向朋友验证我的戒指路线,但是当我尝试从http :body请求中读取POST时是一个Java InputStream),它已关闭:

23:01:20.505 ERROR [io.undertow.request] (XNIO-1 task-3) Undertow request failed HttpServerExchange{ POST /paypal/ipn}
java.io.IOException: UT000034: Stream is closed
    at io.undertow.io.UndertowInputStream.read(UndertowInputStream.java:84) ~[undertow-core-1.1.0.Final.jar:1.1.0.Final]
    at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284) ~[na:1.8.0_45]
    at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326) ~[na:1.8.0_45]
    at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178) ~[na:1.8.0_45]
    at java.io.InputStreamReader.read(InputStreamReader.java:184) ~[na:1.8.0_45]
    at java.io.BufferedReader.fill(BufferedReader.java:161) ~[na:1.8.0_45]
    at java.io.BufferedReader.read(BufferedReader.java:182) ~[na:1.8.0_45]
    at clojure.core$slurp.doInvoke(core.clj:6650) ~[clojure-1.7.0-beta1.jar:na]
    at clojure.lang.RestFn.invoke(RestFn.java:410) ~[clojure-1.7.0-beta1.jar:na]

如果删除处理程序,问题就会消失。我发现一个possible solution called groundhog可以捕获并存储所有请求。我使用的库clojure-paypal-ipn originally called reset on the stream,但Undertow(或者其他几个Java / Clojure服务器)不支持,所以我解决了这个问题。

这是a related discussion with weavejester, author of compojure.

以下是我的代码的一些片段:

(defroutes routes
  ...
  (POST "/paypal/ipn" [] (payment/paypal-ipn-handler 
                          payment/paypal-data 
                          payment/paypal-error 
                          paypal-sandbox?))
  (route/resources "/"))

(defn authenticate-routes
  "Add Friend handler to routes"
  [routes-set]
  (handler/site
    (friend/authenticate routes-set friend-settings)))

;; middleware below from immutant.web.middleware
(defn -main [& {:as args}]
  (web/run
    (-> routes
      (web-middleware/wrap-session {:timeout 20})

      (authenticate-routes) ; use friend handler

      ;; wrap the handler with websocket support
      ;; websocket requests will go to the callbacks, ring requests to the handler
      (web-middleware/wrap-websocket websocket-callbacks))
    args))

以下是payment.cljpaypal-datapaypal-errorpprint输入的内容):

(defn req->body-str [req]
  "Get request body from ring POST http request"
  (let [input-stream (:body req)]
      (let [raw-body-str (slurp input-stream)]
          raw-body-str)))

(defn paypal-ipn-handler
  ([on-success on-failure] (paypal-ipn-handler on-success on-failure true))
  ([on-success on-failure sandbox?]
   (fn [req]
     (let [body-str (req->body-str req)
           ipn-data (paypal/parse-paypal-ipn-string body-str)]
       (do
         (.start (Thread. (fn [] (paypal/handle-ipn ipn-data on-success on-failure sandbox?))))
         ; respond to PayPal right away, then go and process the ipn-data
         {:status  200
          :headers {"Content-Type" "text/html"}
          :body    ""})))))

0 个答案:

没有答案