common/src/main/java/HTTPClient/Response.java
/**
     * Parses the headers received into a new Response structure.
     *
     * @param  headers a (newline separated) list of headers
     * @exception ProtocolException if any part of the headers do not
     *            conform
     */
Parses the headers received into a new Response structure.
@param  headers a (newline separated) list of headers
@exception ProtocolException if any part of the headers do not
conform
Parses the headers received into a new Response structure . @param headers a ( newline separated ) list of headers @exception ProtocolException if any part of the headers do not conform
private void parseResponseHeaders(String headers) throws ProtocolException
    {
	String          sts_line = null;
	StringTokenizer lines = new StringTokenizer(headers, "\r\n"),
			elem;
	if (Log.isEnabled(Log.RESP))
	    Log.write(Log.RESP, "Resp:  Parsing Response headers from Request "+
				"\"" + method + " " + resource + "\":  (" +
				inp_stream.hashCode() + ")\n\n" + headers);
	if (!headers.regionMatches(true, 0, "HTTP/", 0, 5)  &&
	    !headers.regionMatches(true, 0, "HTTP ", 0, 5))	
	{
	    Version    = "HTTP/0.9";
	    StatusCode = 200;
	    ReasonLine = "OK";
	    try
		{ Data = headers.getBytes("8859_1"); }
	    catch (UnsupportedEncodingException uee)
		{ throw new Error(uee.toString()); }
	    return;
	}
	try
	{
	    sts_line = lines.nextToken();
	    elem     = new StringTokenizer(sts_line, " \t");
	    Version    = elem.nextToken();
	    StatusCode = Integer.valueOf(elem.nextToken()).intValue();
	    if (Version.equalsIgnoreCase("HTTP"))	
		Version = "HTTP/1.0";
	}
	catch (NoSuchElementException e)
	{
	    throw new ProtocolException("Invalid HTTP status line received: " +
					sts_line);
	}
	try
	    { ReasonLine = elem.nextToken("").trim(); }
	catch (NoSuchElementException e)
	    { ReasonLine = ""; }
	/* If the status code shows an error and we're sending (or have sent)
	 * an entity and it's length is delimited by a Content-length header,
	 * then we must close the the connection (if indeed it hasn't already
	 * been done) - RFC-2616, Section 8.2.2 .
	 */
	String auth;
	if (StatusCode >= 300  &&  sent_entity &&
	    /* NTLM hack */
	    !((StatusCode == 407  ||  StatusCode == 401)  &&
	      ((auth = (String) Headers.get("WWW-Authenticate")) != null  ||
	       (auth = (String) Headers.get("Proxy-Authenticate")) != null)  &&
	       auth.startsWith("NTLM"))
	   )
	{
	    if (stream_handler != null)
		stream_handler.markForClose(this);
	}
	parseHeaderFields(lines, Headers);
	try
	{
		if(System.getProperty("HTTPClient.useBasicFirst", "false").equalsIgnoreCase("true"))
		{
			String old_value  = (String) Headers.get("Proxy-Authenticate");
			if (old_value != null)
			{
				StringTokenizer tok = new StringTokenizer(old_value, ",");
				String[] list = new String[tok.countTokens()];
				for (int idx=0; idx<list.length; idx++)
				{
					String token = tok.nextToken().trim();
					if(token.toLowerCase().startsWith("basic"))
					{
						list[idx] = list[0];
						list[0] = token;
					}
					else
					{
						list[idx] = token;
					}					
				}
				String result = "";
				for (int idx=0; idx<list.length - 1; idx++)
				{
					result += list[idx] + ", ";
				}							
				result += list[list.length - 1];
				Headers.put("Proxy-Authenticate", result);
			}
			old_value  = (String) Headers.get("WWW-Authenticate");
			if (old_value != null)
			{
				StringTokenizer tok = new StringTokenizer(old_value, ", ");
				String[] list = new String[tok.countTokens()];
				for (int idx=0; idx<list.length; idx++)
				{
					String token = tok.nextToken().trim();
					if(token.toLowerCase().startsWith("basic"))
					{
						list[idx] = list[0];
						list[0] = token;
					}
					else
					{
						list[idx] = token;
					}					
				}
				String result = "";
				for (int idx=0; idx<list.length - 1; idx++)
				{
					result += list[idx] + ", ";
				}							
				result += list[list.length - 1];
				Headers.put("WWW-Authenticate", result);
			}
		}
	}catch(Exception e){ ; }
	/* make sure the connection isn't closed prematurely if we have
	 * trailer fields
	 */
	if (Headers.get("Trailer") != null  &&  resp_inp_stream != null)
	    resp_inp_stream.dontTruncate();
	int vers;
	if (Version.equalsIgnoreCase("HTTP/0.9")  ||
	    Version.equalsIgnoreCase("HTTP/1.0"))
	    vers = 0;
	else
	    vers = 1;
	try
	{
	    String con = (String) Headers.get("Connection"),
		  pcon = (String) Headers.get("Proxy-Connection");
	    if ((vers == 1  &&  con != null  &&  Util.hasToken(con, "close"))
		||
		(vers == 0  &&
		 !((!used_proxy && con != null &&
					Util.hasToken(con, "keep-alive"))  ||
		   (used_proxy && pcon != null &&
					Util.hasToken(pcon, "keep-alive")))
		)
	       )
		if (stream_handler != null)
		    stream_handler.markForClose(this);
	}
	catch (ParseException pe) { }
    }
