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