Clojure Immutant 2 server Undertow; how to handle

2019-05-18 15:36发布

问题:

Putting the following line into my compojure defroutes to use clojure-paypal-ipn calling the two handler functions.

(POST "/paypal/ipn" [] (make-ipn-handler payment/paypal-data payment/paypal-error))

How do I handle a POST request within Immutant?

I'm using Immutant 2.0 with Clojure 1.7-beta1 and building off the Immutant feature demo. Posting directly with cURL give these same results, although I'm trying to use PayPal IPN.

17:29:40.220 ERROR [io.undertow.request] (XNIO-1 task-1) Undertow request failed HttpServerExchange{ POST /paypal/ipn}
java.io.IOException: mark/reset not supported
    at java.io.InputStream.reset(InputStream.java:348) ~[na:1.8.0_45]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_45]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_45]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_45]
    at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_45]
    at clojure.lang.Reflector.invokeMatchingMethod(Reflector.java:93) ~[clojure-1.7.0-beta1.jar:na]
    at clojure.lang.Reflector.invokeNoArgInstanceMember(Reflector.java:313) ~[clojure-1.7.0-beta1.jar:na]
    at clojure_paypal_ipn.core$req__GT_raw_body_str.invoke(core.clj:59) ~[na:na]
    at clojure_paypal_ipn.core$make_ipn_handler$fn__16823.invoke(core.clj:69) ~[na:na]
    at compojure.response$eval6101$fn__6102.invoke(response.clj:34) ~[na:na]
    at compojure.response$eval6068$fn__6069$G__6059__6076.invoke(response.clj:7) ~[na:na]
    at compojure.core$make_route$fn__7513.invoke(core.clj:113) ~[na:na]
    at compojure.core$wrap_route_middleware$fn__7509.invoke(core.clj:103) ~[na:na]
    at compojure.core$if_route$fn__7488.invoke(core.clj:41) ~[na:na]
    at compojure.core$if_method$fn__7480.invoke(core.clj:27) ~[na:na]
    at compojure.core$routing$fn__7519.invoke(core.clj:127) ~[na:na]
    at clojure.core$some.invoke(core.clj:2568) ~[clojure-1.7.0-beta1.jar:na]
    at compojure.core$routing.doInvoke(core.clj:127) ~[na:na]
    at clojure.lang.RestFn.applyTo(RestFn.java:139) ~[clojure-1.7.0-beta1.jar:na]
    at clojure.core$apply.invoke(core.clj:630) ~[clojure-1.7.0-beta1.jar:na]
    at compojure.core$routes$fn__7523.invoke(core.clj:132) ~[na:na]
    at immutant.web.internal.undertow$wrap_undertow_session$fn__5292.invoke(undertow.clj:69) ~[na:na]
    at immutant.web.internal.undertow$create_http_handler$reify__5362.handleRequest(undertow.clj:130) ~[na:na]
    at org.projectodd.wunderboss.web.async.websocket.UndertowWebsocket$2.handleRequest(UndertowWebsocket.java:96) ~[wunderboss-web-0.6.1.jar:na]
    at io.undertow.server.session.SessionAttachmentHandler.handleRequest(SessionAttachmentHandler.java:68) ~[undertow-core-1.1.0.Final.jar:1.1.0.Final]
    at io.undertow.server.Connectors.executeRootHandler(Connectors.java:197) ~[undertow-core-1.1.0.Final.jar:1.1.0.Final]
    at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:759) ~[undertow-core-1.1.0.Final.jar:1.1.0.Final]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) ~[na:1.8.0_45]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) ~[na:1.8.0_45]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_45]

回答1:

I don't think this is necessarily a problem with Immutant handling a POST. The problem occurs during the dispatch to the IPN handler, which is attempting to invoke reset on Undertow's InputStream. I would expect the stream's markSupported method to return false. It's not clear to me why reset is being called here, since it doesn't appear mark is ever called. Without being familiar with that library, I might suggest buffering the input somehow... maybe invoke parse-paypal-ipn-string directly? I'm really just guessing, though.