<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css" style="display:none"><!-- p { margin-top: 0px; margin-bottom: 0px; }--></style>
</head>
<body dir="ltr" style="font-size:12pt;color:#000000;background-color:#FFFFFF;font-family:Calibri,Arial,Helvetica,sans-serif;">
<p>Hi,<br>
</p>
<p><br>
</p>
<p>sorry, for a kind of off-topic question, but as the FormDataProcessor is the topic here, I was wondering if&nbsp;there is already some kind of&nbsp;data sanitization​ in the core form handling of Undertow. If not, which handler is a good starting point to hook into
 for this?<br>
</p>
<p><br>
</p>
<p>Best regards,<br>
</p>
<p>Sven<br>
</p>
<p><br>
</p>
<div style="color: rgb(33, 33, 33);">
<hr tabindex="-1" style="display:inline-block; width:98%">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" color="#000000" style="font-size:11pt"><b>Von:</b> undertow-dev-bounces@lists.jboss.org &lt;undertow-dev-bounces@lists.jboss.org&gt; im Auftrag von Bill O'Neil &lt;bill@dartalley.com&gt;<br>
<b>Gesendet:</b> Mittwoch, 25. Januar 2017 14:53<br>
<b>An:</b> Stuart Douglas<br>
<b>Cc:</b> Oliver Dain; undertow-dev@lists.jboss.org<br>
<b>Betreff:</b> Re: [undertow-dev] FormDataProcessor ending exchange prematurely</font>
<div>&nbsp;</div>
</div>
<div>
<div dir="ltr">Hey Stuart, your last response reminded me of of a question I had hopefully its not unrelated. I was curious how and when you should dispatch back to the IO thread. For example lets say we have a simple handler that does a SQL query that is composed
 with the AccessLogHandler.
