Search in sources :

Example 1 with RequestInfo

use of org.apache.coyote.RequestInfo in project tomcat by apache.

the class AjpProcessor method service.

@Override
public SocketState service(SocketWrapperBase<?> socket) throws IOException {
    RequestInfo rp = request.getRequestProcessor();
    rp.setStage(org.apache.coyote.Constants.STAGE_PARSE);
    // Setting up the socket
    this.socketWrapper = socket;
    boolean cping = false;
    boolean keptAlive = false;
    while (!getErrorState().isError() && !protocol.isPaused()) {
        // Parsing the request header
        try {
            // Get first message of the request
            if (!readMessage(requestHeaderMessage, !keptAlive)) {
                break;
            }
            // Processing the request so make sure the connection rather
            // than keep-alive timeout is used
            socketWrapper.setReadTimeout(protocol.getConnectionTimeout());
            // Check message type, process right away and break if
            // not regular request processing
            int type = requestHeaderMessage.getByte();
            if (type == Constants.JK_AJP13_CPING_REQUEST) {
                if (protocol.isPaused()) {
                    recycle();
                    break;
                }
                cping = true;
                try {
                    socketWrapper.write(true, pongMessageArray, 0, pongMessageArray.length);
                    socketWrapper.flush(true);
                } catch (IOException e) {
                    setErrorState(ErrorState.CLOSE_CONNECTION_NOW, e);
                }
                recycle();
                continue;
            } else if (type != Constants.JK_AJP13_FORWARD_REQUEST) {
                // been swallowed in finish().
                if (getLog().isDebugEnabled()) {
                    getLog().debug("Unexpected message: " + type);
                }
                setErrorState(ErrorState.CLOSE_CONNECTION_NOW, null);
                break;
            }
            keptAlive = true;
            request.setStartTime(System.currentTimeMillis());
        } catch (IOException e) {
            setErrorState(ErrorState.CLOSE_CONNECTION_NOW, e);
            break;
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
            getLog().debug(sm.getString("ajpprocessor.header.error"), t);
            // 400 - Bad Request
            response.setStatus(400);
            setErrorState(ErrorState.CLOSE_CLEAN, t);
            getAdapter().log(request, response, 0);
        }
        if (!getErrorState().isError()) {
            // Setting up filters, and parse some request headers
            rp.setStage(org.apache.coyote.Constants.STAGE_PREPARE);
            try {
                prepareRequest();
            } catch (Throwable t) {
                ExceptionUtils.handleThrowable(t);
                getLog().debug(sm.getString("ajpprocessor.request.prepare"), t);
                // 500 - Internal Server Error
                response.setStatus(500);
                setErrorState(ErrorState.CLOSE_CLEAN, t);
                getAdapter().log(request, response, 0);
            }
        }
        if (!getErrorState().isError() && !cping && protocol.isPaused()) {
            // 503 - Service unavailable
            response.setStatus(503);
            setErrorState(ErrorState.CLOSE_CLEAN, null);
            getAdapter().log(request, response, 0);
        }
        cping = false;
        // Process the request in the adapter
        if (!getErrorState().isError()) {
            try {
                rp.setStage(org.apache.coyote.Constants.STAGE_SERVICE);
                getAdapter().service(request, response);
            } catch (InterruptedIOException e) {
                setErrorState(ErrorState.CLOSE_CONNECTION_NOW, e);
            } catch (Throwable t) {
                ExceptionUtils.handleThrowable(t);
                getLog().error(sm.getString("ajpprocessor.request.process"), t);
                // 500 - Internal Server Error
                response.setStatus(500);
                setErrorState(ErrorState.CLOSE_CLEAN, t);
                getAdapter().log(request, response, 0);
            }
        }
        if (isAsync() && !getErrorState().isError()) {
            break;
        }
        // Finish the response if not done yet
        if (!responseFinished && getErrorState().isIoAllowed()) {
            try {
                action(ActionCode.COMMIT, null);
                finishResponse();
            } catch (IOException ioe) {
                setErrorState(ErrorState.CLOSE_CONNECTION_NOW, ioe);
            } catch (Throwable t) {
                ExceptionUtils.handleThrowable(t);
                setErrorState(ErrorState.CLOSE_NOW, t);
            }
        }
        // and error, and update the statistics counter
        if (getErrorState().isError()) {
            response.setStatus(500);
        }
        request.updateCounters();
        rp.setStage(org.apache.coyote.Constants.STAGE_KEEPALIVE);
        // Set keep alive timeout for next request
        socketWrapper.setReadTimeout(protocol.getKeepAliveTimeout());
        recycle();
    }
    rp.setStage(org.apache.coyote.Constants.STAGE_ENDED);
    if (getErrorState().isError() || protocol.isPaused()) {
        return SocketState.CLOSED;
    } else {
        if (isAsync()) {
            return SocketState.LONG;
        } else {
            return SocketState.OPEN;
        }
    }
}
Also used : InterruptedIOException(java.io.InterruptedIOException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) RequestInfo(org.apache.coyote.RequestInfo)

