2010-03-31 04:19:45 +02:00
|
|
|
/*
|
2012-06-04 22:31:44 +02:00
|
|
|
* Copyright 2012 The Netty Project
|
2010-03-31 04:19:45 +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:
|
2010-03-31 04:19:45 +02:00
|
|
|
*
|
2012-06-04 22:31:44 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2010-03-31 04:19:45 +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
|
2010-03-31 04:19:45 +02:00
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
2011-12-09 04:38:59 +01:00
|
|
|
package io.netty.handler.codec.http;
|
2010-03-31 04:19:45 +02:00
|
|
|
|
2012-06-10 04:08:43 +02:00
|
|
|
import io.netty.buffer.ByteBuf;
|
2012-05-31 00:35:14 +02:00
|
|
|
import io.netty.channel.Channel;
|
2012-06-07 07:52:33 +02:00
|
|
|
import io.netty.channel.ChannelHandlerContext;
|
2015-11-29 05:36:32 +01:00
|
|
|
import io.netty.channel.ChannelPipeline;
|
2016-01-10 22:19:55 +01:00
|
|
|
import io.netty.channel.CombinedChannelDuplexHandler;
|
2012-05-31 00:35:14 +02:00
|
|
|
import io.netty.handler.codec.PrematureChannelClosureException;
|
2016-01-29 03:59:51 +01:00
|
|
|
import io.netty.util.ReferenceCountUtil;
|
2010-03-31 04:19:45 +02:00
|
|
|
|
2012-06-12 10:02:00 +02:00
|
|
|
import java.util.ArrayDeque;
|
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
|
|
|
import java.util.List;
|
2012-05-20 07:19:11 +02:00
|
|
|
import java.util.Queue;
|
2012-05-31 00:35:14 +02:00
|
|
|
import java.util.concurrent.atomic.AtomicLong;
|
2012-05-20 07:19:11 +02:00
|
|
|
|
2010-03-31 04:19:45 +02:00
|
|
|
/**
|
|
|
|
* A combination of {@link HttpRequestEncoder} and {@link HttpResponseDecoder}
|
|
|
|
* which enables easier client side HTTP implementation. {@link HttpClientCodec}
|
|
|
|
* provides additional state management for <tt>HEAD</tt> and <tt>CONNECT</tt>
|
|
|
|
* requests, which {@link HttpResponseDecoder} lacks. Please refer to
|
|
|
|
* {@link HttpResponseDecoder} to learn what additional state management needs
|
|
|
|
* to be done for <tt>HEAD</tt> and <tt>CONNECT</tt> and why
|
|
|
|
* {@link HttpResponseDecoder} can not handle it by itself.
|
2012-05-31 00:35:14 +02:00
|
|
|
*
|
|
|
|
* If the {@link Channel} is closed and there are missing responses,
|
|
|
|
* a {@link PrematureChannelClosureException} is thrown.
|
|
|
|
*
|
2010-03-31 04:19:45 +02:00
|
|
|
* @see HttpServerCodec
|
|
|
|
*/
|
2016-01-10 22:19:55 +01:00
|
|
|
public final class HttpClientCodec extends CombinedChannelDuplexHandler<HttpResponseDecoder, HttpRequestEncoder>
|
|
|
|
implements HttpClientUpgradeHandler.SourceCodec {
|
2010-03-31 04:19:45 +02:00
|
|
|
|
|
|
|
/** A queue that is used for correlating a request and a response. */
|
2013-02-06 04:55:42 +01:00
|
|
|
private final Queue<HttpMethod> queue = new ArrayDeque<HttpMethod>();
|
2017-05-08 22:45:13 +02:00
|
|
|
private final boolean parseHttpAfterConnectRequest;
|
2010-03-31 04:19:45 +02:00
|
|
|
|
|
|
|
/** If true, decoding stops (i.e. pass-through) */
|
2013-06-13 07:43:39 +02:00
|
|
|
private boolean done;
|
2010-03-31 04:19:45 +02:00
|
|
|
|
2012-05-31 00:35:14 +02:00
|
|
|
private final AtomicLong requestResponseCounter = new AtomicLong();
|
|
|
|
private final boolean failOnMissingResponse;
|
|
|
|
|
2010-03-31 04:19:45 +02:00
|
|
|
/**
|
|
|
|
* Creates a new instance with the default decoder options
|
|
|
|
* ({@code maxInitialLineLength (4096}}, {@code maxHeaderSize (8192)}, and
|
|
|
|
* {@code maxChunkSize (8192)}).
|
|
|
|
*/
|
|
|
|
public HttpClientCodec() {
|
2012-05-31 00:35:14 +02:00
|
|
|
this(4096, 8192, 8192, false);
|
2010-03-31 04:19:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new instance with the specified decoder options.
|
|
|
|
*/
|
2013-02-06 04:55:42 +01:00
|
|
|
public HttpClientCodec(int maxInitialLineLength, int maxHeaderSize, int maxChunkSize) {
|
2012-05-31 00:35:14 +02:00
|
|
|
this(maxInitialLineLength, maxHeaderSize, maxChunkSize, false);
|
|
|
|
}
|
|
|
|
|
2013-11-14 08:19:49 +01:00
|
|
|
/**
|
|
|
|
* Creates a new instance with the specified decoder options.
|
|
|
|
*/
|
2012-05-31 00:35:14 +02:00
|
|
|
public HttpClientCodec(
|
2013-02-06 04:55:42 +01:00
|
|
|
int maxInitialLineLength, int maxHeaderSize, int maxChunkSize, boolean failOnMissingResponse) {
|
2013-11-14 08:19:49 +01:00
|
|
|
this(maxInitialLineLength, maxHeaderSize, maxChunkSize, failOnMissingResponse, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new instance with the specified decoder options.
|
|
|
|
*/
|
|
|
|
public HttpClientCodec(
|
|
|
|
int maxInitialLineLength, int maxHeaderSize, int maxChunkSize, boolean failOnMissingResponse,
|
|
|
|
boolean validateHeaders) {
|
2017-05-08 22:45:13 +02:00
|
|
|
this(maxInitialLineLength, maxHeaderSize, maxChunkSize, failOnMissingResponse, validateHeaders, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new instance with the specified decoder options.
|
|
|
|
*/
|
|
|
|
public HttpClientCodec(
|
|
|
|
int maxInitialLineLength, int maxHeaderSize, int maxChunkSize, boolean failOnMissingResponse,
|
|
|
|
boolean validateHeaders, boolean parseHttpAfterConnectRequest) {
|
2016-01-10 22:19:55 +01:00
|
|
|
init(new Decoder(maxInitialLineLength, maxHeaderSize, maxChunkSize, validateHeaders), new Encoder());
|
2012-05-31 00:35:14 +02:00
|
|
|
this.failOnMissingResponse = failOnMissingResponse;
|
2017-05-08 22:45:13 +02:00
|
|
|
this.parseHttpAfterConnectRequest = parseHttpAfterConnectRequest;
|
2010-03-31 04:19:45 +02:00
|
|
|
}
|
|
|
|
|
2016-02-05 23:33:57 +01:00
|
|
|
/**
|
|
|
|
* Creates a new instance with the specified decoder options.
|
|
|
|
*/
|
|
|
|
public HttpClientCodec(
|
|
|
|
int maxInitialLineLength, int maxHeaderSize, int maxChunkSize, boolean failOnMissingResponse,
|
|
|
|
boolean validateHeaders, int initialBufferSize) {
|
2017-05-08 22:45:13 +02:00
|
|
|
this(maxInitialLineLength, maxHeaderSize, maxChunkSize, failOnMissingResponse, validateHeaders,
|
|
|
|
initialBufferSize, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new instance with the specified decoder options.
|
|
|
|
*/
|
|
|
|
public HttpClientCodec(
|
|
|
|
int maxInitialLineLength, int maxHeaderSize, int maxChunkSize, boolean failOnMissingResponse,
|
|
|
|
boolean validateHeaders, int initialBufferSize, boolean parseHttpAfterConnectRequest) {
|
2016-02-05 23:33:57 +01:00
|
|
|
init(new Decoder(maxInitialLineLength, maxHeaderSize, maxChunkSize, validateHeaders, initialBufferSize),
|
|
|
|
new Encoder());
|
2017-05-08 22:45:13 +02:00
|
|
|
this.parseHttpAfterConnectRequest = parseHttpAfterConnectRequest;
|
2016-02-05 23:33:57 +01:00
|
|
|
this.failOnMissingResponse = failOnMissingResponse;
|
|
|
|
}
|
|
|
|
|
2016-01-29 03:59:51 +01:00
|
|
|
/**
|
|
|
|
* Prepares to upgrade to another protocol from HTTP. Disables the {@link Encoder}.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void prepareUpgradeFrom(ChannelHandlerContext ctx) {
|
|
|
|
((Encoder) outboundHandler()).upgraded = true;
|
|
|
|
}
|
|
|
|
|
2015-01-20 01:48:11 +01:00
|
|
|
/**
|
|
|
|
* Upgrades to another protocol from HTTP. Removes the {@link Decoder} and {@link Encoder} from
|
|
|
|
* the pipeline.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void upgradeFrom(ChannelHandlerContext ctx) {
|
2015-11-29 05:36:32 +01:00
|
|
|
final ChannelPipeline p = ctx.pipeline();
|
2016-01-10 22:19:55 +01:00
|
|
|
p.remove(this);
|
2014-02-14 04:20:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setSingleDecode(boolean singleDecode) {
|
2016-01-10 22:19:55 +01:00
|
|
|
inboundHandler().setSingleDecode(singleDecode);
|
2014-02-14 04:20:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isSingleDecode() {
|
2016-01-10 22:19:55 +01:00
|
|
|
return inboundHandler().isSingleDecode();
|
2014-02-14 04:20:25 +01:00
|
|
|
}
|
|
|
|
|
2010-03-31 04:19:45 +02:00
|
|
|
private final class Encoder extends HttpRequestEncoder {
|
2013-04-03 11:32:33 +02:00
|
|
|
|
2016-01-29 03:59:51 +01:00
|
|
|
boolean upgraded;
|
|
|
|
|
2010-03-31 04:19:45 +02:00
|
|
|
@Override
|
2012-12-25 18:54:55 +01:00
|
|
|
protected void encode(
|
2013-08-05 08:22:04 +02:00
|
|
|
ChannelHandlerContext ctx, Object msg, List<Object> out) throws Exception {
|
2016-01-29 03:59:51 +01:00
|
|
|
|
|
|
|
if (upgraded) {
|
|
|
|
out.add(ReferenceCountUtil.retain(msg));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-16 05:22:50 +01:00
|
|
|
if (msg instanceof HttpRequest && !done) {
|
2014-06-24 10:39:46 +02:00
|
|
|
queue.offer(((HttpRequest) msg).method());
|
2010-03-31 04:19:45 +02:00
|
|
|
}
|
2012-05-31 00:35:14 +02:00
|
|
|
|
2012-05-20 07:19:11 +02:00
|
|
|
super.encode(ctx, msg, out);
|
2012-05-31 00:35:14 +02:00
|
|
|
|
2017-05-08 22:45:13 +02:00
|
|
|
if (failOnMissingResponse && !done) {
|
2012-05-31 00:35:14 +02:00
|
|
|
// check if the request is chunked if so do not increment
|
2013-01-14 16:52:30 +01:00
|
|
|
if (msg instanceof LastHttpContent) {
|
2012-05-31 00:35:14 +02:00
|
|
|
// increment as its the last chunk
|
|
|
|
requestResponseCounter.incrementAndGet();
|
|
|
|
}
|
|
|
|
}
|
2010-03-31 04:19:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private final class Decoder extends HttpResponseDecoder {
|
2013-11-14 08:19:49 +01:00
|
|
|
Decoder(int maxInitialLineLength, int maxHeaderSize, int maxChunkSize, boolean validateHeaders) {
|
|
|
|
super(maxInitialLineLength, maxHeaderSize, maxChunkSize, validateHeaders);
|
2010-03-31 04:19:45 +02:00
|
|
|
}
|
|
|
|
|
2016-02-05 23:33:57 +01:00
|
|
|
Decoder(int maxInitialLineLength, int maxHeaderSize, int maxChunkSize, boolean validateHeaders,
|
|
|
|
int initialBufferSize) {
|
|
|
|
super(maxInitialLineLength, maxHeaderSize, maxChunkSize, validateHeaders, initialBufferSize);
|
|
|
|
}
|
|
|
|
|
2010-03-31 04:19:45 +02:00
|
|
|
@Override
|
2013-04-03 11:32:33 +02:00
|
|
|
protected void decode(
|
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
|
|
|
ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception {
|
2010-03-31 04:19:45 +02:00
|
|
|
if (done) {
|
2013-03-14 12:45:24 +01:00
|
|
|
int readable = actualReadableBytes();
|
|
|
|
if (readable == 0) {
|
|
|
|
// if non is readable just return null
|
|
|
|
// https://github.com/netty/netty/issues/1159
|
2013-04-03 11:32:33 +02:00
|
|
|
return;
|
2013-03-14 12:45:24 +01:00
|
|
|
}
|
2013-04-03 11:32:33 +02:00
|
|
|
out.add(buffer.readBytes(readable));
|
2010-03-31 04:19:45 +02:00
|
|
|
} else {
|
Revamp the core API to reduce memory footprint and consumption
The API changes made so far turned out to increase the memory footprint
and consumption while our intention was actually decreasing them.
Memory consumption issue:
When there are many connections which does not exchange data frequently,
the old Netty 4 API spent a lot more memory than 3 because it always
allocates per-handler buffer for each connection unless otherwise
explicitly stated by a user. In a usual real world load, a client
doesn't always send requests without pausing, so the idea of having a
buffer whose life cycle if bound to the life cycle of a connection
didn't work as expected.
Memory footprint issue:
The old Netty 4 API decreased overall memory footprint by a great deal
in many cases. It was mainly because the old Netty 4 API did not
allocate a new buffer and event object for each read. Instead, it
created a new buffer for each handler in a pipeline. This works pretty
well as long as the number of handlers in a pipeline is only a few.
However, for a highly modular application with many handlers which
handles connections which lasts for relatively short period, it actually
makes the memory footprint issue much worse.
Changes:
All in all, this is about retaining all the good changes we made in 4 so
far such as better thread model and going back to the way how we dealt
with message events in 3.
To fix the memory consumption/footprint issue mentioned above, we made a
hard decision to break the backward compatibility again with the
following changes:
- Remove MessageBuf
- Merge Buf into ByteBuf
- Merge ChannelInboundByte/MessageHandler and ChannelStateHandler into ChannelInboundHandler
- Similar changes were made to the adapter classes
- Merge ChannelOutboundByte/MessageHandler and ChannelOperationHandler into ChannelOutboundHandler
- Similar changes were made to the adapter classes
- Introduce MessageList which is similar to `MessageEvent` in Netty 3
- Replace inboundBufferUpdated(ctx) with messageReceived(ctx, MessageList)
- Replace flush(ctx, promise) with write(ctx, MessageList, promise)
- Remove ByteToByteEncoder/Decoder/Codec
- Replaced by MessageToByteEncoder<ByteBuf>, ByteToMessageDecoder<ByteBuf>, and ByteMessageCodec<ByteBuf>
- Merge EmbeddedByteChannel and EmbeddedMessageChannel into EmbeddedChannel
- Add SimpleChannelInboundHandler which is sometimes more useful than
ChannelInboundHandlerAdapter
- Bring back Channel.isWritable() from Netty 3
- Add ChannelInboundHandler.channelWritabilityChanges() event
- Add RecvByteBufAllocator configuration property
- Similar to ReceiveBufferSizePredictor in Netty 3
- Some existing configuration properties such as
DatagramChannelConfig.receivePacketSize is gone now.
- Remove suspend/resumeIntermediaryDeallocation() in ByteBuf
This change would have been impossible without @normanmaurer's help. He
fixed, ported, and improved many parts of the changes.
2013-05-28 13:40:19 +02:00
|
|
|
int oldSize = out.size();
|
|
|
|
super.decode(ctx, buffer, out);
|
2012-05-31 00:35:14 +02:00
|
|
|
if (failOnMissingResponse) {
|
Revamp the core API to reduce memory footprint and consumption
The API changes made so far turned out to increase the memory footprint
and consumption while our intention was actually decreasing them.
Memory consumption issue:
When there are many connections which does not exchange data frequently,
the old Netty 4 API spent a lot more memory than 3 because it always
allocates per-handler buffer for each connection unless otherwise
explicitly stated by a user. In a usual real world load, a client
doesn't always send requests without pausing, so the idea of having a
buffer whose life cycle if bound to the life cycle of a connection
didn't work as expected.
Memory footprint issue:
The old Netty 4 API decreased overall memory footprint by a great deal
in many cases. It was mainly because the old Netty 4 API did not
allocate a new buffer and event object for each read. Instead, it
created a new buffer for each handler in a pipeline. This works pretty
well as long as the number of handlers in a pipeline is only a few.
However, for a highly modular application with many handlers which
handles connections which lasts for relatively short period, it actually
makes the memory footprint issue much worse.
Changes:
All in all, this is about retaining all the good changes we made in 4 so
far such as better thread model and going back to the way how we dealt
with message events in 3.
To fix the memory consumption/footprint issue mentioned above, we made a
hard decision to break the backward compatibility again with the
following changes:
- Remove MessageBuf
- Merge Buf into ByteBuf
- Merge ChannelInboundByte/MessageHandler and ChannelStateHandler into ChannelInboundHandler
- Similar changes were made to the adapter classes
- Merge ChannelOutboundByte/MessageHandler and ChannelOperationHandler into ChannelOutboundHandler
- Similar changes were made to the adapter classes
- Introduce MessageList which is similar to `MessageEvent` in Netty 3
- Replace inboundBufferUpdated(ctx) with messageReceived(ctx, MessageList)
- Replace flush(ctx, promise) with write(ctx, MessageList, promise)
- Remove ByteToByteEncoder/Decoder/Codec
- Replaced by MessageToByteEncoder<ByteBuf>, ByteToMessageDecoder<ByteBuf>, and ByteMessageCodec<ByteBuf>
- Merge EmbeddedByteChannel and EmbeddedMessageChannel into EmbeddedChannel
- Add SimpleChannelInboundHandler which is sometimes more useful than
ChannelInboundHandlerAdapter
- Bring back Channel.isWritable() from Netty 3
- Add ChannelInboundHandler.channelWritabilityChanges() event
- Add RecvByteBufAllocator configuration property
- Similar to ReceiveBufferSizePredictor in Netty 3
- Some existing configuration properties such as
DatagramChannelConfig.receivePacketSize is gone now.
- Remove suspend/resumeIntermediaryDeallocation() in ByteBuf
This change would have been impossible without @normanmaurer's help. He
fixed, ported, and improved many parts of the changes.
2013-05-28 13:40:19 +02:00
|
|
|
int size = out.size();
|
|
|
|
for (int i = oldSize; i < size; i++) {
|
|
|
|
decrement(out.get(i));
|
|
|
|
}
|
2012-05-31 00:35:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void decrement(Object msg) {
|
|
|
|
if (msg == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-14 16:52:30 +01:00
|
|
|
// check if it's an Header and its transfer encoding is not chunked.
|
|
|
|
if (msg instanceof LastHttpContent) {
|
2012-05-31 00:35:14 +02:00
|
|
|
requestResponseCounter.decrementAndGet();
|
2010-03-31 04:19:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-01-16 05:22:50 +01:00
|
|
|
protected boolean isContentAlwaysEmpty(HttpMessage msg) {
|
2014-06-24 10:39:46 +02:00
|
|
|
final int statusCode = ((HttpResponse) msg).status().code();
|
2017-10-13 12:40:53 +02:00
|
|
|
if (statusCode == 100 || statusCode == 101) {
|
|
|
|
// 100-continue and 101 switching protocols response should be excluded from paired comparison.
|
2017-11-03 08:28:34 +01:00
|
|
|
// Just delegate to super method which has all the needed handling.
|
|
|
|
return super.isContentAlwaysEmpty(msg);
|
2010-10-18 10:19:03 +02:00
|
|
|
}
|
|
|
|
|
2013-01-30 07:42:18 +01:00
|
|
|
// Get the getMethod of the HTTP request that corresponds to the
|
2010-03-31 04:19:45 +02:00
|
|
|
// current response.
|
|
|
|
HttpMethod method = queue.poll();
|
|
|
|
|
2013-01-16 05:22:50 +01:00
|
|
|
char firstChar = method.name().charAt(0);
|
2010-04-16 08:53:12 +02:00
|
|
|
switch (firstChar) {
|
|
|
|
case 'H':
|
|
|
|
// According to 4.3, RFC2616:
|
2017-11-03 08:28:34 +01:00
|
|
|
// All responses to the HEAD request method MUST NOT include a
|
2010-04-16 08:53:12 +02:00
|
|
|
// message-body, even though the presence of entity-header fields
|
|
|
|
// might lead one to believe they do.
|
|
|
|
if (HttpMethod.HEAD.equals(method)) {
|
2011-08-02 01:34:56 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// The following code was inserted to work around the servers
|
|
|
|
// that behave incorrectly. It has been commented out
|
|
|
|
// because it does not work with well behaving servers.
|
|
|
|
// Please note, even if the 'Transfer-Encoding: chunked'
|
|
|
|
// header exists in the HEAD response, the response should
|
|
|
|
// have absolutely no content.
|
|
|
|
//
|
|
|
|
//// Interesting edge case:
|
|
|
|
//// Some poorly implemented servers will send a zero-byte
|
|
|
|
//// chunk if Transfer-Encoding of the response is 'chunked'.
|
|
|
|
////
|
|
|
|
//// return !msg.isChunked();
|
2010-03-31 04:19:45 +02:00
|
|
|
}
|
2010-04-16 08:53:12 +02:00
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
// Successful CONNECT request results in a response with empty body.
|
2010-10-18 10:19:03 +02:00
|
|
|
if (statusCode == 200) {
|
2010-04-16 08:53:12 +02:00
|
|
|
if (HttpMethod.CONNECT.equals(method)) {
|
2017-05-08 22:45:13 +02:00
|
|
|
// Proxy connection established - Parse HTTP only if configured by parseHttpAfterConnectRequest,
|
|
|
|
// else pass through.
|
|
|
|
if (!parseHttpAfterConnectRequest) {
|
|
|
|
done = true;
|
|
|
|
queue.clear();
|
|
|
|
}
|
2010-04-16 08:53:12 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2010-03-31 04:19:45 +02:00
|
|
|
}
|
2010-04-16 08:53:12 +02:00
|
|
|
|
2010-03-31 04:19:45 +02:00
|
|
|
return super.isContentAlwaysEmpty(msg);
|
|
|
|
}
|
2012-05-31 00:35:14 +02:00
|
|
|
|
|
|
|
@Override
|
2012-06-07 07:52:33 +02:00
|
|
|
public void channelInactive(ChannelHandlerContext ctx)
|
2012-05-31 00:35:14 +02:00
|
|
|
throws Exception {
|
|
|
|
super.channelInactive(ctx);
|
|
|
|
|
|
|
|
if (failOnMissingResponse) {
|
|
|
|
long missingResponses = requestResponseCounter.get();
|
|
|
|
if (missingResponses > 0) {
|
|
|
|
ctx.fireExceptionCaught(new PrematureChannelClosureException(
|
|
|
|
"channel gone inactive with " + missingResponses +
|
|
|
|
" missing response(s)"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-03-31 04:19:45 +02:00
|
|
|
}
|
|
|
|
}
|