use of org.apache.coyote.http2.HpackEncoder.State in project tomcat by apache.
the class Http2UpgradeHandler method writePushHeaders.
protected void writePushHeaders(Stream stream, int pushedStreamId, Request coyoteRequest, int payloadSize) throws IOException {
if (log.isDebugEnabled()) {
log.debug(sm.getString("upgradeHandler.writePushHeaders", connectionId, stream.getIdentifier(), Integer.toString(pushedStreamId)));
}
// This ensures the Stream processing thread has control of the socket.
synchronized (socketWrapper) {
byte[] header = new byte[9];
ByteBuffer target = ByteBuffer.allocate(payloadSize);
boolean first = true;
State state = null;
byte[] pushedStreamIdBytes = new byte[4];
ByteUtil.set31Bits(pushedStreamIdBytes, 0, pushedStreamId);
target.put(pushedStreamIdBytes);
while (state != State.COMPLETE) {
state = getHpackEncoder().encode(coyoteRequest.getMimeHeaders(), target);
target.flip();
ByteUtil.setThreeBytes(header, 0, target.limit());
if (first) {
first = false;
header[3] = FrameType.PUSH_PROMISE.getIdByte();
} else {
header[3] = FrameType.CONTINUATION.getIdByte();
}
if (state == State.COMPLETE) {
header[4] += FLAG_END_OF_HEADERS;
}
if (log.isDebugEnabled()) {
log.debug(target.limit() + " bytes");
}
ByteUtil.set31Bits(header, 5, stream.getIdentifier().intValue());
socketWrapper.write(true, header, 0, header.length);
socketWrapper.write(true, target);
socketWrapper.flush(true);
}
}
}
use of org.apache.coyote.http2.HpackEncoder.State in project tomcat by apache.
the class TestHttp2Limits method populateHeadersPayload.
private void populateHeadersPayload(ByteBuffer headersPayload, List<String[]> customHeaders, String path) throws Exception {
MimeHeaders headers = new MimeHeaders();
headers.addValue(":method").setString("GET");
headers.addValue(":path").setString(path);
headers.addValue(":authority").setString("localhost:" + getPort());
for (String[] customHeader : customHeaders) {
headers.addValue(customHeader[0]).setString(customHeader[1]);
}
State state = hpackEncoder.encode(headers, headersPayload);
if (state != State.COMPLETE) {
throw new Exception("Unable to build headers");
}
headersPayload.flip();
log.debug("Headers payload generated of size [" + headersPayload.limit() + "]");
}
use of org.apache.coyote.http2.HpackEncoder.State in project tomcat by apache.
the class Http2UpgradeHandler method writeHeaders.
void writeHeaders(Stream stream, Response coyoteResponse, int payloadSize) throws IOException {
if (log.isDebugEnabled()) {
log.debug(sm.getString("upgradeHandler.writeHeaders", connectionId, stream.getIdentifier()));
}
if (!stream.canWrite()) {
return;
}
prepareHeaders(coyoteResponse);
byte[] header = new byte[9];
ByteBuffer target = ByteBuffer.allocate(payloadSize);
boolean first = true;
State state = null;
// This ensures the Stream processing thread has control of the socket.
synchronized (socketWrapper) {
while (state != State.COMPLETE) {
state = getHpackEncoder().encode(coyoteResponse.getMimeHeaders(), target);
target.flip();
ByteUtil.setThreeBytes(header, 0, target.limit());
if (first) {
first = false;
header[3] = FrameType.HEADERS.getIdByte();
if (stream.getOutputBuffer().hasNoBody()) {
header[4] = FLAG_END_OF_STREAM;
}
} else {
header[3] = FrameType.CONTINUATION.getIdByte();
}
if (state == State.COMPLETE) {
header[4] += FLAG_END_OF_HEADERS;
}
if (log.isDebugEnabled()) {
log.debug(target.limit() + " bytes");
}
ByteUtil.set31Bits(header, 5, stream.getIdentifier().intValue());
try {
socketWrapper.write(true, header, 0, header.length);
socketWrapper.write(true, target);
socketWrapper.flush(true);
} catch (IOException ioe) {
handleAppInitiatedIOException(ioe);
}
}
}
}
use of org.apache.coyote.http2.HpackEncoder.State in project tomcat by apache.
the class Http2AsyncUpgradeHandler method writeHeaders.
@Override
void writeHeaders(Stream stream, Response coyoteResponse, int payloadSize) throws IOException {
if (log.isDebugEnabled()) {
log.debug(sm.getString("upgradeHandler.writeHeaders", connectionId, stream.getIdentifier()));
}
if (!stream.canWrite()) {
return;
}
prepareHeaders(coyoteResponse);
boolean first = true;
State state = null;
ArrayList<ByteBuffer> bufs = new ArrayList<>();
// This ensures the Stream processing thread has control of the socket.
while (state != State.COMPLETE) {
byte[] header = new byte[9];
ByteBuffer target = ByteBuffer.allocate(payloadSize);
state = getHpackEncoder().encode(coyoteResponse.getMimeHeaders(), target);
target.flip();
ByteUtil.setThreeBytes(header, 0, target.limit());
if (first) {
first = false;
header[3] = FrameType.HEADERS.getIdByte();
if (stream.getOutputBuffer().hasNoBody()) {
header[4] = FLAG_END_OF_STREAM;
}
} else {
header[3] = FrameType.CONTINUATION.getIdByte();
}
if (state == State.COMPLETE) {
header[4] += FLAG_END_OF_HEADERS;
}
if (log.isDebugEnabled()) {
log.debug(target.limit() + " bytes");
}
ByteUtil.set31Bits(header, 5, stream.getIdentifier().intValue());
bufs.add(ByteBuffer.wrap(header));
bufs.add(target);
}
socketWrapper.write(BlockingMode.SEMI_BLOCK, getWriteTimeout(), TimeUnit.MILLISECONDS, null, SocketWrapperBase.COMPLETE_WRITE, applicationErrorCompletion, bufs.toArray(BYTEBUFFER_ARRAY));
handleAsyncException();
}
use of org.apache.coyote.http2.HpackEncoder.State in project tomcat by apache.
the class Http2AsyncUpgradeHandler method writePushHeaders.
@Override
protected void writePushHeaders(Stream stream, int pushedStreamId, Request coyoteRequest, int payloadSize) throws IOException {
if (log.isDebugEnabled()) {
log.debug(sm.getString("upgradeHandler.writePushHeaders", connectionId, stream.getIdentifier(), Integer.toString(pushedStreamId)));
}
// This ensures the Stream processing thread has control of the socket.
boolean first = true;
State state = null;
ArrayList<ByteBuffer> bufs = new ArrayList<>();
byte[] pushedStreamIdBytes = new byte[4];
ByteUtil.set31Bits(pushedStreamIdBytes, 0, pushedStreamId);
while (state != State.COMPLETE) {
byte[] header = new byte[9];
ByteBuffer target = ByteBuffer.allocate(payloadSize);
target.put(pushedStreamIdBytes);
state = getHpackEncoder().encode(coyoteRequest.getMimeHeaders(), target);
target.flip();
ByteUtil.setThreeBytes(header, 0, target.limit());
if (first) {
first = false;
header[3] = FrameType.PUSH_PROMISE.getIdByte();
} else {
header[3] = FrameType.CONTINUATION.getIdByte();
}
if (state == State.COMPLETE) {
header[4] += FLAG_END_OF_HEADERS;
}
if (log.isDebugEnabled()) {
log.debug(target.limit() + " bytes");
}
ByteUtil.set31Bits(header, 5, stream.getIdentifier().intValue());
bufs.add(ByteBuffer.wrap(header));
bufs.add(target);
}
socketWrapper.write(BlockingMode.SEMI_BLOCK, getWriteTimeout(), TimeUnit.MILLISECONDS, null, SocketWrapperBase.COMPLETE_WRITE, applicationErrorCompletion, bufs.toArray(BYTEBUFFER_ARRAY));
handleAsyncException();
}
Aggregations