geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rickmcgu...@apache.org
Subject svn commit: r627365 - in /geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec: HttpIoHandler.java HttpMessage.java HttpRequestEncoder.java
Date Wed, 13 Feb 2008 11:56:13 GMT
Author: rickmcguire
Date: Wed Feb 13 03:56:08 2008
New Revision: 627365

URL: http://svn.apache.org/viewvc?rev=627365&view=rev
Log:
GERONIMO-3839 some request headers may be added twice

Patch provided by Sangjin Lee 


Modified:
    geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpIoHandler.java
    geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpMessage.java
    geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpRequestEncoder.java

Modified: geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpIoHandler.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpIoHandler.java?rev=627365&r1=627364&r2=627365&view=diff
==============================================================================
--- geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpIoHandler.java
(original)
+++ geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpIoHandler.java
Wed Feb 13 03:56:08 2008
@@ -151,8 +151,9 @@
             if (!request.getRequestMethod().equals(HttpRequestMessage.REQUEST_GET)) {
                 request.setRequestMethod(HttpRequestMessage.REQUEST_GET);
             }
-            // we also need to clear out the parameters
+            // we also need to clear out the parameters and the content
             request.clearAllParameters();
+            request.clearContent();
 
             //Send the redirect
             client.sendRequest(request);

Modified: geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpMessage.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpMessage.java?rev=627365&r1=627364&r2=627365&view=diff
==============================================================================
--- geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpMessage.java
(original)
+++ geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpMessage.java
Wed Feb 13 03:56:08 2008
@@ -105,6 +105,16 @@
     }
 
     /**
+     * Removes content, if any.
+     */
+    public void clearContent() {
+        // simply nulling the content will do
+        if (content != null) {
+            content = null;
+        }
+    }
+
+    /**
      * Gets the cookies.
      * 
      * @return the cookies

Modified: geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpRequestEncoder.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpRequestEncoder.java?rev=627365&r1=627364&r2=627365&view=diff
==============================================================================
--- geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpRequestEncoder.java
(original)
+++ geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpRequestEncoder.java
Wed Feb 13 03:56:08 2008
@@ -49,8 +49,8 @@
     /** The Constant CRLF. */
     private static final byte[] CRLF = new byte[] {0x0D, 0x0A};
     
-    /** The Constant POST_CONTENT_TYPE. */
-    private static final String POST_CONTENT_TYPE = "application/x-www-form-urlencoded";
+    /** The Constant FORM_POST_CONTENT_TYPE. */
+    private static final String FORM_POST_CONTENT_TYPE = "application/x-www-form-urlencoded";
 
     static {
         Set<Class<?>> types = new HashSet<Class<?>>();
@@ -104,9 +104,10 @@
             }
 
             CharsetEncoder encoder = Charset.forName(HttpMessage.HTTP_ELEMENT_CHARSET).newEncoder();
-            buf.putString(msg.getRequestMethod(), encoder);
+            String method = msg.getRequestMethod();
+            buf.putString(method, encoder);
             buf.putString(" ", encoder);
