Search in sources :

Example 1 with ServletBlockingHttpExchange

use of io.undertow.servlet.core.ServletBlockingHttpExchange in project undertow by undertow-io.

the class ServletInitialHandler method handleRequest.

@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
    final String path = exchange.getRelativePath();
    if (isForbiddenPath(path)) {
        exchange.setStatusCode(StatusCodes.NOT_FOUND);
        return;
    }
    final ServletPathMatch info = paths.getServletHandlerByPath(path);
    //https://issues.jboss.org/browse/WFLY-3439
    //if the request is an upgrade request then we don't want to redirect
    //as there is a good chance the web socket client won't understand the redirect
    //we make an exception for HTTP2 upgrade requests, as this would have already be handled at
    //the connector level if it was going to be handled.
    String upgradeString = exchange.getRequestHeaders().getFirst(Headers.UPGRADE);
    boolean isUpgradeRequest = upgradeString != null && !upgradeString.startsWith(HTTP2_UPGRADE_PREFIX);
    if (info.getType() == ServletPathMatch.Type.REDIRECT && !isUpgradeRequest) {
        //we redirect on GET requests to the root context to add an / to the end
        if (exchange.getRequestMethod().equals(Methods.GET) || exchange.getRequestMethod().equals(Methods.HEAD)) {
            exchange.setStatusCode(StatusCodes.FOUND);
        } else {
            exchange.setStatusCode(StatusCodes.TEMPORARY_REDIRECT);
        }
        exchange.getResponseHeaders().put(Headers.LOCATION, RedirectBuilder.redirect(exchange, exchange.getRelativePath() + "/", true));
        return;
    } else if (info.getType() == ServletPathMatch.Type.REWRITE) {
        //this can only happen if the path ends with a /
        //otherwise there would be a redirect instead
        exchange.setRelativePath(info.getRewriteLocation());
        exchange.setRequestPath(exchange.getResolvedPath() + info.getRewriteLocation());
    }
    final HttpServletResponseImpl response = new HttpServletResponseImpl(exchange, servletContext);
    final HttpServletRequestImpl request = new HttpServletRequestImpl(exchange, servletContext);
    final ServletRequestContext servletRequestContext = new ServletRequestContext(servletContext.getDeployment(), request, response, info);
    //set the max request size if applicable
    if (info.getServletChain().getManagedServlet().getMaxRequestSize() > 0) {
        exchange.setMaxEntitySize(info.getServletChain().getManagedServlet().getMaxRequestSize());
    }
    exchange.putAttachment(ServletRequestContext.ATTACHMENT_KEY, servletRequestContext);
    exchange.startBlocking(new ServletBlockingHttpExchange(exchange));
    servletRequestContext.setServletPathMatch(info);
    Executor executor = info.getServletChain().getExecutor();
    if (executor == null) {
        executor = servletContext.getDeployment().getExecutor();
    }
    if (exchange.isInIoThread() || executor != null) {
        //either the exchange has not been dispatched yet, or we need to use a special executor
        exchange.dispatch(executor, dispatchHandler);
    } else {
        dispatchRequest(exchange, servletRequestContext, info.getServletChain(), DispatcherType.REQUEST);
    }
}
Also used : Executor(java.util.concurrent.Executor) HttpServletRequestImpl(io.undertow.servlet.spec.HttpServletRequestImpl) HttpString(io.undertow.util.HttpString) HttpServletResponseImpl(io.undertow.servlet.spec.HttpServletResponseImpl) ServletBlockingHttpExchange(io.undertow.servlet.core.ServletBlockingHttpExchange)

Example 2 with ServletBlockingHttpExchange

use of io.undertow.servlet.core.ServletBlockingHttpExchange in project undertow by undertow-io.

the class ServletInitialHandler method dispatchMockRequest.

@Override
public void dispatchMockRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException {
    final DefaultByteBufferPool bufferPool = new DefaultByteBufferPool(false, 1024, 0, 0);
    MockServerConnection connection = new MockServerConnection(bufferPool);
    HttpServerExchange exchange = new HttpServerExchange(connection);
    exchange.setRequestScheme(request.getScheme());
    exchange.setRequestMethod(new HttpString(request.getMethod()));
    exchange.setProtocol(Protocols.HTTP_1_0);
    exchange.setResolvedPath(request.getContextPath());
    String relative;
    if (request.getPathInfo() == null) {
        relative = request.getServletPath();
    } else {
        relative = request.getServletPath() + request.getPathInfo();
    }
    exchange.setRelativePath(relative);
    final ServletPathMatch info = paths.getServletHandlerByPath(request.getServletPath());
    final HttpServletResponseImpl oResponse = new HttpServletResponseImpl(exchange, servletContext);
    final HttpServletRequestImpl oRequest = new HttpServletRequestImpl(exchange, servletContext);
    final ServletRequestContext servletRequestContext = new ServletRequestContext(servletContext.getDeployment(), oRequest, oResponse, info);
    servletRequestContext.setServletRequest(request);
    servletRequestContext.setServletResponse(response);
    //set the max request size if applicable
    if (info.getServletChain().getManagedServlet().getMaxRequestSize() > 0) {
        exchange.setMaxEntitySize(info.getServletChain().getManagedServlet().getMaxRequestSize());
    }
    exchange.putAttachment(ServletRequestContext.ATTACHMENT_KEY, servletRequestContext);
    exchange.startBlocking(new ServletBlockingHttpExchange(exchange));
    servletRequestContext.setServletPathMatch(info);
    try {
        dispatchRequest(exchange, servletRequestContext, info.getServletChain(), DispatcherType.REQUEST);
    } catch (Exception e) {
        if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        }
        throw new ServletException(e);
    }
}
Also used : DefaultByteBufferPool(io.undertow.server.DefaultByteBufferPool) HttpString(io.undertow.util.HttpString) HttpServletResponseImpl(io.undertow.servlet.spec.HttpServletResponseImpl) ServletBlockingHttpExchange(io.undertow.servlet.core.ServletBlockingHttpExchange) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) HttpServerExchange(io.undertow.server.HttpServerExchange) ServletException(javax.servlet.ServletException) HttpServletRequestImpl(io.undertow.servlet.spec.HttpServletRequestImpl) HttpString(io.undertow.util.HttpString)

Aggregations

ServletBlockingHttpExchange (io.undertow.servlet.core.ServletBlockingHttpExchange)2 HttpServletRequestImpl (io.undertow.servlet.spec.HttpServletRequestImpl)2 HttpServletResponseImpl (io.undertow.servlet.spec.HttpServletResponseImpl)2 HttpString (io.undertow.util.HttpString)2 DefaultByteBufferPool (io.undertow.server.DefaultByteBufferPool)1 HttpServerExchange (io.undertow.server.HttpServerExchange)1 IOException (java.io.IOException)1 Executor (java.util.concurrent.Executor)1 ServletException (javax.servlet.ServletException)1