Search in sources :

Example 51 with HttpServerExchange

use of io.undertow.server.HttpServerExchange in project undertow by undertow-io.

the class Http2ServerConnection method sendOutOfBandResponse.

@Override
public HttpServerExchange sendOutOfBandResponse(HttpServerExchange exchange) {
    if (exchange == null || !HttpContinue.requiresContinueResponse(exchange)) {
        throw UndertowMessages.MESSAGES.outOfBandResponseOnlyAllowedFor100Continue();
    }
    final HttpServerExchange newExchange = new HttpServerExchange(this);
    for (HttpString header : exchange.getRequestHeaders().getHeaderNames()) {
        newExchange.getRequestHeaders().putAll(header, exchange.getRequestHeaders().get(header));
    }
    newExchange.setProtocol(exchange.getProtocol());
    newExchange.setRequestMethod(exchange.getRequestMethod());
    exchange.setRequestURI(exchange.getRequestURI(), exchange.isHostIncludedInRequestURI());
    exchange.setRequestPath(exchange.getRequestPath());
    exchange.setRelativePath(exchange.getRelativePath());
    newExchange.setPersistent(true);
    Connectors.terminateRequest(newExchange);
    newExchange.addResponseWrapper(new ConduitWrapper<StreamSinkConduit>() {

        @Override
        public StreamSinkConduit wrap(ConduitFactory<StreamSinkConduit> factory, HttpServerExchange exchange) {
            HeaderMap headers = newExchange.getResponseHeaders();
            DateUtils.addDateHeaderIfRequired(exchange);
            headers.add(STATUS, exchange.getStatusCode());
            Connectors.flattenCookies(exchange);
            Http2HeadersStreamSinkChannel sink = new Http2HeadersStreamSinkChannel(channel, requestChannel.getStreamId(), headers);
            StreamSinkChannelWrappingConduit ret = new StreamSinkChannelWrappingConduit(sink);
            ret.setWriteReadyHandler(new WriteReadyHandler.ChannelListenerHandler(Connectors.getConduitSinkChannel(exchange)));
            return ret;
        }
    });
    continueSent = true;
    return newExchange;
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) Http2HeadersStreamSinkChannel(io.undertow.protocols.http2.Http2HeadersStreamSinkChannel) HeaderMap(io.undertow.util.HeaderMap) StreamSinkConduit(org.xnio.conduits.StreamSinkConduit) StreamSinkChannelWrappingConduit(org.xnio.conduits.StreamSinkChannelWrappingConduit) HttpString(io.undertow.util.HttpString)

Example 52 with HttpServerExchange

use of io.undertow.server.HttpServerExchange in project undertow by undertow-io.

the class Http2UpgradeHandler method handleHttp2Upgrade.

private void handleHttp2Upgrade(HttpServerExchange exchange, final String upgrade, String settings, final byte[] data) throws IOException {
    // required by spec
    final ByteBuffer settingsFrame = FlexBase64.decodeURL(settings);
    exchange.getResponseHeaders().put(Headers.UPGRADE, upgrade);
    exchange.upgradeChannel(new HttpUpgradeListener() {

        @Override
        public void handleUpgrade(StreamConnection streamConnection, HttpServerExchange exchange) {
            OptionMap undertowOptions = exchange.getConnection().getUndertowOptions();
            Http2Channel channel = new Http2Channel(streamConnection, upgrade, exchange.getConnection().getByteBufferPool(), null, false, true, true, settingsFrame, undertowOptions);
            Http2ReceiveListener receiveListener = new Http2ReceiveListener(new HttpHandler() {

                @Override
                public void handleRequest(HttpServerExchange exchange) throws Exception {
                    // as the request was only to create the initial request
                    if (exchange.getRequestHeaders().contains("X-HTTP2-connect-only")) {
                        exchange.endExchange();
                        return;
                    }
                    exchange.setProtocol(Protocols.HTTP_2_0);
                    next.handleRequest(exchange);
                }
            }, undertowOptions, exchange.getConnection().getBufferSize(), null);
            channel.getReceiveSetter().set(receiveListener);
            receiveListener.handleInitialRequest(exchange, channel, data);
            channel.resumeReceives();
        }
    });
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) OptionMap(org.xnio.OptionMap) Http2Channel(io.undertow.protocols.http2.Http2Channel) HttpUpgradeListener(io.undertow.server.HttpUpgradeListener) StreamConnection(org.xnio.StreamConnection) ByteBuffer(java.nio.ByteBuffer) ImmediatePooledByteBuffer(io.undertow.util.ImmediatePooledByteBuffer)

