You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by qi...@apache.org on 2009/01/04 05:43:56 UTC

svn commit: r731174 - in /harmony/enhanced/classlib/trunk/modules/luni/src: main/java/org/apache/harmony/luni/internal/net/www/protocol/http/ test/api/common/org/apache/harmony/luni/tests/internal/net/www/protocol/http/

Author: qiuxx
Date: Sat Jan  3 20:43:56 2009
New Revision: 731174

URL: http://svn.apache.org/viewvc?rev=731174&view=rev
Log:
Apply for HARMONY-6036, ([classlib] [luni] HttpURLConnection does not have the "Accept" header)

Modified:
    harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/HttpURLConnection.java
    harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/internal/net/www/protocol/http/HttpURLConnectionTest.java

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/HttpURLConnection.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/HttpURLConnection.java?rev=731174&r1=731173&r2=731174&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/HttpURLConnection.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/HttpURLConnection.java Sat Jan  3 20:43:56 2009
@@ -1136,6 +1136,45 @@
         } else {
             output.append("1\r\n"); //$NON-NLS-1$
         }
+        // add user-specified request headers if any
+        boolean hasContentLength = false;
+        for (int i = 0; i < reqHeader.length(); i++) {
+            String key = reqHeader.getKey(i);
+            if (key != null) {
+                String lKey = key.toLowerCase();
+                if ((os != null && !os.isChunked())
+                        || (!lKey.equals("transfer-encoding") && !lKey //$NON-NLS-1$
+                                .equals("content-length"))) { //$NON-NLS-1$
+                    output.append(key);
+                    String value = reqHeader.get(i);
+                    /*
+                     * duplicates are allowed under certain conditions see
+                     * http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2
+                     */
+                    if (lKey.equals("content-length")) { //$NON-NLS-1$
+                        hasContentLength = true;
+                        /*
+                         * if both setFixedLengthStreamingMode and
+                         * content-length are set, use fixedContentLength first
+                         */
+                        if(fixedContentLength >= 0){
+                            value = String.valueOf(fixedContentLength);
+                        }
+                    }
+                    if (value != null) {
+                        output.append(": "); //$NON-NLS-1$
+                        output.append(value);
+                    }
+                    output.append("\r\n"); //$NON-NLS-1$
+                }
+            }
+        }
+        if (fixedContentLength >= 0 && !hasContentLength) {
+            output.append("content-length: "); //$NON-NLS-1$
+            output.append(String.valueOf(fixedContentLength));
+            output.append("\r\n"); //$NON-NLS-1$
+        }
+        
         if (reqHeader.get("User-Agent") == null) { //$NON-NLS-1$
             output.append("User-Agent: "); //$NON-NLS-1$
             String agent = getSystemProperty("http.agent"); //$NON-NLS-1$
@@ -1157,6 +1196,9 @@
             }
             output.append("\r\n"); //$NON-NLS-1$
         }
+        if (reqHeader.get("Accept") == null) { //$NON-NLS-1$
+            output.append("Accept: *; */*\r\n"); //$NON-NLS-1$
+        }
         if (httpVersion > 0 && reqHeader.get("Connection") == null) { //$NON-NLS-1$
             output.append("Connection: Keep-Alive\r\n"); //$NON-NLS-1$
         }
@@ -1176,43 +1218,6 @@
                 output.append("Transfer-Encoding: chunked\r\n"); //$NON-NLS-1$
             }
         }
-
-        boolean hasContentLength = false;
-        // then the user-specified request headers, if any
-        for (int i = 0; i < reqHeader.length(); i++) {
-            String key = reqHeader.getKey(i);
-            if (key != null) {
-                String lKey = key.toLowerCase();
-                if ((os != null && !os.isChunked())
-                        || (!lKey.equals("transfer-encoding") && !lKey //$NON-NLS-1$
-                                .equals("content-length"))) { //$NON-NLS-1$
-                    output.append(key);
-                    output.append(": "); //$NON-NLS-1$
-                    /*
-                     * duplicates are allowed under certain conditions see
-                     * http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2
-                     */
-                    if (lKey.equals("content-length")) { //$NON-NLS-1$
-                        hasContentLength = true;
-                        /*
-                         * if both setFixedLengthStreamingMode and
-                         * content-length are set, use fixedContentLength first
-                         */
-                        output.append((fixedContentLength >= 0) ? String
-                                        .valueOf(fixedContentLength)
-                                        : reqHeader.get(i));
-                    } else {
-                        output.append(reqHeader.get(i));
-                    }
-                    output.append("\r\n"); //$NON-NLS-1$
-                }
-            }
-        }
-        if (fixedContentLength >= 0 && !hasContentLength) {
-            output.append("content-length: "); //$NON-NLS-1$
-            output.append(String.valueOf(fixedContentLength));
-            output.append("\r\n"); //$NON-NLS-1$
-        }
         // end the headers
         output.append("\r\n"); //$NON-NLS-1$
         return output.toString().getBytes("ISO8859_1"); //$NON-NLS-1$

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/internal/net/www/protocol/http/HttpURLConnectionTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/internal/net/www/protocol/http/HttpURLConnectionTest.java?rev=731174&r1=731173&r2=731174&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/internal/net/www/protocol/http/HttpURLConnectionTest.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/internal/net/www/protocol/http/HttpURLConnectionTest.java Sat Jan  3 20:43:56 2009
@@ -915,6 +915,38 @@
             System.setSecurityManager(null);
         }
     }
+    
+    /*
+     * @test HttpURLConnection.setRequestProperty
+     */
+    public void testSetRequestProperty() throws Exception {
+        MockHTTPServer httpServer = new MockHTTPServer(
+                "HTTP Server for User-Specified Request Property", 2);
+        httpServer.start();
+        synchronized (bound) {
+            if (!httpServer.started) {
+                bound.wait(5000);
+            }
+        }
+
+        HttpURLConnection urlConnection = (HttpURLConnection) new URL(
+                "http://localhost:" + httpServer.port()).openConnection();
+        assertEquals(0, urlConnection.getRequestProperties().size());
+
+        final String PROPERTY1 = "Accept";
+        final String PROPERTY2 = "Connection";
+        urlConnection.setRequestProperty(PROPERTY1, null);
+        urlConnection.setRequestProperty(PROPERTY1, null);
+        urlConnection.setRequestProperty(PROPERTY2, "keep-alive");
+        assertEquals(2, urlConnection.getRequestProperties().size());
+        assertNull(urlConnection.getRequestProperty(PROPERTY1));
+        assertEquals("keep-alive", urlConnection.getRequestProperty(PROPERTY2));
+
+        urlConnection.setRequestProperty(PROPERTY1, "/");
+        urlConnection.setRequestProperty(PROPERTY2, null);
+        assertEquals("/", urlConnection.getRequestProperty(PROPERTY1));
+        assertNull(urlConnection.getRequestProperty(PROPERTY2));
+    }
 
     private static class MySecurityManager extends SecurityManager {