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

The following examples show how to use org.apache.tomcat.util.net.AbstractEndpoint.Handler.SocketState#UPGRADED . 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: WsFrameServer.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
private SocketState doOnDataAvailable() throws IOException {
    onDataAvailable();
    while (isOpen()) {
        switch (getReadState()) {
        case PROCESSING:
            if (!changeReadState(ReadState.PROCESSING, ReadState.WAITING)) {
                continue;
            }
            return SocketState.UPGRADED;
        case SUSPENDING_PROCESS:
            if (!changeReadState(ReadState.SUSPENDING_PROCESS, ReadState.SUSPENDED)) {
                continue;
            }
            return SocketState.SUSPENDED;
        default:
            throw new IllegalStateException(
                    sm.getString("wsFrameServer.illegalReadState", getReadState()));
        }
    }

    return SocketState.CLOSED;
}
 
Example 2
Source File: TestUpgradeInternalHandler.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Override
public SocketState upgradeDispatch(SocketEvent status) {
    System.out.println("Processing: " + status);
    switch (status) {
    case OPEN_READ:
        // Note: there's always an initial read event at the moment (reading should be skipped since it ends up in the internal buffer)
        break;
    case OPEN_WRITE:
        break;
    case STOP:
    case DISCONNECT:
    case ERROR:
    case TIMEOUT:
    case CONNECT_FAIL:
        return SocketState.CLOSED;
    }
    return SocketState.UPGRADED;
}
 
Example 3
Source File: Http2UpgradeHandler.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Override
public SocketState upgradeDispatch(SocketEvent status) {
    if (log.isDebugEnabled()) {
        log.debug(sm.getString("upgradeHandler.upgradeDispatch.entry", connectionId, status));
    }

    // WebConnection is not used so passing null here is fine
    // Might not be necessary. init() will handle that.
    init(null);


    SocketState result = SocketState.CLOSED;

    try {
        pingManager.sendPing(false);

        switch(status) {
        case OPEN_READ:
            try {
                // There is data to read so use the read timeout while
                // reading frames ...
                socketWrapper.setReadTimeout(getReadTimeout());
                // ... and disable the connection timeout
                setConnectionTimeout(-1);
                while (true) {
                    try {
                        if (!parser.readFrame(false)) {
                            break;
                        }
                    } catch (StreamException se) {
                        // Stream errors are not fatal to the connection so
                        // continue reading frames
                        Stream stream = getStream(se.getStreamId(), false);
                        if (stream == null) {
                            sendStreamReset(se);
                        } else {
                            stream.close(se);
                        }
                    }
                    if (overheadCount.get() > 0) {
                        throw new ConnectionException(
                                sm.getString("upgradeHandler.tooMuchOverhead", connectionId),
                                Http2Error.ENHANCE_YOUR_CALM);
                    }
                }

                // Need to know the correct timeout before starting the read
                // but that may not be known at this time if one or more
                // requests are currently being processed so don't set a
                // timeout for the socket...
                socketWrapper.setReadTimeout(-1);

                // ...set a timeout on the connection
                setConnectionTimeoutForStreamCount(activeRemoteStreamCount.get());

            } catch (Http2Exception ce) {
                // Really ConnectionException
                if (log.isDebugEnabled()) {
                    log.debug(sm.getString("upgradeHandler.connectionError"), ce);
                }
                closeConnection(ce);
                break;
            }

            if (connectionState.get() != ConnectionState.CLOSED) {
                result = SocketState.UPGRADED;
            }
            break;

        case OPEN_WRITE:
            processWrites();

            result = SocketState.UPGRADED;
            break;

        case TIMEOUT:
            closeConnection(null);
            break;

        case DISCONNECT:
        case ERROR:
        case STOP:
        case CONNECT_FAIL:
            close();
            break;
        }
    } catch (IOException ioe) {
        if (log.isDebugEnabled()) {
            log.debug(sm.getString("upgradeHandler.ioerror", connectionId), ioe);
        }
        close();
    }

    if (log.isDebugEnabled()) {
        log.debug(sm.getString("upgradeHandler.upgradeDispatch.exit", connectionId, result));
    }
    return result;
}
 