Example 2 with RequestInfo

use of org.apache.coyote.RequestInfo in project tomcat by apache.

the class AsyncContextImpl method logDebug.

private void logDebug(String method) {
    String rHashCode;
    String crHashCode;
    String rpHashCode;
    String stage;
    StringBuilder uri = new StringBuilder();
    if (request == null) {
        rHashCode = "null";
        crHashCode = "null";
        rpHashCode = "null";
        stage = "-";
        uri.append("N/A");
    } else {
        rHashCode = Integer.toHexString(request.hashCode());
        org.apache.coyote.Request coyoteRequest = request.getCoyoteRequest();
        if (coyoteRequest == null) {
            crHashCode = "null";
            rpHashCode = "null";
            stage = "-";
        } else {
            crHashCode = Integer.toHexString(coyoteRequest.hashCode());
            RequestInfo rp = coyoteRequest.getRequestProcessor();
            if (rp == null) {
                rpHashCode = "null";
                stage = "-";
            } else {
                rpHashCode = Integer.toHexString(rp.hashCode());
                stage = Integer.toString(rp.getStage());
            }
        }
        uri.append(request.getRequestURI());
        if (request.getQueryString() != null) {
            uri.append('?');
            uri.append(request.getQueryString());
        }
    }
    String threadName = Thread.currentThread().getName();
    int len = threadName.length();
    if (len > 20) {
        threadName = threadName.substring(len - 20, len);
    }
    String msg = String.format("Req: %1$8s  CReq: %2$8s  RP: %3$8s  Stage: %4$s  " + "Thread: %5$20s  State: %6$20s  Method: %7$11s  URI: %8$s", rHashCode, crHashCode, rpHashCode, stage, threadName, "N/A", method, uri);
    if (log.isTraceEnabled()) {
        log.trace(msg, new DebugException());
    } else {
        log.debug(msg);
    }
}
Also used : RequestInfo(org.apache.coyote.RequestInfo)

Example 3 with RequestInfo

use of org.apache.coyote.RequestInfo in project tomcat by apache.

the class Http11Processor method service.

