Java Code Examples for org.apache.tomcat.util.net.AbstractEndpoint.Handler.SocketState#LONG

The following examples show how to use org.apache.tomcat.util.net.AbstractEndpoint.Handler.SocketState#LONG . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StreamProcessor.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public SocketState service(SocketWrapperBase<?> socket) throws IOException {
    try {
        adapter.service(request, response);
    } catch (Exception e) {
        if (log.isDebugEnabled()) {
            log.debug(sm.getString("streamProcessor.service.error"), e);
        }
        response.setStatus(500);
        setErrorState(ErrorState.CLOSE_NOW, e);
    }

    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();
    }

    if (getErrorState().isError()) {
        action(ActionCode.CLOSE, null);
        request.updateCounters();
        return SocketState.CLOSED;
    } else if (isAsync()) {
        return SocketState.LONG;
    } else {
        action(ActionCode.CLOSE, null);
        request.updateCounters();
        return SocketState.CLOSED;
    }
}
 
Example 2
Source File: Http11AprProcessor.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Process pipelined HTTP requests using the specified input and output
 * streams.
 *
 * @throws IOException error during an I/O operation
 */
@Override
public SocketState event(SocketStatus status)
    throws IOException {

    RequestInfo rp = request.getRequestProcessor();

    try {
        rp.setStage(org.apache.coyote.Constants.STAGE_SERVICE);
        if (!getAdapter().event(request, response, status)) {
            setErrorState(ErrorState.CLOSE_NOW, null);
        }
    } catch (InterruptedIOException e) {
        setErrorState(ErrorState.CLOSE_NOW, e);
    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
        // 500 - Internal Server Error
        response.setStatus(500);
        setErrorState(ErrorState.CLOSE_NOW, t);
        getAdapter().log(request, response, 0);
        log.error(sm.getString("http11processor.request.process"), t);
    }

    rp.setStage(org.apache.coyote.Constants.STAGE_ENDED);

    if (getErrorState().isError() || status==SocketStatus.STOP) {
        return SocketState.CLOSED;
    } else if (!comet) {
        inputBuffer.nextRequest();
        outputBuffer.nextRequest();
        return SocketState.OPEN;
    } else {
        return SocketState.LONG;
    }
}
 
Example 3
Source File: AsyncStateMachine.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
public synchronized SocketState asyncPostProcess() {
    if (state == AsyncState.COMPLETE_PENDING) {
        doComplete();
        return SocketState.ASYNC_END;
    } else if (state == AsyncState.DISPATCH_PENDING) {
        doDispatch();
        return SocketState.ASYNC_END;
    } else  if (state == AsyncState.STARTING) {
        state = AsyncState.STARTED;
        return SocketState.LONG;
    } else if (state == AsyncState.MUST_COMPLETE) {
        asyncCtxt.fireOnComplete();
        state = AsyncState.DISPATCHED;
        return SocketState.ASYNC_END;
    } else if (state == AsyncState.COMPLETING) {
        asyncCtxt.fireOnComplete();
        state = AsyncState.DISPATCHED;
        return SocketState.ASYNC_END;
    } else if (state == AsyncState.MUST_DISPATCH) {
        state = AsyncState.DISPATCHING;
        return SocketState.ASYNC_END;
    } else if (state == AsyncState.DISPATCHING) {
        state = AsyncState.DISPATCHED;
        return SocketState.ASYNC_END;
    } else if (state == AsyncState.STARTED) {
        // This can occur if an async listener does a dispatch to an async
        // servlet during onTimeout
        return SocketState.LONG;
    } else {
        throw new IllegalStateException(
                sm.getString("asyncStateMachine.invalidAsyncState",
                        "asyncPostProcess()", state));
    }
}
 
Example 4
Source File: Http11AprProcessor.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Process pipelined HTTP requests using the specified input and output
 * streams.
 *
 * @throws IOException error during an I/O operation
 */
