package org.apache.http.impl.nio.codecs;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.ConnectionClosedException;
import org.apache.http.Header;
import org.apache.http.MalformedChunkCodingException;
import org.apache.http.MessageConstraintException;
import org.apache.http.ParseException;
import org.apache.http.TruncatedChunkException;
import org.apache.http.config.MessageConstraints;
import org.apache.http.impl.io.HttpTransportMetricsImpl;
import org.apache.http.message.BufferedHeader;
import org.apache.http.nio.reactor.SessionInputBuffer;
import org.apache.http.util.Args;
import org.apache.http.util.CharArrayBuffer;
public class ChunkDecoder extends AbstractContentDecoder {
private static final int READ_CONTENT = 0;
private static final int = 1;
private static final int COMPLETED = 2;
private int state;
private boolean endOfChunk;
private boolean endOfStream;
private CharArrayBuffer lineBuf;
private long chunkSize;
private long pos;
private final MessageConstraints constraints;
private final List<CharArrayBuffer> trailerBufs;
private Header[] ;
public ChunkDecoder(
final ReadableByteChannel channel,
final SessionInputBuffer buffer,
final MessageConstraints constraints,
final HttpTransportMetricsImpl metrics) {
super(channel, buffer, metrics);
this.state = READ_CONTENT;
this.chunkSize = -1L;
this.pos = 0L;
this.endOfChunk = false;
this.endOfStream = false;
this.constraints = constraints != null ? constraints : MessageConstraints.DEFAULT;
this.trailerBufs = new ArrayList<CharArrayBuffer>();
}
public ChunkDecoder(
final ReadableByteChannel channel,
final SessionInputBuffer buffer,
final HttpTransportMetricsImpl metrics) {
this(channel, buffer, null, metrics);
}
private void readChunkHead() throws IOException {
if (this.lineBuf == null) {
this.lineBuf = new CharArrayBuffer(32);
} else {
this.lineBuf.clear();
}
if (this.endOfChunk) {
if (this.buffer.readLine(this.lineBuf, this.endOfStream)) {
if (!this.lineBuf.isEmpty()) {
throw new MalformedChunkCodingException("CRLF expected at end of chunk");
}
} else {
if (this.buffer.length() > 2 || this.endOfStream) {
throw new MalformedChunkCodingException("CRLF expected at end of chunk");
}
return;
}
this.endOfChunk = false;
}
final boolean lineComplete = this.buffer.readLine(this.lineBuf, this.endOfStream);
final int maxLineLen = this.constraints.getMaxLineLength();
if (maxLineLen > 0 &&
(this.lineBuf.length() > maxLineLen ||
(!lineComplete && this.buffer.length() > maxLineLen))) {
throw new MessageConstraintException("Maximum line length limit exceeded");
}
if (lineComplete) {
int separator = this.lineBuf.indexOf(';');
if (separator < 0) {
separator = this.lineBuf.length();
}
final String s = this.lineBuf.substringTrimmed(0, separator);
try {
this.chunkSize = Long.parseLong(s, 16);
} catch (final NumberFormatException e) {
throw new MalformedChunkCodingException("Bad chunk header: " + s);
}
this.pos = 0L;
} else if (this.endOfStream) {
throw new ConnectionClosedException(
"Premature end of chunk coded message body: closing chunk expected");
}
}
private void () throws IOException {
final CharArrayBuffer current = this.lineBuf;
final int count = this.trailerBufs.size();
if ((this.lineBuf.charAt(0) == ' ' || this.lineBuf.charAt(0) == '\t') && count > 0) {
final CharArrayBuffer previous = this.trailerBufs.get(count - 1);
int i = 0;
while (i < current.length()) {
final char ch = current.charAt(i);
if (ch != ' ' && ch != '\t') {
break;
}
i++;
}
final int maxLineLen = this.constraints.getMaxLineLength();
if (maxLineLen > 0 && previous.length() + 1 + current.length() - i > maxLineLen) {
throw new MessageConstraintException("Maximum line length limit exceeded");
}
previous.append(' ');
previous.append(current, i, current.length() - i);
} else {
this.trailerBufs.add(current);
this.lineBuf = null;
}
}
private void () throws IOException {
final int count = this.trailerBufs.size();
if (count > 0) {
this.footers = new Header[this.trailerBufs.size()];
for (int i = 0; i < this.trailerBufs.size(); i++) {
try {
this.footers[i] = new BufferedHeader(this.trailerBufs.get(i));
} catch (final ParseException ex) {
throw new IOException(ex);
}
}
}
this.trailerBufs.clear();
}
@Override
public int read(final ByteBuffer dst) throws IOException {
Args.notNull(dst, "Byte buffer");
if (this.state == COMPLETED) {
return -1;
}
int totalRead = 0;
while (this.state != COMPLETED) {
if (!this.buffer.hasData() || this.chunkSize == -1L) {
final int bytesRead = fillBufferFromChannel();
if (bytesRead == -1) {
this.endOfStream = true;
}
}
switch (this.state) {
case READ_CONTENT:
if (this.chunkSize == -1L) {
readChunkHead();
if (this.chunkSize == -1L) {
return totalRead;
}
if (this.chunkSize == 0L) {
this.chunkSize = -1L;
this.state = READ_FOOTERS;
break;
}
}
final long maxLen = this.chunkSize - this.pos;
final int len = this.buffer.read(dst, (int) Math.min(maxLen, Integer.MAX_VALUE));
if (len > 0) {
this.pos += len;
totalRead += len;
} else {
if (!this.buffer.hasData() && this.endOfStream) {
this.state = COMPLETED;
setCompleted();
throw new TruncatedChunkException(
"Truncated chunk (expected size: %,d; actual size: %,d)",
chunkSize, pos);
}
}
if (this.pos == this.chunkSize) {
this.chunkSize = -1L;
this.pos = 0L;
this.endOfChunk = true;
break;
}
return totalRead;
case READ_FOOTERS:
if (this.lineBuf == null) {
this.lineBuf = new CharArrayBuffer(32);
} else {
this.lineBuf.clear();
}
if (!this.buffer.readLine(this.lineBuf, this.endOfStream)) {
if (this.endOfStream) {
this.state = COMPLETED;
setCompleted();
}
return totalRead;
}
if (this.lineBuf.length() > 0) {
final int maxHeaderCount = this.constraints.getMaxHeaderCount();
if (maxHeaderCount > 0 && trailerBufs.size() >= maxHeaderCount) {
throw new MessageConstraintException("Maximum header count exceeded");
}
parseHeader();
} else {
this.state = COMPLETED;
setCompleted();
processFooters();
}
break;
}
}
return totalRead;
}
public Header[] () {
return this.footers != null ? this.footers.clone() : new Header[] {};
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append("[chunk-coded; completed: ");
sb.append(this.completed);
sb.append("]");
return sb.toString();
}
}