Java Code Examples for io.netty.handler.codec.http2.Http2HeadersFrame#isEndStream()

The following examples show how to use io.netty.handler.codec.http2.Http2HeadersFrame#isEndStream() . 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: H2PriorKnowledgeFeatureParityTest.java    From servicetalk with Apache License 2.0 6 votes vote down vote up
private void onHeadersRead(ChannelHandlerContext ctx, Http2HeadersFrame headers) {
    if (headers.isEndStream()) {
        ctx.write(new DefaultHttp2HeadersFrame(headers.headers(), true));
    } else {
        Http2Headers outHeaders = new DefaultHttp2Headers();
        if (headers.headers().contains(EXPECT, CONTINUE)) {
            if (headers.headers().contains(EXPECT_FAIL_HEADER)) {
                outHeaders.status(
                        io.netty.handler.codec.http.HttpResponseStatus.EXPECTATION_FAILED.codeAsText());
                ctx.write(new DefaultHttp2HeadersFrame(outHeaders, true));
                return;
            } else {
                outHeaders.status(io.netty.handler.codec.http.HttpResponseStatus.CONTINUE.codeAsText());
            }
        } else {
            outHeaders.status(io.netty.handler.codec.http.HttpResponseStatus.OK.codeAsText());
        }

        CharSequence contentType = headers.headers().get(CONTENT_TYPE);
        if (contentType != null) {
            outHeaders.add(CONTENT_TYPE, contentType);
        }
        outHeaders.add(HttpHeaderNames.COOKIE, headers.headers().getAll(HttpHeaderNames.COOKIE));
        ctx.write(new DefaultHttp2HeadersFrame(outHeaders));
    }
}
 
Example 2
Source File: Http2ServerResponseHandler.java    From tutorials with MIT License 6 votes vote down vote up
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof Http2HeadersFrame) {
        Http2HeadersFrame msgHeader = (Http2HeadersFrame) msg;
        if (msgHeader.isEndStream()) {
            ByteBuf content = ctx.alloc()
                .buffer();
            content.writeBytes(RESPONSE_BYTES.duplicate());

            Http2Headers headers = new DefaultHttp2Headers().status(HttpResponseStatus.OK.codeAsText());
            ctx.write(new DefaultHttp2HeadersFrame(headers).stream(msgHeader.stream()));
            ctx.write(new DefaultHttp2DataFrame(content, true).stream(msgHeader.stream()));
        }

    } else {
        super.channelRead(ctx, msg);
    }
}
 
Example 3
Source File: HelloWorldHttp2Handler.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
/**
 * If receive a frame with end-of-stream set, send a pre-canned response.
 */
private static void onHeadersRead(ChannelHandlerContext ctx, Http2HeadersFrame headers)
        throws Exception {
    if (headers.isEndStream()) {
        ByteBuf content = ctx.alloc().buffer();
        content.writeBytes(RESPONSE_BYTES.duplicate());
        ByteBufUtil.writeAscii(content, " - via HTTP/2");
        sendResponse(ctx, content);
    }
}
 
Example 4
Source File: HelloWorldHttp2Handler.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
/**
 * If receive a frame with end-of-stream set, send a pre-canned response.
 */
private static void onHeadersRead(ChannelHandlerContext ctx, Http2HeadersFrame headers)
        throws Exception {
    if (headers.isEndStream()) {
        ByteBuf content = ctx.alloc().buffer();
        content.writeBytes(RESPONSE_BYTES.duplicate());
        ByteBufUtil.writeAscii(content, " - via HTTP/2");
        sendResponse(ctx, headers.stream(), content);
    }
}
 
Example 5
Source File: AccessLogHandlerH2.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("FutureReturnValueIgnored")
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
	boolean lastContent = false;
	if (msg instanceof Http2HeadersFrame) {
		final Http2HeadersFrame responseHeaders = (Http2HeadersFrame) msg;
		final Http2Headers headers = responseHeaders.headers();
		lastContent = responseHeaders.isEndStream();

		accessLog.status(headers.status())
		         .chunked(true);
	}
	if (msg instanceof Http2DataFrame) {
		final Http2DataFrame data = (Http2DataFrame) msg;
		lastContent = data.isEndStream();

		accessLog.increaseContentLength(data.content().readableBytes());
	}
	if (lastContent) {
		ctx.write(msg, promise.unvoid())
		   .addListener(future -> {
		       if (future.isSuccess()) {
		           accessLog.log();
		       }
		   });
		return;
	}
	//"FutureReturnValueIgnored" this is deliberate
	ctx.write(msg, promise);
}
 
Example 6
Source File: H2ToStH1ClientDuplexHandler.java    From servicetalk with Apache License 2.0 4 votes vote down vote up
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    if (msg instanceof Http2HeadersFrame) {
        Http2HeadersFrame headersFrame = (Http2HeadersFrame) msg;
        Http2Headers h2Headers = headersFrame.headers();
        final HttpResponseStatus httpStatus;
        if (!readHeaders) {
            CharSequence status = h2Headers.getAndRemove(STATUS.value());
            if (status == null) {
                throw new IllegalArgumentException("a response must have " + STATUS + " header");
            }
            httpStatus = HttpResponseStatus.of(status);
            if (httpStatus.statusClass().equals(INFORMATIONAL_1XX)) {
                // We don't expose 1xx "interim responses" [2] to the user, and discard them to make way for the
                // "real" response.
                //
                // for a response only, zero or more HEADERS frames (each followed
                //        by zero or more CONTINUATION frames) containing the message
                //        headers of informational (1xx) HTTP responses. [1]
                // A client MUST be able to parse one or more 1xx responses received
                //    prior to a final response, even if the client does not expect one.  A
                //    user agent MAY ignore unexpected 1xx responses. [2]
                // 1xx responses are terminated by the first empty line after
                //    the status-line (the empty line signaling the end of the header
                //    section). [2]
                // [1] https://tools.ietf.org/html/rfc7540#section-8.1
                // [2] https://tools.ietf.org/html/rfc7231#section-6.2
                return;
            }
            readHeaders = true;
        } else {
            httpStatus = null;
        }

        if (headersFrame.isEndStream()) {
            if (httpStatus != null) {
                fireFullResponse(ctx, h2Headers, httpStatus);
            } else {
                ctx.fireChannelRead(h2HeadersToH1HeadersClient(h2Headers, null));
            }
        } else if (httpStatus == null) {
            throw new IllegalArgumentException("a response must have " + STATUS + " header");
        } else {
            StreamingHttpResponse response = newResponse(httpStatus, HTTP_2_0,
                    h2HeadersToH1HeadersClient(h2Headers, httpStatus), allocator, headersFactory);
            ctx.fireChannelRead(response);
        }
    } else if (msg instanceof Http2DataFrame) {
        readDataFrame(ctx, msg);
    } else {
        ctx.fireChannelRead(msg);
    }
}
 
Example 7
Source File: H2ToStH1ServerDuplexHandler.java    From servicetalk with Apache License 2.0 4 votes vote down vote up
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    if (msg instanceof Http2HeadersFrame) {
        Http2HeadersFrame headersFrame = (Http2HeadersFrame) msg;
        Http2Headers h2Headers = headersFrame.headers();
        final HttpRequestMethod httpMethod;
        final String path;
        if (!readHeaders) {
            CharSequence method = h2Headers.getAndRemove(METHOD.value());
            CharSequence pathSequence = h2Headers.getAndRemove(PATH.value());
            if (pathSequence == null || method == null) {
                throw new IllegalArgumentException("a request must have " + METHOD + " and " +
                        PATH + " headers");
            }
            path = pathSequence.toString();
            httpMethod = sequenceToHttpRequestMethod(method);
            readHeaders = true;
        } else {
            httpMethod = null;
            path = null;
        }

        if (headersFrame.isEndStream()) {
            if (httpMethod != null) {
                fireFullRequest(ctx, h2Headers, httpMethod, path);
            } else {
                ctx.fireChannelRead(h2TrailersToH1TrailersServer(h2Headers));
            }
        } else if (httpMethod == null) {
            throw new IllegalArgumentException("a request must have " + METHOD + " and " +
                    PATH + " headers");
        } else {
            StreamingHttpRequest request = newRequest(httpMethod, path, HTTP_2_0,
                    h2HeadersToH1HeadersServer(h2Headers, httpMethod), allocator, headersFactory);
            ctx.fireChannelRead(request);
        }
    } else if (msg instanceof Http2DataFrame) {
        readDataFrame(ctx, msg);
    } else {
        ctx.fireChannelRead(msg);
    }
}