From ff638cac9cb157f8e688c0357f4eb7b169cbd241 Mon Sep 17 00:00:00 2001 From: Aayush Atharva Date: Tue, 10 Nov 2020 12:26:36 +0530 Subject: [PATCH] Add ByteBuf parameter HttpConversionUtil (#10785) Motivation: `HttpConversionUtil#toFullHttpResponse` and `HttpConversionUtil#toFullHttpRequest` has `ByteBufAllocator` which is used for building `FullHttpMessage` and then data can be appended with `FullHttpMessage#content`. However, there can be cases when we already have `ByteBuf` ready with data. So we need a parameter to add `ByteBuf` directly into `FullHttpMessage` while creating it. Modification: Added `ByteBuf` parameter, Result: More functionality for handling `FullHttpMessage` content. --- .../codec/http2/HttpConversionUtil.java | 44 +++++++++++++++++-- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/codec-http2/src/main/java/io/netty/handler/codec/http2/HttpConversionUtil.java b/codec-http2/src/main/java/io/netty/handler/codec/http2/HttpConversionUtil.java index 6ba0713315..45b3f66134 100644 --- a/codec-http2/src/main/java/io/netty/handler/codec/http2/HttpConversionUtil.java +++ b/codec-http2/src/main/java/io/netty/handler/codec/http2/HttpConversionUtil.java @@ -14,6 +14,7 @@ */ package io.netty.handler.codec.http2; +import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufAllocator; import io.netty.handler.codec.UnsupportedValueConverter; import io.netty.handler.codec.http.DefaultFullHttpRequest; @@ -212,12 +213,30 @@ public final class HttpConversionUtil { * @throws Http2Exception see {@link #addHttp2ToHttpHeaders(int, Http2Headers, FullHttpMessage, boolean)} */ public static FullHttpResponse toFullHttpResponse(int streamId, Http2Headers http2Headers, ByteBufAllocator alloc, + boolean validateHttpHeaders) throws Http2Exception { + return toFullHttpResponse(streamId, http2Headers, alloc.buffer(), validateHttpHeaders); + } + + /** + * Create a new object to contain the response data + * + * @param streamId The stream associated with the response + * @param http2Headers The initial set of HTTP/2 headers to create the response with + * @param content {@link ByteBuf} content to put in {@link FullHttpResponse} + * @param validateHttpHeaders + * @return A new response object which represents headers/data + * @throws Http2Exception see {@link #addHttp2ToHttpHeaders(int, Http2Headers, FullHttpMessage, boolean)} + */ + public static FullHttpResponse toFullHttpResponse(int streamId, Http2Headers http2Headers, ByteBuf content, boolean validateHttpHeaders) throws Http2Exception { HttpResponseStatus status = parseStatus(http2Headers.status()); // HTTP/2 does not define a way to carry the version or reason phrase that is included in an // HTTP/1.1 status line. - FullHttpResponse msg = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, alloc.buffer(), + FullHttpResponse msg = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, content, validateHttpHeaders); try { addHttp2ToHttpHeaders(streamId, http2Headers, msg, false); @@ -245,15 +264,32 @@ public final class HttpConversionUtil { * @throws Http2Exception see {@link #addHttp2ToHttpHeaders(int, Http2Headers, FullHttpMessage, boolean)} */ public static FullHttpRequest toFullHttpRequest(int streamId, Http2Headers http2Headers, ByteBufAllocator alloc, - boolean validateHttpHeaders) - throws Http2Exception { + boolean validateHttpHeaders) throws Http2Exception { + return toFullHttpRequest(streamId, http2Headers, alloc.buffer(), validateHttpHeaders); + } + + /** + * Create a new object to contain the request data + * + * @param streamId The stream associated with the request + * @param http2Headers The initial set of HTTP/2 headers to create the request with + * @param content {@link ByteBuf} content to put in {@link FullHttpRequest} + * @param validateHttpHeaders + * @return A new request object which represents headers/data + * @throws Http2Exception see {@link #addHttp2ToHttpHeaders(int, Http2Headers, FullHttpMessage, boolean)} + */ + public static FullHttpRequest toFullHttpRequest(int streamId, Http2Headers http2Headers, ByteBuf content, + boolean validateHttpHeaders) throws Http2Exception { // HTTP/2 does not define a way to carry the version identifier that is included in the HTTP/1.1 request line. final CharSequence method = checkNotNull(http2Headers.method(), "method header cannot be null in conversion to HTTP/1.x"); final CharSequence path = checkNotNull(http2Headers.path(), "path header cannot be null in conversion to HTTP/1.x"); FullHttpRequest msg = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.valueOf(method - .toString()), path.toString(), alloc.buffer(), validateHttpHeaders); + .toString()), path.toString(), content, validateHttpHeaders); try { addHttp2ToHttpHeaders(streamId, http2Headers, msg, false); } catch (Http2Exception e) {