common/src/main/java/HTTPClient/Util.java
/**
     * This parses the value part of a header. The result is a Vector of
     * HttpHeaderElement's. The syntax the header must conform to is:
     *
     * <PRE>
     * header  = [ element ] *( "," [ element ] )
     * element = name [ "=" [ value ] ] *( ";" [ param ] )
     * param   = name [ "=" [ value ] ]
     * 
     * name    = token
     * value   = ( token | quoted-string )
     * 
     * token         = 1*&lt;any char except "=", ",", ";", &lt;"&gt; and
     *                       white space&gt;
     * quoted-string = &lt;"&gt; *( text | quoted-char ) &lt;"&gt;
     * text          = any char except &lt;"&gt;
     * quoted-char   = "\" char
     * </PRE>
     *
     * Any amount of white space is allowed between any part of the header,
     * element or param and is ignored. A missing value in any element or
     * param will be stored as the empty string; if the "=" is also missing
     * <var>null</var> will be stored instead.
     *
     * @param header  the value part of the header.
     * @param dequote if true all quoted strings are dequoted.
     * @return a Vector containing all the elements; each entry is an
     *         instance of <var>HttpHeaderElement</var>.
     * @exception ParseException if the above syntax rules are violated.
     * @see HTTPClient.HttpHeaderElement
     */
This parses the value part of a header. The result is a Vector of
HttpHeaderElement's. The syntax the header must conform to is.



name    = token
value   = ( token | quoted-string )



Any amount of white space is allowed between any part of the header,
element or param and is ignored. A missing value in any element or
param will be stored as the empty string; if the "=" is also missing
null will be stored instead.

@param header  the value part of the header.
@param dequote if true all quoted strings are dequoted.
@return a Vector containing all the elements; each entry is an
instance of HttpHeaderElement.
@exception ParseException if the above syntax rules are violated.
This parses the value part of a header . The result is a Vector of HttpHeaderElement ' s . The syntax the header must conform to is . name = token value = ( token | quoted - string ) Any amount of white space is allowed between any part of the header element or param and is ignored . A missing value in any element or param will be stored as the empty string ; if the " = " is also missing null will be stored instead . @param header the value part of the header . @param dequote if true all quoted strings are dequoted . @return a Vector containing all the elements ; each entry is an instance of HttpHeaderElement . @exception ParseException if the above syntax rules are violated .
public final static Vector<HttpHeaderElement> parseHeader(String header, boolean dequote)
	    throws ParseException
    {
	if (header == null)  return null;
	char[]  buf    = header.toCharArray();
	Vector<HttpHeaderElement>  elems  = new Vector<>();
	boolean first  = true;
	int     beg = -1, end = 0, len = buf.length, abeg[] = new int[1];
	String  elem_name, elem_value;
	elements: while (true)
	{
	    if (!first)				
	    {
		beg = skipSpace(buf, end);
		if (beg == len)  break;
		if (buf[beg] != ',')
		    throw new ParseException("Bad header format: '" + header +
					     "'\nExpected \",\" at position " +
					     beg);
	    }
	    first = false;
	    beg = skipSpace(buf, beg+1);
	    if (beg == len)  break elements;
	    if (buf[beg] == ',')		
	    {
		end = beg;
		continue elements;
	    }
	    if (buf[beg] == '='  ||  buf[beg] == ';'  ||  buf[beg] == '"')
		throw new ParseException("Bad header format: '" + header +
					 "'\nEmpty element name at position " +
					 beg);
	    end = beg+1;			
	    while (end < len  &&  !Character.isWhitespace(buf[end])  &&
		   buf[end] != '='  &&  buf[end] != ','  &&  buf[end] != ';')
		end++;
	    elem_name = new String(buf, beg, end-beg);
	    beg = skipSpace(buf, end);
	    if (beg < len  &&  buf[beg] == '=')	
	    {
		abeg[0] = beg+1;
		elem_value = parseValue(buf, abeg, header, dequote);
		end = abeg[0];
	    }
	    else
	    {
		elem_value = null;
		end = beg;
	    }
	    NVPair[] params = new NVPair[0];
	    params: while (true)
	    {
		String param_name, param_value;
		beg = skipSpace(buf, end);	
		if (beg == len  ||  buf[beg] != ';')
		    break params;
		beg = skipSpace(buf, beg+1);
		if (beg == len  ||  buf[beg] == ',')
		{
		    end = beg;
		    break params;
		}
		if (buf[beg] == ';')		
		{
		    end = beg;
		    continue params;
		}
		if (buf[beg] == '='  ||  buf[beg] == '"')
		    throw new ParseException("Bad header format: '" + header +
					 "'\nEmpty parameter name at position "+
					 beg);
		end = beg+1;			
		while (end < len  &&  !Character.isWhitespace(buf[end])  &&
		       buf[end] != '='  &&  buf[end] != ','  && buf[end] != ';')
		    end++;
		param_name = new String(buf, beg, end-beg);
		beg = skipSpace(buf, end);
		if (beg < len  &&  buf[beg] == '=')	
		{
		    abeg[0] = beg+1;
		    param_value = parseValue(buf, abeg, header, dequote);
		    end = abeg[0];
		}
		else
		{
		    param_value = null;
		    end = beg;
		}
		params = Util.resizeArray(params, params.length+1);
		params[params.length-1] = new NVPair(param_name, param_value);
	    }
	    elems.addElement(
		      new HttpHeaderElement(elem_name, elem_value, params));
	}
	return elems;
    }
public final static Vector < HttpHeaderElement > parseHeader ( String header , boolean dequote ) throws ParseException { if ( header == null ) return null ; char [ ] buf = header . toCharArray ( ) ; Vector < HttpHeaderElement > elems = new Vector < > ( ) ; boolean first = true ; int beg = - 1 , end = 0 , len = buf . length , abeg [ ] = new int [ 1 ] ; String elem_name , elem_value ; elements : while ( true ) { if ( ! first ) { beg = skipSpace ( buf , end ) ; if ( beg == len ) break ; if ( buf [ beg ] != ',' ) throw new ParseException ( "Bad header format: '" + header + "'\nExpected \",\" at position " + beg ) ; } first = false ; beg = skipSpace ( buf , beg + 1 ) ; if ( beg == len ) break elements ; if ( buf [ beg ] == ',' ) { end = beg ; continue elements ; } if ( buf [ beg ] == '=' || buf [ beg ] == ';' || buf [ beg ] == '"' ) throw new ParseException ( "Bad header format: '" + header + "'\nEmpty element name at position " + beg ) ; end = beg + 1 ; while ( end < len && ! Character . isWhitespace ( buf [ end ] ) && buf [ end ] != '=' && buf [ end ] != ',' && buf [ end ] != ';' ) end ++ ; elem_name = new String ( buf , beg , end - beg ) ; beg = skipSpace ( buf , end ) ; if ( beg < len && buf [ beg ] == '=' ) { abeg [ 0 ] = beg + 1 ; elem_value = parseValue ( buf , abeg , header , dequote ) ; end = abeg [ 0 ] ; } else { elem_value = null ; end = beg ; } NVPair [ ] params = new NVPair [ 0 ] ; params : while ( true ) { String param_name , param_value ; beg = skipSpace ( buf , end ) ; if ( beg == len || buf [ beg ] != ';' ) break params ; beg = skipSpace ( buf , beg + 1 ) ; if ( beg == len || buf [ beg ] == ',' ) { end = beg ; break params ; } if ( buf [ beg ] == ';' ) { end = beg ; continue params ; } if ( buf [ beg ] == '=' || buf [ beg ] == '"' ) throw new ParseException ( "Bad header format: '" + header + "'\nEmpty parameter name at position " + beg ) ; end = beg + 1 ; while ( end < len && ! Character . isWhitespace ( buf [ end ] ) && buf [ end ] != '=' && buf [ end ] != ',' && buf [ end ] != ';' ) end ++ ; param_name = new String ( buf , beg , end - beg ) ; beg = skipSpace ( buf , end ) ; if ( beg < len && buf [ beg ] == '=' ) { abeg [ 0 ] = beg + 1 ; param_value = parseValue ( buf , abeg , header , dequote ) ; end = abeg [ 0 ] ; } else { param_value = null ; end = beg ; } params = Util . resizeArray ( params , params . length + 1 ) ; params [ params . length - 1 ] = new NVPair ( param_name , param_value ) ; } elems . addElement ( new HttpHeaderElement ( elem_name , elem_value , params ) ) ; } return elems ; }
This parses the value part of a header.
This parses the value part of a header .
// find required "," // skip empty elements // extract element name // element value // expect ";" // skip empty parameters // extract param name // element value