Hello,

I added the two methods for setting/adding default request headers for 
setting headers such as 'User-Agent', which most often stay fixed during 
the whole life of an application and do not change from request to request.

I'm including the patch to HttpMethod (interface) and HttpMethodBase 
(impl of that interface).

Originally I wanted to add the ability to set the default request 
headers for 1) efficiency, and 2) simpler code.

However, now after I added them it seems to me that this is actually not 
the most efficient way of doing it (checking 2 HashMaps vs. 1 HashMap in 
  5 of those add*RequestHeader methods).  It is probably more efficient 
to just call setRequestHeader(String, String) over and over, which is 
what I wanted to avoid in the first place, thinking that it's inefficient.

On the other hand, the call that uses HTTP Client can be a bit simpler 
and cleaner because it won't have to call setRequestHeader(String, 
String) over and over for the non-variable headers, between method 
recycling .

Therefore, a question for all: what do you think, do these patches 
improve things or not?
Please let me know if they can or cannot be applied to the versions in 
the repository.  At this point I'm fine either way :)

Thanks,
Otis
Index: HttpMethod.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethod.java,v
retrieving revision 1.10
diff -u -r1.10 HttpMethod.java
--- HttpMethod.java     5 Jan 2002 11:16:00 -0000       1.10
+++ HttpMethod.java     13 Jan 2002 18:53:06 -0000
@@ -100,6 +100,16 @@
     public String getPath();
 
     /**
+     * Sets the specified request header as default, overwriting any
+     * previous value.  Specifying an empty or <code>null</code> value
+     * for the headerValue parameter will remove the specified header.
+     * Note that header-name matching is case-insensitive.
+     * @param headerName the header's name
+     * @param headerValue the header's value
+     */
+    public void setDefaultRequestHeader(String headerName, String headerValue);
+
+    /**
      * Set the specified request header, overwriting any
      * previous value.
      * Note that header-name matching is case insensitive.
@@ -115,6 +125,15 @@
      * @param header the header
      */
     public void setRequestHeader(Header header);
+
+    /**
+     * Adds the specified request header, NOT overwriting any
+     * previous value.
+     * Note that header-name matching is case insensitive.
+     * @param headerName the header's name
+     * @param headerValue the header's value
+     */
+    public void addDefaultRequestHeader(String headerName, String headerValue);
 
     /**
      * Adds the specified request header, NOT overwriting any
Index: HttpMethodBase.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java,v
retrieving revision 1.22
diff -u -r1.22 HttpMethodBase.java
--- HttpMethodBase.java 5 Jan 2002 11:16:00 -0000       1.22
+++ HttpMethodBase.java 13 Jan 2002 18:55:59 -0000
@@ -156,6 +156,49 @@
     }
 
     /**
+     * Sets the specified request header as default, overwriting any
+     * previous value.  Specifying an empty or <code>null</code> value
+     * for the headerValue parameter will remove the specified header.
+     * Note that header-name matching is case-insensitive.
+     * @param headerName the header's name
+     * @param headerValue the header's value
+     */
+     public void setDefaultRequestHeader(String headerName, String headerValue) {
+        if(null == headerValue || headerValue.equals("")) {
+           defaultRequestHeaders.remove(headerName.toLowerCase());
+       }
+       else {
+           Header header = new Header(headerName, headerValue);
+           defaultRequestHeaders.put(headerName.toLowerCase(), header);
+       }
+    }
+
+    /**
+     * Adds the specified request header, NOT overwriting any
+     * previous value.
+     * Note that header-name matching is case insensitive.
+     * @param headerName the header's name
+     * @param headerValue the header's value
+     */
+    public void addDefaultRequestHeader(String headerName, String headerValue) {
+        // "It must be possible to combine the multiple header fields into
+        // one "field-name: field-value" pair, without changing the
+        // semantics of the message, by appending each subsequent field-value
+        // to the first, each separated by a comma."
+        //   - HTTP/1.0 (4.3)
+       headerName = headerName.toLowerCase();
+        Header header = (Header)(defaultRequestHeaders.get(headerName));
+        if(null == header) {
+            header = new Header(headerName, headerValue);
+        } else {
+            header.setValue( (null == header.getValue() ? "" : header.getValue()) +
+                             ", " +
+                             (null == headerValue ? "" : headerValue));
+        }
+        defaultRequestHeaders.put(headerName, header);
+    }
+
+    /**
      * Set the specified request header, overwriting any
      * previous value.
      * Note that header-name matching is case-insensitive.
@@ -657,6 +700,14 @@
         while(it.hasNext()) {
             conn.print(((Header)it.next()).toExternalForm());
         }
+        it = defaultRequestHeaders.keySet().iterator();
+        while(it.hasNext()) {
+           String headerName = (String)it.next();
+           if(!requestHeaders.containsKey(headerName))
+           {
+               
+conn.print(((Header)defaultRequestHeaders.get(headerName)).toExternalForm());
+           }
+        }
     }
 
     /**
@@ -693,7 +744,8 @@
      * already exists.
      */
     protected void addUserAgentRequestHeader(HttpState state, HttpConnection conn) 
