Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a MiddlewareBuilder for easily chaining HttpHandlers #407

Closed
wants to merge 1 commit into from

Conversation

billoneil
Copy link
Contributor

This is something I added on a side project of mine and thought it might be useful. If there is already a way to do this or you have any suggestions please let me know. If you feel it doesn't belong thats also fine.

Here is a more real world example of how I use it on my side project.

private static final HttpHandler wrapWithMiddleware(HttpHandler handler) {
    return MiddlewareBuilder.begin(BlockingHandler::new)
                            .next(CustomHandlers::requestLogging)
                            .next(CustomHandlers::statusCodeMetrics)
                            .next(Server::exceptionHandler)
                            .complete(handler);
}

@undertow-pull-request
Copy link

Can one of the admins verify this patch?

@ctomc
Copy link
Contributor

ctomc commented Jun 3, 2016

this is ok to test

@undertow-pull-request
Copy link

Windows Build 1675 outcome was FAILURE using a merge of 412669c
Summary: Tests failed: 1, passed: 2179, ignored: 713 Build time: 00:20:07

Failed tests

io.undertow.server.handlers.proxy.LoadBalancingProxyHttpsTestCase.testDuplicateHeaders: java.lang.AssertionError: Test failed with i=3 expected:<200> but was:<500>
------- Stderr: -------
13:11:34,308 DEBUG (XNIO-1 I/O-1) [io.undertow.server.handlers.proxy] <ProxyHandler.java:497> Sending request ClientRequest{path='/session', method=GET, protocol=HTTP/1.1} to target 127.0.0.1 for exchange HttpServerExchange{ GET /session request {Connection=[close], a=[b, b, b, b, b, b, b, b], User-Agent=[Apache-HttpClient/4.2.6 (java 1.5)], Host=[localhost:7777]} response {}}
13:11:34,310 DEBUG (XNIO-1 I/O-1) [io.undertow.server.handlers.proxy] <ProxyHandler.java:504> Sent request ClientRequest{path='/session', method=GET, protocol=HTTP/1.1} to target 127.0.0.1 for exchange HttpServerExchange{ GET /session request {Connection=[close], a=[b, b, b, b, b, b, b, b], User-Agent=[Apache-HttpClient/4.2.6 (java 1.5)], Host=[localhost:7777]} response {}}
13:11:34,315 DEBUG (XNIO-1 I/O-1) [io.undertow.server.handlers.proxy] <ProxyHandler.java:614> Received response io.undertow.client.ClientResponse@ef0e5e for request ClientRequest{path='/session', method=GET, protocol=HTTP/1.1} for exchange HttpServerExchange{ GET /session request {Connection=[close], a=[b, b, b, b, b, b, b, b], User-Agent=[Apache-HttpClient/4.2.6 (java 1.5)], Host=[localhost:7777]} response {}}
13:11:34,320 DEBUG (XNIO-1 I/O-3) [io.undertow.server.handlers.proxy] <ProxyHandler.java:497> Sending request ClientRequest{path='/session', method=GET, protocol=HTTP/1.1} to target 127.0.0.1 for exchange HttpServerExchange{ GET /session request {Connection=[close], a=[b, b, b, b, b, b, b, b], Cookie=[JSESSIONID=LFUTOryCQlQo1mp8YK-zs9H_9-YcV5Ex9jAfSB4W.s1], User-Agent=[Apache-HttpClient/4.2.6 (java 1.5)], Cookie2=[$Version=1], Host=[localhost:7777]} response {}}
13:11:34,322 DEBUG (XNIO-1 I/O-3) [io.undertow.server.handlers.proxy] <ProxyHandler.java:504> Sent request ClientRequest{path='/session', method=GET, protocol=HTTP/1.1} to target 127.0.0.1 for exchange HttpServerExchange{ GET /session request {Connection=[close], a=[b, b, b, b, b, b, b, b], Cookie=[JSESSIONID=LFUTOryCQlQo1mp8YK-zs9H_9-YcV5Ex9jAfSB4W.s1], User-Agent=[Apache-HttpClient/4.2.6 (java 1.5)], Cookie2=[$Version=1], Host=[localhost:7777]} response {}}
13:11:34,327 DEBUG (XNIO-1 I/O-3) [io.undertow.server.handlers.proxy] <ProxyHandler.java:614> Received response io.undertow.client.ClientResponse@11c78e3 for request ClientRequest{path='/session', method=GET, protocol=HTTP/1.1} for exchange HttpServerExchange{ GET /session request {Connection=[close], a=[b, b, b, b, b, b, b, b], Cookie=[JSESSIONID=LFUTOryCQlQo1mp8YK-zs9H_9-YcV5Ex9jAfSB4W.s1], User-Agent=[Apache-HttpClient/4.2.6 (java 1.5)], Cookie2=[$Version=1], Host=[localhost:7777]} response {}}
13:11:34,334 DEBUG (XNIO-1 I/O-5) [io.undertow.server.handlers.proxy] <ProxyHandler.java:497> Sending request ClientRequest{path='/session', method=GET, protocol=HTTP/1.1} to target 127.0.0.1 for exchange HttpServerExchange{ GET /session request {Connection=[close], a=[b, b, b, b, b, b, b, b], Cookie=[JSESSIONID=LFUTOryCQlQo1mp8YK-zs9H_9-YcV5Ex9jAfSB4W.s1], User-Agent=[Apache-HttpClient/4.2.6 (java 1.5)], Cookie2=[$Version=1], Host=[localhost:7777]} response {}}
13:11:34,336 DEBUG (XNIO-1 I/O-5) [io.undertow.server.handlers.proxy] <ProxyHandler.java:504> Sent request ClientRequest{path='/session', method=GET, protocol=HTTP/1.1} to target 127.0.0.1 for exchange HttpServerExchange{ GET /session request {Connection=[close], a=[b, b, b, b, b, b, b, b], Cookie=[JSESSIONID=LFUTOryCQlQo1mp8YK-zs9H_9-YcV5Ex9jAfSB4W.s1], User-Agent=[Apache-HttpClient/4.2.6 (java 1.5)], Cookie2=[$Version=1], Host=[localhost:7777]} response {}}
13:11:34,341 DEBUG (XNIO-1 I/O-5) [io.undertow.server.handlers.proxy] <ProxyHandler.java:614> Received response io.undertow.client.ClientResponse@17f0d28 for request ClientRequest{path='/session', method=GET, protocol=HTTP/1.1} for exchange HttpServerExchange{ GET /session request {Connection=[close], a=[b, b, b, b, b, b, b, b], Cookie=[JSESSIONID=LFUTOryCQlQo1mp8YK-zs9H_9-YcV5Ex9jAfSB4W.s1], User-Agent=[Apache-HttpClient/4.2.6 (java 1.5)], Cookie2=[$Version=1], Host=[localhost:7777]} response {}}
13:11:34,344 DEBUG (XNIO-1 I/O-7) [io.undertow.server.handlers.proxy] <ProxyHandler.java:497> Sending request ClientRequest{path='/session', method=GET, protocol=HTTP/1.1} to target 127.0.0.1 for exchange HttpServerExchange{ GET /session request {Connection=[close], a=[b, b, b, b, b, b, b, b], Cookie=[JSESSIONID=LFUTOryCQlQo1mp8YK-zs9H_9-YcV5Ex9jAfSB4W.s1], User-Agent=[Apache-HttpClient/4.2.6 (java 1.5)], Cookie2=[$Version=1], Host=[localhost:7777]} response {}}
13:11:34,347 DEBUG (XNIO-1 I/O-7) [io.undertow.server.handlers.proxy] <ProxyHandler.java:504> Sent request ClientRequest{path='/session', method=GET, protocol=HTTP/1.1} to target 127.0.0.1 for exchange HttpServerExchange{ GET /session request {Connection=[close], a=[b, b, b, b, b, b, b, b], Cookie=[JSESSIONID=LFUTOryCQlQo1mp8YK-zs9H_9-YcV5Ex9jAfSB4W.s1], User-Agent=[Apache-HttpClient/4.2.6 (java 1.5)], Cookie2=[$Version=1], Host=[localhost:7777]} response {}}
13:11:34,349 DEBUG (XNIO-1 I/O-7) [io.undertow.request.io] <SslConduit.java:608> UT005013: An IOException occurred: java.io.IOException: javax.net.ssl.SSLException: Inbound closed before receiving peer's close_notify: possible truncation attack?
    at io.undertow.protocols.ssl.SslConduit.notifyReadClosed(SslConduit.java:608)
    at io.undertow.protocols.ssl.SslConduit.closed(SslConduit.java:973)
    at io.undertow.protocols.ssl.SslConduit.close(SslConduit.java:1068)
    at io.undertow.protocols.ssl.SslConduit.doUnwrap(SslConduit.java:789)
    at io.undertow.protocols.ssl.SslConduit.doWrap(SslConduit.java:839)
    at io.undertow.protocols.ssl.SslConduit.write(SslConduit.java:371)
    at io.undertow.client.http.HttpRequestConduit.processWrite(HttpRequestConduit.java:113)
    at io.undertow.client.http.HttpRequestConduit.flush(HttpRequestConduit.java:573)
    at io.undertow.conduits.AbstractFixedLengthStreamSinkConduit.flush(AbstractFixedLengthStreamSinkConduit.java:229)
    at org.xnio.conduits.ConduitStreamSinkChannel.flush(ConduitStreamSinkChannel.java:162)
    at org.xnio.ChannelListeners$14.handleEvent(ChannelListeners.java:413)
    at org.xnio.ChannelListeners$14.handleEvent(ChannelListeners.java:409)
    at org.xnio.ChannelListeners.invokeChannelListener(ChannelListeners.java:92)
    at org.xnio.conduits.WriteReadyHandler$ChannelListenerHandler.writeReady(WriteReadyHandler.java:65)
    at io.undertow.protocols.ssl.SslConduit$SslWriteReadyHandler.writeReady(SslConduit.java:1214)
    at io.undertow.protocols.ssl.SslConduit$4.run(SslConduit.java:430)
    at org.xnio.nio.WorkerThread.safeRun(WorkerThread.java:580)
    at org.xnio.nio.WorkerThread.run(WorkerThread.java:464)
