2008-11-19 08:22:15 +01:00
|
|
|
/*
|
2012-06-04 22:31:44 +02:00
|
|
|
* Copyright 2012 The Netty Project
|
2009-06-19 19:48:17 +02:00
|
|
|
*
|
2011-12-09 06:18:34 +01:00
|
|
|
* 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:
|
2008-11-19 08:22:15 +01:00
|
|
|
*
|
2012-06-04 22:31:44 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2008-11-19 08:22:15 +01:00
|
|
|
*
|
2009-08-28 09:15:49 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
2011-12-09 06:18:34 +01:00
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
2009-08-28 09:15:49 +02:00
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2008-11-19 08:22:15 +01:00
|
|
|
*/
|
2011-12-09 04:38:59 +01:00
|
|
|
package io.netty.handler.codec.http;
|
2008-11-19 08:22:15 +01:00
|
|
|
|
2012-06-10 04:08:43 +02:00
|
|
|
import io.netty.buffer.ByteBuf;
|
2012-06-11 10:02:00 +02:00
|
|
|
import io.netty.buffer.Unpooled;
|
2012-06-07 07:52:33 +02:00
|
|
|
import io.netty.channel.ChannelHandlerContext;
|
2011-12-09 04:38:59 +01:00
|
|
|
import io.netty.channel.ChannelPipeline;
|
2014-11-04 17:02:51 +01:00
|
|
|
import io.netty.handler.codec.ByteToMessageDecoder;
|
2012-09-28 08:19:08 +02:00
|
|
|
import io.netty.handler.codec.DecoderResult;
|
2016-06-07 10:27:53 +02:00
|
|
|
import io.netty.handler.codec.PrematureChannelClosureException;
|
2012-05-16 16:02:06 +02:00
|
|
|
import io.netty.handler.codec.TooLongFrameException;
|
2015-04-01 01:23:52 +02:00
|
|
|
import io.netty.util.ByteProcessor;
|
2013-12-03 12:04:07 +01:00
|
|
|
import io.netty.util.internal.AppendableCharSequence;
|
2008-11-19 08:22:15 +01:00
|
|
|
|
2012-05-20 07:19:11 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
2008-11-19 08:22:15 +01:00
|
|
|
/**
|
2013-01-16 05:22:50 +01:00
|
|
|
* Decodes {@link ByteBuf}s into {@link HttpMessage}s and
|
2013-01-14 16:52:30 +01:00
|
|
|
* {@link HttpContent}s.
|
2009-06-19 17:35:19 +02:00
|
|
|
*
|
|
|
|
* <h3>Parameters that prevents excessive memory consumption</h3>
|
2009-06-19 18:45:30 +02:00
|
|
|
* <table border="1">
|
2009-06-19 17:35:19 +02:00
|
|
|
* <tr>
|
|
|
|
* <th>Name</th><th>Meaning</th>
|
|
|
|
* </tr>
|
|
|
|
* <tr>
|
|
|
|
* <td>{@code maxInitialLineLength}</td>
|
|
|
|
* <td>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 TooLongFrameException} will be raised.</td>
|
|
|
|
* </tr>
|
|
|
|
* <tr>
|
|
|
|
* <td>{@code maxHeaderSize}</td>
|
|
|
|
* <td>The maximum length of all headers. If the sum of the length of each
|
|
|
|
* header exceeds this value, a {@link TooLongFrameException} will be raised.</td>
|
|
|
|
* </tr>
|
|
|
|
* <tr>
|
|
|
|
* <td>{@code maxChunkSize}</td>
|
|
|
|
* <td>The maximum length of the content or each chunk. If the content length
|
2009-11-17 07:02:42 +01:00
|
|
|
* (or the length of each chunk) exceeds this value, the content or chunk
|
2013-01-14 16:52:30 +01:00
|
|
|
* will be split into multiple {@link HttpContent}s whose length is
|
2009-11-17 07:02:42 +01:00
|
|
|
* {@code maxChunkSize} at maximum.</td>
|
2009-06-19 17:35:19 +02:00
|
|
|
* </tr>
|
|
|
|
* </table>
|
|
|
|
*
|
2009-11-17 07:02:42 +01:00
|
|
|
* <h3>Chunked Content</h3>
|
|
|
|
*
|
|
|
|
* If the content of an HTTP message is greater than {@code maxChunkSize} or
|
|
|
|
* the transfer encoding of the HTTP message is 'chunked', this decoder
|
2013-01-16 05:22:50 +01:00
|
|
|
* generates one {@link HttpMessage} instance and its following
|
2013-01-14 16:52:30 +01:00
|
|
|
* {@link HttpContent}s per single HTTP message to avoid excessive memory
|
2009-11-17 07:02:42 +01:00
|
|
|
* consumption. For example, the following HTTP message:
|
|
|
|
* <pre>
|
|
|
|
* GET / HTTP/1.1
|
|
|
|
* Transfer-Encoding: chunked
|
|
|
|
*
|
|
|
|
* 1a
|
|
|
|
* abcdefghijklmnopqrstuvwxyz
|
|
|
|
* 10
|
|
|
|
* 1234567890abcdef
|
|
|
|
* 0
|
2009-11-17 08:19:28 +01:00
|
|
|
* Content-MD5: ...
|
|
|
|
* <i>[blank line]</i>
|
2010-01-26 10:43:20 +01:00
|
|
|
* </pre>
|
2013-01-14 16:52:30 +01:00
|
|
|
* triggers {@link HttpRequestDecoder} to generate 3 objects:
|
2009-11-17 07:02:42 +01:00
|
|
|
* <ol>
|
2013-01-16 05:22:50 +01:00
|
|
|
* <li>An {@link HttpRequest},</li>
|
2013-01-14 16:52:30 +01:00
|
|
|
* <li>The first {@link HttpContent} whose content is {@code 'abcdefghijklmnopqrstuvwxyz'},</li>
|
|
|
|
* <li>The second {@link LastHttpContent} whose content is {@code '1234567890abcdef'}, which marks
|
|
|
|
* the end of the content.</li>
|
2009-11-17 07:02:42 +01:00
|
|
|
* </ol>
|
|
|
|
*
|
2013-01-14 16:52:30 +01:00
|
|
|
* If you prefer not to handle {@link HttpContent}s by yourself for your
|
|
|
|
* convenience, insert {@link HttpObjectAggregator} after this decoder in the
|
2009-11-17 07:02:42 +01:00
|
|
|
* {@link ChannelPipeline}. However, please note that your server might not
|
|
|
|
* be as memory efficient as without the aggregator.
|
|
|
|
*
|
2009-06-19 17:35:19 +02:00
|
|
|
* <h3>Extensibility</h3>
|
|
|
|
*
|
|
|
|
* Please note that this decoder is designed to be extended to implement
|
|
|
|
* a protocol derived from HTTP, such as
|
|
|
|
* <a href="http://en.wikipedia.org/wiki/Real_Time_Streaming_Protocol">RTSP</a> and
|
|
|
|
* <a href="http://en.wikipedia.org/wiki/Internet_Content_Adaptation_Protocol">ICAP</a>.
|
|
|
|
* To implement the decoder of such a derived protocol, extend this class and
|
|
|
|
* implement all abstract methods properly.
|
2008-11-19 08:22:15 +01:00
|
|
|
*/
|
2014-11-04 17:02:51 +01:00
|
|
|
public abstract class HttpObjectDecoder extends ByteToMessageDecoder {
|
2014-08-27 08:29:25 +02:00
|
|
|
private static final String EMPTY_VALUE = "";
|
2008-11-19 08:22:15 +01:00
|
|
|
|
2009-03-09 09:50:24 +01:00
|
|
|
private final int maxChunkSize;
|
2013-01-20 10:40:54 +01:00
|
|
|
private final boolean chunkedSupported;
|
2013-11-13 15:05:12 +01:00
|
|
|
protected final boolean validateHeaders;
|
2014-08-27 08:29:25 +02:00
|
|
|
private final HeaderParser headerParser;
|
|
|
|
private final LineParser lineParser;
|
2013-07-30 15:46:53 +02:00
|
|
|
|
2013-01-16 05:22:50 +01:00
|
|
|
private HttpMessage message;
|
2010-02-19 10:00:00 +01:00
|
|
|
private long chunkSize;
|
2013-12-18 13:55:15 +01:00
|
|
|
private long contentLength = Long.MIN_VALUE;
|
2014-02-20 22:41:54 +01:00
|
|
|
private volatile boolean resetRequested;
|
2009-02-12 07:09:29 +01:00
|
|
|
|
2014-08-27 08:29:25 +02:00
|
|
|
// These will be updated by splitHeader(...)
|
|
|
|
private CharSequence name;
|
|
|
|
private CharSequence value;
|
|
|
|
|
2014-11-04 17:02:51 +01:00
|
|
|
private LastHttpContent trailer;
|
|
|
|
|
2008-12-03 10:00:29 +01:00
|
|
|
/**
|
2013-01-14 16:52:30 +01:00
|
|
|
* The internal state of {@link HttpObjectDecoder}.
|
2009-06-19 17:35:19 +02:00
|
|
|
* <em>Internal use only</em>.
|
2008-12-03 10:00:29 +01:00
|
|
|
*/
|
2014-11-04 17:02:51 +01:00
|
|
|
private enum State {
|
2009-02-12 06:02:22 +01:00
|
|
|
SKIP_CONTROL_CHARS,
|
2008-11-19 08:22:15 +01:00
|
|
|
READ_INITIAL,
|
|
|
|
READ_HEADER,
|
2009-02-12 08:17:29 +01:00
|
|
|
READ_VARIABLE_LENGTH_CONTENT,
|
2008-11-19 08:22:15 +01:00
|
|
|
READ_FIXED_LENGTH_CONTENT,
|
|
|
|
READ_CHUNK_SIZE,
|
|
|
|
READ_CHUNKED_CONTENT,
|
2009-02-12 06:41:22 +01:00
|
|
|
READ_CHUNK_DELIMITER,
|
2012-09-28 08:16:29 +02:00
|
|
|
READ_CHUNK_FOOTER,
|
2013-12-18 12:29:13 +01:00
|
|
|
BAD_MESSAGE,
|
|
|
|
UPGRADED
|
2008-11-19 08:22:15 +01:00
|
|
|
}
|
|
|
|
|
2014-11-04 17:02:51 +01:00
|
|
|
private State currentState = State.SKIP_CONTROL_CHARS;
|
|
|
|
|
2009-06-19 17:35:19 +02:00
|
|
|
/**
|
|
|
|
* Creates a new instance with the default
|
2009-09-07 05:27:26 +02:00
|
|
|
* {@code maxInitialLineLength (4096}}, {@code maxHeaderSize (8192)}, and
|
|
|
|
* {@code maxChunkSize (8192)}.
|
2009-06-19 17:35:19 +02:00
|
|
|
*/
|
2013-01-14 16:52:30 +01:00
|
|
|
protected HttpObjectDecoder() {
|
2013-01-20 10:40:54 +01:00
|
|
|
this(4096, 8192, 8192, true);
|
2009-03-10 09:57:02 +01:00
|
|
|
}
|
|
|
|
|
2009-06-19 17:35:19 +02:00
|
|
|
/**
|
|
|
|
* Creates a new instance with the specified parameters.
|
|
|
|
*/
|
2013-01-14 16:52:30 +01:00
|
|
|
protected HttpObjectDecoder(
|
2013-01-20 10:40:54 +01:00
|
|
|
int maxInitialLineLength, int maxHeaderSize, int maxChunkSize, boolean chunkedSupported) {
|
2013-11-13 15:05:12 +01:00
|
|
|
this(maxInitialLineLength, maxHeaderSize, maxChunkSize, chunkedSupported, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new instance with the specified parameters.
|
|
|
|
*/
|
|
|
|
protected HttpObjectDecoder(
|
|
|
|
int maxInitialLineLength, int maxHeaderSize, int maxChunkSize,
|
|
|
|
boolean chunkedSupported, boolean validateHeaders) {
|
2016-02-05 23:33:57 +01:00
|
|
|
this(maxInitialLineLength, maxHeaderSize, maxChunkSize, chunkedSupported, validateHeaders, 128);
|
|
|
|
}
|
2009-03-10 09:57:02 +01:00
|
|
|
|
2016-02-05 23:33:57 +01:00
|
|
|
protected HttpObjectDecoder(
|
|
|
|
int maxInitialLineLength, int maxHeaderSize, int maxChunkSize,
|
|
|
|
boolean chunkedSupported, boolean validateHeaders, int initialBufferSize) {
|
2009-03-10 08:20:27 +01:00
|
|
|
if (maxInitialLineLength <= 0) {
|
|
|
|
throw new IllegalArgumentException(
|
|
|
|
"maxInitialLineLength must be a positive integer: " +
|
2013-11-13 15:05:12 +01:00
|
|
|
maxInitialLineLength);
|
2009-03-10 08:20:27 +01:00
|
|
|
}
|
|
|
|
if (maxHeaderSize <= 0) {
|
|
|
|
throw new IllegalArgumentException(
|
|
|
|
"maxHeaderSize must be a positive integer: " +
|
2010-08-26 06:18:53 +02:00
|
|
|
maxHeaderSize);
|
2009-03-10 08:20:27 +01:00
|
|
|
}
|
2013-12-16 16:17:35 +01:00
|
|
|
if (maxChunkSize <= 0) {
|
2009-03-09 09:50:24 +01:00
|
|
|
throw new IllegalArgumentException(
|
2009-03-10 08:53:15 +01:00
|
|
|
"maxChunkSize must be a positive integer: " +
|
2009-03-09 09:50:24 +01:00
|
|
|
maxChunkSize);
|
|
|
|
}
|
2016-02-05 23:33:57 +01:00
|
|
|
AppendableCharSequence seq = new AppendableCharSequence(initialBufferSize);
|
|
|
|
lineParser = new LineParser(seq, maxInitialLineLength);
|
|
|
|
headerParser = new HeaderParser(seq, maxHeaderSize);
|
2009-03-09 09:50:24 +01:00
|
|
|
this.maxChunkSize = maxChunkSize;
|
2013-01-20 10:40:54 +01:00
|
|
|
this.chunkedSupported = chunkedSupported;
|
2013-11-13 15:05:12 +01:00
|
|
|
this.validateHeaders = validateHeaders;
|
2009-03-09 09:50:24 +01:00
|
|
|
}
|
|
|
|
|
2008-11-19 08:22:15 +01:00
|
|
|
@Override
|
Remove MessageList from public API and change ChannelInbound/OutboundHandler accordingly
I must admit MesageList was pain in the ass. Instead of forcing a
handler always loop over the list of messages, this commit splits
messageReceived(ctx, list) into two event handlers:
- messageReceived(ctx, msg)
- mmessageReceivedLast(ctx)
When Netty reads one or more messages, messageReceived(ctx, msg) event
is triggered for each message. Once the current read operation is
finished, messageReceivedLast() is triggered to tell the handler that
the last messageReceived() was the last message in the current batch.
Similarly, for outbound, write(ctx, list) has been split into two:
- write(ctx, msg)
- flush(ctx, promise)
Instead of writing a list of message with a promise, a user is now
supposed to call write(msg) multiple times and then call flush() to
actually flush the buffered messages.
Please note that write() doesn't have a promise with it. You must call
flush() to get notified on completion. (or you can use writeAndFlush())
Other changes:
- Because MessageList is completely hidden, codec framework uses
List<Object> instead of MessageList as an output parameter.
2013-07-08 12:03:40 +02:00
|
|
|
protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception {
|
2014-02-20 22:41:54 +01:00
|
|
|
if (resetRequested) {
|
|
|
|
resetNow();
|
|
|
|
}
|
|
|
|
|
2014-11-04 17:02:51 +01:00
|
|
|
switch (currentState) {
|
2009-02-12 06:02:22 +01:00
|
|
|
case SKIP_CONTROL_CHARS: {
|
2014-11-04 17:02:51 +01:00
|
|
|
if (!skipControlCharacters(buffer)) {
|
|
|
|
return;
|
2009-02-12 06:02:22 +01:00
|
|
|
}
|
2014-11-04 17:02:51 +01:00
|
|
|
currentState = State.READ_INITIAL;
|
2009-02-12 06:02:22 +01:00
|
|
|
}
|
2012-09-28 08:16:29 +02:00
|
|
|
case READ_INITIAL: try {
|
2014-11-04 17:02:51 +01:00
|
|
|
AppendableCharSequence line = lineParser.parse(buffer);
|
|
|
|
if (line == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
String[] initialLine = splitInitialLine(line);
|
2009-03-10 08:20:27 +01:00
|
|
|
if (initialLine.length < 3) {
|
|
|
|
// Invalid initial line - ignore.
|
2014-11-04 17:02:51 +01:00
|
|
|
currentState = State.SKIP_CONTROL_CHARS;
|
2013-04-03 11:32:33 +02:00
|
|
|
return;
|
2009-03-10 08:20:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
message = createMessage(initialLine);
|
2014-11-04 17:02:51 +01:00
|
|
|
currentState = State.READ_HEADER;
|
2014-08-27 08:29:25 +02:00
|
|
|
// fall-through
|
2012-09-28 08:16:29 +02:00
|
|
|
} catch (Exception e) {
|
2015-03-03 10:56:32 +01:00
|
|
|
out.add(invalidMessage(buffer, e));
|
2013-04-03 11:32:33 +02:00
|
|
|
return;
|
2008-11-19 08:22:15 +01:00
|
|
|
}
|
2012-09-28 08:16:29 +02:00
|
|
|
case READ_HEADER: try {
|
2009-03-04 14:50:19 +01:00
|
|
|
State nextState = readHeaders(buffer);
|
2014-11-04 17:02:51 +01:00
|
|
|
if (nextState == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
currentState = nextState;
|
2014-08-27 08:29:25 +02:00
|
|
|
switch (nextState) {
|
2014-10-25 09:42:36 +02:00
|
|
|
case SKIP_CONTROL_CHARS:
|
|
|
|
// fast-path
|
|
|
|
// No content is expected.
|
|
|
|
out.add(message);
|
|
|
|
out.add(LastHttpContent.EMPTY_LAST_CONTENT);
|
|
|
|
resetNow();
|
|
|
|
return;
|
|
|
|
case READ_CHUNK_SIZE:
|
|
|
|
if (!chunkedSupported) {
|
|
|
|
throw new IllegalArgumentException("Chunked messages not supported");
|
|
|
|
}
|
|
|
|
// Chunked encoding - generate HttpMessage first. HttpChunks will follow.
|
|
|
|
out.add(message);
|
|
|
|
return;
|
|
|
|
default:
|
2015-07-21 21:41:03 +02:00
|
|
|
/**
|
|
|
|
* <a href="https://tools.ietf.org/html/rfc7230#section-3.3.3">RFC 7230, 3.3.3</a> states that if a
|
|
|
|
* request does not have either a transfer-encoding or a content-length header then the message body
|
|
|
|
* length is 0. However for a response the body length is the number of octets received prior to the
|
|
|
|
* server closing the connection. So we treat this as variable length chunked encoding.
|
|
|
|
*/
|
2014-10-25 09:42:36 +02:00
|
|
|
long contentLength = contentLength();
|
|
|
|
if (contentLength == 0 || contentLength == -1 && isDecodingRequest()) {
|
2014-08-27 08:29:25 +02:00
|
|
|
out.add(message);
|
|
|
|
out.add(LastHttpContent.EMPTY_LAST_CONTENT);
|
|
|
|
resetNow();
|
|
|
|
return;
|
2014-10-25 09:42:36 +02:00
|
|
|
}
|
2009-03-09 09:50:24 +01:00
|
|
|
|
2014-10-25 09:42:36 +02:00
|
|
|
assert nextState == State.READ_FIXED_LENGTH_CONTENT ||
|
|
|
|
nextState == State.READ_VARIABLE_LENGTH_CONTENT;
|
2013-12-16 16:17:35 +01:00
|
|
|
|
2014-10-25 09:42:36 +02:00
|
|
|
out.add(message);
|
2013-12-16 16:17:35 +01:00
|
|
|
|
2014-10-25 09:42:36 +02:00
|
|
|
if (nextState == State.READ_FIXED_LENGTH_CONTENT) {
|
|
|
|
// chunkSize will be decreased as the READ_FIXED_LENGTH_CONTENT state reads data chunk by chunk.
|
|
|
|
chunkSize = contentLength;
|
|
|
|
}
|
2013-12-16 16:17:35 +01:00
|
|
|
|
2014-10-25 09:42:36 +02:00
|
|
|
// We return here, this forces decode to be called again where we will decode the content
|
|
|
|
return;
|
2014-08-27 08:29:25 +02:00
|
|
|
}
|
2012-09-28 08:16:29 +02:00
|
|
|
} catch (Exception e) {
|
2015-03-03 10:56:32 +01:00
|
|
|
out.add(invalidMessage(buffer, e));
|
2013-04-03 11:32:33 +02:00
|
|
|
return;
|
2008-11-19 08:22:15 +01:00
|
|
|
}
|
2009-02-12 08:17:29 +01:00
|
|
|
case READ_VARIABLE_LENGTH_CONTENT: {
|
2009-03-09 09:50:24 +01:00
|
|
|
// Keep reading data as a chunk until the end of connection is reached.
|
2014-11-04 17:02:51 +01:00
|
|
|
int toRead = Math.min(buffer.readableBytes(), maxChunkSize);
|
2013-12-16 16:17:35 +01:00
|
|
|
if (toRead > 0) {
|
2016-04-14 10:31:48 +02:00
|
|
|
ByteBuf content = buffer.readRetainedSlice(toRead);
|
2014-08-27 08:29:25 +02:00
|
|
|
out.add(new DefaultHttpContent(content));
|
2009-03-09 09:50:24 +01:00
|
|
|
}
|
2013-04-03 11:32:33 +02:00
|
|
|
return;
|
2009-03-09 09:50:24 +01:00
|
|
|
}
|
2008-11-19 08:22:15 +01:00
|
|
|
case READ_FIXED_LENGTH_CONTENT: {
|
2014-11-04 17:02:51 +01:00
|
|
|
int readLimit = buffer.readableBytes();
|
2012-07-04 15:27:10 +02:00
|
|
|
|
2012-07-04 15:14:05 +02:00
|
|
|
// Check if the buffer is readable first as we use the readable byte count
|
|
|
|
// to create the HttpChunk. This is needed as otherwise we may end up with
|
|
|
|
// create a HttpChunk instance that contains an empty buffer and so is
|
|
|
|
// handled like it is the last HttpChunk.
|
|
|
|
//
|
|
|
|
// See https://github.com/netty/netty/issues/433
|
|
|
|
if (readLimit == 0) {
|
2013-04-03 11:32:33 +02:00
|
|
|
return;
|
2012-07-04 15:14:05 +02:00
|
|
|
}
|
|
|
|
|
2013-12-16 16:17:35 +01:00
|
|
|
int toRead = Math.min(readLimit, maxChunkSize);
|
2012-12-24 21:52:04 +01:00
|
|
|
if (toRead > chunkSize) {
|
|
|
|
toRead = (int) chunkSize;
|
2012-06-05 18:56:01 +02:00
|
|
|
}
|
2016-04-14 10:31:48 +02:00
|
|
|
ByteBuf content = buffer.readRetainedSlice(toRead);
|
2013-12-16 16:17:35 +01:00
|
|
|
chunkSize -= toRead;
|
2009-03-09 09:50:24 +01:00
|
|
|
|
|
|
|
if (chunkSize == 0) {
|
|
|
|
// Read all content.
|
2013-11-13 15:05:12 +01:00
|
|
|
out.add(new DefaultLastHttpContent(content, validateHeaders));
|
2014-02-20 22:41:54 +01:00
|
|
|
resetNow();
|
2013-12-16 16:17:35 +01:00
|
|
|
} else {
|
|
|
|
out.add(new DefaultHttpContent(content));
|
2009-03-09 09:50:24 +01:00
|
|
|
}
|
2013-04-03 11:32:33 +02:00
|
|
|
return;
|
2009-03-09 09:50:24 +01:00
|
|
|
}
|
2008-11-19 08:22:15 +01:00
|
|
|
/**
|
|
|
|
* everything else after this point takes care of reading chunked content. basically, read chunk size,
|
|
|
|
* read chunk, read and ignore the CRLF and repeat until 0
|
|
|
|
*/
|
2012-09-28 08:16:29 +02:00
|
|
|
case READ_CHUNK_SIZE: try {
|
2014-01-14 16:08:29 +01:00
|
|
|
AppendableCharSequence line = lineParser.parse(buffer);
|
2014-11-04 17:02:51 +01:00
|
|
|
if (line == null) {
|
|
|
|
return;
|
|
|
|
}
|
2013-07-30 15:46:53 +02:00
|
|
|
int chunkSize = getChunkSize(line.toString());
|
2009-03-09 09:50:24 +01:00
|
|
|
this.chunkSize = chunkSize;
|
2008-11-19 08:22:15 +01:00
|
|
|
if (chunkSize == 0) {
|
2014-11-04 17:02:51 +01:00
|
|
|
currentState = State.READ_CHUNK_FOOTER;
|
2013-04-03 11:32:33 +02:00
|
|
|
return;
|
2008-11-19 08:22:15 +01:00
|
|
|
}
|
2014-11-04 17:02:51 +01:00
|
|
|
currentState = State.READ_CHUNKED_CONTENT;
|
2014-08-27 08:29:25 +02:00
|
|
|
// fall-through
|
2012-09-28 08:16:29 +02:00
|
|
|
} catch (Exception e) {
|
2015-03-03 10:56:32 +01:00
|
|
|
out.add(invalidChunk(buffer, e));
|
2013-04-03 11:32:33 +02:00
|
|
|
return;
|
2008-11-19 08:22:15 +01:00
|
|
|
}
|
|
|
|
case READ_CHUNKED_CONTENT: {
|
2009-05-08 14:45:10 +02:00
|
|
|
assert chunkSize <= Integer.MAX_VALUE;
|
2013-12-16 16:17:35 +01:00
|
|
|
int toRead = Math.min((int) chunkSize, maxChunkSize);
|
2014-11-04 17:02:51 +01:00
|
|
|
toRead = Math.min(toRead, buffer.readableBytes());
|
2014-08-27 08:29:25 +02:00
|
|
|
if (toRead == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2016-04-14 10:31:48 +02:00
|
|
|
HttpContent chunk = new DefaultHttpContent(buffer.readRetainedSlice(toRead));
|
2013-12-16 16:17:35 +01:00
|
|
|
chunkSize -= toRead;
|
|
|
|
|
|
|
|
out.add(chunk);
|
2009-03-09 09:50:24 +01:00
|
|
|
|
2014-08-27 08:29:25 +02:00
|
|
|
if (chunkSize != 0) {
|
2013-12-16 16:17:35 +01:00
|
|
|
return;
|
2009-03-09 09:50:24 +01:00
|
|
|
}
|
2014-11-04 17:02:51 +01:00
|
|
|
currentState = State.READ_CHUNK_DELIMITER;
|
2014-08-27 08:29:25 +02:00
|
|
|
// fall-through
|
2008-11-19 08:22:15 +01:00
|
|
|
}
|
2009-02-12 06:41:22 +01:00
|
|
|
case READ_CHUNK_DELIMITER: {
|
2014-11-04 17:02:51 +01:00
|
|
|
final int wIdx = buffer.writerIndex();
|
|
|
|
int rIdx = buffer.readerIndex();
|
|
|
|
while (wIdx > rIdx) {
|
|
|
|
byte next = buffer.getByte(rIdx++);
|
|
|
|
if (next == HttpConstants.LF) {
|
|
|
|
currentState = State.READ_CHUNK_SIZE;
|
|
|
|
break;
|
2009-02-12 06:48:25 +01:00
|
|
|
}
|
2008-11-19 08:22:15 +01:00
|
|
|
}
|
2014-11-04 17:02:51 +01:00
|
|
|
buffer.readerIndex(rIdx);
|
|
|
|
return;
|
2008-11-19 08:22:15 +01:00
|
|
|
}
|
2012-09-28 08:16:29 +02:00
|
|
|
case READ_CHUNK_FOOTER: try {
|
2013-01-14 16:52:30 +01:00
|
|
|
LastHttpContent trailer = readTrailingHeaders(buffer);
|
2014-11-04 17:02:51 +01:00
|
|
|
if (trailer == null) {
|
|
|
|
return;
|
|
|
|
}
|
2013-12-16 16:17:35 +01:00
|
|
|
out.add(trailer);
|
2014-02-20 22:41:54 +01:00
|
|
|
resetNow();
|
2013-12-16 16:17:35 +01:00
|
|
|
return;
|
2012-09-28 08:16:29 +02:00
|
|
|
} catch (Exception e) {
|
2015-03-03 10:56:32 +01:00
|
|
|
out.add(invalidChunk(buffer, e));
|
2013-04-03 11:32:33 +02:00
|
|
|
return;
|
2012-09-28 08:16:29 +02:00
|
|
|
}
|
|
|
|
case BAD_MESSAGE: {
|
|
|
|
// Keep discarding until disconnection.
|
2014-11-04 17:02:51 +01:00
|
|
|
buffer.skipBytes(buffer.readableBytes());
|
2013-12-18 12:29:13 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case UPGRADED: {
|
2014-11-04 17:02:51 +01:00
|
|
|
int readableBytes = buffer.readableBytes();
|
2014-02-06 10:34:05 +01:00
|
|
|
if (readableBytes > 0) {
|
|
|
|
// Keep on consuming as otherwise we may trigger an DecoderException,
|
|
|
|
// other handler will replace this codec with the upgraded protocol codec to
|
|
|
|
// take the traffic over at some point then.
|
|
|
|
// See https://github.com/netty/netty/issues/2173
|
2014-11-04 17:02:51 +01:00
|
|
|
out.add(buffer.readBytes(readableBytes));
|
2014-02-06 10:34:05 +01:00
|
|
|
}
|
2013-12-18 12:29:13 +01:00
|
|
|
break;
|
2008-11-19 08:22:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-13 03:57:06 +02:00
|
|
|
@Override
|
Remove MessageList from public API and change ChannelInbound/OutboundHandler accordingly
I must admit MesageList was pain in the ass. Instead of forcing a
handler always loop over the list of messages, this commit splits
messageReceived(ctx, list) into two event handlers:
- messageReceived(ctx, msg)
- mmessageReceivedLast(ctx)
When Netty reads one or more messages, messageReceived(ctx, msg) event
is triggered for each message. Once the current read operation is
finished, messageReceivedLast() is triggered to tell the handler that
the last messageReceived() was the last message in the current batch.
Similarly, for outbound, write(ctx, list) has been split into two:
- write(ctx, msg)
- flush(ctx, promise)
Instead of writing a list of message with a promise, a user is now
supposed to call write(msg) multiple times and then call flush() to
actually flush the buffered messages.
Please note that write() doesn't have a promise with it. You must call
flush() to get notified on completion. (or you can use writeAndFlush())
Other changes:
- Because MessageList is completely hidden, codec framework uses
List<Object> instead of MessageList as an output parameter.
2013-07-08 12:03:40 +02:00
|
|
|
protected void decodeLast(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
|
2016-02-25 16:20:41 +01:00
|
|
|
super.decodeLast(ctx, in, out);
|
2013-06-13 03:57:06 +02:00
|
|
|
|
2016-04-09 22:25:28 +02:00
|
|
|
if (resetRequested) {
|
|
|
|
// If a reset was requested by decodeLast() we need to do it now otherwise we may produce a
|
|
|
|
// LastHttpContent while there was already one.
|
|
|
|
resetNow();
|
|
|
|
}
|
2013-06-13 03:57:06 +02:00
|
|
|
// Handle the last unfinished message.
|
|
|
|
if (message != null) {
|
2015-08-22 17:25:57 +02:00
|
|
|
boolean chunked = HttpUtil.isTransferEncodingChunked(message);
|
2014-11-04 17:02:51 +01:00
|
|
|
if (currentState == State.READ_VARIABLE_LENGTH_CONTENT && !in.isReadable() && !chunked) {
|
2014-08-27 08:29:25 +02:00
|
|
|
// End of connection.
|
|
|
|
out.add(LastHttpContent.EMPTY_LAST_CONTENT);
|
2016-06-07 10:27:53 +02:00
|
|
|
resetNow();
|
2014-08-27 08:29:25 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-06-07 10:27:53 +02:00
|
|
|
|
|
|
|
if (currentState == State.READ_HEADER) {
|
|
|
|
// If we are still in the state of reading headers we need to create a new invalid message that
|
|
|
|
// signals that the connection was closed before we received the headers.
|
|
|
|
out.add(invalidMessage(Unpooled.EMPTY_BUFFER,
|
|
|
|
new PrematureChannelClosureException("Connection closed before received headers")));
|
|
|
|
resetNow();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-13 04:51:03 +02:00
|
|
|
// Check if the closure of the connection signifies the end of the content.
|
2013-06-13 03:57:06 +02:00
|
|
|
boolean prematureClosure;
|
2014-08-27 08:29:25 +02:00
|
|
|
if (isDecodingRequest() || chunked) {
|
2013-06-13 03:57:06 +02:00
|
|
|
// The last request did not wait for a response.
|
|
|
|
prematureClosure = true;
|
|
|
|
} else {
|
|
|
|
// Compare the length of the received content and the 'Content-Length' header.
|
|
|
|
// If the 'Content-Length' header is absent, the length of the content is determined by the end of the
|
|
|
|
// connection, so it is perfectly fine.
|
2013-12-18 13:55:15 +01:00
|
|
|
prematureClosure = contentLength() > 0;
|
2013-06-13 03:57:06 +02:00
|
|
|
}
|
|
|
|
|
2013-06-13 04:51:03 +02:00
|
|
|
if (!prematureClosure) {
|
2013-12-16 16:17:35 +01:00
|
|
|
out.add(LastHttpContent.EMPTY_LAST_CONTENT);
|
2013-06-13 03:57:06 +02:00
|
|
|
}
|
2016-06-07 10:27:53 +02:00
|
|
|
resetNow();
|
2013-06-13 03:57:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-21 21:41:03 +02:00
|
|
|
@Override
|
|
|
|
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
|
2016-02-13 00:30:09 +01:00
|
|
|
if (evt instanceof HttpExpectationFailedEvent) {
|
2015-08-04 00:09:44 +02:00
|
|
|
switch (currentState) {
|
|
|
|
case READ_FIXED_LENGTH_CONTENT:
|
|
|
|
case READ_VARIABLE_LENGTH_CONTENT:
|
|
|
|
case READ_CHUNK_SIZE:
|
|
|
|
reset();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2015-07-21 21:41:03 +02:00
|
|
|
}
|
|
|
|
super.userEventTriggered(ctx, evt);
|
|
|
|
}
|
|
|
|
|
2013-01-16 05:22:50 +01:00
|
|
|
protected boolean isContentAlwaysEmpty(HttpMessage msg) {
|
|
|
|
if (msg instanceof HttpResponse) {
|
|
|
|
HttpResponse res = (HttpResponse) msg;
|
2014-06-24 10:39:46 +02:00
|
|
|
int code = res.status().code();
|
2012-05-20 07:19:11 +02:00
|
|
|
|
2012-03-20 15:40:34 +01:00
|
|
|
// Correctly handle return codes of 1xx.
|
2012-05-20 07:19:11 +02:00
|
|
|
//
|
|
|
|
// See:
|
2012-03-20 15:40:34 +01:00
|
|
|
// - http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html Section 4.4
|
|
|
|
// - https://github.com/netty/netty/issues/222
|
2012-03-20 15:44:59 +01:00
|
|
|
if (code >= 100 && code < 200) {
|
2013-12-16 16:17:35 +01:00
|
|
|
// One exception: Hixie 76 websocket handshake response
|
2015-08-17 15:23:31 +02:00
|
|
|
return !(code == 101 && !res.headers().contains(HttpHeaderNames.SEC_WEBSOCKET_ACCEPT)
|
|
|
|
&& res.headers().contains(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET, true));
|
2009-03-30 04:14:11 +02:00
|
|
|
}
|
2012-03-09 03:07:26 +01:00
|
|
|
|
2009-03-30 04:14:11 +02:00
|
|
|
switch (code) {
|
|
|
|
case 204: case 205: case 304:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-02-20 22:41:54 +01:00
|
|
|
/**
|
|
|
|
* Resets the state of the decoder so that it is ready to decode a new message.
|
|
|
|
* This method is useful for handling a rejected request with {@code Expect: 100-continue} header.
|
|
|
|
*/
|
|
|
|
public void reset() {
|
|
|
|
resetRequested = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void resetNow() {
|
2013-12-18 12:29:13 +01:00
|
|
|
HttpMessage message = this.message;
|
|
|
|
this.message = null;
|
2014-08-27 08:29:25 +02:00
|
|
|
name = null;
|
|
|
|
value = null;
|
2013-12-18 13:55:15 +01:00
|
|
|
contentLength = Long.MIN_VALUE;
|
2014-08-27 08:29:25 +02:00
|
|
|
lineParser.reset();
|
|
|
|
headerParser.reset();
|
2014-11-04 17:02:51 +01:00
|
|
|
trailer = null;
|
2013-12-18 12:29:13 +01:00
|
|
|
if (!isDecodingRequest()) {
|
|
|
|
HttpResponse res = (HttpResponse) message;
|
2014-06-24 10:39:46 +02:00
|
|
|
if (res != null && res.status().code() == 101) {
|
2014-11-04 17:02:51 +01:00
|
|
|
currentState = State.UPGRADED;
|
2013-12-18 12:29:13 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-21 05:45:03 +02:00
|
|
|
resetRequested = false;
|
2014-11-04 17:02:51 +01:00
|
|
|
currentState = State.SKIP_CONTROL_CHARS;
|
2008-11-19 08:22:15 +01:00
|
|
|
}
|
|
|
|
|
2015-03-03 10:56:32 +01:00
|
|
|
private HttpMessage invalidMessage(ByteBuf in, Exception cause) {
|
2014-11-04 17:02:51 +01:00
|
|
|
currentState = State.BAD_MESSAGE;
|
2015-03-03 10:56:32 +01:00
|
|
|
|
|
|
|
// Advance the readerIndex so that ByteToMessageDecoder does not complain
|
|
|
|
// when we produced an invalid message without consuming anything.
|
|
|
|
in.skipBytes(in.readableBytes());
|
|
|
|
|
2012-09-28 08:16:29 +02:00
|
|
|
if (message != null) {
|
2013-03-12 05:02:50 +01:00
|
|
|
message.setDecoderResult(DecoderResult.failure(cause));
|
2012-09-28 08:16:29 +02:00
|
|
|
} else {
|
|
|
|
message = createInvalidMessage();
|
2013-01-30 07:42:18 +01:00
|
|
|
message.setDecoderResult(DecoderResult.failure(cause));
|
2012-09-28 08:16:29 +02:00
|
|
|
}
|
2014-01-10 08:16:02 +01:00
|
|
|
|
|
|
|
HttpMessage ret = message;
|
|
|
|
message = null;
|
|
|
|
return ret;
|
2012-09-28 08:16:29 +02:00
|
|
|
}
|
|
|
|
|
2015-03-03 10:56:32 +01:00
|
|
|
private HttpContent invalidChunk(ByteBuf in, Exception cause) {
|
2014-11-04 17:02:51 +01:00
|
|
|
currentState = State.BAD_MESSAGE;
|
2015-03-03 10:56:32 +01:00
|
|
|
|
|
|
|
// Advance the readerIndex so that ByteToMessageDecoder does not complain
|
|
|
|
// when we produced an invalid message without consuming anything.
|
|
|
|
in.skipBytes(in.readableBytes());
|
|
|
|
|
2014-02-19 23:58:51 +01:00
|
|
|
HttpContent chunk = new DefaultLastHttpContent(Unpooled.EMPTY_BUFFER);
|
2013-01-30 07:42:18 +01:00
|
|
|
chunk.setDecoderResult(DecoderResult.failure(cause));
|
2014-02-27 00:53:53 +01:00
|
|
|
message = null;
|
2014-11-04 17:02:51 +01:00
|
|
|
trailer = null;
|
2012-09-28 08:16:29 +02:00
|
|
|
return chunk;
|
|
|
|
}
|
|
|
|
|
2014-11-04 17:02:51 +01:00
|
|
|
private static boolean skipControlCharacters(ByteBuf buffer) {
|
|
|
|
boolean skiped = false;
|
|
|
|
final int wIdx = buffer.writerIndex();
|
|
|
|
int rIdx = buffer.readerIndex();
|
|
|
|
while (wIdx > rIdx) {
|
|
|
|
int c = buffer.getUnsignedByte(rIdx++);
|
|
|
|
if (!Character.isISOControl(c) && !Character.isWhitespace(c)) {
|
|
|
|
rIdx--;
|
|
|
|
skiped = true;
|
2009-02-12 06:02:22 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-11-04 17:02:51 +01:00
|
|
|
buffer.readerIndex(rIdx);
|
|
|
|
return skiped;
|
2009-02-12 06:02:22 +01:00
|
|
|
}
|
|
|
|
|
2012-11-09 23:07:37 +01:00
|
|
|
private State readHeaders(ByteBuf buffer) {
|
2013-01-16 05:22:50 +01:00
|
|
|
final HttpMessage message = this.message;
|
2013-01-16 16:33:40 +01:00
|
|
|
final HttpHeaders headers = message.headers();
|
|
|
|
|
2014-01-14 16:08:29 +01:00
|
|
|
AppendableCharSequence line = headerParser.parse(buffer);
|
2014-11-04 17:02:51 +01:00
|
|
|
if (line == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2013-07-30 15:46:53 +02:00
|
|
|
if (line.length() > 0) {
|
2009-06-15 10:05:27 +02:00
|
|
|
do {
|
2009-06-15 10:13:52 +02:00
|
|
|
char firstChar = line.charAt(0);
|
2010-07-07 08:43:34 +02:00
|
|
|
if (name != null && (firstChar == ' ' || firstChar == '\t')) {
|
2017-02-13 22:31:09 +01:00
|
|
|
String trimmedLine = line.toString().trim();
|
|
|
|
StringBuilder buf = new StringBuilder(value.length() + trimmedLine.length() + 1);
|
2014-11-08 23:46:30 +01:00
|
|
|
buf.append(value)
|
|
|
|
.append(' ')
|
2017-02-13 22:31:09 +01:00
|
|
|
.append(trimmedLine);
|
2014-10-25 09:42:36 +02:00
|
|
|
value = buf.toString();
|
2009-06-15 10:13:52 +02:00
|
|
|
} else {
|
2010-07-07 08:43:34 +02:00
|
|
|
if (name != null) {
|
2013-01-16 16:33:40 +01:00
|
|
|
headers.add(name, value);
|
2010-07-07 08:43:34 +02:00
|
|
|
}
|
2014-08-27 08:29:25 +02:00
|
|
|
splitHeader(line);
|
2009-06-15 10:05:27 +02:00
|
|
|
}
|
2009-06-15 10:35:24 +02:00
|
|
|
|
2014-01-14 16:08:29 +01:00
|
|
|
line = headerParser.parse(buffer);
|
2014-11-04 17:02:51 +01:00
|
|
|
if (line == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2013-07-30 15:46:53 +02:00
|
|
|
} while (line.length() > 0);
|
2014-08-27 08:29:25 +02:00
|
|
|
}
|
2010-07-07 08:43:34 +02:00
|
|
|
|
2014-08-27 08:29:25 +02:00
|
|
|
// Add the last header.
|
|
|
|
if (name != null) {
|
|
|
|
headers.add(name, value);
|
2008-11-19 08:22:15 +01:00
|
|
|
}
|
2014-08-27 08:29:25 +02:00
|
|
|
// reset name and value fields
|
|
|
|
name = null;
|
|
|
|
value = null;
|
2008-11-30 17:22:03 +01:00
|
|
|
|
2008-12-03 10:00:29 +01:00
|
|
|
State nextState;
|
2009-03-30 04:14:11 +02:00
|
|
|
|
|
|
|
if (isContentAlwaysEmpty(message)) {
|
2015-08-22 17:25:57 +02:00
|
|
|
HttpUtil.setTransferEncodingChunked(message, false);
|
2009-03-30 04:14:11 +02:00
|
|
|
nextState = State.SKIP_CONTROL_CHARS;
|
2015-08-22 17:25:57 +02:00
|
|
|
} else if (HttpUtil.isTransferEncodingChunked(message)) {
|
2008-12-03 10:00:29 +01:00
|
|
|
nextState = State.READ_CHUNK_SIZE;
|
2013-12-18 13:55:15 +01:00
|
|
|
} else if (contentLength() >= 0) {
|
2009-02-12 05:37:48 +01:00
|
|
|
nextState = State.READ_FIXED_LENGTH_CONTENT;
|
|
|
|
} else {
|
2009-02-12 08:17:29 +01:00
|
|
|
nextState = State.READ_VARIABLE_LENGTH_CONTENT;
|
2008-11-19 08:22:15 +01:00
|
|
|
}
|
2009-03-04 14:50:19 +01:00
|
|
|
return nextState;
|
2008-11-19 08:22:15 +01:00
|
|
|
}
|
|
|
|
|
2013-12-18 13:55:15 +01:00
|
|
|
private long contentLength() {
|
|
|
|
if (contentLength == Long.MIN_VALUE) {
|
2016-02-01 02:24:14 +01:00
|
|
|
contentLength = HttpUtil.getContentLength(message, -1L);
|
2013-12-18 13:55:15 +01:00
|
|
|
}
|
|
|
|
return contentLength;
|
|
|
|
}
|
|
|
|
|
2013-01-14 16:52:30 +01:00
|
|
|
private LastHttpContent readTrailingHeaders(ByteBuf buffer) {
|
2014-01-14 16:08:29 +01:00
|
|
|
AppendableCharSequence line = headerParser.parse(buffer);
|
2014-11-04 17:02:51 +01:00
|
|
|
if (line == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-08-27 08:29:25 +02:00
|
|
|
CharSequence lastHeader = null;
|
2013-07-30 15:46:53 +02:00
|
|
|
if (line.length() > 0) {
|
2014-11-04 17:02:51 +01:00
|
|
|
LastHttpContent trailer = this.trailer;
|
|
|
|
if (trailer == null) {
|
|
|
|
trailer = this.trailer = new DefaultLastHttpContent(Unpooled.EMPTY_BUFFER, validateHeaders);
|
|
|
|
}
|
2009-11-17 05:55:56 +01:00
|
|
|
do {
|
|
|
|
char firstChar = line.charAt(0);
|
|
|
|
if (lastHeader != null && (firstChar == ' ' || firstChar == '\t')) {
|
2013-01-16 05:22:50 +01:00
|
|
|
List<String> current = trailer.trailingHeaders().getAll(lastHeader);
|
2012-11-09 17:24:04 +01:00
|
|
|
if (!current.isEmpty()) {
|
2009-11-17 06:39:01 +01:00
|
|
|
int lastPos = current.size() - 1;
|
2014-09-19 01:04:35 +02:00
|
|
|
String lineTrimmed = line.toString().trim();
|
|
|
|
CharSequence currentLastPos = current.get(lastPos);
|
|
|
|
StringBuilder b = new StringBuilder(currentLastPos.length() + lineTrimmed.length());
|
2014-11-08 23:46:30 +01:00
|
|
|
b.append(currentLastPos)
|
|
|
|
.append(lineTrimmed);
|
2014-09-19 01:04:35 +02:00
|
|
|
current.set(lastPos, b.toString());
|
2009-11-17 06:39:01 +01:00
|
|
|
} else {
|
|
|
|
// Content-Length, Transfer-Encoding, or Trailer
|
|
|
|
}
|
2009-11-17 05:55:56 +01:00
|
|
|
} else {
|
2014-08-27 08:29:25 +02:00
|
|
|
splitHeader(line);
|
|
|
|
CharSequence headerName = name;
|
2015-08-13 04:05:37 +02:00
|
|
|
if (!HttpHeaderNames.CONTENT_LENGTH.contentEqualsIgnoreCase(headerName) &&
|
|
|
|
!HttpHeaderNames.TRANSFER_ENCODING.contentEqualsIgnoreCase(headerName) &&
|
|
|
|
!HttpHeaderNames.TRAILER.contentEqualsIgnoreCase(headerName)) {
|
2014-08-27 08:29:25 +02:00
|
|
|
trailer.trailingHeaders().add(headerName, value);
|
2009-11-17 06:39:01 +01:00
|
|
|
}
|
|
|
|
lastHeader = name;
|
2014-08-27 08:29:25 +02:00
|
|
|
// reset name and value fields
|
|
|
|
name = null;
|
|
|
|
value = null;
|
2009-11-17 05:55:56 +01:00
|
|
|
}
|
|
|
|
|
2014-01-14 16:08:29 +01:00
|
|
|
line = headerParser.parse(buffer);
|
2014-11-04 17:02:51 +01:00
|
|
|
if (line == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2013-07-30 15:46:53 +02:00
|
|
|
} while (line.length() > 0);
|
2009-11-17 05:55:56 +01:00
|
|
|
|
2014-11-04 17:02:51 +01:00
|
|
|
this.trailer = null;
|
2009-11-17 06:39:01 +01:00
|
|
|
return trailer;
|
2009-11-17 05:55:56 +01:00
|
|
|
}
|
2009-11-17 06:39:01 +01:00
|
|
|
|
2013-01-16 05:22:50 +01:00
|
|
|
return LastHttpContent.EMPTY_LAST_CONTENT;
|
2009-11-17 05:55:56 +01:00
|
|
|
}
|
|
|
|
|
2009-02-12 08:32:53 +01:00
|
|
|
protected abstract boolean isDecodingRequest();
|
2013-01-16 05:22:50 +01:00
|
|
|
protected abstract HttpMessage createMessage(String[] initialLine) throws Exception;
|
|
|
|
protected abstract HttpMessage createInvalidMessage();
|
2012-09-28 08:16:29 +02:00
|
|
|
|
2012-05-20 07:19:11 +02:00
|
|
|
private static int getChunkSize(String hex) {
|
2009-02-12 06:23:39 +01:00
|
|
|
hex = hex.trim();
|
|
|
|
for (int i = 0; i < hex.length(); i ++) {
|
|
|
|
char c = hex.charAt(i);
|
|
|
|
if (c == ';' || Character.isWhitespace(c) || Character.isISOControl(c)) {
|
|
|
|
hex = hex.substring(0, i);
|
|
|
|
break;
|
|
|
|
}
|
2009-02-12 06:10:25 +01:00
|
|
|
}
|
2009-02-12 06:23:39 +01:00
|
|
|
|
2009-02-12 06:10:25 +01:00
|
|
|
return Integer.parseInt(hex, 16);
|
2008-11-19 08:22:15 +01:00
|
|
|
}
|
|
|
|
|
2013-12-03 12:04:07 +01:00
|
|
|
private static String[] splitInitialLine(AppendableCharSequence sb) {
|
2009-06-15 09:30:07 +02:00
|
|
|
int aStart;
|
|
|
|
int aEnd;
|
|
|
|
int bStart;
|
|
|
|
int bEnd;
|
|
|
|
int cStart;
|
|
|
|
int cEnd;
|
|
|
|
|
|
|
|
aStart = findNonWhitespace(sb, 0);
|
|
|
|
aEnd = findWhitespace(sb, aStart);
|
|
|
|
|
|
|
|
bStart = findNonWhitespace(sb, aEnd);
|
|
|
|
bEnd = findWhitespace(sb, bStart);
|
|
|
|
|
|
|
|
cStart = findNonWhitespace(sb, bEnd);
|
|
|
|
cEnd = findEndOfString(sb);
|
|
|
|
|
|
|
|
return new String[] {
|
2015-07-25 17:03:45 +02:00
|
|
|
sb.subStringUnsafe(aStart, aEnd),
|
|
|
|
sb.subStringUnsafe(bStart, bEnd),
|
|
|
|
cStart < cEnd? sb.subStringUnsafe(cStart, cEnd) : "" };
|
2008-11-19 08:22:15 +01:00
|
|
|
}
|
|
|
|
|
2014-08-27 08:29:25 +02:00
|
|
|
private void splitHeader(AppendableCharSequence sb) {
|
2009-09-10 12:34:49 +02:00
|
|
|
final int length = sb.length();
|
2009-06-15 09:30:07 +02:00
|
|
|
int nameStart;
|
|
|
|
int nameEnd;
|
|
|
|
int colonEnd;
|
|
|
|
int valueStart;
|
|
|
|
int valueEnd;
|
|
|
|
|
|
|
|
nameStart = findNonWhitespace(sb, 0);
|
2009-09-10 12:34:49 +02:00
|
|
|
for (nameEnd = nameStart; nameEnd < length; nameEnd ++) {
|
2009-06-15 09:30:07 +02:00
|
|
|
char ch = sb.charAt(nameEnd);
|
|
|
|
if (ch == ':' || Character.isWhitespace(ch)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-10 12:34:49 +02:00
|
|
|
for (colonEnd = nameEnd; colonEnd < length; colonEnd ++) {
|
2009-06-15 09:30:07 +02:00
|
|
|
if (sb.charAt(colonEnd) == ':') {
|
|
|
|
colonEnd ++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-25 17:03:45 +02:00
|
|
|
name = sb.subStringUnsafe(nameStart, nameEnd);
|
2009-06-15 09:30:07 +02:00
|
|
|
valueStart = findNonWhitespace(sb, colonEnd);
|
2009-09-10 12:34:49 +02:00
|
|
|
if (valueStart == length) {
|
2014-08-27 08:29:25 +02:00
|
|
|
value = EMPTY_VALUE;
|
|
|
|
} else {
|
|
|
|
valueEnd = findEndOfString(sb);
|
2015-07-25 17:03:45 +02:00
|
|
|
value = sb.subStringUnsafe(valueStart, valueEnd);
|
2009-09-10 12:34:49 +02:00
|
|
|
}
|
2009-06-15 09:30:07 +02:00
|
|
|
}
|
|
|
|
|
2015-07-25 17:03:45 +02:00
|
|
|
private static int findNonWhitespace(AppendableCharSequence sb, int offset) {
|
|
|
|
for (int result = offset; result < sb.length(); ++result) {
|
|
|
|
if (!Character.isWhitespace(sb.charAtUnsafe(result))) {
|
|
|
|
return result;
|
2009-06-15 09:30:07 +02:00
|
|
|
}
|
|
|
|
}
|
2015-07-25 17:03:45 +02:00
|
|
|
return sb.length();
|
2009-06-15 09:30:07 +02:00
|
|
|
}
|
|
|
|
|
2015-07-25 17:03:45 +02:00
|
|
|
private static int findWhitespace(AppendableCharSequence sb, int offset) {
|
|
|
|
for (int result = offset; result < sb.length(); ++result) {
|
|
|
|
if (Character.isWhitespace(sb.charAtUnsafe(result))) {
|
|
|
|
return result;
|
2009-06-15 09:30:07 +02:00
|
|
|
}
|
|
|
|
}
|
2015-07-25 17:03:45 +02:00
|
|
|
return sb.length();
|
2009-06-15 09:30:07 +02:00
|
|
|
}
|
|
|
|
|
2015-07-25 17:03:45 +02:00
|
|
|
private static int findEndOfString(AppendableCharSequence sb) {
|
|
|
|
for (int result = sb.length() - 1; result > 0; --result) {
|
|
|
|
if (!Character.isWhitespace(sb.charAtUnsafe(result))) {
|
|
|
|
return result + 1;
|
2009-06-15 09:30:07 +02:00
|
|
|
}
|
2008-11-19 08:22:15 +01:00
|
|
|
}
|
2015-07-25 17:03:45 +02:00
|
|
|
return 0;
|
2008-11-19 08:22:15 +01:00
|
|
|
}
|
2014-01-14 16:08:29 +01:00
|
|
|
|
2015-04-01 01:23:52 +02:00
|
|
|
private static class HeaderParser implements ByteProcessor {
|
2014-01-14 16:08:29 +01:00
|
|
|
private final AppendableCharSequence seq;
|
2014-08-27 08:29:25 +02:00
|
|
|
private final int maxLength;
|
|
|
|
private int size;
|
2014-01-14 16:08:29 +01:00
|
|
|
|
2014-08-27 08:29:25 +02:00
|
|
|
HeaderParser(AppendableCharSequence seq, int maxLength) {
|
2014-01-14 16:08:29 +01:00
|
|
|
this.seq = seq;
|
2014-08-27 08:29:25 +02:00
|
|
|
this.maxLength = maxLength;
|
2014-01-14 16:08:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public AppendableCharSequence parse(ByteBuf buffer) {
|
2015-03-03 10:56:32 +01:00
|
|
|
final int oldSize = size;
|
2014-01-14 16:08:29 +01:00
|
|
|
seq.reset();
|
|
|
|
int i = buffer.forEachByte(this);
|
2014-11-04 17:02:51 +01:00
|
|
|
if (i == -1) {
|
2015-03-03 10:56:32 +01:00
|
|
|
size = oldSize;
|
2014-11-04 17:02:51 +01:00
|
|
|
return null;
|
|
|
|
}
|
2014-01-14 16:08:29 +01:00
|
|
|
buffer.readerIndex(i + 1);
|
|
|
|
return seq;
|
|
|
|
}
|
|
|
|
|
2014-08-27 08:29:25 +02:00
|
|
|
public void reset() {
|
|
|
|
size = 0;
|
|
|
|
}
|
|
|
|
|
2014-01-14 16:08:29 +01:00
|
|
|
@Override
|
|
|
|
public boolean process(byte value) throws Exception {
|
2017-03-24 06:07:43 +01:00
|
|
|
char nextByte = (char) (value & 0xFF);
|
2014-01-14 16:08:29 +01:00
|
|
|
if (nextByte == HttpConstants.CR) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextByte == HttpConstants.LF) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-03-03 10:56:32 +01:00
|
|
|
|
|
|
|
if (++ size > maxLength) {
|
2014-01-14 16:08:29 +01:00
|
|
|
// TODO: Respond with Bad Request and discard the traffic
|
|
|
|
// or close the connection.
|
|
|
|
// No need to notify the upstream handlers - just log.
|
|
|
|
// If decoding a response, just throw an exception.
|
2014-08-27 08:29:25 +02:00
|
|
|
throw newException(maxLength);
|
2014-01-14 16:08:29 +01:00
|
|
|
}
|
2015-03-03 10:56:32 +01:00
|
|
|
|
2014-01-14 16:08:29 +01:00
|
|
|
seq.append(nextByte);
|
|
|
|
return true;
|
|
|
|
}
|
2014-08-27 08:29:25 +02:00
|
|
|
|
|
|
|
protected TooLongFrameException newException(int maxLength) {
|
2014-10-25 09:42:36 +02:00
|
|
|
return new TooLongFrameException("HTTP header is larger than " + maxLength + " bytes.");
|
2014-08-27 08:29:25 +02:00
|
|
|
}
|
2014-01-14 16:08:29 +01:00
|
|
|
}
|
|
|
|
|
2014-11-04 17:02:51 +01:00
|
|
|
private static final class LineParser extends HeaderParser {
|
2014-01-14 16:08:29 +01:00
|
|
|
|
2014-08-27 08:29:25 +02:00
|
|
|
LineParser(AppendableCharSequence seq, int maxLength) {
|
|
|
|
super(seq, maxLength);
|
2014-01-14 16:08:29 +01:00
|
|
|
}
|
|
|
|
|
2014-08-27 08:29:25 +02:00
|
|
|
@Override
|
2014-01-14 16:08:29 +01:00
|
|
|
public AppendableCharSequence parse(ByteBuf buffer) {
|
2014-08-27 08:29:25 +02:00
|
|
|
reset();
|
|
|
|
return super.parse(buffer);
|
2014-01-14 16:08:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-08-27 08:29:25 +02:00
|
|
|
protected TooLongFrameException newException(int maxLength) {
|
2014-10-25 09:42:36 +02:00
|
|
|
return new TooLongFrameException("An HTTP line is larger than " + maxLength + " bytes.");
|
2014-01-14 16:08:29 +01:00
|
|
|
}
|
|
|
|
}
|
2008-11-19 08:22:15 +01:00
|
|
|
}
|