<div><br>
</div>
<div>AccessLogHandler -&gt; BlockingHandler -&gt; CustomSqlHandler In this approach I believe the final access log statement will be logged from the worker thread not the initial IO thread.</div>
<div><br>
</div>
<div>Is it possible / recommended to kick back to an IO thread once blocking is complete?</div>
<div><br>
</div>
<div>AccessLogHandler -&gt; BlockingHandler -&gt; CustomSqlHandler -&gt; DispatchBackToIOThread? This way the final logging of AccessLogHandler is handled in the IO thread.</div>
<div><br>
</div>
<div>I'm not very familiar with the best ways to mix the nonblocking and blocking handlers.</div>
<div><br>
</div>
<div><br>
</div>
</div>
<div class="gmail_extra"><br>
<div class="gmail_quote">On Wed, Jan 25, 2017 at 12:24 AM, Stuart Douglas <span dir="ltr">
&lt;<a href="mailto:sdouglas@redhat.com" target="_blank">sdouglas@redhat.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex; border-left:1px #ccc solid; padding-left:1ex">
The handler is executed<br>
io.undertow.server.Connectors#<wbr>executeRootHandler, which means that<br>
when the call stack returns the exchange will be ended.<br>
<br>
Conceptually this is similar to how dispatching to a thread pool<br>
works, when you call dispatch(HttpHandler) the exchange will be ended<br>
when the call stack returns, even though you are no longer on the IO<br>
thread (unless you call dispatch again).<br>
<br>
Stuart<br>
<div>
<div class="h5"><br>
<br>
On Wed, Jan 25, 2017 at 3:57 PM, Oliver Dain &lt;<a href="mailto:oliver@analyticspot.com">oliver@analyticspot.com</a>&gt; wrote:<br>
&gt; I have some code for handling file uploads. It uses FormDataProcessor and<br>
&gt; tries to do everything asynchronously. I call &quot;FormDataParser.parse&quot; passing<br>
&gt; in another handler. I'll call that handler the OnFormDataAvailable. The<br>
&gt; OnFormDataAvailable handler checks to see if it's on the IO thread. If it<br>
&gt; is, it calls dispatch. Either way, once we're sure we're dispatched that<br>
&gt; handler calls yet another handler.<br>
&gt;<br>
&gt; What I've seen is that my OnFormDataAvailable handler (the one called by<br>
&gt; parse()) is not on the IO thread so it doesn't need to call dispatch. And<br>
&gt; yet, the exchange gets ended before the handler it calls is even close to<br>
&gt; complete.<br>
&gt;<br>
&gt; I've found a fix, but I don't understand why it's necessary. Specifically,<br>
&gt; if OnFormDataAvailable is not on the IO thread when its invoked it calls the<br>
&gt; 0-argument version of &quot;HttpServerExchange.dispatch()<wbr>&quot; (which you've told me<br>
&gt; in another conversation shouldn't ever be necessary). If I do that,<br>
&gt; everything is fine.<br>
&gt;<br>
&gt; For completeness, here's the complete code:<br>
&gt;<br>
&gt; public class FormDataParsingHandler {<br>
&gt;&nbsp; &nbsp;private static final Logger log =<br>
&gt; LoggerFactory.getLogger(<wbr>FormDataParsingHandler.class);<br>
&gt;&nbsp; &nbsp;private static final FormParserFactory formParserFactory =<br>
&gt; FormParserFactory.builder().<wbr>build();<br>
&gt;&nbsp; &nbsp;public static final AttachmentKey&lt;FormData&gt; FORM_DATA_ATTACHMENT_KEY =<br>
&gt; AttachmentKey.create(FormData.<wbr>class);<br>
&gt;<br>
&gt;&nbsp; &nbsp;/**<br>
&gt;&nbsp; &nbsp; * The only public method - this is what gets exposed as the HttpHandler.<br>
&gt;&nbsp; &nbsp; */<br>
&gt;&nbsp; &nbsp;public CompletableFuture&lt;FormData&gt; parseForm(HttpServerExchange exchange)<br>
&gt; {<br>
&gt;&nbsp; &nbsp; &nbsp;<a href="http://log.info" rel="noreferrer" target="_blank">log.info</a>(&quot;audio file upload request received.&quot;);<br>
&gt;&nbsp; &nbsp; &nbsp;FormDataParser parser = formParserFactory.<wbr>createParser(exchange);<br>
&gt;&nbsp; &nbsp; &nbsp;if (parser == null) {<br>
&gt;&nbsp; &nbsp; &nbsp; &nbsp;log.warn(&quot;No parser found that can handle this content type. Headers<br>
&gt; were: {}&quot;, exchange.getRequestHeaders());<br>
&gt;&nbsp; &nbsp; &nbsp; &nbsp;throw new UserVisibleException(&quot;No parser for the given content<br>
&gt; type.&quot;, ResponseCodes.BAD_REQUEST);<br>
&gt;&nbsp; &nbsp; &nbsp;}<br>
&gt;<br>
&gt;&nbsp; &nbsp; &nbsp;CompletableFuture&lt;FormData&gt; toComplete = new CompletableFuture&lt;&gt;();<br>
&gt;&nbsp; &nbsp; &nbsp;try {<br>
&gt;&nbsp; &nbsp; &nbsp; &nbsp;parser.parse(new OnFormDataAvailable(<wbr>toComplete));<br>
&gt;&nbsp; &nbsp; &nbsp;} catch (Exception e) {<br>
&gt;&nbsp; &nbsp; &nbsp; &nbsp;log.error(&quot;Error parsing form data:&quot;, e);<br>
&gt;&nbsp; &nbsp; &nbsp; &nbsp;throw wrapAsUnchecked(e);<br>
&gt;&nbsp; &nbsp; &nbsp;}<br>
&gt;<br>
&gt;&nbsp; &nbsp; &nbsp;exchange.<wbr>addExchangeCompleteListener((<wbr>ex, nextListener) -&gt; {<br>
&gt;&nbsp; &nbsp; &nbsp; &nbsp;// Must close the parser so it can free any temporary files that were<br>
&gt; created.<br>
&gt;&nbsp; &nbsp; &nbsp; &nbsp;try {<br>
&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;parser.close();<br>
&gt;&nbsp; &nbsp; &nbsp; &nbsp;} catch (IOException e) {<br>
&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;log.error(&quot;Error closing the FormDataParser. Request was handled<br>
&gt; successfully but temporary files may not &quot;<br>
&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&#43; &quot;have been cleaned up.&quot;, e);<br>
&gt;&nbsp; &nbsp; &nbsp; &nbsp;}<br>
&gt;&nbsp; &nbsp; &nbsp; &nbsp;nextListener.proceed();<br>
&gt;&nbsp; &nbsp; &nbsp;});<br>
&gt;<br>
&gt;&nbsp; &nbsp; &nbsp;return toComplete;<br>
&gt;&nbsp; &nbsp;}<br>
&gt;<br>
&gt;&nbsp; &nbsp;// The FormDataParser calls an HttpHandler when it's complete so we add a<br>
&gt; silly handler here that does nothing but<br>
&gt;&nbsp; &nbsp;// complete this method's future when the form data is available.<br>
&gt;&nbsp; &nbsp;private static class OnFormDataAvailable implements HttpHandler {<br>
&gt;&nbsp; &nbsp; &nbsp;private final CompletableFuture&lt;FormData&gt; toComplete;<br>
&gt;<br>
&gt;&nbsp; &nbsp; &nbsp;private OnFormDataAvailable(<wbr>CompletableFuture&lt;FormData&gt; toComplete) {<br>
&gt;&nbsp; &nbsp; &nbsp; &nbsp;this.toComplete = toComplete;<br>
&gt;&nbsp; &nbsp; &nbsp;}<br>
&gt;<br>
&gt;&nbsp; &nbsp; &nbsp;@Override<br>
&gt;&nbsp; &nbsp; &nbsp;public void handleRequest(<wbr>HttpServerExchange exchange) throws Exception<br>
&gt; {<br>
&gt;&nbsp; &nbsp; &nbsp; &nbsp;// Before we complete the future we have to re-dispatch or we'll fall<br>
&gt; off the end of this method and Undertow<br>
&gt;&nbsp; &nbsp; &nbsp; &nbsp;// will complete the exchange on our behalf.<br>
&gt;&nbsp; &nbsp; &nbsp; &nbsp;FormData data = exchange.getAttachment(<wbr>FormDataParser.FORM_DATA);<br>
&gt;&nbsp; &nbsp; &nbsp; &nbsp;if (exchange.isInIoThread()) {<br>
&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;log.debug(&quot;Was on the IO thread. Re-dispatching.&quot;);<br>
&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exchange.dispatch(<wbr>SameThreadExecutor.INSTANCE, () -&gt;<br>
&gt; afterDistpach(data));<br>
&gt;&nbsp; &nbsp; &nbsp; &nbsp;} else {<br>
&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// THIS THE MYSTERY LINE. WHY IS THIS NEEDED?<br>
&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exchange.dispatch();<br>
&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;afterDistpach(data);<br>
&gt;&nbsp; &nbsp; &nbsp; &nbsp;}<br>
&gt;&nbsp; &nbsp; &nbsp;}<br>
&gt;<br>
&gt;&nbsp; &nbsp; &nbsp;private void afterDistpach(FormData data) {<br>
&gt;&nbsp; &nbsp; &nbsp; &nbsp;if (data == null) {<br>
&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;toComplete.<wbr>completeExceptionally(<br>
&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;new UserVisibleException(&quot;Parsing of data failed.&quot;,<br>
&gt; ResponseCodes.BAD_REQUEST));<br>
&gt;&nbsp; &nbsp; &nbsp; &nbsp;} else {<br>
&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;toComplete.complete(data);<br>
&gt;&nbsp; &nbsp; &nbsp; &nbsp;}<br>
&gt;&nbsp; &nbsp; &nbsp;}<br>
&gt;&nbsp; &nbsp;}<br>
&gt; }<br>
&gt;<br>
&gt; --<br>
&gt; CTO, Analytic Spot<br>
&gt; 44 West Broadway #222<br>
&gt; Eugene, OR 97401<br>
&gt; <a href="http://analyticspot.com" rel="noreferrer" target="_blank">analyticspot.com</a> â€˘
<a href="tel:425-296-6556" value="&#43;14252966556">425-296-6556</a><br>
&gt; <a href="http://www.linkedin.com/in/oliverdain" rel="noreferrer" target="_blank">
www.linkedin.com/in/oliverdain</a><br>
&gt;<br>
</div>
</div>
&gt; ______________________________<wbr>_________________<br>
&gt; undertow-dev mailing list<br>
&gt; <a href="mailto:undertow-dev@lists.jboss.org">undertow-dev@lists.jboss.org</a><br>
&gt; <a href="https://lists.jboss.org/mailman/listinfo/undertow-dev" rel="noreferrer" target="_blank">
https://lists.jboss.org/<wbr>mailman/listinfo/undertow-dev</a><br>
<br>
______________________________<wbr>_________________<br>
undertow-dev mailing list<br>
<a href="mailto:undertow-dev@lists.jboss.org">undertow-dev@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/undertow-dev" rel="noreferrer" target="_blank">https://lists.jboss.org/<wbr>mailman/listinfo/undertow-dev</a></blockquote>
</div>
<br>
</div>
</div>
</div>
</body>
</html>