-            if (msg.getRequestMethod().equals(HttpRequestMessage.REQUEST_CONNECT)) {
+            if (method.equals(HttpRequestMessage.REQUEST_CONNECT)) {
                 buf.putString(msg.getHost(), encoder);
                 buf.putString(":", encoder);
                 buf.putString(msg.getPort() + "", encoder);
@@ -117,7 +118,7 @@
                     buf.putString(msg.getUrl().getFile(), encoder);
                 }
             //If its a GET, append the attributes
-            if (msg.getRequestMethod().equals(HttpRequestMessage.REQUEST_GET) &&
attrCount > 0) {
+                if (method.equals(HttpRequestMessage.REQUEST_GET) && attrCount >
0) {
                 //If there is not already a ? in the query, append one, otherwise append
a &
                     if (!msg.getUrl().getFile().contains("?")) {
                     buf.putString("?", encoder);
@@ -131,20 +132,57 @@
             buf.put(CRLF);
 
             //This header is required for HTTP/1.1
-            buf.putString("Host: ", encoder);
-            buf.putString(msg.getHost(), encoder);
+            
+            String hostHeader = msg.getHost(); 
             if ((msg.getProtocol().equals("http") && msg.getPort() != 80)
                 || (msg.getProtocol().equals("https") && msg.getPort() != 443)) {
-                buf.putString(":", encoder);
-                buf.putString(msg.getPort() + "", encoder);
+                hostHeader += ":" + msg.getPort(); 
             }
-            buf.put(CRLF);
-
+            // set the host header, removing any Host header that might already exist. 
+            msg.setHeader("Host", hostHeader); 
+            
             //User agent
             if (msg.getUserAgent() != null) {
-                buf.putString("User-Agent: ", encoder);
-                buf.putString(msg.getUserAgent(), encoder);
-                buf.put(CRLF);
+                msg.setHeader("User-Agent", msg.getUserAgent()); 
+            }
+
+            // potentially for a POST request.  We need to obtain the content information

+            // so we can add the content headers...but the attaching of the content comes
later. 
+            byte content[] = null;  
+            
+            // If this is a POST and parameters are provided, this is a form
+            // post; any existing content is an error and will be ignored and 
+            // the content type will be set accordingly
+            if (method.equals(HttpRequestMessage.REQUEST_POST) && attrCount >
0) {
+                content = urlAttrs.getBytes();
+
+                // these override any headers that might already be in the set 
+                msg.setHeader(HttpMessage.CONTENT_TYPE, FORM_POST_CONTENT_TYPE);
+            } else if (msg.getContent() != null) {
+                // if the message body was provided by the caller, then use it
+                // (as long as the method is not among the types for which
+                // entities are disallowed)
+                if (!method.equals(HttpRequestMessage.REQUEST_TRACE) && 
+                        !method.equals(HttpRequestMessage.REQUEST_CONNECT)) {
+                    content = msg.getContent();
+                }
+            }
+            
+            // set the proper content length header
+            if (content != null && content.length > 0) {
+                msg.setHeader(HttpMessage.CONTENT_LENGTH, String.valueOf(content.length));
+            } else {
+                // remove any existing content related headers
+                msg.removeHeader(HttpMessage.CONTENT_TYPE);
+                msg.removeHeader(HttpMessage.CONTENT_LENGTH);
+            }
+
+            //Process authentication
+            AuthState state = msg.getAuthState();
+            if (state != null) {
+                String auth = state.getAuthScheme().authenticate(msg.getCredential(new AuthScope(msg.getHost(),
msg.getPort(), state.getAuthScheme().getRealm())),msg);
+                msg.setHeader("Authorization", auth); 
+                state.setAuthAttempted(true);
             }
 
             //Process any headers we have
@@ -156,29 +194,13 @@
             //to handle these issues for the request
             processCookies(msg, buf, encoder);
 
-            //If this is a POST, then we need a content length and type
-            if (msg.getRequestMethod().equals(HttpRequestMessage.REQUEST_POST)) {
-                byte content[] = urlAttrs.getBytes();
-
-                //Type
-                buf.putString(HttpMessage.CONTENT_TYPE, encoder);
-                buf.putString(": ", encoder);
-                buf.putString(POST_CONTENT_TYPE, encoder);
-                buf.put(CRLF);
-
-                //Length
-                buf.putString(HttpMessage.CONTENT_LENGTH, encoder);
-                buf.putString(": ", encoder);
-                buf.putString(content.length + "", encoder);
-                buf.put(CRLF);
-                //Blank line
-                buf.put(CRLF);
+            //Blank line indicates end of the headers 
+            buf.put(CRLF);
+            
+            //If this is a POST, then we have content to attach after the blank line 
+            if (content != null) {
                 buf.put(content);
-            } else {
-                //Blank line
-                buf.put(CRLF);
-            }
-
+            } 
         } catch (CharacterCodingException ex) {
             ex.printStackTrace();
         }



Mime
View raw message