Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   *
   * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
   *
   * The contents of this file are subject to the terms of either the GNU
   * General Public License Version 2 only ("GPL") or the Common Development
   * and Distribution License("CDDL") (collectively, the "License").  You
   * may not use this file except in compliance with the License.  You can
  * obtain a copy of the License at
  * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
  * or packager/legal/LICENSE.txt.  See the License for the specific
  * language governing permissions and limitations under the License.
  *
  * When distributing the software, include this License Header Notice in each
  * file and include the License file at packager/legal/LICENSE.txt.
  *
  * GPL Classpath Exception:
  * Oracle designates this particular file as subject to the "Classpath"
  * exception as provided by Oracle in the GPL Version 2 section of the License
  * file that accompanied this code.
  *
  * Modifications:
  * If applicable, add the following below the License Header, with the fields
  * enclosed by brackets [] replaced by your own identifying information:
  * "Portions Copyright [year] [name of copyright owner]"
  *
  * Contributor(s):
  * If you wish your version of this file to be governed by only the CDDL or
  * only the GPL Version 2, indicate your decision by adding "[Contributor]
  * elects to include this software in this distribution under the [CDDL or GPL
  * Version 2] license."  If you don't indicate a single choice of license, a
  * recipient has the option to distribute your version of this file under
  * either the CDDL, the GPL Version 2 or to extend the choice of license to
  * its licensees as provided above.  However, if you add GPL Version 2 code
  * and therefore, elected the GPL Version 2 license, then the option applies
  * only if the new code is made subject to such option by the copyright
  * holder.
  */
 
 package com.sun.enterprise.web.connector.grizzly.algorithms;
 
 
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'

