FileDocCategorySizeDatePackage
SeekHeaderAlgorithm.javaAPI DocGlassfish v2 API5922Fri May 04 22:37:06 BST 2007com.sun.enterprise.web.connector.grizzly.algorithms

SeekHeaderAlgorithm

public 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.
author
Jean-Francois Arcand

Fields Summary
Constructors Summary
public SeekHeaderAlgorithm()

        super();
    
Methods Summary
public booleanparse(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.

return
true if we need to call back the SelectorThread This occurs when the stream doesn't contains all the request bytes. false if the stream contains all request bytes.
paran
byteBuffer the bytes read.
return
true if the algorithm determines the end of the stream.

  
        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();
            }
        }