OSDN Git Service

Fixing the delimiter for the HTTP "Accept" header to be well-formed.
authorJesse Wilson <jessewilson@google.com>
Fri, 11 Sep 2009 23:20:49 +0000 (16:20 -0700)
committerJesse Wilson <jessewilson@google.com>
Fri, 11 Sep 2009 23:23:00 +0000 (16:23 -0700)
See bug 2107897.

libcore/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/HttpURLConnection.java
libcore/luni/src/test/java/org/apache/harmony/luni/tests/java/net/URLConnectionTest.java
libcore/support/src/test/java/tests/support/Support_TestWebServer.java

index 104a981..2dea92b 100644 (file)
@@ -1385,7 +1385,9 @@ public class HttpURLConnection extends java.net.HttpURLConnection {
             output.append("\r\n"); //$NON-NLS-1$
         }
         if (reqHeader.get("Accept") == null) { //$NON-NLS-1$
-            output.append("Accept: *; */*\r\n"); //$NON-NLS-1$
+            // BEGIN android-changed
+            output.append("Accept: *, */*\r\n"); //$NON-NLS-1$
+            // END android-changed
         }
         if (httpVersion > 0 && reqHeader.get("Connection") == null) { //$NON-NLS-1$
             output.append("Connection: Keep-Alive\r\n"); //$NON-NLS-1$
index bcd73d8..68ccb91 100644 (file)
 package org.apache.harmony.luni.tests.java.net;
 
 import dalvik.annotation.BrokenTest;
-import dalvik.annotation.KnownFailure;
 import dalvik.annotation.TestLevel;
 import dalvik.annotation.TestTargetClass;
 import dalvik.annotation.TestTargetNew;
 import dalvik.annotation.TestTargets;
-
 import junit.framework.TestCase;
-
 import tests.support.Support_Configuration;
 import tests.support.Support_PortManager;
 import tests.support.Support_TestWebData;
@@ -59,7 +56,6 @@ import java.net.URLConnection;
 import java.net.URLStreamHandler;
 import java.net.UnknownServiceException;
 import java.security.Permission;
-import java.text.DateFormat;
 import java.text.ParseException;
 import java.util.Arrays;
 import java.util.Calendar;
@@ -463,6 +459,28 @@ public class URLConnectionTest extends TestCase {
         }
     }
 
+    public void testHttpPostHeaders() throws IOException {
+        String path = "/" + Math.random();
+        HttpURLConnection connection = (HttpURLConnection)
+                new URL("http://localhost:" + port + path).openConnection();
+
+        // post a request
+        connection.setDoOutput(true);
+        OutputStreamWriter writer
+                = new OutputStreamWriter(connection.getOutputStream());
+        writer.write("hello");
+        writer.flush();
+        assertEquals(200, connection.getResponseCode());
+
+        // validate the request by asking the server what was received
+        Map<String, String> headers = server.pathToRequest().get(path).getHeaders();
+        assertEquals("*, */*", headers.get("Accept"));
+        assertEquals("application/x-www-form-urlencoded", headers.get("Content-Type"));
+        assertEquals("5", headers.get("Content-Length"));
+        assertEquals("localhost:" + port, headers.get("Host"));
+        // TODO: test User-Agent?
+    }
+
     /**
      * @throws IOException 
      * @tests {@link java.net.URLConnection#getAllowUserInteraction()}
index 8ee7248..609e993 100644 (file)
@@ -19,9 +19,9 @@ package tests.support;
 import java.io.*;
 import java.lang.Thread;
 import java.net.*;
-import java.text.DateFormat;
 import java.text.SimpleDateFormat;
 import java.util.*;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.logging.Logger;
 
 /**
@@ -42,6 +42,10 @@ public class Support_TestWebServer implements Support_HttpConstants {
     /* Where worker threads stand idle */
     Vector threads = new Vector();
 
+    /** maps the recently requested URLs to the full request snapshot */
+    private final Map<String, Request> pathToRequest
+            = new ConcurrentHashMap<String, Request>();
+
     /* List of all active worker threads */
     Vector activeThreads = new Vector();
 
@@ -206,7 +210,7 @@ public class Support_TestWebServer implements Support_HttpConstants {
      * a redirect code with the Location response header set to the value
      * specified.
      * @param redirect The location to be redirected to
-     * @param redirectCode The code to send when redirecting
+     * @param code The code to send when redirecting
      */
     public void setRedirect(String redirect, int code) {
         redirectHost = redirect;
@@ -215,6 +219,14 @@ public class Support_TestWebServer implements Support_HttpConstants {
     }
 
     /**
+     * Returns a map from recently-requested paths (like "/index.html") to a
+     * snapshot of the request data.
+     */
+    public Map<String, Request> pathToRequest() {
+        return pathToRequest;
+    }
+
+    /**
      * Cause the thread accepting connections on the server socket to close
      */
     public void close() {
@@ -328,6 +340,28 @@ public class Support_TestWebServer implements Support_HttpConstants {
     static final byte[] EOL = {(byte)'\r', (byte)'\n' };
 
     /**
+     * An immutable snapshot of an HTTP request.
+     */
+    public static class Request {
+        private final String path;
+        private final Map<String, String> headers;
+        // TODO: include posted content?
+
+        public Request(String path, Map<String, String> headers) {
+            this.path = path;
+            this.headers = new LinkedHashMap<String, String>(headers);
+        }
+
+        public String getPath() {
+            return path;
+        }
+
+        public Map<String, String> getHeaders() {
+            return headers;
+        }
+    }
+
+    /**
      * The worker thread handles all interactions with a current open
      * connection. If pipelining is turned on, this will allow this
      * thread to continuously operate on numerous requests before the
@@ -347,6 +381,9 @@ public class Support_TestWebServer implements Support_HttpConstants {
         /* Reference to current requests test file/data */
         private String testID;
 
+        /* The requested path, such as "/test1" */
+        private String path;
+
         /* Reference to test number from testID */
         private int testNum;
 
@@ -359,7 +396,7 @@ public class Support_TestWebServer implements Support_HttpConstants {
         boolean running = false;
 
         /* Request headers are stored here */
-        private Hashtable<String, String> headers = new Hashtable<String, String>();
+        private Map<String, String> headers = new LinkedHashMap<String, String>();
 
         /* Create a new worker thread */
         Worker() {
@@ -559,10 +596,8 @@ public class Support_TestWebServer implements Support_HttpConstants {
                     i++;
                 }
 
-                testID = new String(buf, 0, index, i-index);
-                if (testID.startsWith("/")) {
-                    testID = testID.substring(1);
-                }
+                path = new String(buf, 0, index, i-index);
+                testID = path.substring(1);
 
                 return nread;
             }
@@ -601,7 +636,7 @@ public class Support_TestWebServer implements Support_HttpConstants {
             while (buf[i] == ' ') {
                 i++;
             }
-            String headerValue = new String(buf, i, nread-1);
+            String headerValue = new String(buf, i, nread-i);
 
             headers.put(headerName, headerValue);
             return nread;
@@ -666,6 +701,8 @@ public class Support_TestWebServer implements Support_HttpConstants {
                     // If status line found, read any headers
                     nread = readHeaders(is);
 
+                    pathToRequest().put(path, new Request(path, headers));
+
                     // Then read content (if any)
                     // TODO handle chunked encoding from the client
                     if (headers.get(requestHeaders[REQ_CONTENT_LENGTH]) != null) {