@Override
public SocketState event(SocketStatus status)
    throws IOException {

    RequestInfo rp = request.getRequestProcessor();

    try {
        rp.setStage(org.apache.coyote.Constants.STAGE_SERVICE);
        if (!getAdapter().event(request, response, status)) {
            setErrorState(ErrorState.CLOSE_NOW, null);
        }
    } catch (InterruptedIOException e) {
        setErrorState(ErrorState.CLOSE_NOW, e);
    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
        // 500 - Internal Server Error
        response.setStatus(500);
        setErrorState(ErrorState.CLOSE_NOW, t);
        getAdapter().log(request, response, 0);
        log.error(sm.getString("http11processor.request.process"), t);
    }

    rp.setStage(org.apache.coyote.Constants.STAGE_ENDED);

    if (getErrorState().isError() || status==SocketStatus.STOP) {
        return SocketState.CLOSED;
    } else if (!comet) {
        inputBuffer.nextRequest();
        outputBuffer.nextRequest();
        return SocketState.OPEN;
    } else {
        return SocketState.LONG;
    }
}
 
Example 5
Source File: AbstractHttp11Processor.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Override
public SocketState asyncDispatch(SocketStatus status) {

    RequestInfo rp = request.getRequestProcessor();
    try {
        rp.setStage(org.apache.coyote.Constants.STAGE_SERVICE);
        if (!getAdapter().asyncDispatch(request, response, status)) {
            setErrorState(ErrorState.CLOSE_NOW, null);
        }
        resetTimeouts();
    } catch (InterruptedIOException e) {
        setErrorState(ErrorState.CLOSE_NOW, e);
    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
        setErrorState(ErrorState.CLOSE_NOW, t);
        getLog().error(sm.getString("http11processor.request.process"), t);
    } finally {
        if (getErrorState().isError()) {
            // 500 - Internal Server Error
            response.setStatus(500);
            adapter.log(request, response, 0);
        }
    }

    rp.setStage(org.apache.coyote.Constants.STAGE_ENDED);

    if (getErrorState().isError()) {
        return SocketState.CLOSED;
    } else if (isAsync()) {
        return SocketState.LONG;
    } else {
        if (!keepAlive) {
            return SocketState.CLOSED;
        } else {
            getInputBuffer().nextRequest();
            getOutputBuffer().nextRequest();
            return SocketState.OPEN;
        }
    }
}
 
Example 6
Source File: AsyncStateMachine.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
public synchronized SocketState asyncPostProcess() {

        // Unpause any non-container threads that may be waiting for this
        // container thread to complete this method. Note because of the syncs
        // those non-container threads won't start back up until until this
        // method exits.
        notifyAll();

        if (state == AsyncState.STARTING) {
            state = AsyncState.STARTED;
            return SocketState.LONG;
        } else if (state == AsyncState.MUST_COMPLETE) {
            asyncCtxt.fireOnComplete();
            state = AsyncState.DISPATCHED;
            return SocketState.ASYNC_END;
        } else if (state == AsyncState.COMPLETING) {
            asyncCtxt.fireOnComplete();
            state = AsyncState.DISPATCHED;
            return SocketState.ASYNC_END;
        } else if (state == AsyncState.MUST_DISPATCH) {
            state = AsyncState.DISPATCHING;
            return SocketState.ASYNC_END;
        } else if (state == AsyncState.DISPATCHING) {
            state = AsyncState.DISPATCHED;
            return SocketState.ASYNC_END;
        } else if (state == AsyncState.STARTED) {
            // This can occur if an async listener does a dispatch to an async
            // servlet during onTimeout
            return SocketState.LONG;
        } else {
            throw new IllegalStateException(
                    sm.getString("asyncStateMachine.invalidAsyncState",
                            "asyncPostProcess()", state));
        }
    }
 