throws IOException, HttpException {
-        if (!requestHeaders.containsKey("user-agent")) {
+        if (!requestHeaders.containsKey("user-agent") &&
+           !defaultRequestHeaders.containsKey("user-agent")) {
             setRequestHeader(HttpMethodBase.USER_AGENT);
         }
     }
@@ -705,7 +757,8 @@
      */
     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?)
-        if (!requestHeaders.containsKey("host")) {
+        if (!requestHeaders.containsKey("host") &&
+           !defaultRequestHeaders.containsKey("host")) {
             setRequestHeader("Host",conn.getHost());
         }
     }
@@ -716,7 +769,8 @@
      * already exists.
      */
     protected void addCookieRequestHeader(HttpState state, HttpConnection conn) 
throws IOException, HttpException {
-        if (!requestHeaders.containsKey("cookie")) {
+        if (!requestHeaders.containsKey("cookie") &&
+           !defaultRequestHeaders.containsKey("cookie")) {
             Header cookieHeader = Cookie.createCookieHeader(conn.getHost(), 
conn.getPort(), getPath(), conn.isSecure(), new Date(), state.getCookies());
             if(null != cookieHeader) {
                 setRequestHeader(cookieHeader);
@@ -731,7 +785,8 @@
      */
     protected void addAuthorizationRequestHeader(HttpState state, HttpConnection 
conn) throws IOException, HttpException {
         // add authorization header, if needed
-        if(!requestHeaders.containsKey("authorization")) {
+        if (!requestHeaders.containsKey("authorization") &&
+           !defaultRequestHeaders.containsKey("authorization")) {
             Header wwwAuthenticateHeader = 
(Header)(responseHeaders.get("www-authenticate"));
             if(null != wwwAuthenticateHeader) {
                 try {
@@ -752,7 +807,8 @@
     protected void addContentLengthRequestHeader(HttpState state, HttpConnection 
conn) throws IOException, HttpException {
         // add content length or chunking
         int len = getRequestContentLength();
-        if(!requestHeaders.containsKey("content-length")) {
+        if (!requestHeaders.containsKey("content-length") &&
+           !defaultRequestHeaders.containsKey("content-length")) {
             if(-1 < len) {
                 setRequestHeader("Content-Length",String.valueOf(len));
             } else if(http11 && (len < 0)) {
@@ -1110,7 +1166,7 @@
         path = null;
         followRedirects = false;
         queryString = null;
-        requestHeaders.clear();
+        requestHeaders = defaultRequestHeaders;
         responseHeaders.clear();
         statusCode = -1;
         statusText = null;
@@ -1206,6 +1262,8 @@
     private boolean followRedirects = false;
     /** My query string, if any. */
     private String queryString = null;
+    /** Default request headers, if any. */
+    private HashMap defaultRequestHeaders = new HashMap();
     /** My request headers, if any. */
     private HashMap requestHeaders = new HashMap();
     /** My response headers, if any. */

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to