common/src/main/java/HTTPClient/Cookie.java
/**
     * Parses the Set-Cookie header into an array of Cookies.
     *
     * @param set_cookie the Set-Cookie header received from the server
     * @param req the request used
     * @return an array of Cookies as parsed from the Set-Cookie header
     * @exception ProtocolException if an error occurs during parsing
     */
Parses the Set-Cookie header into an array of Cookies.
@param set_cookie the Set-Cookie header received from the server
@param req the request used
@return an array of Cookies as parsed from the Set-Cookie header
@exception ProtocolException if an error occurs during parsing
Parses the Set - Cookie header into an array of Cookies . @param set_cookie the Set - Cookie header received from the server @param req the request used @return an array of Cookies as parsed from the Set - Cookie header @exception ProtocolException if an error occurs during parsing
protected static Cookie[] parse(String set_cookie, RoRequest req)
		throws ProtocolException
    {
        int    beg = 0,
               end = 0,
	       start = 0;
        if (set_cookie.toLowerCase().indexOf("httponly") != -1) {
           We remove all instances of "HttpOnly," that follow a semi-colon,
           and all instances of HttpOnly following a semi-colon at the end of
           the cookie. This leaves something that is more conventional
           This shouldn't break any valid cookies.
					set_cookie = set_cookie.replaceAll(";(?i)\\s*HttpOnly,",";,");
          set_cookie = set_cookie.replaceAll(";(?i)\\s*HttpOnly$",";");
          set_cookie = set_cookie.replaceAll(";(?i)\\s*HttpOnly; (?i)\\s*secure$",";");
          set_cookie = set_cookie.replaceAll(";(?i)\\s*HttpOnly;",";");
        }
        char[] buf = set_cookie.toCharArray();
        int    len = buf.length;
        Cookie cookie_arr[] = new Cookie[0], curr;
        cookies: while (true)                     get all cookies
        {
            beg = Util.skipSpace(buf, beg);
            if (beg >= len)  break;	 no more left
	    if (buf[beg] == ',')	 empty header
	    {
		beg++;
		continue;
	    }
	    curr  = new Cookie(req);
	    start = beg;
	     get cookie name and value first
	    end = set_cookie.indexOf('=', beg);
	    if (end == -1)
		throw new ProtocolException("Bad Set-Cookie header: " +
					    set_cookie + "\nNo '=' found " +
					    "for token starting at " +
					    "position " + beg);
	    curr.name = set_cookie.substring(beg, end).trim(); 
	    beg = Util.skipSpace(buf, end+1);
	    int comma = set_cookie.indexOf(',', beg);
	    int semic = set_cookie.indexOf(';', beg);
	    if (comma == -1  &&  semic == -1)  end = len;
	    else if (comma == -1)  end = semic;
	    else if (semic == -1)  end = comma;
	    else
	    {
		if (comma > semic)
		    end = semic;
		else
		{
		     try to handle broken servers which put commas
		     into cookie values
		    int eq = set_cookie.indexOf('=', comma);
		    if (eq > 0  &&  eq < semic)
			end = set_cookie.lastIndexOf(',', eq);
		    else
			end = semic;
		}
	    }
	    curr.value = set_cookie.substring(beg, end).trim();
	    beg = end;
	     now parse attributes
	    boolean legal = true;
	    parts: while (true)			 parse all parts
	    {
		if (beg >= len  ||  buf[beg] == ',')  break;
		 skip empty fields
		if (buf[beg] == ';')
		{
		    beg = Util.skipSpace(buf, beg+1);
		    continue;
		}
		 first check for secure, as this is the only one w/o a '='
		if ((beg+6 <= len)  &&
		    set_cookie.regionMatches(true, beg, "secure", 0, 6))
		{
		    curr.secure = true;
		    beg += 6;
		    beg = Util.skipSpace(buf, beg);
		    if (beg < len  &&  buf[beg] == ';')	 consume ";"
			beg = Util.skipSpace(buf, beg+1);
		    else if (beg < len  &&  buf[beg] != ',')
			throw new ProtocolException("Bad Set-Cookie header: " +
						    set_cookie + "\nExpected " +
						    "';' or ',' at position " +
						    beg);
		    continue;
		}
		 alright, must now be of the form x=y
		end = set_cookie.indexOf('=', beg);
		if (end == -1)
		    throw new ProtocolException("Bad Set-Cookie header: " +
						set_cookie + "\nNo '=' found " +
						"for token starting at " +
						"position " + beg);
		String name = set_cookie.substring(beg, end).trim();
		beg = Util.skipSpace(buf, end+1);
		if (name.equalsIgnoreCase("expires"))
		{
		    /* Netscape ignores quotes around the date, and some twits
		     * actually send that...
		     */
		    if (set_cookie.charAt(beg) == '\"')
			beg = Util.skipSpace(buf, beg+1);
		    /* cut off the weekday if it is there. This is a little
		     * tricky because the comma is also used between cookies
		     * themselves. To make sure we don't inadvertantly
		     * mistake a date for a weekday we only skip letters.
		     */
		    int pos = beg;
		    while (pos < len  &&
			   (buf[pos] >= 'a'  &&  buf[pos] <= 'z'  ||
			    buf[pos] >= 'A'  &&  buf[pos] <= 'Z'))
			pos++;
		    pos = Util.skipSpace(buf, pos);
		    if (pos < len  &&  buf[pos] == ','  &&  pos > beg)
			beg = pos+1;
		}
		comma = set_cookie.indexOf(',', beg);
		semic = set_cookie.indexOf(';', beg);
		if (comma == -1  &&  semic == -1)  end = len;
		else if (comma == -1)  end = semic;
		else if (semic == -1)  end = comma;
		else end = Math.min(comma, semic);
		String value = set_cookie.substring(beg, end).trim();
		legal &= setAttribute(curr, name, value, set_cookie);
		beg = end;
		if (beg < len  &&  buf[beg] == ';')	 consume ";"
		    beg = Util.skipSpace(buf, beg+1);
	    }
	    if (legal)
	    {
		cookie_arr = Util.resizeArray(cookie_arr, cookie_arr.length+1);
		cookie_arr[cookie_arr.length-1] = curr;
	    } else
		Log.write(Log.COOKI, "Cooki: Ignoring cookie: " + curr);
	}
	return cookie_arr;
    }
