tests/utils/monitor/org/apache/test/Response.java
/**
 * @param string the response message - HTTPheaders
 * @return correctly formatted (as best we can) http headers.
 */
@param string the response message - HTTPheaders
@return correctly formatted (as best we can) http headers.
@param string the response message - HTTPheaders @return correctly formatted ( as best we can ) http headers .
private String correctChunkedData(String request)
{
    StringBuffer theCorrectedResponse = new StringBuffer();
    String theChunkStartPatternREGEX = "(\\p{XDigit}+)("+CR+"?"+LF+")";
    String theNextChunkSizeREGEX ="("+CR+"?"+LF+")"+theChunkStartPatternREGEX;
    Pattern chunkStartPattern=Pattern.compile(theChunkStartPatternREGEX);
    Pattern theNextChunkSizePattern = Pattern.compile(theNextChunkSizeREGEX);
    Matcher chunkStartMatcher = chunkStartPattern.matcher(request);
    Matcher theNextChunkSizeMatcher = theNextChunkSizePattern.matcher(request);    
    int chunkStart = 0;
    int chunkEnd=0;
    int chunkSize = 0;
    int nextChunkSize = -1;
    int nextChunkStart= -1;
    int nextChunkEnd= -1;
    String nextChunkSizeString="";
    while(chunkStartMatcher.find() && nextChunkSize!=0)
    {
        String chunkSizeString=""; 
        if(nextChunkSize==-1)
        {
            chunkSizeString = request.substring(chunkStartMatcher.start(), chunkStartMatcher.end());
            chunkSize = Integer.parseInt(chunkSizeString.trim(), 16);
            chunkStart = chunkStartMatcher.end();
            chunkSizeString = chunkSizeString.trim()+CRLF;
        }
        else
        {
            chunkSizeString = nextChunkSizeString;
            chunkSize = nextChunkSize;
            chunkStart = nextChunkStart;
            chunkSizeString = CRLF+chunkSizeString.trim()+CRLF;
        }
        String chunkFromSize=null;
        if(chunkStart+chunkSize>request.length())
        {
            System.err.println( "ChunksSize String is wrong");
            System.err.println( "chunksStart = "+chunkStart);
            System.err.println( "ChunkSize = "+chunkSize);
            System.err.println( "request.length = "+request.length());
        }
        else
            chunkFromSize = request.substring(chunkStart, chunkStart+chunkSize);
        String chunk=""; 
        int endOfThisChunk;
        if(!theNextChunkSizeMatcher.find())
            endOfThisChunk = request.length()-1;
        else
        {
            chunkEnd = theNextChunkSizeMatcher.start();
            nextChunkSizeString = request.substring(theNextChunkSizeMatcher.start(), theNextChunkSizeMatcher.end());
            nextChunkStart = theNextChunkSizeMatcher.end();
            nextChunkSize = Integer.parseInt(nextChunkSizeString.trim(), 16);
            endOfThisChunk = theNextChunkSizeMatcher.start();
        }
        chunk = request.substring(chunkStart, endOfThisChunk);
        if(chunkFromSize==null)
        {
            chunkFromSize = chunk;
            chunkSize=chunk.length();
            System.err.println( "Guessing the size is "+chunkFromSize.length());
            if(!chunkSizeString.startsWith(""+CR))
            {
                chunkSizeString = ""+Integer.toHexString(chunk.length())+CRLF;
            }
            else
                chunkSizeString = CRLF+Integer.toHexString(chunk.length())+CRLF;
        }
        if(chunk.length() !=chunkSize)
        {
            System.out.println( "Warning: Chunksize is not correct");
            System.out.println( "chunk.length = "+chunk.length());
            System.out.println( "ChunkSize = "+chunkSize);
            System.out.println( "chunkString "+chunkSizeString);
            if (System.getProperty("os.name").toLowerCase( ).startsWith(
            "windows"))
            {
                System.out.println( "Warning: Chunksize is not correct. On windows so not attempting to fix");
            }
            else
            {
                System.out.println( "Warning: Chunksize is not correct. Attempting to correct with additional \\r\\n");
            	String newChunk = chunk.replaceAll(""+LF, CRLF);
            	if(newChunk.length()+chunkSizeString.trim().length()+2 !=chunkSize)
            	{
            	    System.err.println( "Attempt to correct the chunksize failed");
            	}
            }
        }
        theCorrectedResponse.append(chunkSizeString);
        theCorrectedResponse.append(chunk);
    }
    if(theCorrectedResponse.charAt(theCorrectedResponse.length()-1) == LF)
    {
        theCorrectedResponse.deleteCharAt(theCorrectedResponse.length()-1);
    }
    theCorrectedResponse.append(CRLF+"0"+DOUBLE_CRLF);
    return theCorrectedResponse.toString();
}
private String correctChunkedData ( String request ) { StringBuffer theCorrectedResponse = new StringBuffer ( ) ; String theChunkStartPatternREGEX = "(\\p{XDigit}+)(" + CR + "?" + LF + ")" ; String theNextChunkSizeREGEX = "(" + CR + "?" + LF + ")" + theChunkStartPatternREGEX ; Pattern chunkStartPattern = Pattern . compile ( theChunkStartPatternREGEX ) ; Pattern theNextChunkSizePattern = Pattern . compile ( theNextChunkSizeREGEX ) ; Matcher chunkStartMatcher = chunkStartPattern . matcher ( request ) ; Matcher theNextChunkSizeMatcher = theNextChunkSizePattern . matcher ( request ) ; int chunkStart = 0 ; int chunkEnd = 0 ; int chunkSize = 0 ; int nextChunkSize = - 1 ; int nextChunkStart = - 1 ; int nextChunkEnd = - 1 ; String nextChunkSizeString = "" ; while ( chunkStartMatcher . find ( ) && nextChunkSize != 0 ) { String chunkSizeString = "" ; if ( nextChunkSize == - 1 ) { chunkSizeString = request . substring ( chunkStartMatcher . start ( ) , chunkStartMatcher . end ( ) ) ; chunkSize = Integer . parseInt ( chunkSizeString . trim ( ) , 16 ) ; chunkStart = chunkStartMatcher . end ( ) ; chunkSizeString = chunkSizeString . trim ( ) + CRLF ; } else { chunkSizeString = nextChunkSizeString ; chunkSize = nextChunkSize ; chunkStart = nextChunkStart ; chunkSizeString = CRLF + chunkSizeString . trim ( ) + CRLF ; } String chunkFromSize = null ; if ( chunkStart + chunkSize > request . length ( ) ) { System . err . println ( "ChunksSize String is wrong" ) ; System . err . println ( "chunksStart = " + chunkStart ) ; System . err . println ( "ChunkSize = " + chunkSize ) ; System . err . println ( "request.length = " + request . length ( ) ) ; } else chunkFromSize = request . substring ( chunkStart , chunkStart + chunkSize ) ; String chunk = "" ; int endOfThisChunk ; if ( ! theNextChunkSizeMatcher . find ( ) ) endOfThisChunk = request . length ( ) - 1 ; else { chunkEnd = theNextChunkSizeMatcher . start ( ) ; nextChunkSizeString = request . substring ( theNextChunkSizeMatcher . start ( ) , theNextChunkSizeMatcher . end ( ) ) ; nextChunkStart = theNextChunkSizeMatcher . end ( ) ; nextChunkSize = Integer . parseInt ( nextChunkSizeString . trim ( ) , 16 ) ; endOfThisChunk = theNextChunkSizeMatcher . start ( ) ; } chunk = request . substring ( chunkStart , endOfThisChunk ) ; if ( chunkFromSize == null ) { chunkFromSize = chunk ; chunkSize = chunk . length ( ) ; System . err . println ( "Guessing the size is " + chunkFromSize . length ( ) ) ; if ( ! chunkSizeString . startsWith ( "" + CR ) ) { chunkSizeString = "" + Integer . toHexString ( chunk . length ( ) ) + CRLF ; } else chunkSizeString = CRLF + Integer . toHexString ( chunk . length ( ) ) + CRLF ; } if ( chunk . length ( ) != chunkSize ) { System . out . println ( "Warning: Chunksize is not correct" ) ; System . out . println ( "chunk.length = " + chunk . length ( ) ) ; System . out . println ( "ChunkSize = " + chunkSize ) ; System . out . println ( "chunkString " + chunkSizeString ) ; if ( System . getProperty ( "os.name" ) . toLowerCase ( ) . startsWith ( "windows" ) ) { System . out . println ( "Warning: Chunksize is not correct. On windows so not attempting to fix" ) ; } else { System . out . println ( "Warning: Chunksize is not correct. Attempting to correct with additional \\r\\n" ) ; String newChunk = chunk . replaceAll ( "" + LF , CRLF ) ; if ( newChunk . length ( ) + chunkSizeString . trim ( ) . length ( ) + 2 != chunkSize ) { System . err . println ( "Attempt to correct the chunksize failed" ) ; } } } theCorrectedResponse . append ( chunkSizeString ) ; theCorrectedResponse . append ( chunk ) ; } if ( theCorrectedResponse . charAt ( theCorrectedResponse . length ( ) - 1 ) == LF ) { theCorrectedResponse . deleteCharAt ( theCorrectedResponse . length ( ) - 1 ) ; } theCorrectedResponse . append ( CRLF + "0" + DOUBLE_CRLF ) ; return theCorrectedResponse . toString ( ) ; }
@param string the response message - HTTPheaders
 @return correctly formatted (as best we can) http headers.
