Top Description Inners Fields Constructors Methods
io.netty.handler.codec.http

public final Class HttpClientCodec

extends CombinedChannelDuplexHandler<HttpResponseDecoder, HttpRequestEncoder>
implements SourceCodec
Class Inheritance
All Implemented Interfaces
io.netty.handler.codec.http.HttpClientUpgradeHandler.SourceCodec
Imports
io.netty.buffer.ByteBuf, io.netty.channel.Channel, .ChannelHandlerContext, .ChannelPipeline, .CombinedChannelDuplexHandler, io.netty.handler.codec.PrematureChannelClosureException, java.util.ArrayDeque, .List, .Queue, java.util.concurrent.atomic.AtomicLong

A combination of HttpRequestEncoder and HttpResponseDecoder which enables easier client side HTTP implementation. HttpClientCodec provides additional state management for HEAD and CONNECT requests, which HttpResponseDecoder lacks. Please refer to HttpResponseDecoder to learn what additional state management needs to be done for HEAD and CONNECT and why HttpResponseDecoder can not handle it by itself.

If the Channel is closed and there are missing responses, a PrematureChannelClosureException is thrown.

Header Validation

It is recommended to always enable header validation.

Without header validation, your system can become vulnerable to CWE-113: Improper Neutralization of CRLF Sequences in HTTP Headers ('HTTP Response Splitting') .

This recommendation stands even when both peers in the HTTP exchange are trusted, as it helps with defence-in-depth.

See Also
HttpServerCodec

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
private class

Field Summary

Modifier and TypeField and Description
public static final boolean
public static final boolean
private boolean
done

If true, decoding stops (i.e. pass-through)

private final boolean
private final boolean
private final Queue<HttpMethod>
queue

A queue that is used for correlating a request and a response.

private final AtomicLong

Constructor Summary

AccessConstructor and Description
public
HttpClientCodec()

Creates a new instance with the default decoder options (maxInitialLineLength (4096), maxHeaderSize (8192), and maxChunkSize (8192)).

public
HttpClientCodec(int maxInitialLineLength, int maxHeaderSize, int maxChunkSize)

Creates a new instance with the specified decoder options.

public
HttpClientCodec(int maxInitialLineLength, int maxHeaderSize, int maxChunkSize, boolean failOnMissingResponse)

Creates a new instance with the specified decoder options.

public
HttpClientCodec(int maxInitialLineLength, int maxHeaderSize, int maxChunkSize, boolean failOnMissingResponse, boolean validateHeaders)

Deprecated Prefer the HttpClientCodec(int, int, int, boolean) constructor, to always enable header validation.
Creates a new instance with the specified decoder options.
public
HttpClientCodec(int maxInitialLineLength, int maxHeaderSize, int maxChunkSize, boolean failOnMissingResponse, boolean validateHeaders, boolean parseHttpAfterConnectRequest)

Deprecated Prefer the HttpClientCodec(HttpDecoderConfig, boolean, boolean) constructor, to always enable header validation.
Creates a new instance with the specified decoder options.
public
HttpClientCodec(int maxInitialLineLength, int maxHeaderSize, int maxChunkSize, boolean failOnMissingResponse, boolean validateHeaders, int initialBufferSize)

Deprecated Prefer the HttpClientCodec(HttpDecoderConfig, boolean, boolean) constructor, to always enable header validation.
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)

Deprecated Prefer the HttpClientCodec(HttpDecoderConfig, boolean, boolean) constructor, to always enable header validation.
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, boolean allowDuplicateContentLengths)

Deprecated Prefer the HttpClientCodec(HttpDecoderConfig, boolean, boolean) constructor, to always enable header validation.
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, boolean allowDuplicateContentLengths, boolean allowPartialChunks)

Deprecated Prefer the HttpClientCodec(HttpDecoderConfig, boolean, boolean) constructor, to always enable header validation.
Creates a new instance with the specified decoder options.
public
HttpClientCodec(HttpDecoderConfig config, boolean parseHttpAfterConnectRequest, boolean failOnMissingResponse)

Creates a new instance with the specified decoder options.

Method Summary

Modifier and TypeMethod and Description
public boolean
public void
public void
setSingleDecode(boolean singleDecode)

public void
Inherited from io.netty.channel.CombinedChannelDuplexHandler:
bindchannelActivechannelInactivechannelReadchannelReadCompletechannelRegisteredchannelUnregisteredchannelWritabilityChangedcloseconnectderegisterdisconnectexceptionCaughtflushhandlerAddedhandlerRemovedinboundHandlerinitoutboundHandlerreadremoveInboundHandlerremoveOutboundHandleruserEventTriggeredwrite