protected static Cookie [ ] parse ( String set_cookie , RoRequest req ) throws ProtocolException { int beg = 0 , end = 0 , start = 0 ; if ( set_cookie . toLowerCase ( ) . indexOf ( "httponly" ) != - 1 ) { set_cookie = set_cookie . replaceAll ( ";(?i)\\s*HttpOnly," , ";," ) ; set_cookie = set_cookie . replaceAll ( ";(?i)\\s*HttpOnly$" , ";" ) ; set_cookie = set_cookie . replaceAll ( ";(?i)\\s*HttpOnly; (?i)\\s*secure$" , ";" ) ; set_cookie = set_cookie . replaceAll ( ";(?i)\\s*HttpOnly;" , ";" ) ; } char [ ] buf = set_cookie . toCharArray ( ) ; int len = buf . length ; Cookie cookie_arr [ ] = new Cookie [ 0 ] , curr ; cookies : while ( true ) { beg = Util . skipSpace ( buf , beg ) ; if ( beg >= len ) break ; if ( buf [ beg ] == ',' ) { beg ++ ; continue ; } curr = new Cookie ( req ) ; start = beg ; end = set_cookie . indexOf ( '=' , beg ) ; if ( end == - 1 ) throw new ProtocolException ( "Bad Set-Cookie header: " + set_cookie + "\nNo '=' found " + "for token starting at " + "position " + beg ) ; curr . name = set_cookie . substring ( beg , end ) . trim ( ) ; beg = Util . skipSpace ( buf , end + 1 ) ; int comma = set_cookie . indexOf ( ',' , beg ) ; int semic = set_cookie . indexOf ( ';' , beg ) ; if ( comma == - 1 && semic == - 1 ) end = len ; else if ( comma == - 1 ) end = semic ; else if ( semic == - 1 ) end = comma ; else { if ( comma > semic ) end = semic ; else { int eq = set_cookie . indexOf ( '=' , comma ) ; if ( eq > 0 && eq < semic ) end = set_cookie . lastIndexOf ( ',' , eq ) ; else end = semic ; } } curr . value = set_cookie . substring ( beg , end ) . trim ( ) ; beg = end ; boolean legal = true ; parts : while ( true ) { if ( beg >= len || buf [ beg ] == ',' ) break ; if ( buf [ beg ] == ';' ) { beg = Util . skipSpace ( buf , beg + 1 ) ; continue ; } if ( ( beg + 6 <= len ) && set_cookie . regionMatches ( true , beg , "secure" , 0 , 6 ) ) { curr . secure = true ; beg += 6 ; beg = Util . skipSpace ( buf , beg ) ; if ( beg < len && buf [ beg ] == ';' ) beg = Util . skipSpace ( buf , beg + 1 ) ; else if ( beg < len && buf [ beg ] != ',' ) throw new ProtocolException ( "Bad Set-Cookie header: " + set_cookie + "\nExpected " + "';' or ',' at position " + beg ) ; continue ; } end = set_cookie . indexOf ( '=' , beg ) ; if ( end == - 1 ) throw new ProtocolException ( "Bad Set-Cookie header: " + set_cookie + "\nNo '=' found " + "for token starting at " + "position " + beg ) ; String name = set_cookie . substring ( beg , end ) . trim ( ) ; beg = Util . skipSpace ( buf , end + 1 ) ; if ( name . equalsIgnoreCase ( "expires" ) ) { /* Netscape ignores quotes around the date, and some twits
		     * actually send that...
		     */ if ( set_cookie . charAt ( beg ) == '\"' ) beg = Util . skipSpace ( buf , beg + 1 ) ; /* cut off the weekday if it is there. This is a little
		     * tricky because the comma is also used between cookies
		     * themselves. To make sure we don't inadvertantly
		     * mistake a date for a weekday we only skip letters.
		     */ int pos = beg ; while ( pos < len && ( buf [ pos ] >= 'a' && buf [ pos ] <= 'z' || buf [ pos ] >= 'A' && buf [ pos ] <= 'Z' ) ) pos ++ ; pos = Util . skipSpace ( buf , pos ) ; if ( pos < len && buf [ pos ] == ',' && pos > beg ) beg = pos + 1 ; } comma = set_cookie . indexOf ( ',' , beg ) ; semic = set_cookie . indexOf ( ';' , beg ) ; if ( comma == - 1 && semic == - 1 ) end = len ; else if ( comma == - 1 ) end = semic ; else if ( semic == - 1 ) end = comma ; else end = Math . min ( comma , semic ) ; String value = set_cookie . substring ( beg , end ) . trim ( ) ; legal &= setAttribute ( curr , name , value , set_cookie ) ; beg = end ; if ( beg < len && buf [ beg ] == ';' ) beg = Util . skipSpace ( buf , beg + 1 ) ; } if ( legal ) { cookie_arr = Util . resizeArray ( cookie_arr , cookie_arr . length + 1 ) ; cookie_arr [ cookie_arr . length - 1 ] = curr ; } else Log . write ( Log . COOKI , "Cooki: Ignoring cookie: " + curr ) ; } return cookie_arr ; }
Parses the Set-Cookie header into an array of Cookies.
Parses the Set - Cookie header into an array of Cookies .
// Cope with .NET nonsense. // See http://www.hanselman.com/blog/HttpOnlyCookiesOnASPNET11.aspx // Typical section of a .NET cookie: //    ... ; path=/;HttpOnly, language=en-US; path=/;HttpOnly // // We remove all instances of "HttpOnly," that follow a semi-colon, // and all instances of HttpOnly following a semi-colon at the end of // the cookie. This leaves something that is more conventional // This shouldn't break any valid cookies. // get all cookies // no more left // empty header // get cookie name and value first // try to handle broken servers which put commas // into cookie values // now parse attributes // parse all parts // skip empty fields // first check for secure, as this is the only one w/o a '=' // consume ";" // alright, must now be of the form x=y // consume ";"