Example 7
Source File: Http11Processor.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
protected SocketState dispatchEndRequest() {
    if (!keepAlive) {
        return SocketState.CLOSED;
    } else {
        endRequest();
        inputBuffer.nextRequest();
        outputBuffer.nextRequest();
        if (socketWrapper.isReadPending()) {
            return SocketState.LONG;
        } else {
            return SocketState.OPEN;
        }
    }
}
 
Example 8
Source File: AsyncStateMachine.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
public synchronized SocketState asyncPostProcess() {
    if (state == AsyncState.COMPLETE_PENDING) {
        clearNonBlockingListeners();
        state = AsyncState.COMPLETING;
        return SocketState.ASYNC_END;
    } else if (state == AsyncState.DISPATCH_PENDING) {
        clearNonBlockingListeners();
        state = AsyncState.DISPATCHING;
        return SocketState.ASYNC_END;
    } else  if (state == AsyncState.STARTING || state == AsyncState.READ_WRITE_OP) {
        state = AsyncState.STARTED;
        return SocketState.LONG;
    } else if (state == AsyncState.MUST_COMPLETE || state == AsyncState.COMPLETING) {
        asyncCtxt.fireOnComplete();
        state = AsyncState.DISPATCHED;
        return SocketState.ASYNC_END;
    } else if (state == AsyncState.MUST_DISPATCH) {
        state = AsyncState.DISPATCHING;
        return SocketState.ASYNC_END;
    } else if (state == AsyncState.DISPATCHING) {
        state = AsyncState.DISPATCHED;
        return SocketState.ASYNC_END;
    } else if (state == AsyncState.STARTED) {
        // This can occur if an async listener does a dispatch to an async
        // servlet during onTimeout
        return SocketState.LONG;
    } else {
        throw new IllegalStateException(
                sm.getString("asyncStateMachine.invalidAsyncState",
                        "asyncPostProcess()", state));
    }
}
 
Example 9
Source File: AbstractAjpProcessor.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@Override
public SocketState asyncDispatch(SocketStatus status) {

    RequestInfo rp = request.getRequestProcessor();
    try {
        rp.setStage(org.apache.coyote.Constants.STAGE_SERVICE);
        if(!getAdapter().asyncDispatch(request, response, status)) {
            setErrorState(ErrorState.CLOSE_NOW, null);
        }
        resetTimeouts();
    } catch (InterruptedIOException e) {
        setErrorState(ErrorState.CLOSE_NOW, e);
    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
        setErrorState(ErrorState.CLOSE_NOW, t);
        getLog().error(sm.getString("http11processor.request.process"), t);
    } finally {
        if (getErrorState().isError()) {
            // 500 - Internal Server Error
            response.setStatus(500);
            adapter.log(request, response, 0);
        }
    }

    rp.setStage(org.apache.coyote.Constants.STAGE_ENDED);

    if (isAsync()) {
        if (getErrorState().isError()) {
            request.updateCounters();
            return SocketState.CLOSED;
        } else {
            return SocketState.LONG;
        }
    } else {
        request.updateCounters();
        if (getErrorState().isError()) {
            return SocketState.CLOSED;
        } else {
            return SocketState.OPEN;
        }
    }
}
 
