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;
|
2011-12-09 04:38:59 +01:00
|
|
|
import io.netty.channel.ChannelPipeline;
|
2012-05-16 16:02:06 +02:00
|
|
|
import io.netty.handler.codec.TooLongFrameException;
|
2009-06-19 17:35:19 +02:00
|
|
|
|
2008-11-19 08:22:15 +01:00
|
|
|
|
|
|
|
/**
|
2013-01-16 05:22:50 +01:00
|
|
|
* Decodes {@link ByteBuf}s into {@link HttpResponse}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 "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
|
|
|
|
* exceeds this value, the transfer encoding of the decoded response will be
|
|
|
|
* converted to 'chunked' and the content will be split into multiple
|
2013-01-14 16:52:30 +01:00
|
|
|
* {@link HttpContent}s. If the transfer encoding of the HTTP response is
|
2009-06-19 17:35:19 +02:00
|
|
|
* 'chunked' already, each chunk will be split into smaller chunks if the
|
|
|
|
* length of the chunk exceeds this value. If you prefer not to handle
|
2013-01-14 16:52:30 +01:00
|
|
|
* {@link HttpContent}s in your handler, insert {@link HttpObjectAggregator}
|
2009-06-19 17:35:19 +02:00
|
|
|
* after this decoder in the {@link ChannelPipeline}.</td>
|
|
|
|
* </tr>
|
|
|
|
* </table>
|
2008-11-19 08:22:15 +01:00
|
|
|
*
|
2010-03-31 04:19:45 +02:00
|
|
|
* <h3>Decoding a response for a <tt>HEAD</tt> request</h3>
|
|
|
|
* <p>
|
|
|
|
* Unlike other HTTP requests, the successful response of a <tt>HEAD</tt>
|
|
|
|
* request does not have any content even if there is <tt>Content-Length</tt>
|
|
|
|
* header. Because {@link HttpResponseDecoder} is not able to determine if the
|
|
|
|
* response currently being decoded is associated with a <tt>HEAD</tt> request,
|
2013-01-16 05:22:50 +01:00
|
|
|
* you must override {@link #isContentAlwaysEmpty(HttpMessage)} to return
|
2010-03-31 04:19:45 +02:00
|
|
|
* <tt>true</tt> for the response of the <tt>HEAD</tt> request.
|
|
|
|
* </p><p>
|
|
|
|
* If you are writing an HTTP client that issues a <tt>HEAD</tt> request,
|
|
|
|
* please use {@link HttpClientCodec} instead of this decoder. It will perform
|
|
|
|
* additional state management to handle the responses for <tt>HEAD</tt>
|
|
|
|
* requests correctly.
|
|
|
|
* </p>
|
|
|
|
*
|
|
|
|
* <h3>Decoding a response for a <tt>CONNECT</tt> request</h3>
|
|
|
|
* <p>
|
|
|
|
* You also need to do additional state management to handle the response of a
|
|
|
|
* <tt>CONNECT</tt> request properly, like you did for <tt>HEAD</tt>. One
|
|
|
|
* difference is that the decoder should stop decoding completely after decoding
|
|
|
|
* the successful 200 response since the connection is not an HTTP connection
|
|
|
|
* anymore.
|
|
|
|
* </p><p>
|
|
|
|
* {@link HttpClientCodec} also handles this edge case correctly, so you have to
|
|
|
|
* use {@link HttpClientCodec} if you are writing an HTTP client that issues a
|
|
|
|
* <tt>CONNECT</tt> request.
|
|
|
|
* </p>
|
2008-11-19 08:22:15 +01:00
|
|
|
*/
|
2013-01-14 16:52:30 +01:00
|
|
|
public class HttpResponseDecoder extends HttpObjectDecoder {
|
2009-02-12 07:09:29 +01:00
|
|
|
|
2012-09-28 08:16:29 +02:00
|
|
|
private static final HttpResponseStatus UNKNOWN_STATUS = new HttpResponseStatus(999, "Unknown");
|
|
|
|
|
2009-06-19 17:35:19 +02:00
|
|
|
/**
|
|
|
|
* Creates a new instance with the default
|
2014-11-04 17:02:51 +01:00
|
|
|
* {@code maxInitialLineLength (4096)}, {@code maxHeaderSize (8192)}, and
|
2010-01-27 09:13:38 +01:00
|
|
|
* {@code maxChunkSize (8192)}.
|
2009-06-19 17:35:19 +02:00
|
|
|
*/
|
2009-03-10 09:57:02 +01:00
|
|
|
public HttpResponseDecoder() {
|
|
|
|
}
|
|
|
|
|
2009-06-19 17:35:19 +02:00
|
|
|
/**
|
|
|
|
* Creates a new instance with the specified parameters.
|
|
|
|
*/
|
2009-03-10 08:20:27 +01:00
|
|
|
public HttpResponseDecoder(
|
|
|
|
int maxInitialLineLength, int maxHeaderSize, int maxChunkSize) {
|
2013-01-20 10:40:54 +01:00
|
|
|
super(maxInitialLineLength, maxHeaderSize, maxChunkSize, true);
|
2009-02-12 07:09:29 +01:00
|
|
|
}
|
|
|
|
|
2013-11-13 15:05:12 +01:00
|
|
|
public HttpResponseDecoder(
|
|
|
|
int maxInitialLineLength, int maxHeaderSize, int maxChunkSize, boolean validateHeaders) {
|
|
|
|
super(maxInitialLineLength, maxHeaderSize, maxChunkSize, true, validateHeaders);
|
|
|
|
}
|
|
|
|
|
2016-02-05 23:33:57 +01:00
|
|
|
public HttpResponseDecoder(
|
|
|
|
int maxInitialLineLength, int maxHeaderSize, int maxChunkSize, boolean validateHeaders,
|
|
|
|
int initialBufferSize) {
|
|
|
|
super(maxInitialLineLength, maxHeaderSize, maxChunkSize, true, validateHeaders, initialBufferSize);
|
|
|
|
}
|
|
|
|
|
2008-11-19 08:22:15 +01:00
|
|
|
@Override
|
2013-01-16 05:22:50 +01:00
|
|
|
protected HttpMessage createMessage(String[] initialLine) {
|
|
|
|
return new DefaultHttpResponse(
|
2012-06-08 12:28:12 +02:00
|
|
|
HttpVersion.valueOf(initialLine[0]),
|
2018-01-25 07:01:52 +01:00
|
|
|
HttpResponseStatus.valueOf(Integer.parseInt(initialLine[1]), initialLine[2]), validateHeaders);
|
2008-11-19 08:22:15 +01:00
|
|
|
}
|
2009-02-12 08:32:53 +01:00
|
|
|
|
2012-09-28 08:16:29 +02:00
|
|
|
@Override
|
2013-01-16 05:22:50 +01:00
|
|
|
protected HttpMessage createInvalidMessage() {
|
2015-01-30 08:08:15 +01:00
|
|
|
return new DefaultFullHttpResponse(HttpVersion.HTTP_1_0, UNKNOWN_STATUS, validateHeaders);
|
2012-09-28 08:16:29 +02:00
|
|
|
}
|
|
|
|
|
2009-02-12 08:32:53 +01:00
|
|
|
@Override
|
|
|
|
protected boolean isDecodingRequest() {
|
|
|
|
return false;
|
|
|
|
}
|
2008-11-19 08:22:15 +01:00
|
|
|
}
|