private void parseResponseHeaders ( String headers ) throws ProtocolException { String sts_line = null ; StringTokenizer lines = new StringTokenizer ( headers , "\r\n" ) , elem ; if ( Log . isEnabled ( Log . RESP ) ) Log . write ( Log . RESP , "Resp:  Parsing Response headers from Request " + "\"" + method + " " + resource + "\":  (" + inp_stream . hashCode ( ) + ")\n\n" + headers ) ; if ( ! headers . regionMatches ( true , 0 , "HTTP/" , 0 , 5 ) && ! headers . regionMatches ( true , 0 , "HTTP " , 0 , 5 ) ) { Version = "HTTP/0.9" ; StatusCode = 200 ; ReasonLine = "OK" ; try { Data = headers . getBytes ( "8859_1" ) ; } catch ( UnsupportedEncodingException uee ) { throw new Error ( uee . toString ( ) ) ; } return ; } try { sts_line = lines . nextToken ( ) ; elem = new StringTokenizer ( sts_line , " \t" ) ; Version = elem . nextToken ( ) ; StatusCode = Integer . valueOf ( elem . nextToken ( ) ) . intValue ( ) ; if ( Version . equalsIgnoreCase ( "HTTP" ) ) Version = "HTTP/1.0" ; } catch ( NoSuchElementException e ) { throw new ProtocolException ( "Invalid HTTP status line received: " + sts_line ) ; } try { ReasonLine = elem . nextToken ( "" ) . trim ( ) ; } catch ( NoSuchElementException e ) { ReasonLine = "" ; } /* If the status code shows an error and we're sending (or have sent)
	 * an entity and it's length is delimited by a Content-length header,
	 * then we must close the the connection (if indeed it hasn't already
	 * been done) - RFC-2616, Section 8.2.2 .
	 */ String auth ; if ( StatusCode >= 300 && sent_entity && /* NTLM hack */ ! ( ( StatusCode == 407 || StatusCode == 401 ) && ( ( auth = ( String ) Headers . get ( "WWW-Authenticate" ) ) != null || ( auth = ( String ) Headers . get ( "Proxy-Authenticate" ) ) != null ) && auth . startsWith ( "NTLM" ) ) ) { if ( stream_handler != null ) stream_handler . markForClose ( this ) ; } parseHeaderFields ( lines , Headers ) ; try { if ( System . getProperty ( "HTTPClient.useBasicFirst" , "false" ) . equalsIgnoreCase ( "true" ) ) { String old_value = ( String ) Headers . get ( "Proxy-Authenticate" ) ; if ( old_value != null ) { StringTokenizer tok = new StringTokenizer ( old_value , "," ) ; String [ ] list = new String [ tok . countTokens ( ) ] ; for ( int idx = 0 ; idx < list . length ; idx ++ ) { String token = tok . nextToken ( ) . trim ( ) ; if ( token . toLowerCase ( ) . startsWith ( "basic" ) ) { list [ idx ] = list [ 0 ] ; list [ 0 ] = token ; } else { list [ idx ] = token ; } } String result = "" ; for ( int idx = 0 ; idx < list . length - 1 ; idx ++ ) { result += list [ idx ] + ", " ; } result += list [ list . length - 1 ] ; Headers . put ( "Proxy-Authenticate" , result ) ; } old_value = ( String ) Headers . get ( "WWW-Authenticate" ) ; if ( old_value != null ) { StringTokenizer tok = new StringTokenizer ( old_value , ", " ) ; String [ ] list = new String [ tok . countTokens ( ) ] ; for ( int idx = 0 ; idx < list . length ; idx ++ ) { String token = tok . nextToken ( ) . trim ( ) ; if ( token . toLowerCase ( ) . startsWith ( "basic" ) ) { list [ idx ] = list [ 0 ] ; list [ 0 ] = token ; } else { list [ idx ] = token ; } } String result = "" ; for ( int idx = 0 ; idx < list . length - 1 ; idx ++ ) { result += list [ idx ] + ", " ; } result += list [ list . length - 1 ] ; Headers . put ( "WWW-Authenticate" , result ) ; } } } catch ( Exception e ) { ; } /* make sure the connection isn't closed prematurely if we have
	 * trailer fields
	 */ if ( Headers . get ( "Trailer" ) != null && resp_inp_stream != null ) resp_inp_stream . dontTruncate ( ) ; int vers ; if ( Version . equalsIgnoreCase ( "HTTP/0.9" ) || Version . equalsIgnoreCase ( "HTTP/1.0" ) ) vers = 0 ; else vers = 1 ; try { String con = ( String ) Headers . get ( "Connection" ) , pcon = ( String ) Headers . get ( "Proxy-Connection" ) ; if ( ( vers == 1 && con != null && Util . hasToken ( con , "close" ) ) || ( vers == 0 && ! ( ( ! used_proxy && con != null && Util . hasToken ( con , "keep-alive" ) ) || ( used_proxy && pcon != null && Util . hasToken ( pcon , "keep-alive" ) ) ) ) ) if ( stream_handler != null ) stream_handler . markForClose ( this ) ; } catch ( ParseException pe ) { } }
Parses the headers received into a new Response structure.
Parses the headers received into a new Response structure .
// Detect and handle HTTP/0.9 responses // NCSA bug // get the status line // NCSA bug // get the rest of the headers //@gusbro: HACK // El siguiente Hack es para que el Basic quede primero // Se activa si la SystemProperty 'HTTPClient.useBasicFirst'=true // Mark the end of the connection if it's not to be kept alive // parse connection header