Example 10
Source File: AbstractProcessor.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Override
public final SocketState dispatch(SocketEvent status) throws IOException {

    if (status == SocketEvent.OPEN_WRITE && response.getWriteListener() != null) {
        asyncStateMachine.asyncOperation();
        try {
            if (flushBufferedWrite()) {
                return SocketState.LONG;
            }
        } catch (IOException ioe) {
            if (getLog().isDebugEnabled()) {
                getLog().debug("Unable to write async data.", ioe);
            }
            status = SocketEvent.ERROR;
            request.setAttribute(RequestDispatcher.ERROR_EXCEPTION, ioe);
        }
    } else if (status == SocketEvent.OPEN_READ && request.getReadListener() != null) {
        dispatchNonBlockingRead();
    } else if (status == SocketEvent.ERROR) {
        // An I/O error occurred on a non-container thread. This includes:
        // - read/write timeouts fired by the Poller (NIO & APR)
        // - completion handler failures in NIO2

        if (request.getAttribute(RequestDispatcher.ERROR_EXCEPTION) == null) {
            // Because the error did not occur on a container thread the
            // request's error attribute has not been set. If an exception
            // is available from the socketWrapper, use it to set the
            // request's error attribute here so it is visible to the error
            // handling.
            request.setAttribute(RequestDispatcher.ERROR_EXCEPTION, socketWrapper.getError());
        }

        if (request.getReadListener() != null || response.getWriteListener() != null) {
            // The error occurred during non-blocking I/O. Set the correct
            // state else the error handling will trigger an ISE.
            asyncStateMachine.asyncOperation();
        }
    }

    RequestInfo rp = request.getRequestProcessor();
    try {
        rp.setStage(org.apache.coyote.Constants.STAGE_SERVICE);
        if (!getAdapter().asyncDispatch(request, response, status)) {
            setErrorState(ErrorState.CLOSE_NOW, null);
        }
    } catch (InterruptedIOException e) {
        setErrorState(ErrorState.CLOSE_CONNECTION_NOW, e);
    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
        setErrorState(ErrorState.CLOSE_NOW, t);
        getLog().error(sm.getString("http11processor.request.process"), t);
    }

    rp.setStage(org.apache.coyote.Constants.STAGE_ENDED);

    SocketState state;

    if (getErrorState().isError()) {
        request.updateCounters();
        state = SocketState.CLOSED;
    } else if (isAsync()) {
        state = SocketState.LONG;
    } else {
        request.updateCounters();
        state = dispatchEndRequest();
    }

    if (getLog().isDebugEnabled()) {
        getLog().debug("Socket: [" + socketWrapper +
                "], Status in: [" + status +
                "], State out: [" + state + "]");
    }

    return state;
}
 
Example 11
Source File: AbstractProcessorLight.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Override
public SocketState process(SocketWrapperBase<?> socketWrapper, SocketEvent status)
        throws IOException {

    SocketState state = SocketState.CLOSED;
    Iterator<DispatchType> dispatches = null;
    do {
        if (dispatches != null) {
            DispatchType nextDispatch = dispatches.next();
            if (getLog().isDebugEnabled()) {
                getLog().debug("Processing dispatch type: [" + nextDispatch + "]");
            }
            state = dispatch(nextDispatch.getSocketStatus());
            if (!dispatches.hasNext()) {
                state = checkForPipelinedData(state, socketWrapper);
            }
        } else if (status == SocketEvent.DISCONNECT) {
            // Do nothing here, just wait for it to get recycled
        } else if (isAsync() || isUpgrade() || state == SocketState.ASYNC_END) {
            state = dispatch(status);
            state = checkForPipelinedData(state, socketWrapper);
        } else if (status == SocketEvent.OPEN_WRITE) {
            // Extra write event likely after async, ignore
            state = SocketState.LONG;
        } else if (status == SocketEvent.OPEN_READ) {
            /**
             * 调用对应的Service方法.
             * {@link Http11Processor#service(org.apache.tomcat.util.net.SocketWrapperBase)}
             */
            state = service(socketWrapper);
        } else if (status == SocketEvent.CONNECT_FAIL) {
            logAccess(socketWrapper);
        } else {
            // Default to closing the socket if the SocketEvent passed in
            // is not consistent with the current state of the Processor
            state = SocketState.CLOSED;
        }

        if (getLog().isDebugEnabled()) {
            getLog().debug("Socket: [" + socketWrapper +
                    "], Status in: [" + status +
                    "], State out: [" + state + "]");
        }

        if (state != SocketState.CLOSED && isAsync()) {
            state = asyncPostProcess();
            if (getLog().isDebugEnabled()) {
                getLog().debug("Socket: [" + socketWrapper +
                        "], State after async post processing: [" + state + "]");
            }
        }

        if (dispatches == null || !dispatches.hasNext()) {
            // Only returns non-null iterator if there are
            // dispatches to process.
            dispatches = getIteratorAndClearDispatches();
        }
    } while (state == SocketState.ASYNC_END ||
            dispatches != null && state != SocketState.CLOSED);

    return state;
}
 
