}
if( header.isChunked() ){
state = STATE_READING_CHUNKS;
chunkState = READING_CHUNK_HEADER;
return new HttpResponse( header, new byte[0] );
}
if( header.isConnectionCloseSet() && header.getHeader( HttpHeader.CONTENT_LENGTH ) == null ){
log.trace( "{} Switching to STATE_CLOSE_CONNECTION", this );
state = STATE_CLOSE_CONNECTION;
}
if( header.getContentLength() == 0 ){
state = STATE_READING_HEADER;
log.debug( "Readirect or the like, with no content, reading header completed!" );
return new HttpResponse( header, new byte[0] );
}
}
if( header == null )
throw new ProtocolException("No header available, though state shows being AFTER reading-header...");
if( header != null && state == STATE_CLOSE_CONNECTION ){
//
// ok, the server closes the connection, thus the body contains all data
// that will be sent until we read 0/EOF bytes...
//
ByteBuffer b = ByteBuffer.allocate( 2 * 1024 ); // we try to read 2k-chunks...
int bytes = in.read( b );
if( bytes > 0 ){
//
// if we read some bytes these will be saved as chunks, as there
// MIGHT some more following...
//
b.flip();
chunks.add( b );
return null;
} else {
//
// as no more bytes can be read, we assume the channel to be empty...
// the only exception from this is: if we did not read ANY bytes until
// now, we assume there will at least be some more coming...
//
if( chunks.isEmpty() )
return new HttpResponse( header, new byte[0] );
int size = 0;
for( ByteBuffer bb : chunks )
size += bb.limit();
// no we merge all the stuff we previously read into the final message body:
//
ByteBuffer body = ByteBuffer.allocate( size );
for( ByteBuffer bb : chunks )
body.put( bb );
log.info(this + " Completed reading body: " + size + " bytes read.");
resNum++;
return new HttpResponse( header, body.array() );
}
}
if( state == STATE_READING_BODY ){
if( header.getContentLength() > 0 ){
ByteBuffer body = readBody( header.getContentLength() );
if( body != null ){
resNum++;
return new HttpResponse( header, body.array() );
}
}
if( header.isChunked() )
state = STATE_READING_CHUNKS;