org.xbib.helianthus.internal.http.Http1ClientCodec Maven / Gradle / Ivy
package org.xbib.helianthus.internal.http;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.CombinedChannelDuplexHandler;
import io.netty.handler.codec.PrematureChannelClosureException;
import io.netty.handler.codec.http.HttpClientUpgradeHandler;
import io.netty.handler.codec.http.HttpMessage;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpRequestEncoder;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.HttpResponseDecoder;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.LastHttpContent;
import io.netty.util.ReferenceCountUtil;
import java.util.ArrayDeque;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.atomic.AtomicLong;
/**
* A combination of {@link HttpRequestEncoder} and {@link HttpResponseDecoder}
* which enables easier client side HTTP implementation. {@link Http1ClientCodec}
* provides additional state management for HEAD and CONNECT
* requests, which {@link HttpResponseDecoder} lacks. Please refer to
* {@link HttpResponseDecoder} to learn what additional state management needs
* to be done for HEAD and CONNECT and why
* {@link HttpResponseDecoder} can not handle it by itself.
*
* If the {@link Channel} is closed and there are missing responses,
* a {@link PrematureChannelClosureException} is thrown.
*
* @see HttpServerCodec
*/
public class Http1ClientCodec extends CombinedChannelDuplexHandler
implements HttpClientUpgradeHandler.SourceCodec {
/**
* A queue that is used for correlating a request and a response.
*/
private final Queue queue = new ArrayDeque<>();
private final AtomicLong requestResponseCounter = new AtomicLong();
private final boolean failOnMissingResponse;
/**
* If true, decoding stops (i.e. pass-through)
*/
private boolean done;
/**
* Creates a new instance with the default decoder options
* ({@code maxInitialLineLength (4096}}, {@code maxHeaderSize (8192)}, and
* {@code maxChunkSize (8192)}).
*/
public Http1ClientCodec() {
this(4096, 8192, 8192, false);
}
/**
* Creates a new instance with the specified decoder options.
*/
public Http1ClientCodec(int maxInitialLineLength, int maxHeaderSize, int maxChunkSize) {
this(maxInitialLineLength, maxHeaderSize, maxChunkSize, false);
}
/**
* Creates a new instance with the specified decoder options.
*/
public Http1ClientCodec(
int maxInitialLineLength, int maxHeaderSize, int maxChunkSize, boolean failOnMissingResponse) {
this(maxInitialLineLength, maxHeaderSize, maxChunkSize, failOnMissingResponse, true);
}
/**
* Creates a new instance with the specified decoder options.
*/
public Http1ClientCodec(
int maxInitialLineLength, int maxHeaderSize, int maxChunkSize, boolean failOnMissingResponse,
boolean validateHeaders) {
init(new Decoder(maxInitialLineLength, maxHeaderSize, maxChunkSize, validateHeaders), new Encoder());
this.failOnMissingResponse = failOnMissingResponse;
}
@Override
public void prepareUpgradeFrom(ChannelHandlerContext ctx) {
((Encoder) outboundHandler()).upgraded = true;
}
/**
* Upgrades to another protocol from HTTP. Removes the {@link Decoder} and {@link Encoder} from
* the pipeline.
*/
@Override
public void upgradeFrom(ChannelHandlerContext ctx) {
final ChannelPipeline p = ctx.pipeline();
p.remove(this);
}
public boolean isSingleDecode() {
return inboundHandler().isSingleDecode();
}
public void setSingleDecode(boolean singleDecode) {
inboundHandler().setSingleDecode(singleDecode);
}
private final class Encoder extends HttpRequestEncoder {
boolean upgraded;
@Override
protected void encode(ChannelHandlerContext ctx, Object msg, List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy