SeekHeaderAlgorithmpublic final class SeekHeaderAlgorithm extends ContentLengthAlgorithm Predict if the NIO channel has been fully read or not. This lagorithm will
first search for the content-length header, and use that value to determine if
the bytes has been fully read or not. If the content-length isn't included,
it will search for the end of the HTTP stream, which is a '\r\n' without
buffering the body. |
Constructors Summary |
---|
public SeekHeaderAlgorithm()
super();
|
Methods Summary |
---|
public boolean | parse(java.nio.ByteBuffer byteBuffer)Parse the ByteBuffer and try to determine if the bytes
stream has been fully read from the SocketChannel .
Drain the SocketChannel and determine if the request bytes
has been fully read. For POST method, parse the bytes and seek for the
content-type header to determine the length of the request bytes.
isFound = false;
curLimit = byteBuffer.limit();
curPosition = byteBuffer.position();
// Rule a - if we know the content length, verify all bytes are read
// Return true only if all bytes has been read.
if ( contentLength != -1 ){
byteBuffer.flip();
return isFound;
}
try{
// Rule b - If nothing, return to the Selector.
if (byteBuffer.position() == 0)
return false;
byteBuffer.flip();
lastValid = byteBuffer.limit();
// Rule c - Parse the request line
if ( !requestLineParsed ) {
requestLineParsed = parseRequestLine(byteBuffer);
if ( !requestLineParsed ) {
return false;
}
}
// Rule d -- Parse the headers, looking for content-length
while (parseHeader(byteBuffer));
return isFound;
} catch (BufferUnderflowException bue) {
SelectorThread.logger().log(Level.SEVERE,
"readTask.bufferunderflow", bue);
return false;
} finally {
byteBuffer.limit(curLimit);
byteBuffer.position(curPosition);
if (isFound){
byteBuffer.flip();
}
}
|
|