Example 12
Source File: Http11NioProcessor.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
/**
 * Process pipelined HTTP requests using the specified input and output
 * streams.
 *
 * @throws IOException error during an I/O operation
 */
@Override
public SocketState event(SocketStatus status) throws IOException {

    long soTimeout = endpoint.getSoTimeout();

    RequestInfo rp = request.getRequestProcessor();
    final NioEndpoint.KeyAttachment attach = (NioEndpoint.KeyAttachment)socketWrapper.getSocket().getAttachment();
    try {
        rp.setStage(org.apache.coyote.Constants.STAGE_SERVICE);
        if (!getAdapter().event(request, response, status)) {
            setErrorState(ErrorState.CLOSE_NOW, null);
        }
        if (!getErrorState().isError()) {
            if (attach != null) {
                attach.setComet(comet);
                if (comet) {
                    Integer comettimeout = (Integer) request.getAttribute(
                            org.apache.coyote.Constants.COMET_TIMEOUT_ATTR);
                    if (comettimeout != null) {
                        attach.setTimeout(comettimeout.longValue());
                    }
                } else {
                    //reset the timeout
                    if (keepAlive) {
                        attach.setTimeout(keepAliveTimeout);
                    } else {
                        attach.setTimeout(soTimeout);
                    }
                }

            }
        }
    } catch (InterruptedIOException e) {
        setErrorState(ErrorState.CLOSE_NOW, e);
    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
        // 500 - Internal Server Error
        response.setStatus(500);
        setErrorState(ErrorState.CLOSE_NOW, t);
        log.error(sm.getString("http11processor.request.process"), t);
        getAdapter().log(request, response, 0);
    }

    rp.setStage(org.apache.coyote.Constants.STAGE_ENDED);

    if (getErrorState().isError() || status==SocketStatus.STOP) {
        return SocketState.CLOSED;
    } else if (!comet) {
        if (keepAlive) {
            inputBuffer.nextRequest();
            outputBuffer.nextRequest();
            return SocketState.OPEN;
        } else {
            return SocketState.CLOSED;
        }
    } else {
        return SocketState.LONG;
    }
}
 
Example 13
Source File: JIoEndpoint.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@Override
public void run() {
    boolean launch = false;
    synchronized (socket) {
        try {
            SocketState state = SocketState.OPEN;

            try {
                // SSL handshake
                serverSocketFactory.handshake(socket.getSocket());
            } catch (Throwable t) {
                ExceptionUtils.handleThrowable(t);
                if (log.isDebugEnabled()) {
                    log.debug(sm.getString("endpoint.err.handshake"), t);
                }
                // Tell to close the socket
                state = SocketState.CLOSED;
            }

            if ((state != SocketState.CLOSED)) {
                if (status == null) {
                    state = handler.process(socket, SocketStatus.OPEN_READ);
                } else {
                    state = handler.process(socket,status);
                }
            }
            if (state == SocketState.CLOSED) {
                // Close socket
                if (log.isTraceEnabled()) {
                    log.trace("Closing socket:"+socket);
                }
                countDownConnection();
                try {
                    socket.getSocket().close();
                } catch (IOException e) {
                    // Ignore
                }
            } else if (state == SocketState.OPEN ||
                    state == SocketState.UPGRADING ||
                    state == SocketState.UPGRADING_TOMCAT  ||
                    state == SocketState.UPGRADED){
                socket.setKeptAlive(true);
                socket.access();
                launch = true;
            } else if (state == SocketState.LONG) {
                socket.access();
                waitingRequests.add(socket);
            }
        } finally {
            if (launch) {
                try {
                    getExecutor().execute(new SocketProcessor(socket, SocketStatus.OPEN_READ));
                } catch (RejectedExecutionException x) {
                    log.warn("Socket reprocessing request was rejected for:"+socket,x);
                    try {
                        //unable to handle connection at this time
                        handler.process(socket, SocketStatus.DISCONNECT);
                    } finally {
                        countDownConnection();
                    }


                } catch (NullPointerException npe) {
                    if (running) {
                        log.error(sm.getString("endpoint.launch.fail"),
                                npe);
                    }
                }
            }
        }
    }
    socket = null;
    // Finish up this request
}
 
