in http/src/main/java/org/apache/mina/http/HttpServerDecoder.java [81:132]
public HttpPdu decode(ByteBuffer msg, HttpDecoderState context) {
LOG.debug("decode : {}", msg);
if (msg.remaining() <= 0) {
return null;
}
switch (context.getState()) {
case HEAD:
LOG.debug("decoding HEAD");
// concat the old buffer and the new incoming one
msg = ByteBuffer.allocate(context.getPartial().remaining() + msg.remaining()).put(context.getPartial())
.put(msg);
msg.flip();
// now let's decode like it was a new message
case NEW:
LOG.debug("decoding NEW");
HttpRequestImpl rq = parseHttpRequestHead(msg);
if (rq == null) {
// we copy the incoming BB because it's going to be recycled by the inner IoProcessor for next reads
context.setPartial(ByteBuffer.allocate(msg.remaining()));
context.getPartial().put(msg);
context.getPartial().flip();
} else {
return rq;
}
return null;
case BODY:
LOG.debug("decoding BODY");
int chunkSize = msg.remaining();
// send the chunk of body
HttpContentChunk chunk = new HttpContentChunk(msg);
// do we have reach end of body ?
context.setRemainingBytes(context.getRemainingBytes() - chunkSize);
if (context.getRemainingBytes() <= 0) {
LOG.debug("end of HTTP body");
context.setState(DecoderState.NEW);
context.setRemainingBytes(0);
context.setState(DecoderState.DONE);
return chunk;
}
break;
case DONE:
return new HttpEndOfContent();
default:
throw new IllegalStateException("Unknonwn decoder state : " + context.getState());
}
return null;
}