@param string the response message - HTTPheaders @return correctly formatted ( as best we can ) http headers .
// A chunk Is defined by 
 // <hexnumber><potentially CR><LF><any data><potentially CR><LF><hexnumber>
 // means this is the first time round the loop so work it out
 // ensure the string has the correct crlf on it
 // we've already been round the loop and got the chunkSize
 // ensure the string has the correct crlf on it
 // this one came with a different parsing algorithm so add on crlf to *both* ends !
 //        System.out.println( "chunkString = "+chunkSizeString.trim());
 //        System.out.println( "Chunk size = "+chunkSize);
 //        System.out.println( "Chunked Start="+chunkStart);
 //        System.out.println( "chunkend = "+chunkEnd);
 // We can work this out though it's probably the last or only chunk so just guess !
 // do this later on so we don't duplicate work> 
 // chunkFromSize is null so we can see later on that we need to guess
 // Now find the end of the chunk (and while we do that we actually find the next chunk size too !
 // get the nextchunkSize while we're here!
 // we need to set this value now we have probably worked out what it is
 // this stops us from stopping but hopefully we won't fail too !
 // we now also have to take into account the chunkSize and it's \r\n
 // test it again to see whether we made it right or not
 // make sure we set the chunksize string
 // Then it's the beginning of the message
 // Check that the chunk size really is the same size as it should be.
 // then we might be on a unix system and lost the CR of a CRLF combo
 // append the chunk to the rest of the correct messages
 // append the last chunk on 
 // sometimes the chunk already has a \n on it so we need to remove it
