geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jgenen...@apache.org
Subject svn commit: r567197 - in /geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec: HttpMessage.java HttpRequestEncoder.java protocol/HttpDecoder.java
Date Sat, 18 Aug 2007 01:30:05 GMT
Author: jgenender
Date: Fri Aug 17 18:30:04 2007
New Revision: 567197

URL: http://svn.apache.org/viewvc?view=rev&rev=567197
Log:
Get requests encoding properly

Modified:
    geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpMessage.java
    geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpRequestEncoder.java
    geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/protocol/HttpDecoder.java

Modified: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpMessage.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpMessage.java?view=diff&rev=567197&r1=567196&r2=567197
==============================================================================
--- geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpMessage.java (original)
+++ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpMessage.java Fri
Aug 17 18:30:04 2007
@@ -28,6 +28,9 @@
 
 public class HttpMessage {
     
+    public final static String CONTENT_TYPE = "Content-Type";
+    public final static String CONTENT_LENGTH = "Content-Length";
+
     protected List<NameValuePair> headers = new ArrayList<NameValuePair>();
     protected List<Cookie> cookies = new ArrayList<Cookie>();
     protected String contentType;

Modified: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpRequestEncoder.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpRequestEncoder.java?view=diff&rev=567197&r1=567196&r2=567197
==============================================================================
--- geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpRequestEncoder.java
(original)
+++ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpRequestEncoder.java
Fri Aug 17 18:30:04 2007
@@ -26,11 +26,9 @@
 import org.apache.mina.common.IoSession;
 import org.apache.mina.common.ByteBuffer;
 import org.apache.ahc.util.NameValuePair;
+import org.apache.ahc.util.EncodingUtil;
 
-import java.util.Set;
-import java.util.HashSet;
-import java.util.Collections;
-import java.util.List;
+import java.util.*;
 import java.nio.charset.CharsetEncoder;
 import java.nio.charset.CharacterCodingException;
 import java.nio.charset.Charset;
@@ -39,6 +37,7 @@
 public class HttpRequestEncoder extends ProtocolEncoderAdapter {
     private static final Set TYPES;
     private static final byte[] CRLF = new byte[]{0x0D, 0x0A};
+    private static final String POST_CONTENT_TYPE = "application/x-www-form-urlencoded";
     private URL url;
 
     static {
@@ -64,11 +63,34 @@
         buf.setAutoExpand(true);
 
         try {
-            // output all headers except the content length
+            //If we have content, lets create the query string
+            int attrCount = msg.getAttributes().size();
+            String urlAttrs = "";
+            if (attrCount > 0){
+                NameValuePair attrs[] = new NameValuePair[attrCount];
+                Set<Map.Entry<String,String>> set = msg.getAttributes().entrySet();
+                int i = 0;
+                for (Map.Entry<String, String> entry: set){
+                    attrs[i].setName(entry.getKey());
+                    attrs[i++].setValue(entry.getValue());
+                }
+                urlAttrs = EncodingUtil.formUrlEncode(attrs, Charset.defaultCharset().toString());
+            }
+
             CharsetEncoder encoder = Charset.defaultCharset().newEncoder();
             buf.putString(msg.getRequestMethod(), encoder);
             buf.putString(" ", encoder);
             buf.putString(msg.getPath(), encoder);
+            //If its a GET, append the attributes
+            if (msg.getRequestMethod().equals(HttpRequestMessage.REQUEST_GET) &&
attrCount > 0) {
+                //If there is not already a ? in the query, append one, otherwise append
a &
+                if (!msg.getPath().contains("?")){
+                    buf.putString("?", encoder);
+                } else {
+                    buf.putString("&", encoder);
+                }
+                buf.putString(urlAttrs, encoder);
+            }
             buf.putString(" HTTP/1.1", encoder);
             buf.put(CRLF);
 
@@ -112,11 +134,27 @@
 
             //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);
+                buf.put(content);
+            } else {
+                //Blank line
+                buf.put(CRLF);
             }
 
-            //Blank line
-            buf.put(CRLF);
         } catch (CharacterCodingException ex) {
             ex.printStackTrace();
         }

Modified: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/protocol/HttpDecoder.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/protocol/HttpDecoder.java?view=diff&rev=567197&r1=567196&r2=567197
==============================================================================
--- geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/protocol/HttpDecoder.java
(original)
+++ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/protocol/HttpDecoder.java
Fri Aug 17 18:30:04 2007
@@ -21,6 +21,7 @@
 
 import org.apache.ahc.codec.HttpResponseMessage;
 import org.apache.ahc.codec.Cookie;
+import org.apache.ahc.codec.HttpMessage;
 import org.apache.ahc.util.DateUtil;
 import org.apache.mina.common.ByteBuffer;
 
@@ -40,8 +41,6 @@
      */
     private static final byte LF = 10;
 
-    public final static String CONTENT_TYPE = "Content-Type";
-    public final static String CONTENT_LENGTH = "Content-Length";
     public final static String SET_COOKIE = "Set-Cookie";
 
     public final static String COOKIE_COMMENT = "comment";
@@ -110,11 +109,11 @@
             if (cookie != null) msg.addCookie(cookie);
         }
 
-        if (name.equalsIgnoreCase(CONTENT_TYPE)) {
+        if (name.equalsIgnoreCase(HttpMessage.CONTENT_TYPE)) {
             msg.setContentType(value);
         }
 
-        if (name.equalsIgnoreCase(CONTENT_LENGTH)) {
+        if (name.equalsIgnoreCase(HttpMessage.CONTENT_LENGTH)) {
             msg.setContentLength(Integer.parseInt(value));
         }
 



Mime
View raw message