Example 14
Source File: AbstractAjpProcessor.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Override
public SocketState asyncDispatch(SocketStatus status) {

    RequestInfo rp = request.getRequestProcessor();
    try {
        rp.setStage(org.apache.coyote.Constants.STAGE_SERVICE);
        if(!getAdapter().asyncDispatch(request, response, status)) {
            setErrorState(ErrorState.CLOSE_NOW, null);
        }
        resetTimeouts();
    } catch (InterruptedIOException e) {
        setErrorState(ErrorState.CLOSE_NOW, e);
    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
        setErrorState(ErrorState.CLOSE_NOW, t);
        getLog().error(sm.getString("http11processor.request.process"), t);
    } finally {
        if (getErrorState().isError()) {
            // 500 - Internal Server Error
            response.setStatus(500);
            adapter.log(request, response, 0);
        }
    }

    rp.setStage(org.apache.coyote.Constants.STAGE_ENDED);

    if (isAsync()) {
        if (getErrorState().isError()) {
            request.updateCounters();
            return SocketState.CLOSED;
        } else {
            return SocketState.LONG;
        }
    } else {
        request.updateCounters();
        if (getErrorState().isError()) {
            return SocketState.CLOSED;
        } else {
            recycle(false);
            return SocketState.OPEN;
        }
    }
}
 
Example 15
Source File: AbstractHttp11Processor.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Override
public SocketState asyncDispatch(SocketStatus status) {

    RequestInfo rp = request.getRequestProcessor();
    try {
        rp.setStage(org.apache.coyote.Constants.STAGE_SERVICE);
        if (!getAdapter().asyncDispatch(request, response, status)) {
            setErrorState(ErrorState.CLOSE_NOW, null);
        }
        resetTimeouts();
    } catch (InterruptedIOException e) {
        setErrorState(ErrorState.CLOSE_NOW, e);
    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
        setErrorState(ErrorState.CLOSE_NOW, t);
        getLog().error(sm.getString("http11processor.request.process"), t);
    } finally {
        if (getErrorState().isError()) {
            // 500 - Internal Server Error
            response.setStatus(500);
            adapter.log(request, response, 0);
        }
    }

    rp.setStage(org.apache.coyote.Constants.STAGE_ENDED);

    if (getErrorState().isError()) {
        return SocketState.CLOSED;
    } else if (isAsync()) {
        return SocketState.LONG;
    } else {
        if (!keepAlive) {
            return SocketState.CLOSED;
        } else {
            endRequest();
            getInputBuffer().nextRequest();
            getOutputBuffer().nextRequest();
            return SocketState.OPEN;
        }
    }
}
 
Example 16
Source File: Http11NioProcessor.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/**
 * Process pipelined HTTP requests using the specified input and output
 * streams.
 *
 * @throws IOException error during an I/O operation
 */
