Return-Path: Delivered-To: apmail-harmony-commits-archive@www.apache.org Received: (qmail 41828 invoked from network); 4 Jan 2009 04:44:20 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 4 Jan 2009 04:44:20 -0000 Received: (qmail 63879 invoked by uid 500); 4 Jan 2009 04:44:20 -0000 Delivered-To: apmail-harmony-commits-archive@harmony.apache.org Received: (qmail 63859 invoked by uid 500); 4 Jan 2009 04:44:20 -0000 Mailing-List: contact commits-help@harmony.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@harmony.apache.org Delivered-To: mailing list commits@harmony.apache.org Received: (qmail 63850 invoked by uid 99); 4 Jan 2009 04:44:20 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 03 Jan 2009 20:44:20 -0800 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 04 Jan 2009 04:44:18 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id F40AF238896F; Sat, 3 Jan 2009 20:43:56 -0800 (PST) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: 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/ Date: Sun, 04 Jan 2009 04:43:56 -0000 To: commits@harmony.apache.org From: qiuxx@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090104044356.F40AF238896F@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 {