@Override
public SocketState service(SocketWrapperBase<?> socketWrapper) throws IOException {
    RequestInfo rp = request.getRequestProcessor();
    rp.setStage(org.apache.coyote.Constants.STAGE_PARSE);
    // Setting up the I/O
    setSocketWrapper(socketWrapper);
    inputBuffer.init(socketWrapper);
    outputBuffer.init(socketWrapper);
    // Flags
    keepAlive = true;
    openSocket = false;
    readComplete = true;
    boolean keptAlive = false;
    while (!getErrorState().isError() && keepAlive && !isAsync() && upgradeToken == null && !protocol.isPaused()) {
        // Parsing the request header
        try {
            if (!inputBuffer.parseRequestLine(keptAlive, protocol.getConnectionTimeout(), protocol.getKeepAliveTimeout())) {
                if (inputBuffer.getParsingRequestLinePhase() == -1) {
                    return SocketState.UPGRADING;
                } else if (handleIncompleteRequestLineRead()) {
                    break;
                }
            }
            if (protocol.isPaused()) {
                // 503 - Service unavailable
                response.setStatus(503);
                setErrorState(ErrorState.CLOSE_CLEAN, null);
            } else {
                keptAlive = true;
                // Set this every time in case limit has been changed via JMX
                request.getMimeHeaders().setLimit(protocol.getMaxHeaderCount());
                if (!inputBuffer.parseHeaders()) {
                    // We've read part of the request, don't recycle it
                    // instead associate it with the socket
                    openSocket = true;
                    readComplete = false;
                    break;
                }
                if (!protocol.getDisableUploadTimeout()) {
                    socketWrapper.setReadTimeout(protocol.getConnectionUploadTimeout());
                }
            }
        } catch (IOException e) {
            if (log.isDebugEnabled()) {
                log.debug(sm.getString("http11processor.header.parse"), e);
            }
            setErrorState(ErrorState.CLOSE_CONNECTION_NOW, e);
            break;
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
            UserDataHelper.Mode logMode = userDataHelper.getNextMode();
            if (logMode != null) {
                String message = sm.getString("http11processor.header.parse");
                switch(logMode) {
                    case INFO_THEN_DEBUG:
                        message += sm.getString("http11processor.fallToDebug");
                    //$FALL-THROUGH$
                    case INFO:
                        log.info(message, t);
                        break;
                    case DEBUG:
                        log.debug(message, t);
                }
            }
            // 400 - Bad Request
            response.setStatus(400);
            setErrorState(ErrorState.CLOSE_CLEAN, t);
            getAdapter().log(request, response, 0);
        }
        // Has an upgrade been requested?
        Enumeration<String> connectionValues = request.getMimeHeaders().values("Connection");
        boolean foundUpgrade = false;
        while (connectionValues.hasMoreElements() && !foundUpgrade) {
            foundUpgrade = connectionValues.nextElement().toLowerCase(Locale.ENGLISH).contains("upgrade");
        }
        if (foundUpgrade) {
            // Check the protocol
            String requestedProtocol = request.getHeader("Upgrade");
            UpgradeProtocol upgradeProtocol = protocol.getUpgradeProtocol(requestedProtocol);
            if (upgradeProtocol != null) {
                if (upgradeProtocol.accept(request)) {
                    // TODO Figure out how to handle request bodies at this
                    // point.
                    response.setStatus(HttpServletResponse.SC_SWITCHING_PROTOCOLS);
                    response.setHeader("Connection", "Upgrade");
                    response.setHeader("Upgrade", requestedProtocol);
                    action(ActionCode.CLOSE, null);
                    getAdapter().log(request, response, 0);
                    InternalHttpUpgradeHandler upgradeHandler = upgradeProtocol.getInternalUpgradeHandler(socketWrapper, getAdapter(), cloneRequest(request));
                    UpgradeToken upgradeToken = new UpgradeToken(upgradeHandler, null, null);
                    action(ActionCode.UPGRADE, upgradeToken);
                    return SocketState.UPGRADING;
                }
            }
        }
        if (!getErrorState().isError()) {
            // Setting up filters, and parse some request headers
            rp.setStage(org.apache.coyote.Constants.STAGE_PREPARE);
            try {
                prepareRequest();
            } catch (Throwable t) {
                ExceptionUtils.handleThrowable(t);
                if (log.isDebugEnabled()) {
                    log.debug(sm.getString("http11processor.request.prepare"), t);
                }
                // 500 - Internal Server Error
                response.setStatus(500);
                setErrorState(ErrorState.CLOSE_CLEAN, t);
                getAdapter().log(request, response, 0);
            }
        }
        int maxKeepAliveRequests = protocol.getMaxKeepAliveRequests();
        if (maxKeepAliveRequests == 1) {
            keepAlive = false;
        } else if (maxKeepAliveRequests > 0 && socketWrapper.decrementKeepAlive() <= 0) {
            keepAlive = false;
        }
        // Process the request in the adapter
        if (!getErrorState().isError()) {
            try {
                rp.setStage(org.apache.coyote.Constants.STAGE_SERVICE);
                getAdapter().service(request, response);
                // committed, so we can't try and set headers.
                if (keepAlive && !getErrorState().isError() && !isAsync() && statusDropsConnection(response.getStatus())) {
                    setErrorState(ErrorState.CLOSE_CLEAN, null);
                }
            } catch (InterruptedIOException e) {
                setErrorState(ErrorState.CLOSE_CONNECTION_NOW, e);
            } catch (HeadersTooLargeException e) {
                log.error(sm.getString("http11processor.request.process"), e);
                // anyway to be safe
                if (response.isCommitted()) {
                    setErrorState(ErrorState.CLOSE_NOW, e);
                } else {
                    response.reset();
                    response.setStatus(500);
                    setErrorState(ErrorState.CLOSE_CLEAN, e);
                    // TODO: Remove
                    response.setHeader("Connection", "close");
                }
            } catch (Throwable t) {
                ExceptionUtils.handleThrowable(t);
                log.error(sm.getString("http11processor.request.process"), t);
                // 500 - Internal Server Error
                response.setStatus(500);
                setErrorState(ErrorState.CLOSE_CLEAN, t);
                getAdapter().log(request, response, 0);
            }
        }
        // Finish the handling of the request
        rp.setStage(org.apache.coyote.Constants.STAGE_ENDINPUT);
        if (!isAsync()) {
            // If this is an async request then the request ends when it has
            // been completed. The AsyncContext is responsible for calling
            // endRequest() in that case.
            endRequest();
        }
        rp.setStage(org.apache.coyote.Constants.STAGE_ENDOUTPUT);
        // and error, and update the statistics counter
        if (getErrorState().isError()) {
            response.setStatus(500);
        }
        if (!isAsync() || getErrorState().isError()) {
            request.updateCounters();
            if (getErrorState().isIoAllowed()) {
                inputBuffer.nextRequest();
                outputBuffer.nextRequest();
            }
        }
        if (!protocol.getDisableUploadTimeout()) {
            int connectionTimeout = protocol.getConnectionTimeout();
            if (connectionTimeout > 0) {
                socketWrapper.setReadTimeout(connectionTimeout);
            } else {
                socketWrapper.setReadTimeout(0);
            }
        }
        rp.setStage(org.apache.coyote.Constants.STAGE_KEEPALIVE);
        if (breakKeepAliveLoop(socketWrapper)) {
            break;
        }
    }
    rp.setStage(org.apache.coyote.Constants.STAGE_ENDED);
    if (getErrorState().isError() || protocol.isPaused()) {
        return SocketState.CLOSED;
    } else if (isAsync()) {
        return SocketState.LONG;
    } else if (isUpgrade()) {
        return SocketState.UPGRADING;
    } else {
        if (sendfileData != null) {
            return SocketState.SENDFILE;
        } else {
            if (openSocket) {
                if (readComplete) {
                    return SocketState.OPEN;
                } else {
                    return SocketState.LONG;
                }
            } else {
                return SocketState.CLOSED;
            }
        }
    }
}
Also used : InterruptedIOException(java.io.InterruptedIOException) UpgradeToken(org.apache.coyote.UpgradeToken) UpgradeProtocol(org.apache.coyote.UpgradeProtocol) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) RequestInfo(org.apache.coyote.RequestInfo) InternalHttpUpgradeHandler(org.apache.coyote.http11.upgrade.InternalHttpUpgradeHandler)

Aggregations

RequestInfo (org.apache.coyote.RequestInfo)3 IOException (java.io.IOException)2 InterruptedIOException (java.io.InterruptedIOException)2 UpgradeProtocol (org.apache.coyote.UpgradeProtocol)1 UpgradeToken (org.apache.coyote.UpgradeToken)1 InternalHttpUpgradeHandler (org.apache.coyote.http11.upgrade.InternalHttpUpgradeHandler)1