Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2012 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.handler.codec.http;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.DecoderResult;
import io.netty.handler.codec.PrematureChannelClosureException;
import io.netty.handler.codec.TooLongFrameException;
import io.netty.util.AsciiString;
import io.netty.util.ByteProcessor;
import io.netty.util.internal.StringUtil;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import static io.netty.util.internal.ObjectUtil.checkNotNull;
/**
* Decodes {@link ByteBuf}s into {@link HttpMessage}s and
* {@link HttpContent}s.
*
*
Parameters that prevents excessive memory consumption
*
*
*
Name
Default value
Meaning
*
*
*
{@code maxInitialLineLength}
*
{@value #DEFAULT_MAX_INITIAL_LINE_LENGTH}
*
The maximum length of the initial line
* (e.g. {@code "GET / HTTP/1.0"} or {@code "HTTP/1.0 200 OK"})
* If the length of the initial line exceeds this value, a
* {@link TooLongHttpLineException} will be raised.
*
*
*
{@code maxHeaderSize}
*
{@value #DEFAULT_MAX_HEADER_SIZE}
*
The maximum length of all headers. If the sum of the length of each
* header exceeds this value, a {@link TooLongHttpHeaderException} will be raised.
*
*
*
{@code maxChunkSize}
*
{@value #DEFAULT_MAX_CHUNK_SIZE}
*
The maximum length of the content or each chunk. If the content length
* (or the length of each chunk) exceeds this value, the content or chunk
* will be split into multiple {@link HttpContent}s whose length is
* {@code maxChunkSize} at maximum.
*
*
*
*
Parameters that control parsing behavior
*
*
*
Name
Default value
Meaning
*
*
*
{@code allowDuplicateContentLengths}
*
{@value #DEFAULT_ALLOW_DUPLICATE_CONTENT_LENGTHS}
*
When set to {@code false}, will reject any messages that contain multiple Content-Length header fields.
* When set to {@code true}, will allow multiple Content-Length headers only if they are all the same decimal value.
* The duplicated field-values will be replaced with a single valid Content-Length field.
* See RFC 7230, Section 3.3.2.
*
*
*
{@code allowPartialChunks}
*
{@value #DEFAULT_ALLOW_PARTIAL_CHUNKS}
*
If the length of a chunk exceeds the {@link ByteBuf}s readable bytes and {@code allowPartialChunks}
* is set to {@code true}, the chunk will be split into multiple {@link HttpContent}s.
* Otherwise, if the chunk size does not exceed {@code maxChunkSize} and {@code allowPartialChunks}
* is set to {@code false}, the {@link ByteBuf} is not decoded into an {@link HttpContent} until
* the readable bytes are greater or equal to the chunk size.
*
*
*
*
Chunked Content
*
* If the content of an HTTP message is greater than {@code maxChunkSize} or
* the transfer encoding of the HTTP message is 'chunked', this decoder
* generates one {@link HttpMessage} instance and its following
* {@link HttpContent}s per single HTTP message to avoid excessive memory
* consumption. For example, the following HTTP message:
*
* triggers {@link HttpRequestDecoder} to generate 3 objects:
*
*
An {@link HttpRequest},
*
The first {@link HttpContent} whose content is {@code 'abcdefghijklmnopqrstuvwxyz'},
*
The second {@link LastHttpContent} whose content is {@code '1234567890abcdef'}, which marks
* the end of the content.
*
*
* If you prefer not to handle {@link HttpContent}s by yourself for your
* convenience, insert {@link HttpObjectAggregator} after this decoder in the
* {@link ChannelPipeline}. However, please note that your server might not
* be as memory efficient as without the aggregator.
*
*
Extensibility
*
* Please note that this decoder is designed to be extended to implement
* a protocol derived from HTTP, such as
* RTSP and
* ICAP.
* To implement the decoder of such a derived protocol, extend this class and
* implement all abstract methods properly.
*
*
Header Validation
*
* It is recommended to always enable header validation.
*
* This recommendation stands even when both peers in the HTTP exchange are trusted,
* as it helps with defence-in-depth.
*/
public abstract class HttpObjectDecoder extends ByteToMessageDecoder {
public static final int DEFAULT_MAX_INITIAL_LINE_LENGTH = 4096;
public static final int DEFAULT_MAX_HEADER_SIZE = 8192;
public static final boolean DEFAULT_CHUNKED_SUPPORTED = true;
public static final boolean DEFAULT_ALLOW_PARTIAL_CHUNKS = true;
public static final int DEFAULT_MAX_CHUNK_SIZE = 8192;
public static final boolean DEFAULT_VALIDATE_HEADERS = true;
public static final int DEFAULT_INITIAL_BUFFER_SIZE = 128;
public static final boolean DEFAULT_ALLOW_DUPLICATE_CONTENT_LENGTHS = false;
private final int maxChunkSize;
private final boolean chunkedSupported;
private final boolean allowPartialChunks;
/**
* This field is no longer used. It is only kept around for backwards compatibility purpose.
*/
@Deprecated
protected final boolean validateHeaders;
protected final HttpHeadersFactory headersFactory;
protected final HttpHeadersFactory trailersFactory;
private final boolean allowDuplicateContentLengths;
private final ByteBuf parserScratchBuffer;
private final HeaderParser headerParser;
private final LineParser lineParser;
private HttpMessage message;
private long chunkSize;
private long contentLength = Long.MIN_VALUE;
private boolean chunked;
private boolean isSwitchingToNonHttp1Protocol;
private final AtomicBoolean resetRequested = new AtomicBoolean();
// These will be updated by splitHeader(...)
private AsciiString name;
private String value;
private LastHttpContent trailer;
@Override
protected void handlerRemoved0(ChannelHandlerContext ctx) throws Exception {
try {
parserScratchBuffer.release();
} finally {
super.handlerRemoved0(ctx);
}
}
/**
* The internal state of {@link HttpObjectDecoder}.
* Internal use only.
*/
private enum State {
SKIP_CONTROL_CHARS,
READ_INITIAL,
READ_HEADER,
READ_VARIABLE_LENGTH_CONTENT,
READ_FIXED_LENGTH_CONTENT,
READ_CHUNK_SIZE,
READ_CHUNKED_CONTENT,
READ_CHUNK_DELIMITER,
READ_CHUNK_FOOTER,
BAD_MESSAGE,
UPGRADED
}
private State currentState = State.SKIP_CONTROL_CHARS;
/**
* Creates a new instance with the default
* {@code maxInitialLineLength (4096)}, {@code maxHeaderSize (8192)}, and
* {@code maxChunkSize (8192)}.
*/
protected HttpObjectDecoder() {
this(new HttpDecoderConfig());
}
/**
* Creates a new instance with the specified parameters.
*
* @deprecated Use {@link #HttpObjectDecoder(HttpDecoderConfig)} instead.
*/
@Deprecated
protected HttpObjectDecoder(
int maxInitialLineLength, int maxHeaderSize, int maxChunkSize, boolean chunkedSupported) {
this(new HttpDecoderConfig()
.setMaxInitialLineLength(maxInitialLineLength)
.setMaxHeaderSize(maxHeaderSize)
.setMaxChunkSize(maxChunkSize)
.setChunkedSupported(chunkedSupported));
}
/**
* Creates a new instance with the specified parameters.
*
* @deprecated Use {@link #HttpObjectDecoder(HttpDecoderConfig)} instead.
*/
@Deprecated
protected HttpObjectDecoder(
int maxInitialLineLength, int maxHeaderSize, int maxChunkSize,
boolean chunkedSupported, boolean validateHeaders) {
this(new HttpDecoderConfig()
.setMaxInitialLineLength(maxInitialLineLength)
.setMaxHeaderSize(maxHeaderSize)
.setMaxChunkSize(maxChunkSize)
.setChunkedSupported(chunkedSupported)
.setValidateHeaders(validateHeaders));
}
/**
* Creates a new instance with the specified parameters.
*
* @deprecated Use {@link #HttpObjectDecoder(HttpDecoderConfig)} instead.
*/
@Deprecated
protected HttpObjectDecoder(
int maxInitialLineLength, int maxHeaderSize, int maxChunkSize,
boolean chunkedSupported, boolean validateHeaders, int initialBufferSize) {
this(new HttpDecoderConfig()
.setMaxInitialLineLength(maxInitialLineLength)
.setMaxHeaderSize(maxHeaderSize)
.setMaxChunkSize(maxChunkSize)
.setChunkedSupported(chunkedSupported)
.setValidateHeaders(validateHeaders)
.setInitialBufferSize(initialBufferSize));
}
/**
* Creates a new instance with the specified parameters.
*
* @deprecated Use {@link #HttpObjectDecoder(HttpDecoderConfig)} instead.
*/
@Deprecated
protected HttpObjectDecoder(
int maxInitialLineLength, int maxHeaderSize, int maxChunkSize,
boolean chunkedSupported, boolean validateHeaders, int initialBufferSize,
boolean allowDuplicateContentLengths) {
this(new HttpDecoderConfig()
.setMaxInitialLineLength(maxInitialLineLength)
.setMaxHeaderSize(maxHeaderSize)
.setMaxChunkSize(maxChunkSize)
.setChunkedSupported(chunkedSupported)
.setValidateHeaders(validateHeaders)
.setInitialBufferSize(initialBufferSize)
.setAllowDuplicateContentLengths(allowDuplicateContentLengths));
}
/**
* Creates a new instance with the specified parameters.
*
* @deprecated Use {@link #HttpObjectDecoder(HttpDecoderConfig)} instead.
*/
@Deprecated
protected HttpObjectDecoder(
int maxInitialLineLength, int maxHeaderSize, int maxChunkSize,
boolean chunkedSupported, boolean validateHeaders, int initialBufferSize,
boolean allowDuplicateContentLengths, boolean allowPartialChunks) {
this(new HttpDecoderConfig()
.setMaxInitialLineLength(maxInitialLineLength)
.setMaxHeaderSize(maxHeaderSize)
.setMaxChunkSize(maxChunkSize)
.setChunkedSupported(chunkedSupported)
.setValidateHeaders(validateHeaders)
.setInitialBufferSize(initialBufferSize)
.setAllowDuplicateContentLengths(allowDuplicateContentLengths)
.setAllowPartialChunks(allowPartialChunks));
}
/**
* Creates a new instance with the specified configuration.
*/
protected HttpObjectDecoder(HttpDecoderConfig config) {
checkNotNull(config, "config");
parserScratchBuffer = Unpooled.buffer(config.getInitialBufferSize());
lineParser = new LineParser(parserScratchBuffer, config.getMaxInitialLineLength());
headerParser = new HeaderParser(parserScratchBuffer, config.getMaxHeaderSize());
maxChunkSize = config.getMaxChunkSize();
chunkedSupported = config.isChunkedSupported();
headersFactory = config.getHeadersFactory();
trailersFactory = config.getTrailersFactory();
validateHeaders = isValidating(headersFactory);
allowDuplicateContentLengths = config.isAllowDuplicateContentLengths();
allowPartialChunks = config.isAllowPartialChunks();
}
protected boolean isValidating(HttpHeadersFactory headersFactory) {
if (headersFactory instanceof DefaultHttpHeadersFactory) {
DefaultHttpHeadersFactory builder = (DefaultHttpHeadersFactory) headersFactory;
return builder.isValidatingHeaderNames() || builder.isValidatingHeaderValues();
}
return true; // We can't actually tell in this case, but we assume some validation is taking place.
}
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List