Author(s):
Scott Oaks.
Jean-Francois Arcand
 
 public final class StateMachineAlgorithm extends StreamAlgorithmBase{
 
 
     // ---------------------------------------------- Constructor ----------/
     
     
     public StateMachineAlgorithm() {
         if (){
             try {
             } catch (Exception e) {
                  = new DummyHandler();
             }
         } else {
              = new DummyHandler();
         }      
     }
    

    
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.

Returns:
true if the algorithm determines the end of the stream.
Paran:
byteBuffer the bytes read.
 
     public boolean parse(ByteBuffer byteBuffer){               
         boolean isFound = false;
                           
          = byteBuffer.limit();
          = byteBuffer.position();
         
         // Rule a - if we know the content length, verify all bytes are read
        if (  != -1 ){
            isFound = (( + ) <= byteBuffer.position());
            
            if (isFound)
                byteBuffer.flip();
            
            return isFound;
        }
        // Rule b - If nothing, return to the Selector.
        if (byteBuffer.position() == 0)
            return false;
        if (SelectorThread.logger().isLoggable(.))
            SelectorThread.logger().log(.,dump(byteBuffer));
        
        byteBuffer.flip();        
        try {                         
            byte c;
            byte prev_c = ' ';
            
            try{
                if (  != 0 ){
                    byteBuffer.position();
                }
            } catch (RuntimeException ex){
                // If for any reason we aren't able to recover, use the bytes as
                // it is (so DoS are avoided).
                 = 0;
                 = -1;
            }
            
            // Rule c - try to determine the content-length
            while(byteBuffer.hasRemaining()) {
                c = byteBuffer.get();
                // State Machine
                // 0: no special characters have been read, but at beginning
                //	 of line
                // 1: not at beginning of line; parse until the next line
                // 2: read initial G
                // 3: read initial GE
                // 4: read initial GET -- now we know we have a GET request;
                //	   we can parse until we see \r\n\r\n
                //	   we stay state 4-7 until we're done
                // 8: read initial P
                // 9: read initial PO
                // 10: read initial POS
                // 11: read initial POST -- now we can look for content-length
                //	  we can parse until we see content-lenth
                //	   we stay in state 12-27 until we've got length
                // 28: read all of post; parse until \r\n\r\n (see state 4)
                
                // ************************************************************
                // XXX This algorithm needs to use 
                // InternalInputBuffer.parseRequestLine instead
                // ************************************************************
                switch() {
                    case 0: // looking for g or p
                        if (c == 0x47 || c == 0x67)
                             = 2;
                        else if (c == 0x50 || c == 0x70)
                             = 8;
                        else if (c != 0x0d && c != 0x0a)
                             = 1;
                        break;
                    case 1: // looking for next line
                        if (c == 0x0a || c == 0x0d)
                             = 0;
                        break;
                    case 2: // looking for e
                        if (c == 0x45 || c == 0x65)
                             = 3;
                        else  = 1;
                        break;
                    case 3: // looking for t
                        if (c == 0x54 || c == 0x74)
                             = 4;
                        else  = 1;
                        break;
                    case 4: // \r
                        if ( c == 0x0a ){
                             = 5;
                        }
                        break;
                    case 5: // \n or \r
                        if ( c == 0x0d || c == 0x0a){
                             = byteBuffer.position();
                            isFound = true;
                            return isFound;
                        }
                        else  = 4;
                        break;
                    case 8: // looking for o
                        if (c == 0x4F || c == 0x6F)
                             = 9;
                        else  = 1;
                        break;
                    case 9: // looking for s
                        if (c == 0x73 || c == 0x53)
                             = 10;
                        else  = 1;
                        break;
                    case 10: // looking for t
                        if (c == 0x74 || c == 0x54)
                             = 11;
                        else  = 1;
                        break;
                    case 11: // looking for new line
                        if (c == 0x0a || c == 0x0d)                   
                             = 12;
                        else  = 11;
                        break;
                    case 12: // looking for c
                        if (c == 0x43 || c == 0x63)
                             = 13;
                        else if (prev_c == 0x0a 
                            && byteBuffer.position() == ) {
                             = byteBuffer.position();
                            // Content-length not specified.
                            isFound = true;
                            return isFound;
                        } else  = 12;
                        break;
                    case 13: // looking for o
                        if (c == 0x4F || c == 0x6F)
                             = 14;
                        else  = 11;
                        break;
                    case 14: // looking for n
                        if (c == 0x4E || c == 0x6E)
                             = 15;
                        else  = 11;
                        break;
                    case 15: // looking for t
                        if (c == 0x54 || c == 0x74)
                             = 16;
                        else  = 11;
                        break;
                    case 16: // looking for e
                        if (c == 0x45 || c == 0x65)
                             = 17;
                        else  = 11;
                        break;
                    case 17: // looking for n
                        if (c == 0x4E || c == 0x6E)
                             = 18;
                        else  = 11;
                        break;
                    case 18: // looking for t
                        if (c == 0x54 || c == 0x74)
                             = 19;
                        else  = 11;
                        break;
                    case 19: // -
                        if (c == 0x2D || c == 0x2D)
                             = 20;
                        else  = 11;
                        break;
                    case 20: // l
                        if (c == 0x4C || c == 0x6C)
                             = 21;
                        else  = 11;
                        break;
                    case 21: // e
                        if (c == 0x45 || c == 0x65)
                             = 22;
                        else  = 11;
                        break;
                    case 22: // n
                        if (c == 0x4E || c == 0x6E)
                             = 23;
                        else  = 11;
                        break;
                    case 23: // g
                        if (c == 0x47 || c == 0x67)
                             = 24;
                        else  = 11;
                        break;
                    case 24: // t
                        if (c == 0x54 || c == 0x74)
                             = 25;
                        else  = 11;
                        break;
                    case 25: // h
                        if (c == 0x48 || c == 0x68)
                             = 26;
                        else  = 11;
                        break;
                    case 26: // :
                        if (c == 0x3a)
                             = 27;
                        else  = 11;
                        break;
                    case 27: // read length until \r
                        while (c < 0x30 || c > 0x39) {
                            // Read past the whitespace between the : 
                            // and the length
                            c = byteBuffer.get();
                        }
                        StringBuilder sb = new StringBuilder();
                        while (c >= 0x30 && c <= 0x39) {
                            sb.append((charc);
                            c = byteBuffer.get();
                        }
                         = Integer.parseInt(sb.toString());
                        // XXX: We've already read the character past the length.
                        // Can it ever not be 0x0d? Would that be an error
                        // otherwise?                        
                        if (c == 0x0d)
                             = 29;
                        else  = 28;
                        break;
                    case 28: // looking for \r
                        if (c == 0x0d)
                             = 29;
                        break;
                    case 29: // looking for \n
                        if (c == 0x0a)
                             = 30;
                        else  = 28;
                        break;
                    case 30: // looking for \r
                        if (c == 0x0d)
                             = 31;
                        else  = 28;
                        break;
                    case 31: // looking for \n
                        if (c == 0x0a){
                             = byteBuffer.position();
                            // wait until we have fully read the request body. 
                            isFound = (( + 
                                                         <= byteBuffer.limit());
                            return isFound;
                        }
                        else  = 28;
                        break;
                    default:
                        throw new IllegalArgumentException("Unexpected state");
                    }
                    prev_c = c;
                }
                
                // NEITHER A GET OR A POST
                if (  == 0 ) {
                    isFound = true;
                }
            
                return isFound;
        } catch (BufferUnderflowException bue) {
            SelectorThread.logger().log(.,"readTask.bufferunderflow"bue);
            return false;
        } finally {       
            if (  == -1 && ( > 0)){
                // This means we weren't able to able to find the content-length
                // or the end of the stream.
                 = byteBuffer.limit();
            }
            byteBuffer.limit();
            byteBuffer.position();
            
            if (isFound){
                byteBuffer.flip();
            }
        }        
    }

    
    
Return the Handler used by this algorithm.
    public Handler getHandler(){
        return ;
    }
     
    
    
Recycle this object.
    @Override
    public void recycle(){
        super.recycle();
        
         = null;
        if (  != null){
            .attachChannel(null);
        }
    }   
      
        
    
Return the full name of the class responsible for handling OP_READ.
    @Override
    public Class getReadTask(SelectorThread selectorThread){
        return com.sun.enterprise.web.connector.grizzly.XAReadTask.class;
    }
New to GrepCode? Check out our FAQ X