commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d...@apache.org
Subject cvs commit: jakarta-commons/httpclient/src/java/org/apache/commons/httpclient HttpMethodBase.java
Date Sat, 13 Jul 2002 09:06:30 GMT
dion        2002/07/13 02:06:30

  Modified:    httpclient/src/java/org/apache/commons/httpclient
                        HttpMethodBase.java
  Log:
  Patches for adding Host header by Ryan Lubke
  
  Revision  Changes    Path
  1.31      +56 -6     jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java
  
  Index: HttpMethodBase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java,v
  retrieving revision 1.30
  retrieving revision 1.31
  diff -u -r1.30 -r1.31
  --- HttpMethodBase.java	12 Jul 2002 06:02:52 -0000	1.30
  +++ HttpMethodBase.java	13 Jul 2002 09:06:29 -0000	1.31
  @@ -67,6 +67,7 @@
   import java.io.ByteArrayOutputStream;
   import java.io.ByteArrayInputStream;
   import java.io.IOException;
  +import java.util.StringTokenizer;
   import java.util.Iterator;
   import java.util.HashMap;
   import java.util.ArrayList;
  @@ -803,9 +804,23 @@
        * already exists.
        */
       protected void addHostRequestHeader(HttpState state, HttpConnection conn) throws IOException,
HttpException {
  -        // add host (should do this conditionally?, i.e., don't send to http/1.0?)
  +        // Per 19.6.1.1 of RFC 2616, it is legal for HTTP/1.0 based 
  +        // applications to send the Host request-header.
  +        // TODO: Add the ability to disable the sending of this header for HTTP/1.0 requests.
  +
  +        String host = conn.getHost();
  +        int port = conn.getPort();
  +        
           if (!requestHeaders.containsKey("host")) {
  -            setRequestHeader("Host",conn.getHost());
  +            if (!isIpAddress(host)) {
  +                if (port == 80) {
  +                    setRequestHeader("Host", host);
  +                } else {
  +                    setRequestHeader("Host", host + ":" + port);
  +                }
  +            } else {
  +                setRequestHeader("Host", "");
  +            }
           }
       }
   
  @@ -1354,6 +1369,41 @@
           }
   
           return result;
  +    }
  +
  +    /**
  +     * Determines if the provided value is a valid IPv4
  +     * internet address.  
  +     * @param value - value to check
  +     * @return boolean - true if value is valid, otherwise false
  +     *       
  +     */
  +    private static boolean isIpAddress(String value) {
  +        value = value.trim();
  +
  +        // prevent input values of 127.0.0.1. or .127.0.0.1, etc.
  +        if (value.startsWith(".") || value.endsWith(".")) {
  +            return false;
  +        }
  +
  +        StringTokenizer tokenizer = new StringTokenizer(value, ".");
  +        if (tokenizer.countTokens() == 4) {
  +            while (tokenizer.hasMoreTokens()) {
  +                try {
  +                    int i = Integer.parseInt(tokenizer.nextToken());
  +                    if (i < 0 || i > 255) {
  +                        // parsed section of address is not in the proper range
  +                        return false;
  +                    }
  +                } catch (NumberFormatException nfe) {
  +                    return false;
  +                }
  +            }
  +        } else {
  +            // wrong number of tokens
  +            return false;
  +        }
  +        return true;
       }
   
       // ----------------------------------------------------- Instance Variables
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message