Caused by: javax.net.ssl.SSLException: Inbound closed before receiving peer's close_notify: possible truncation attack?
    at sun.security.ssl.Alerts.getSSLException(Alerts.java:208)
    at sun.security.ssl.SSLEngineImpl.fatal(SSLEngineImpl.java:1666)
    at sun.security.ssl.SSLEngineImpl.fatal(SSLEngineImpl.java:1634)
    at sun.security.ssl.SSLEngineImpl.closeInbound(SSLEngineImpl.java:1561)
    at io.undertow.protocols.ssl.SslConduit.notifyReadClosed(SslConduit.java:606)
    ... 17 more

13:11:34,360 ERROR (XNIO-1 I/O-7) [io.undertow.proxy] <ProxyHandler.java:650> UT005028: Proxy request to /session failed: java.io.IOException: An established connection was aborted by the software in your host machine
    at sun.nio.ch.SocketDispatcher.read0(Native Method)
    at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:43)
    at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223)
    at sun.nio.ch.IOUtil.read(IOUtil.java:192)
    at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:380)
    at org.xnio.nio.NioSocketConduit.read(NioSocketConduit.java:286)
    at io.undertow.protocols.ssl.SslConduit.doUnwrap(SslConduit.java:694)
    at io.undertow.protocols.ssl.SslConduit.doWrap(SslConduit.java:839)
    at io.undertow.protocols.ssl.SslConduit.write(SslConduit.java:371)
    at io.undertow.client.http.HttpRequestConduit.processWrite(HttpRequestConduit.java:113)


@stuartwdouglas
Copy link
Contributor

Sorry for the delay in replying. To be honest I am not sure if we need this in Undertow core. We already have HandlerWrapper which kind of does the same job. I think your syntax is a bit nicer but I don't know if that justifies making it a supported part of Undertow

@billoneil
Copy link
Contributor Author

Ah I didn't see those before. No problem.

@billoneil billoneil closed this Jun 13, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants