HTTP/2 to HTTP/1.x headers conversion more accessible
Motivation: Currently there is a HttpConversionUtil.addHttp2ToHttpHeaders which requires a FullHttpMessage, but this may not always be available. There is no interface that can be used with just Http2Headers and HttpHeaders. Modifications: - add an overload for HttpConversionUtil.addHttp2ToHttpHeaders which does not take FullHttpMessage Result: An overload for HttpConversionUtil.addHttp2ToHttpHeaders exists which does not require FullHttpMessage.
This commit is contained in:
parent
076d4ed514
commit
59600f1812
@ -95,10 +95,33 @@ public final class HttpUtil {
|
||||
* <li>remove otherwise.</li>
|
||||
* </ul></li>
|
||||
* </ul>
|
||||
* @see #setKeepAlive(HttpHeaders, HttpVersion, boolean)
|
||||
*/
|
||||
public static void setKeepAlive(HttpMessage message, boolean keepAlive) {
|
||||
HttpHeaders h = message.headers();
|
||||
if (message.protocolVersion().isKeepAliveDefault()) {
|
||||
setKeepAlive(message.headers(), message.protocolVersion(), keepAlive);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value of the {@code "Connection"} header depending on the
|
||||
* protocol version of the specified message. This getMethod sets or removes
|
||||
* the {@code "Connection"} header depending on what the default keep alive
|
||||
* mode of the message's protocol version is, as specified by
|
||||
* {@link HttpVersion#isKeepAliveDefault()}.
|
||||
* <ul>
|
||||
* <li>If the connection is kept alive by default:
|
||||
* <ul>
|
||||
* <li>set to {@code "close"} if {@code keepAlive} is {@code false}.</li>
|
||||
* <li>remove otherwise.</li>
|
||||
* </ul></li>
|
||||
* <li>If the connection is closed by default:
|
||||
* <ul>
|
||||
* <li>set to {@code "keep-alive"} if {@code keepAlive} is {@code true}.</li>
|
||||
* <li>remove otherwise.</li>
|
||||
* </ul></li>
|
||||
* </ul>
|
||||
*/
|
||||
public static void setKeepAlive(HttpHeaders h, HttpVersion httpVersion, boolean keepAlive) {
|
||||
if (httpVersion.isKeepAliveDefault()) {
|
||||
if (keepAlive) {
|
||||
h.remove(HttpHeaderNames.CONNECTION);
|
||||
} else {
|
||||
|
@ -239,21 +239,40 @@ public final class HttpConversionUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* Translate and add HTTP/2 headers to HTTP/1.x headers
|
||||
* Translate and add HTTP/2 headers to HTTP/1.x headers.
|
||||
*
|
||||
* @param streamId The stream associated with {@code sourceHeaders}
|
||||
* @param sourceHeaders The HTTP/2 headers to convert
|
||||
* @param destinationMessage The object which will contain the resulting HTTP/1.x headers
|
||||
* @param streamId The stream associated with {@code sourceHeaders}.
|
||||
* @param sourceHeaders The HTTP/2 headers to convert.
|
||||
* @param destinationMessage The object which will contain the resulting HTTP/1.x headers.
|
||||
* @param addToTrailer {@code true} to add to trailing headers. {@code false} to add to initial headers.
|
||||
* @throws Http2Exception If not all HTTP/2 headers can be translated to HTTP/1.x
|
||||
* @throws Http2Exception If not all HTTP/2 headers can be translated to HTTP/1.x.
|
||||
* @see #addHttp2ToHttpHeaders(int, Http2Headers, HttpHeaders, HttpVersion, boolean, boolean)
|
||||
*/
|
||||
public static void addHttp2ToHttpHeaders(int streamId, Http2Headers sourceHeaders,
|
||||
FullHttpMessage destinationMessage, boolean addToTrailer) throws Http2Exception {
|
||||
HttpHeaders headers = addToTrailer ? destinationMessage.trailingHeaders() : destinationMessage.headers();
|
||||
boolean request = destinationMessage instanceof HttpRequest;
|
||||
Http2ToHttpHeaderTranslator translator = new Http2ToHttpHeaderTranslator(streamId, headers, request);
|
||||
addHttp2ToHttpHeaders(streamId, sourceHeaders,
|
||||
addToTrailer ? destinationMessage.trailingHeaders() : destinationMessage.headers(),
|
||||
destinationMessage.protocolVersion(), addToTrailer, destinationMessage instanceof HttpRequest);
|
||||
}
|
||||
|
||||
/**
|
||||
* Translate and add HTTP/2 headers to HTTP/1.x headers.
|
||||
*
|
||||
* @param streamId The stream associated with {@code sourceHeaders}.
|
||||
* @param inputHeaders The HTTP/2 headers to convert.
|
||||
* @param outputHeaders The object which will contain the resulting HTTP/1.x headers..
|
||||
* @param httpVersion What HTTP/1.x version {@code outputHeaders} should be treated as when doing the conversion.
|
||||
* @param isTrailer {@code true} if {@code outputHeaders} should be treated as trailing headers.
|
||||
* {@code false} otherwise.
|
||||
* @param isReqeust {@code true} if the {@code outputHeaders} will be used in a request message.
|
||||
* {@code false} for response message.
|
||||
* @throws Http2Exception If not all HTTP/2 headers can be translated to HTTP/1.x.
|
||||
*/
|
||||
public static void addHttp2ToHttpHeaders(int streamId, Http2Headers inputHeaders, HttpHeaders outputHeaders,
|
||||
HttpVersion httpVersion, boolean isTrailer, boolean isRequest) throws Http2Exception {
|
||||
Http2ToHttpHeaderTranslator translator = new Http2ToHttpHeaderTranslator(streamId, outputHeaders, isRequest);
|
||||
try {
|
||||
for (Entry<ByteString, ByteString> entry : sourceHeaders) {
|
||||
for (Entry<ByteString, ByteString> entry : inputHeaders) {
|
||||
translator.translate(entry);
|
||||
}
|
||||
} catch (Http2Exception ex) {
|
||||
@ -262,11 +281,11 @@ public final class HttpConversionUtil {
|
||||
throw streamError(streamId, PROTOCOL_ERROR, t, "HTTP/2 to HTTP/1.x headers conversion error");
|
||||
}
|
||||
|
||||
headers.remove(HttpHeaderNames.TRANSFER_ENCODING);
|
||||
headers.remove(HttpHeaderNames.TRAILER);
|
||||
if (!addToTrailer) {
|
||||
headers.setInt(ExtensionHeaderNames.STREAM_ID.text(), streamId);
|
||||
HttpUtil.setKeepAlive(destinationMessage, true);
|
||||
outputHeaders.remove(HttpHeaderNames.TRANSFER_ENCODING);
|
||||
outputHeaders.remove(HttpHeaderNames.TRAILER);
|
||||
if (!isTrailer) {
|
||||
outputHeaders.setInt(ExtensionHeaderNames.STREAM_ID.text(), streamId);
|
||||
HttpUtil.setKeepAlive(outputHeaders, httpVersion, true);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user