/** * Create a new {@link Http2Headers} object which will store the results of the decode operation. * @return a new {@link Http2Headers} object which will store the results of the decode operation. */ protected Http2Headers newHeaders() { return new DefaultHttp2Headers(validateHeaders, (int) headerArraySizeAccumulator); } }
@Override public MultiMap trailers() { synchronized (conn) { if (trailedMap == null) { trailedMap = new Http2HeadersAdaptor(trailers = new DefaultHttp2Headers()); } return trailedMap; } }
@Override public void failure(Throwable error) { ctx.writeAndFlush(new DefaultHttp2Headers().status(HttpResponseStatus.INTERNAL_SERVER_ERROR.codeAsText())) .addListener(ChannelFutureListener.CLOSE); }
@Override public HttpServerResponse writeContinue() { synchronized (conn) { checkHeadWritten(); stream.writeHeaders(new DefaultHttp2Headers().status("100"), false); ctx.flush(); return this; } }
public static Http2Headers toHttp2Headers(HttpHeaders inHeaders, boolean validateHeaders) { if (inHeaders.isEmpty()) { return EmptyHttp2Headers.INSTANCE; } final Http2Headers out = new DefaultHttp2Headers(validateHeaders, inHeaders.size()); toHttp2Headers(inHeaders, out); return out; }
@Before public void setUp() { headers = new DefaultHttp2Headers(); map = new Http2HeadersAdaptor(headers); }
synchronized void sendPush(int streamId, String host, HttpMethod method, MultiMap headers, String path, StreamPriority streamPriority, Handler<AsyncResult<HttpServerResponse>> completionHandler) { Http2Headers headers_ = new DefaultHttp2Headers(); if (method == HttpMethod.OTHER) { throw new IllegalArgumentException("Cannot push HttpMethod.OTHER");
private static Http2Headers http1HeadersToHttp2Headers(FullHttpRequest request) { CharSequence host = request.headers().get(HttpHeaderNames.HOST); Http2Headers http2Headers = new DefaultHttp2Headers() .method(HttpMethod.GET.asciiName()) .path(request.uri()) .scheme(HttpScheme.HTTP.name()); if (host != null) { http2Headers.authority(host); } return http2Headers; }
private static Http2Headers http1HeadersToHttp2Headers(FullHttpRequest request) { CharSequence host = request.headers().get(HttpHeaderNames.HOST); Http2Headers http2Headers = new DefaultHttp2Headers() .method(HttpMethod.GET.asciiName()) .path(request.uri()) .scheme(HttpScheme.HTTP.name()); if (host != null) { http2Headers.authority(host); } return http2Headers; }
protected void sendHttp2Response(ChannelHandlerContext ctx, int streamId, HttpResponseStatus status, String result) { // Send a frame for the response status Http2Headers headers = new DefaultHttp2Headers().status(status.codeAsText()); if (!HttpResponseStatus.OK.equals(status)) { headers.set(RemotingConstants.HEAD_RESPONSE_ERROR, "true"); } if (StringUtils.isNotBlank(result)) { ByteBuf data = ctx.alloc().buffer(); data.writeBytes(result.getBytes(RpcConstants.DEFAULT_CHARSET)); encoder().writeHeaders(ctx, streamId, headers, 0, false, ctx.newPromise()); encoder().writeData(ctx, streamId, data, 0, true, ctx.newPromise()); } else { encoder().writeHeaders(ctx, streamId, headers, 0, true, ctx.newPromise()); } }
protected void sendHttp2Response(ChannelHandlerContext ctx, int streamId, HttpResponseStatus status, String result) { // Send a frame for the response status Http2Headers headers = new DefaultHttp2Headers().status(status.codeAsText()); if (!HttpResponseStatus.OK.equals(status)) { headers.set(RemotingConstants.HEAD_RESPONSE_ERROR, "true"); } if (StringUtils.isNotBlank(result)) { ByteBuf data = ctx.alloc().buffer(); data.writeBytes(result.getBytes(RpcConstants.DEFAULT_CHARSET)); encoder().writeHeaders(ctx, streamId, headers, 0, false, ctx.newPromise()); encoder().writeData(ctx, streamId, data, 0, true, ctx.newPromise()); } else { encoder().writeHeaders(ctx, streamId, headers, 0, true, ctx.newPromise()); } }
@Override public void push(final String method, final String path, final Map<String, Object> headers) { ctx.channel().eventLoop().execute(() -> { AsciiString streamIdHeader = HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text(); Http2Connection connection = encoder.connection(); int nextStreamId = connection.local().incrementAndGetNextStreamId(); Http2Headers h2headers = new DefaultHttp2Headers() .path(path) .method(method) .authority(authority) .scheme(scheme); headers.forEach((n, v) -> h2headers.add(n, v.toString())); encoder.writePushPromise(ctx, streamId, nextStreamId, h2headers, 0, ctx.newPromise()); // TODO: Is there another way of handling a push promise? DefaultFullHttpRequest pushRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.valueOf(method.toUpperCase()), path, Unpooled.EMPTY_BUFFER, new DefaultHttpHeaders(false).set(streamIdHeader, nextStreamId), EmptyHttpHeaders.INSTANCE); ctx.pipeline().fireChannelRead(pushRequest); ctx.pipeline().fireChannelReadComplete(); }); }
private boolean handle100Continue(ChannelHandlerContext ctx, int streamId, Http2Headers headers) { final CharSequence expectValue = headers.get(HttpHeaderNames.EXPECT); if (expectValue == null) { // No 'expect' header. return true; } // '100-continue' is the only allowed expectation. if (!AsciiString.contentEqualsIgnoreCase(HttpHeaderValues.CONTINUE, expectValue)) { return false; } // Send a '100 Continue' response. writer.writeHeaders( ctx, streamId, new DefaultHttp2Headers(false).status(HttpStatus.CONTINUE.codeAsText()), 0, false, ctx.voidPromise()); // Remove the 'expect' header so that it's handled in a way invisible to a Service. headers.remove(HttpHeaderNames.EXPECT); return true; }
@Override public void writeHead(HttpMethod method, String rawMethod, String uri, MultiMap headers, String hostHeader, boolean chunked, ByteBuf content, boolean end, StreamPriority priority) { Http2Headers h = new DefaultHttp2Headers(); h.method(method != HttpMethod.OTHER ? method.name() : rawMethod); if (method == HttpMethod.CONNECT) {
@Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { if (evt instanceof HttpServerUpgradeHandler.UpgradeEvent) { HttpServerUpgradeHandler.UpgradeEvent upgradeEvent = (HttpServerUpgradeHandler.UpgradeEvent) evt; // Map initial request headers to HTTP2 FullHttpRequest request = upgradeEvent.upgradeRequest(); Http2Headers headers = new DefaultHttp2Headers() .method(HttpMethod.GET.asciiName()) .path(request.uri()) .scheme(HttpScheme.HTTP.name()); CharSequence host = request.headers().get(HttpHeaderNames.HOST); if (host != null) { headers.authority(host); } // Process mapped headers onHeadersRead(ctx, 1, headers, 0, true); } super.userEventTriggered(ctx, evt); }
final Http2Headers out = new DefaultHttp2Headers(validateHeaders, inHeaders.size()); if (in instanceof HttpRequest) { HttpRequest request = (HttpRequest) in;
private void writeErrorResponse(ChannelHandlerContext ctx, int streamId, HttpResponseStatus status) throws Http2Exception { final byte[] content = status.toString().getBytes(StandardCharsets.UTF_8); writer.writeHeaders( ctx, streamId, new DefaultHttp2Headers(false) .status(status.codeAsText()) .set(HttpHeaderNames.CONTENT_TYPE, MediaType.PLAIN_TEXT_UTF_8.toString()) .setInt(HttpHeaderNames.CONTENT_LENGTH, content.length), 0, false, ctx.voidPromise()); writer.writeData(ctx, streamId, Unpooled.wrappedBuffer(content), 0, true, ctx.voidPromise()); final Http2Stream stream = writer.connection().stream(streamId); if (stream != null && writer.flowController().hasFlowControlled(stream)) { // Ensure to flush the error response if it's flow-controlled so that it is sent // before an RST_STREAM frame. writer.flowController().writePendingBytes(); } }
private void sendHttp2Response0(HttpResponseStatus status, boolean error, ByteBuf data) { Http2Headers headers = new DefaultHttp2Headers().status(status.codeAsText()); if (request.getSerializeType() > 0) { String serialization = SerializerFactory.getAliasByCode(request.getSerializeType()); headers.set(RemotingConstants.HEAD_SERIALIZE_TYPE, serialization); } else { headers.set(CONTENT_TYPE, "text/plain; charset=" + RpcConstants.DEFAULT_CHARSET.displayName()); } if (error) { headers.set(RemotingConstants.HEAD_RESPONSE_ERROR, "true"); } if (data != null) { encoder.writeHeaders(ctx, streamId, headers, 0, false, ctx.newPromise()); encoder.writeData(ctx, streamId, data, 0, true, ctx.newPromise()); } else { encoder.writeHeaders(ctx, streamId, headers, 0, true, ctx.newPromise()); } } }
private void sendHttp2Response0(HttpResponseStatus status, boolean error, ByteBuf data) { Http2Headers headers = new DefaultHttp2Headers().status(status.codeAsText()); if (request.getSerializeType() > 0) { String serialization = SerializerFactory.getAliasByCode(request.getSerializeType()); headers.set(RemotingConstants.HEAD_SERIALIZE_TYPE, serialization); } else { headers.set(CONTENT_TYPE, "text/plain; charset=" + RpcConstants.DEFAULT_CHARSET.displayName()); } if (error) { headers.set(RemotingConstants.HEAD_RESPONSE_ERROR, "true"); } if (data != null) { encoder.writeHeaders(ctx, streamId, headers, 0, false, ctx.newPromise()); encoder.writeData(ctx, streamId, data, 0, true, ctx.newPromise()); } else { encoder.writeHeaders(ctx, streamId, headers, 0, true, ctx.newPromise()); } } }
protected Http2Headers getHeadersForPushNotification(final ApnsPushNotification pushNotification, final int streamId) { final Http2Headers headers = new DefaultHttp2Headers() .method(HttpMethod.POST.asciiName()) .authority(this.authority) .path(APNS_PATH_PREFIX + pushNotification.getToken()) .scheme(HttpScheme.HTTPS.name()) .addInt(APNS_EXPIRATION_HEADER, pushNotification.getExpiration() == null ? 0 : (int) (pushNotification.getExpiration().getTime() / 1000)); if (pushNotification.getCollapseId() != null) { headers.add(APNS_COLLAPSE_ID_HEADER, pushNotification.getCollapseId()); } if (pushNotification.getPriority() != null) { headers.addInt(APNS_PRIORITY_HEADER, pushNotification.getPriority().getCode()); } if (pushNotification.getTopic() != null) { headers.add(APNS_TOPIC_HEADER, pushNotification.getTopic()); } if (pushNotification.getApnsId() != null) { headers.add(APNS_ID_HEADER, FastUUID.toString(pushNotification.getApnsId())); } return headers; }