@@ -849,24 +849,29 @@ private State readHeaders(ByteBuf buffer) { |
849 | 849 | } |
850 | 850 | if (HttpUtil.isTransferEncodingChunked(message)) { |
851 | 851 | this.chunked = true; |
| 852 | + // The "chunked must be the last encoding" rule (RFC 9112 6.1) is not specific to |
| 853 | + // HTTP/1.1 -- it applies to any message that carries a Transfer-Encoding header at |
| 854 | + // all. This validation must not be gated on protocolVersion(), since a decoder |
| 855 | + // configured with useRfc9112TransferEncoding(false) (see HttpDecoderConfig) can |
| 856 | + // still reach this point for HTTP/1.0 and other non-1.1 messages, and the ordering |
| 857 | + // requirement applies to those just as much as it does to HTTP/1.1. |
| 858 | + // See https://datatracker.ietf.org/doc/html/rfc9112#name-message-body-length |
| 859 | + Iterator<? extends CharSequence> encodingIt = |
| 860 | + message.headers().valueCharSequenceIterator(HttpHeaderNames.TRANSFER_ENCODING); |
| 861 | + CharSequence v = null; |
| 862 | + while (encodingIt.hasNext()) { |
| 863 | + v = encodingIt.next(); |
| 864 | + } |
| 865 | + final int vLen = v.length(); |
| 866 | + final int chunkedValueLength = HttpHeaderValues.CHUNKED.length(); |
| 867 | + // We only need to validate if we have more then the chunked value length contained as otherwise |
| 868 | + // we know it is only chunked. |
| 869 | + if (vLen > chunkedValueLength && !AsciiString.regionMatches(v, true, vLen - chunkedValueLength, |
| 870 | + HttpHeaderValues.CHUNKED, 0, chunkedValueLength)) { |
| 871 | + throw new IllegalArgumentException( |
| 872 | + "chunked must be the last encoding present in the Transfer-Encoding header"); |
| 873 | + } |
852 | 874 | if (message.protocolVersion() == HttpVersion.HTTP_1_1) { |
853 | | - Iterator<? extends CharSequence> encodingIt = |
854 | | - message.headers().valueCharSequenceIterator(HttpHeaderNames.TRANSFER_ENCODING); |
855 | | - // Validate that chunked is the last encoding. |
856 | | - // See https://datatracker.ietf.org/doc/html/rfc9112#name-message-body-length |
857 | | - CharSequence v = null; |
858 | | - while (encodingIt.hasNext()) { |
859 | | - v = encodingIt.next(); |
860 | | - } |
861 | | - final int vLen = v.length(); |
862 | | - final int chunkedValueLength = HttpHeaderValues.CHUNKED.length(); |
863 | | - // We only need to validate if we have more then the chunked value length contained as otherwise |
864 | | - // we know it is only chunked. |
865 | | - if (vLen > chunkedValueLength && !AsciiString.regionMatches(v, true, vLen - chunkedValueLength, |
866 | | - HttpHeaderValues.CHUNKED, 0, chunkedValueLength)) { |
867 | | - throw new IllegalArgumentException( |
868 | | - "chunked must be the last encoding present in the Transfer-Encoding header"); |
869 | | - } |
870 | 875 | if (!contentLengthFields.isEmpty()) { |
871 | 876 | handleTransferEncodingChunkedWithContentLength(message); |
872 | 877 | } |
@@ -926,8 +931,13 @@ private static boolean isLengthEqual(String lengthValue, long contentLength) { |
926 | 931 | * </li> |
927 | 932 | * </ul> |
928 | 933 | * <p> |
929 | | - * <strong>Note:</strong> This method is only called for {@code HTTP/1.1} requests. Earlier HTTP protocol versions |
930 | | - * do not support the {@code Transfer-Encoding} header, and will reject requests that include it. |
| 934 | + * <strong>Note:</strong> This method is only called for {@code HTTP/1.1} requests. Under the |
| 935 | + * default configuration, earlier HTTP protocol versions carrying a {@code Transfer-Encoding} |
| 936 | + * header are rejected before reaching this point. However, if |
| 937 | + * {@link HttpDecoderConfig#setUseRfc9112TransferEncoding(boolean)} is set to {@code false} |
| 938 | + * (see above), that rejection does not happen — a non-{@code HTTP/1.1} message can then |
| 939 | + * legitimately carry {@code Transfer-Encoding} without being rejected for it, though this |
| 940 | + * method specifically remains {@code HTTP/1.1}-only and is not invoked for such messages. |
931 | 941 | */ |
932 | 942 | @SuppressWarnings("unused") |
933 | 943 | protected void handleTransferEncodingChunkedWithContentLength(HttpMessage message) { |
|