Example 4
Source File: StreamInbound.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@Override
public final SocketState onData() throws IOException {
    // Must be start the start of a message (which may consist of multiple
    // frames)
    WsInputStream wsIs = new WsInputStream(processor, getWsOutbound());

    try {
        WsFrame frame = wsIs.nextFrame(false);

        while (frame != null) {
            // TODO User defined extensions may define values for rsv
            if (frame.getRsv() > 0) {
                closeOutboundConnection(
                        Constants.STATUS_PROTOCOL_ERROR, null);
                return SocketState.CLOSED;
            }

            byte opCode = frame.getOpCode();

            if (opCode == Constants.OPCODE_BINARY) {
                doOnBinaryData(wsIs);
            } else if (opCode == Constants.OPCODE_TEXT) {
                InputStreamReader r =
                        new InputStreamReader(wsIs, new Utf8Decoder());
                doOnTextData(r);
            } else if (opCode == Constants.OPCODE_CLOSE){
                closeOutboundConnection(frame);
                return SocketState.CLOSED;
            } else if (opCode == Constants.OPCODE_PING) {
                getWsOutbound().pong(frame.getPayLoad());
            } else if (opCode == Constants.OPCODE_PONG) {
                doOnPong(frame.getPayLoad());
            } else {
                // Unknown OpCode
                closeOutboundConnection(
                        Constants.STATUS_PROTOCOL_ERROR, null);
                return SocketState.CLOSED;
            }
            frame = wsIs.nextFrame(false);
        }
    } catch (MalformedInputException mie) {
        // Invalid UTF-8
        closeOutboundConnection(Constants.STATUS_BAD_DATA, null);
        return SocketState.CLOSED;
    } catch (UnmappableCharacterException uce) {
        // Invalid UTF-8
        closeOutboundConnection(Constants.STATUS_BAD_DATA, null);
        return SocketState.CLOSED;
    } catch (IOException ioe) {
        // Given something must have gone to reach this point, this
        // might not work but try it anyway.
        closeOutboundConnection(Constants.STATUS_PROTOCOL_ERROR, null);
        return SocketState.CLOSED;
    }
    return SocketState.UPGRADED;
}
 
Example 5
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 6
Source File: StreamInbound.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Override
public final SocketState onData() throws IOException {
    // Must be start the start of a message (which may consist of multiple
    // frames)
    WsInputStream wsIs = new WsInputStream(processor, getWsOutbound());

    try {
        WsFrame frame = wsIs.nextFrame(false);

        while (frame != null) {
            // TODO User defined extensions may define values for rsv
            if (frame.getRsv() > 0) {
                closeOutboundConnection(
                        Constants.STATUS_PROTOCOL_ERROR, null);
                return SocketState.CLOSED;
            }

            byte opCode = frame.getOpCode();

            if (opCode == Constants.OPCODE_BINARY) {
                doOnBinaryData(wsIs);
            } else if (opCode == Constants.OPCODE_TEXT) {
                InputStreamReader r =
                        new InputStreamReader(wsIs, new Utf8Decoder());
                doOnTextData(r);
            } else if (opCode == Constants.OPCODE_CLOSE){
                closeOutboundConnection(frame);
                return SocketState.CLOSED;
            } else if (opCode == Constants.OPCODE_PING) {
                getWsOutbound().pong(frame.getPayLoad());
            } else if (opCode == Constants.OPCODE_PONG) {
                doOnPong(frame.getPayLoad());
            } else {
                // Unknown OpCode
                closeOutboundConnection(
                        Constants.STATUS_PROTOCOL_ERROR, null);
                return SocketState.CLOSED;
            }
            frame = wsIs.nextFrame(false);
        }
    } catch (MalformedInputException mie) {
        // Invalid UTF-8
        closeOutboundConnection(Constants.STATUS_BAD_DATA, null);
        return SocketState.CLOSED;
    } catch (UnmappableCharacterException uce) {
        // Invalid UTF-8
        closeOutboundConnection(Constants.STATUS_BAD_DATA, null);
        return SocketState.CLOSED;
    } catch (IOException ioe) {
        // Given something must have gone to reach this point, this
        // might not work but try it anyway.
        closeOutboundConnection(Constants.STATUS_PROTOCOL_ERROR, null);
        return SocketState.CLOSED;
    }
    return SocketState.UPGRADED;
}
 
Example 7
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
}