Field Detail

DEFAULT_FAIL_ON_MISSING_RESPONSEback to summary
public static final boolean DEFAULT_FAIL_ON_MISSING_RESPONSE
DEFAULT_PARSE_HTTP_AFTER_CONNECT_REQUESTback to summary
public static final boolean DEFAULT_PARSE_HTTP_AFTER_CONNECT_REQUEST
doneback to summary
private boolean done

If true, decoding stops (i.e. pass-through)

failOnMissingResponseback to summary
private final boolean failOnMissingResponse
parseHttpAfterConnectRequestback to summary
private final boolean parseHttpAfterConnectRequest
queueback to summary
private final Queue<HttpMethod> queue

A queue that is used for correlating a request and a response.

requestResponseCounterback to summary
private final AtomicLong requestResponseCounter

Constructor Detail

HttpClientCodecback to summary
public HttpClientCodec()

Creates a new instance with the default decoder options (maxInitialLineLength (4096), maxHeaderSize (8192), and maxChunkSize (8192)).

HttpClientCodecback to summary
public HttpClientCodec(int maxInitialLineLength, int maxHeaderSize, int maxChunkSize)

Creates a new instance with the specified decoder options.

HttpClientCodecback to summary
public HttpClientCodec(int maxInitialLineLength, int maxHeaderSize, int maxChunkSize, boolean failOnMissingResponse)

Creates a new instance with the specified decoder options.

HttpClientCodecback to summary
public HttpClientCodec(int maxInitialLineLength, int maxHeaderSize, int maxChunkSize, boolean failOnMissingResponse, boolean validateHeaders)

Deprecated

Prefer the HttpClientCodec(int, int, int, boolean) constructor, to always enable header validation.

Creates a new instance with the specified decoder options.

Annotations
@Deprecated
HttpClientCodecback to summary
public HttpClientCodec(int maxInitialLineLength, int maxHeaderSize, int maxChunkSize, boolean failOnMissingResponse, boolean validateHeaders, boolean parseHttpAfterConnectRequest)

Deprecated

Prefer the HttpClientCodec(HttpDecoderConfig, boolean, boolean) constructor, to always enable header validation.

Creates a new instance with the specified decoder options.

Annotations
@Deprecated
HttpClientCodecback to summary
public HttpClientCodec(int maxInitialLineLength, int maxHeaderSize, int maxChunkSize, boolean failOnMissingResponse, boolean validateHeaders, int initialBufferSize)

Deprecated

Prefer the HttpClientCodec(HttpDecoderConfig, boolean, boolean) constructor, to always enable header validation.

Creates a new instance with the specified decoder options.

Annotations
@Deprecated
HttpClientCodecback to summary
public HttpClientCodec(int maxInitialLineLength, int maxHeaderSize, int maxChunkSize, boolean failOnMissingResponse, boolean validateHeaders, int initialBufferSize, boolean parseHttpAfterConnectRequest)

Deprecated

Prefer the HttpClientCodec(HttpDecoderConfig, boolean, boolean) constructor, to always enable header validation.

Creates a new instance with the specified decoder options.

Annotations
@Deprecated
HttpClientCodecback to summary
public HttpClientCodec(int maxInitialLineLength, int maxHeaderSize, int maxChunkSize, boolean failOnMissingResponse, boolean validateHeaders, int initialBufferSize, boolean parseHttpAfterConnectRequest, boolean allowDuplicateContentLengths)

Deprecated

Prefer the HttpClientCodec(HttpDecoderConfig, boolean, boolean) constructor, to always enable header validation.

Creates a new instance with the specified decoder options.

Annotations
@Deprecated
HttpClientCodecback to summary
public HttpClientCodec(int maxInitialLineLength, int maxHeaderSize, int maxChunkSize, boolean failOnMissingResponse, boolean validateHeaders, int initialBufferSize, boolean parseHttpAfterConnectRequest, boolean allowDuplicateContentLengths, boolean allowPartialChunks)

Deprecated

Prefer the HttpClientCodec(HttpDecoderConfig, boolean, boolean) constructor, to always enable header validation.

Creates a new instance with the specified decoder options.

Annotations
@Deprecated
HttpClientCodecback to summary
public HttpClientCodec(HttpDecoderConfig config, boolean parseHttpAfterConnectRequest, boolean failOnMissingResponse)

