}
private static boolean parseHttpChunkLength(
final HttpPacketParsing httpPacket,
final Buffer input) {
final HeaderParsingState parsingState = httpPacket.getHeaderParsingState();
while (true) {
switch (parsingState.state) {
case 0: {// Initialize chunk parsing
final int pos = input.position();
parsingState.start = pos;
parsingState.offset = pos;
parsingState.packetLimit = pos + MAX_HTTP_CHUNK_SIZE_LENGTH;
}
case 1: { // Skip heading spaces (it's not allowed by the spec, but some servers put it there)
final int nonSpaceIdx = skipSpaces(input,
parsingState.offset, parsingState.packetLimit);
if (nonSpaceIdx == -1) {
parsingState.offset = input.limit();
parsingState.state = 1;
parsingState.checkOverflow("The chunked encoding length prefix is too large");
return false;
}
parsingState.offset = nonSpaceIdx;
parsingState.state = 2;
}
case 2: { // Scan chunk size
int offset = parsingState.offset;
int limit = Math.min(parsingState.packetLimit, input.limit());
long value = parsingState.parsingNumericValue;
while (offset < limit) {
final byte b = input.get(offset);
if (isSpaceOrTab(b) || /*trailing spaces are not allowed by the spec, but some server put it there*/
b == Constants.CR || b == Constants.SEMI_COLON) {
parsingState.checkpoint = offset;
} else if (b == Constants.LF) {
final ContentParsingState contentParsingState =
httpPacket.getContentParsingState();
contentParsingState.chunkContentStart = offset + 1;
contentParsingState.chunkLength = value;
contentParsingState.chunkRemainder = value;
parsingState.state = CHUNK_LENGTH_PARSED_STATE;
return true;
} else if (parsingState.checkpoint == -1) {
if (DEC[b & 0xFF] != -1 && checkOverflow(value)) {
value = (value << 4) + (DEC[b & 0xFF]);
} else {
throw new HttpBrokenContentException("Invalid byte representing a hex value within a chunk length encountered : " + b);
}
} else {
throw new HttpBrokenContentException("Unexpected HTTP chunk header");
}
offset++;
}
parsingState.parsingNumericValue = value;
parsingState.offset = offset;
parsingState.checkOverflow("The chunked encoding length prefix is too large");
return false;
}
}
}