javanettyreactor-nettynetty4

Netty Server HttpContentDecompressor is removing content-encoding header from the request, can we configure it to not do so?


I am using netty io.netty.handler.codec.http.HttpContentDecompressor by adding it into pipeline in my spring boot server, its decompressing the incoming request successfully but also removing the 'content-encoding:gzip' header from request. Is there any way to configure netty server to stop removing this header after decompression as we really want to know if original request had come in the compressed format or decompressed format?


Solution

  • HttpContentDecompressor.decode sources:

    ...
    // set new content encoding,
    CharSequence targetContentEncoding = getTargetContentEncoding(contentEncoding);
    if (HttpHeaderValues.IDENTITY.contentEquals(targetContentEncoding)) {
        // Do NOT set the 'Content-Encoding' header if the target encoding is 'identity'
        // as per: https://tools.ietf.org/html/rfc2616#section-14.11
        headers.remove(HttpHeaderNames.CONTENT_ENCODING);
    } else {
        headers.set(HttpHeaderNames.CONTENT_ENCODING, targetContentEncoding);
    }
    ...
    

    Use the following snippet to prevent Content-Encoding header modification:

    pipeline.addLast(new HttpContentDecompressor() {
        @Override
        protected String getTargetContentEncoding(String contentEncoding) throws Exception {
            return contentEncoding;
        }
    })