package org.glassfish.grizzly.http;
import org.glassfish.grizzly.Buffer;
import org.glassfish.grizzly.filterchain.FilterChainContext;
import org.glassfish.grizzly.http.HttpCodecFilter.ContentParsingState;
public final class FixedLengthTransferEncoding implements TransferEncoding {
public FixedLengthTransferEncoding() {
}
@Override
public boolean (HttpHeader httpPacket) {
final long contentLength = httpPacket.getContentLength();
return contentLength != -1;
}
@Override
public boolean (HttpHeader httpPacket) {
final long contentLength = httpPacket.getContentLength();
return contentLength != -1;
}
@Override
public void (FilterChainContext ctx, HttpHeader httpHeader, HttpContent httpContent) {
final int defaultContentLength = httpContent != null ? httpContent.getContent().remaining() : -1;
httpHeader.makeContentLengthHeader(defaultContentLength);
}
@Override
@SuppressWarnings({ "UnusedDeclaration" })
public ParsingResult (FilterChainContext ctx, HttpHeader httpPacket, Buffer input) {
final HttpPacketParsing httpPacketParsing = (HttpPacketParsing) httpPacket;
final ContentParsingState contentParsingState = httpPacketParsing.getContentParsingState();
if (contentParsingState.chunkRemainder == -1) {
contentParsingState.chunkRemainder = httpPacket.getContentLength();
}
Buffer remainder = null;
final long thisPacketRemaining = contentParsingState.chunkRemainder;
final int available = input.remaining();
if (available > thisPacketRemaining) {
remainder = input.slice((int) (input.position() + thisPacketRemaining), input.limit());
input.limit((int) (input.position() + thisPacketRemaining));
}
contentParsingState.chunkRemainder -= input.remaining();
final boolean isLast = contentParsingState.chunkRemainder == 0;
return ParsingResult.create(httpPacket.httpContentBuilder().content(input).last(isLast).build(), remainder);
}
@Override
public Buffer serializePacket(FilterChainContext ctx, HttpContent httpContent) {
return httpContent.getContent();
}
}