Example 53 with HttpServerExchange

use of io.undertow.server.HttpServerExchange in project undertow by undertow-io.

the class HttpServerConnection method sendOutOfBandResponse.

@Override
public HttpServerExchange sendOutOfBandResponse(HttpServerExchange exchange) {
    if (exchange == null || !HttpContinue.requiresContinueResponse(exchange)) {
        throw UndertowMessages.MESSAGES.outOfBandResponseOnlyAllowedFor100Continue();
    }
    final ConduitState state = resetChannel();
    HttpServerExchange newExchange = new HttpServerExchange(this);
    for (HttpString header : exchange.getRequestHeaders().getHeaderNames()) {
        newExchange.getRequestHeaders().putAll(header, exchange.getRequestHeaders().get(header));
    }
    newExchange.setProtocol(exchange.getProtocol());
    newExchange.setRequestMethod(exchange.getRequestMethod());
    exchange.setRequestURI(exchange.getRequestURI(), exchange.isHostIncludedInRequestURI());
    exchange.setRequestPath(exchange.getRequestPath());
    exchange.setRelativePath(exchange.getRelativePath());
    newExchange.getRequestHeaders().put(Headers.CONNECTION, Headers.KEEP_ALIVE.toString());
    newExchange.getRequestHeaders().put(Headers.CONTENT_LENGTH, 0);
    newExchange.setPersistent(true);
    Connectors.terminateRequest(newExchange);
    newExchange.addResponseWrapper(new ConduitWrapper<StreamSinkConduit>() {

        @Override
        public StreamSinkConduit wrap(ConduitFactory<StreamSinkConduit> factory, HttpServerExchange exchange) {
            HttpResponseConduit httpResponseConduit = new HttpResponseConduit(getSinkChannel().getConduit(), getByteBufferPool(), HttpServerConnection.this, exchange);
            exchange.addExchangeCompleteListener(new ExchangeCompletionListener() {

                @Override
                public void exchangeEvent(HttpServerExchange exchange, NextListener nextListener) {
                    httpResponseConduit.freeContinueResponse();
                    nextListener.proceed();
                }
            });
            ServerFixedLengthStreamSinkConduit fixed = new ServerFixedLengthStreamSinkConduit(httpResponseConduit, false, false);
            fixed.reset(0, exchange);
            return fixed;
        }
    });
    // we restore the read channel immediately, as this out of band response has no read side
    channel.getSourceChannel().setConduit(source(state));
    newExchange.addExchangeCompleteListener(new ExchangeCompletionListener() {

        @Override
        public void exchangeEvent(HttpServerExchange exchange, NextListener nextListener) {
            restoreChannel(state);
        }
    });
    return newExchange;
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) StreamSinkConduit(org.xnio.conduits.StreamSinkConduit) ExchangeCompletionListener(io.undertow.server.ExchangeCompletionListener) HttpString(io.undertow.util.HttpString)

Example 54 with HttpServerExchange

use of io.undertow.server.HttpServerExchange in project undertow by undertow-io.

the class ServerFixedLengthStreamSinkConduit method channelFinished.

@Override
protected void channelFinished() {
    if (exchange != null) {
        HttpServerExchange exchange = this.exchange;
        this.exchange = null;
        Connectors.terminateResponse(exchange);
    }
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange)

Example 55 with HttpServerExchange

use of io.undertow.server.HttpServerExchange in project undertow by undertow-io.

the class ExchangeCompletionTestCase method setup.

@BeforeClass
public static void setup() throws ServletException {
    final PathHandler root = new PathHandler();
    final ServletContainer container = ServletContainer.Factory.newInstance();
    DeploymentInfo builder = new DeploymentInfo().setClassLoader(SimpleServletTestCase.class.getClassLoader()).setContextPath("/servletContext").setClassIntrospecter(TestClassIntrospector.INSTANCE).setDeploymentName("servletContext.war").addServlet(new ServletInfo("servlet", IgnoresRequestAndSetsAttributeServlet.class).addMapping("/sync").addInitParam(IgnoresRequestAndSetsAttributeServlet.ATTRIBUTE_KEY, AN_ATTRIBUTE).addInitParam(IgnoresRequestAndSetsAttributeServlet.ATTRIBUTE_VALUE, A_VALUE)).addServlet(new ServletInfo("asyncservlet", IgnoresRequestAndSetsAttributeAsyncServlet.class).addMapping("/async").addInitParam(IgnoresRequestAndSetsAttributeServlet.ATTRIBUTE_KEY, AN_ATTRIBUTE).addInitParam(IgnoresRequestAndSetsAttributeServlet.ATTRIBUTE_VALUE, A_VALUE).setAsyncSupported(true)).addInitialHandlerChainWrapper(new HandlerWrapper() {

        @Override
        public HttpHandler wrap(final HttpHandler handler) {
            return new HttpHandler() {

                @Override
                public void handleRequest(final HttpServerExchange exchange) throws Exception {
                    exchange.addExchangeCompleteListener(new ExchangeCompletionListener() {

                        @Override
                        public void exchangeEvent(HttpServerExchange exchange, NextListener nextListener) {
                            ServletRequestContext context = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
                            if (context != null) {
                                Object result = context.getServletRequest().getAttribute(AN_ATTRIBUTE);
                                if (result instanceof String) {
                                    completedExchangeAttributes.add((String) result);
                                }
                            }
                            nextListener.proceed();
                        }
                    });
                    handler.handleRequest(exchange);
                }
            };
        }
    });
    DeploymentManager manager = container.addDeployment(builder);
    manager.deploy();
    root.addPrefixPath(builder.getContextPath(), manager.start());
    DefaultServer.setRootHandler(root);
}
Also used : HttpHandler(io.undertow.server.HttpHandler) DeploymentManager(io.undertow.servlet.api.DeploymentManager) PathHandler(io.undertow.server.handlers.PathHandler) ServletRequestContext(io.undertow.servlet.handlers.ServletRequestContext) HandlerWrapper(io.undertow.server.HandlerWrapper) SimpleServletTestCase(io.undertow.servlet.test.SimpleServletTestCase) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) ServletInfo(io.undertow.servlet.api.ServletInfo) HttpServerExchange(io.undertow.server.HttpServerExchange) ServletContainer(io.undertow.servlet.api.ServletContainer) ExchangeCompletionListener(io.undertow.server.ExchangeCompletionListener) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) BeforeClass(org.junit.BeforeClass)

Aggregations

HttpServerExchange (io.undertow.server.HttpServerExchange)277 HttpHandler (io.undertow.server.HttpHandler)127 Test (org.junit.Test)109 IOException (java.io.IOException)90 UnitTest (io.undertow.testutils.category.UnitTest)45 BeforeClass (org.junit.BeforeClass)44 TestHttpClient (io.undertow.testutils.TestHttpClient)42 HttpGet (org.apache.http.client.methods.HttpGet)40 HttpResponse (org.apache.http.HttpResponse)37 HttpString (io.undertow.util.HttpString)36 Header (org.apache.http.Header)24 Undertow (io.undertow.Undertow)19 ByteBuffer (java.nio.ByteBuffer)19 SessionConfig (io.undertow.server.session.SessionConfig)16 Map (java.util.Map)16 Sender (io.undertow.io.Sender)15 ExchangeCompletionListener (io.undertow.server.ExchangeCompletionListener)14 HeaderMap (io.undertow.util.HeaderMap)13 URI (java.net.URI)13 HeaderValues (io.undertow.util.HeaderValues)12