Search in sources :

Example 1 with H2Error

use of org.apache.hc.core5.http2.H2Error in project httpcomponents-core by apache.

the class FramePrinter method printPayload.

public void printPayload(final RawFrame frame, final Appendable appendable) throws IOException {
    final FrameType type = FrameType.valueOf(frame.getType());
    final ByteBuffer buf = frame.getPayloadContent();
    if (buf != null) {
        switch(type) {
            case SETTINGS:
                if ((buf.remaining() % 6) == 0) {
                    while (buf.hasRemaining()) {
                        final int code = buf.getShort();
                        final H2Param param = H2Param.valueOf(code);
                        final int value = buf.getInt();
                        if (param != null) {
                            appendable.append(param.name());
                        } else {
                            appendable.append("0x").append(Integer.toHexString(code));
                        }
                        appendable.append(": ").append(Integer.toString(value)).append("\r\n");
                    }
                } else {
                    appendable.append("Invalid\r\n");
                }
                break;
            case RST_STREAM:
                if (buf.remaining() == 4) {
                    appendable.append("Code ");
                    final int code = buf.getInt();
                    final H2Error error = H2Error.getByCode(code);
                    if (error != null) {
                        appendable.append(error.name());
                    } else {
                        appendable.append("0x").append(Integer.toHexString(code));
                    }
                    appendable.append("\r\n");
                } else {
                    appendable.append("Invalid\r\n");
                }
                break;
            case GOAWAY:
                if (buf.remaining() >= 8) {
                    final int lastStream = buf.getInt();
                    appendable.append("Last stream ").append(Integer.toString(lastStream)).append("\r\n");
                    appendable.append("Code ");
                    final int code2 = buf.getInt();
                    final H2Error error2 = H2Error.getByCode(code2);
                    if (error2 != null) {
                        appendable.append(error2.name());
                    } else {
                        appendable.append("0x").append(Integer.toHexString(code2));
                    }
                    appendable.append("\r\n");
                    final byte[] tmp = new byte[buf.remaining()];
                    buf.get(tmp);
                    appendable.append(new String(tmp, StandardCharsets.US_ASCII));
                    appendable.append("\r\n");
                } else {
                    appendable.append("Invalid\r\n");
                }
                break;
            case WINDOW_UPDATE:
                if (buf.remaining() == 4) {
                    final int increment = buf.getInt();
                    appendable.append("Increment ").append(Integer.toString(increment)).append("\r\n");
                } else {
                    appendable.append("Invalid\r\n");
                }
                break;
            case PUSH_PROMISE:
                if (buf.remaining() > 4) {
                    final int streamId = buf.getInt();
                    appendable.append("Promised stream ").append(Integer.toString(streamId)).append("\r\n");
                    printData(buf, appendable);
                } else {
                    appendable.append("Invalid\r\n");
                }
                break;
            default:
                printData(frame.getPayload(), appendable);
        }
    }
}
Also used : H2Error(org.apache.hc.core5.http2.H2Error) H2Param(org.apache.hc.core5.http2.config.H2Param) ByteBuffer(java.nio.ByteBuffer)

Example 2 with H2Error

use of org.apache.hc.core5.http2.H2Error in project httpcomponents-core by apache.

the class AbstractH2StreamMultiplexer method onException.

public final void onException(final Exception cause) {
    try {
        for (; ; ) {
            final AsyncPingHandler pingHandler = pingHandlers.poll();
            if (pingHandler != null) {
                pingHandler.failed(cause);
            } else {
                break;
            }
        }
        for (; ; ) {
            final Command command = ioSession.poll();
            if (command != null) {
                if (command instanceof ExecutableCommand) {
                    ((ExecutableCommand) command).failed(new ConnectionClosedException());
                } else {
                    command.cancel();
                }
            } else {
                break;
            }
        }
        for (final Iterator<Map.Entry<Integer, H2Stream>> it = streamMap.entrySet().iterator(); it.hasNext(); ) {
            final Map.Entry<Integer, H2Stream> entry = it.next();
            final H2Stream stream = entry.getValue();
            stream.reset(cause);
        }
        streamMap.clear();
        if (!(cause instanceof ConnectionClosedException)) {
            if (connState.compareTo(ConnectionHandshake.GRACEFUL_SHUTDOWN) <= 0) {
                final H2Error errorCode;
                if (cause instanceof H2ConnectionException) {
                    errorCode = H2Error.getByCode(((H2ConnectionException) cause).getCode());
                } else if (cause instanceof ProtocolException) {
                    errorCode = H2Error.PROTOCOL_ERROR;
                } else {
                    errorCode = H2Error.INTERNAL_ERROR;
                }
                final RawFrame goAway = frameFactory.createGoAway(processedRemoteStreamId, errorCode, cause.getMessage());
                commitFrame(goAway);
            }
        }
    } catch (final IOException ignore) {
    } finally {
        connState = ConnectionHandshake.SHUTDOWN;
        final CloseMode closeMode;
        if (cause instanceof ConnectionClosedException) {
            closeMode = CloseMode.GRACEFUL;
        } else if (cause instanceof IOException) {
            closeMode = CloseMode.IMMEDIATE;
        } else {
            closeMode = CloseMode.GRACEFUL;
        }
        ioSession.close(closeMode);
    }
}
Also used : ProtocolException(org.apache.hc.core5.http.ProtocolException) H2ConnectionException(org.apache.hc.core5.http2.H2ConnectionException) ConnectionClosedException(org.apache.hc.core5.http.ConnectionClosedException) H2Error(org.apache.hc.core5.http2.H2Error) IOException(java.io.IOException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PingCommand(org.apache.hc.core5.http2.nio.command.PingCommand) ExecutableCommand(org.apache.hc.core5.http.nio.command.ExecutableCommand) Command(org.apache.hc.core5.reactor.Command) ShutdownCommand(org.apache.hc.core5.http.nio.command.ShutdownCommand) AsyncPingHandler(org.apache.hc.core5.http2.nio.AsyncPingHandler) CloseMode(org.apache.hc.core5.io.CloseMode) RawFrame(org.apache.hc.core5.http2.frame.RawFrame) ExecutableCommand(org.apache.hc.core5.http.nio.command.ExecutableCommand) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

H2Error (org.apache.hc.core5.http2.H2Error)2 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ConnectionClosedException (org.apache.hc.core5.http.ConnectionClosedException)1 ProtocolException (org.apache.hc.core5.http.ProtocolException)1 ExecutableCommand (org.apache.hc.core5.http.nio.command.ExecutableCommand)1 ShutdownCommand (org.apache.hc.core5.http.nio.command.ShutdownCommand)1 H2ConnectionException (org.apache.hc.core5.http2.H2ConnectionException)1 H2Param (org.apache.hc.core5.http2.config.H2Param)1 RawFrame (org.apache.hc.core5.http2.frame.RawFrame)1 AsyncPingHandler (org.apache.hc.core5.http2.nio.AsyncPingHandler)1 PingCommand (org.apache.hc.core5.http2.nio.command.PingCommand)1 CloseMode (org.apache.hc.core5.io.CloseMode)1 Command (org.apache.hc.core5.reactor.Command)1