Creates a new instance with the specified decoder options.

Method Detail

isSingleDecodeback to summary
public boolean isSingleDecode()
prepareUpgradeFromback to summary
public void prepareUpgradeFrom(ChannelHandlerContext ctx)

Implements io.netty.handler.codec.http.HttpClientUpgradeHandler.SourceCodec.prepareUpgradeFrom.

Prepares to upgrade to another protocol from HTTP. Disables the Encoder.

Annotations
@Override
setSingleDecodeback to summary
public void setSingleDecode(boolean singleDecode)
upgradeFromback to summary
public void upgradeFrom(ChannelHandlerContext ctx)

Implements io.netty.handler.codec.http.HttpClientUpgradeHandler.SourceCodec.upgradeFrom.

Upgrades to another protocol from HTTP. Removes the Decoder and Encoder from the pipeline.

Annotations
@Override
io.netty.handler.codec.http back to summary

private final Class HttpClientCodec.Decoder

extends HttpResponseDecoder
Class Inheritance

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public void
protected void
decode(ChannelHandlerContext ctx, ByteBuf
the ByteBuf from which to read data
buffer
,
List<Object>
the List to which decoded messages should be added
out
)

Overrides io.netty.handler.codec.http.HttpObjectDecoder.decode.

Decode the from one ByteBuf to an other.
private void
protected boolean
Inherited from io.netty.handler.codec.http.HttpResponseDecoder:
createInvalidMessagecreateMessageisDecodingRequest

Constructor Detail

Decoderback to summary
pack-priv Decoder(HttpDecoderConfig config)

Method Detail

channelInactiveback to summary
public void channelInactive(ChannelHandlerContext ctx) throws Exception

Overrides io.netty.handler.codec.ByteToMessageDecoder.channelInactive.

Implements io.netty.channel.ChannelInboundHandler.channelInactive.

Doc from io.netty.channel.ChannelInboundHandlerAdapter.channelInactive.

Calls ChannelHandlerContext#fireChannelInactive() to forward to the next ChannelInboundHandler in the ChannelPipeline. Sub-classes may override this method to change behavior.

Annotations
@Override
decodeback to summary
protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception

Overrides io.netty.handler.codec.http.HttpObjectDecoder.decode.

Doc from io.netty.handler.codec.ByteToMessageDecoder.decode.

Decode the from one ByteBuf to an other. This method will be called till either the input ByteBuf has nothing to read when return from this method or till nothing was read from the input ByteBuf.

Parameters
ctx:ChannelHandlerContext

the ChannelHandlerContext which this ByteToMessageDecoder belongs to

buffer:ByteBuf

the ByteBuf from which to read data

out:List<Object>

the List to which decoded messages should be added

Annotations
@Override
Exceptions
Exception:
is thrown if an error occurs
decrementback to summary
private void decrement(Object msg)
isContentAlwaysEmptyback to summary
protected boolean isContentAlwaysEmpty(HttpMessage msg)

Overrides io.netty.handler.codec.http.HttpObjectDecoder.isContentAlwaysEmpty.

Annotations
@Override
io.netty.handler.codec.http back to summary

private final Class HttpClientCodec.Encoder

extends HttpRequestEncoder
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv boolean

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
protected void
encode(ChannelHandlerContext ctx, Object
the message to encode to an other one
msg
,
List<Object>
the List into which the encoded msg should be added needs to do some kind of aggregation
out
)

Overrides io.netty.handler.codec.http.HttpObjectEncoder.encode.

Encode from one message to an other.
Inherited from io.netty.handler.codec.http.HttpRequestEncoder:
acceptOutboundMessageencodeInitialLine

Field Detail

upgradedback to summary
pack-priv boolean upgraded

Constructor Detail

Encoderback to summary
private Encoder()

Method Detail

encodeback to summary
protected void encode(ChannelHandlerContext ctx, Object msg, List<Object> out) throws Exception

Overrides io.netty.handler.codec.http.HttpObjectEncoder.encode.

Doc from io.netty.handler.codec.MessageToMessageEncoder.encode.

Encode from one message to an other. This method will be called for each written message that can be handled by this encoder.

Parameters
ctx:ChannelHandlerContext

the ChannelHandlerContext which this MessageToMessageEncoder belongs to

msg:Object

the message to encode to an other one

out:List<Object>

the List into which the encoded msg should be added needs to do some kind of aggregation

Annotations
@Override
Exceptions
Exception:
is thrown if an error occurs