@Override
public SocketState event(SocketStatus status) throws IOException {

    long soTimeout = endpoint.getSoTimeout();

    RequestInfo rp = request.getRequestProcessor();
    final NioEndpoint.KeyAttachment attach = (NioEndpoint.KeyAttachment)socketWrapper.getSocket().getAttachment();
    try {
        rp.setStage(org.apache.coyote.Constants.STAGE_SERVICE);
        if (!getAdapter().event(request, response, status)) {
            setErrorState(ErrorState.CLOSE_NOW, null);
        }
        if (!getErrorState().isError()) {
            if (attach != null) {
                attach.setComet(comet);
                if (comet) {
                    Integer comettimeout = (Integer) request.getAttribute(
                            org.apache.coyote.Constants.COMET_TIMEOUT_ATTR);
                    if (comettimeout != null) {
                        attach.setTimeout(comettimeout.longValue());
                    }
                } else {
                    //reset the timeout
                    if (keepAlive) {
                        attach.setTimeout(keepAliveTimeout);
                    } else {
                        attach.setTimeout(soTimeout);
                    }
                }

            }
        }
    } catch (InterruptedIOException e) {
        setErrorState(ErrorState.CLOSE_NOW, e);
    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
        // 500 - Internal Server Error
        response.setStatus(500);
        setErrorState(ErrorState.CLOSE_NOW, t);
        log.error(sm.getString("http11processor.request.process"), t);
        getAdapter().log(request, response, 0);
    }

    rp.setStage(org.apache.coyote.Constants.STAGE_ENDED);

    if (getErrorState().isError() || status==SocketStatus.STOP) {
        return SocketState.CLOSED;
    } else if (!comet) {
        if (keepAlive) {
            inputBuffer.nextRequest();
            outputBuffer.nextRequest();
            return SocketState.OPEN;
        } else {
            return SocketState.CLOSED;
        }
    } else {
        return SocketState.LONG;
    }
}
 
Example 17
Source File: JIoEndpoint.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Override
public void run() {
    boolean launch = false;
    synchronized (socket) {
        try {
            SocketState state = SocketState.OPEN;

            try {
                // SSL handshake
                serverSocketFactory.handshake(socket.getSocket());
            } catch (Throwable t) {
                ExceptionUtils.handleThrowable(t);
                if (log.isDebugEnabled()) {
                    log.debug(sm.getString("endpoint.err.handshake"), t);
                }
                // Tell to close the socket
                state = SocketState.CLOSED;
            }

            if ((state != SocketState.CLOSED)) {// 非关闭状态
                if (status == null) {
                    // 调用handler对象的process方法,
                    // 这里handler对象实际上是Http11ConnectionHandler类的实例,
                    // 该对象的初始化过程是在org.apache.coyote.http11.Http11Protocol对象的构造方法中:
                    state = handler.process(socket, SocketStatus.OPEN_READ);
                } else {
                    state = handler.process(socket,status);
                }
            }
            if (state == SocketState.CLOSED) {
                // Close socket
                if (log.isTraceEnabled()) {
                    log.trace("Closing socket:"+socket);
                }
                countDownConnection();
                try {
                    socket.getSocket().close();
                } catch (IOException e) {
                    // Ignore
                }
            } else if (state == SocketState.OPEN ||
                    state == SocketState.UPGRADING ||
                    state == SocketState.UPGRADING_TOMCAT  ||
                    state == SocketState.UPGRADED){
                socket.setKeptAlive(true);
                socket.access();
                launch = true;
            } else if (state == SocketState.LONG) {
                socket.access();
                waitingRequests.add(socket);
            }
        } finally {
            if (launch) {
                try {
                    getExecutor().execute(new SocketProcessor(socket, SocketStatus.OPEN_READ));
                } catch (RejectedExecutionException x) {
                    log.warn("Socket reprocessing request was rejected for:"+socket,x);
                    try {
                        //unable to handle connection at this time
                        handler.process(socket, SocketStatus.DISCONNECT);
                    } finally {
                        countDownConnection();
                    }


                } catch (NullPointerException npe) {
                    if (running) {
                        log.error(sm.getString("endpoint.launch.fail"),
                                npe);
                    }
                }
            }
        }
    }
    socket = null;
    // Finish up this request
}