8133686: HttpURLConnection.getHeaderFields and URLConnection.getRequestProperties methods return field values in reverse order

Reviewed-by: dfuchs
This commit is contained in:
Evan Whelan 2021-09-09 10:23:12 +00:00 committed by Daniel Fuchs
parent aa9311182a
commit 00e059ddb3
7 changed files with 388 additions and 46 deletions

@ -589,6 +589,14 @@ public abstract class URLConnection {
* unmodifiable List of Strings that represents
* the corresponding field values.
*
* This method is overridden by the subclasses of {@code URLConnection}.
*
* In the implementation of these methods, if a given key has multiple
* corresponding values, they must be returned in the order they were added,
* preserving the insertion-order.
*
* @implSpec The default implementation of this method returns an empty map always.
*
* @return a Map of header fields
* @since 1.4
*/
@ -1134,6 +1142,10 @@ public abstract class URLConnection {
* key-value pair. This method will not overwrite
* existing values associated with the same key.
*
* This method could be a no-op if appending a value
* to the map is not supported by the protocol being
* used in a given subclass.
*
* @param key the keyword by which the request is known
* (e.g., "{@code Accept}").
* @param value the value associated with it.
@ -1181,6 +1193,16 @@ public abstract class URLConnection {
* of Strings that represents the corresponding
* field values.
*
* If multiple values for a given key are added via the
* {@link #addRequestProperty(String, String)} method,
* these values will be returned in the order they were
* added. This method must preserve the insertion order
* of such values.
*
* The default implementation of this method preserves the insertion order when
* multiple values are added for a given key. The values are returned in the order they
* were added.
*
* @return a Map of the general request properties for this connection.
* @throws IllegalStateException if already connected
* @since 1.4

@ -1,5 +1,5 @@
/*
* Copyright (c) 1995, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1995, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -245,7 +245,7 @@ class MessageHeader {
String[] excludeList, Map<String, List<String>> include) {
boolean skipIt = false;
Map<String, List<String>> m = new HashMap<>();
for (int i = nkeys; --i >= 0;) {
for (int i = 0; i < nkeys; i++) {
if (excludeList != null) {
// check if the key is in the excludeList.
// if so, don't include it in the Map.

@ -0,0 +1,177 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @bug 8133686
* @summary Ensuring that multiple header values for a given field-name are returned in
* the order they were added for HttpURLConnection.getRequestProperties
* and HttpURLConnection.getHeaderFields
* @library /test/lib
* @run testng HttpURLConnectionHeadersOrder
*/
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import jdk.test.lib.net.URIBuilder;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.io.IOException;
import java.net.*;
import java.util.Arrays;
import java.util.List;
public class HttpURLConnectionHeadersOrder {
private static final String LOCAL_TEST_ENDPOINT = "/headertest";
private static final String ERROR_MESSAGE_TEMPLATE = "Expected Request Properties = %s, Actual Request Properties = %s";
private static final List<String> EXPECTED_HEADER_VALUES = Arrays.asList("a", "b", "c");
private static HttpServer server;
private static URL serverUrl;
@BeforeTest
public void beforeTest() throws Exception {
SimpleHandler handler = new SimpleHandler();
server = createSimpleHttpServer(handler);
serverUrl = URIBuilder.newBuilder()
.scheme("http")
.host(server.getAddress().getAddress())
.port(server.getAddress().getPort())
.path(LOCAL_TEST_ENDPOINT)
.toURL();
}
@AfterTest
public void afterTest() {
if (server != null)
server.stop(0);
}
/**
* - This tests requestProperty insertion-order
* - on the client side by sending a HTTP GET
* - request to a "dummy" server with additional
* - custom request properties
*
* @throws Exception
*/
@Test (priority = 1)
public void testRequestPropertiesOrder() throws Exception {
final var conn = (HttpURLConnection) serverUrl.openConnection();
conn.addRequestProperty("test_req_prop", "a");
conn.addRequestProperty("test_req_prop", "b");
conn.addRequestProperty("test_req_prop", "c");
conn.setRequestMethod("GET");
var requestProperties = conn.getRequestProperties();
var customRequestProps = requestProperties.get("test_req_prop");
conn.disconnect();
Assert.assertNotNull(customRequestProps);
Assert.assertEquals(customRequestProps, EXPECTED_HEADER_VALUES, String.format(ERROR_MESSAGE_TEMPLATE, EXPECTED_HEADER_VALUES.toString(), customRequestProps.toString()));
}
/**
* - This tests whether or not the insertion order is preserved for custom headers
* - on the server's side.
* - The server will return a custom status code (999) if the expected headers
* - are not equal to the actual headers
*
* @throws Exception
*/
@Test (priority = 2)
public void testServerSideRequestHeadersOrder() throws Exception {
final var conn = (HttpURLConnection) serverUrl.openConnection();
conn.addRequestProperty("test_server_handling", "a");
conn.addRequestProperty("test_server_handling", "b");
conn.addRequestProperty("test_server_handling", "c");
int statusCode = conn.getResponseCode();
conn.disconnect();
Assert.assertEquals(statusCode, 999, "The insertion-order was not preserved on the server-side response headers handling");
}
@Test (priority = 3)
public void testClientSideResponseHeadersOrder() throws Exception {
final var conn = (HttpURLConnection) serverUrl.openConnection();
conn.setRequestMethod("GET");
var actualCustomResponseHeaders = conn.getHeaderFields().get("Test_response");
Assert.assertNotNull(actualCustomResponseHeaders, "Error in reading custom response headers");
Assert.assertEquals(EXPECTED_HEADER_VALUES, actualCustomResponseHeaders, String.format(ERROR_MESSAGE_TEMPLATE, EXPECTED_HEADER_VALUES.toString(), actualCustomResponseHeaders.toString()));
}
private static HttpServer createSimpleHttpServer(SimpleHandler handler) throws IOException {
var serverAddress = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
var server = HttpServer.create(serverAddress, 0);
server.createContext(LOCAL_TEST_ENDPOINT, handler);
server.start();
System.out.println("Server started on " + server.getAddress());
return server;
}
private static class SimpleHandler implements HttpHandler {
@Override
public void handle(HttpExchange exchange) throws IOException {
int statusCode = testRequestHeadersOrder(exchange);
sendCustomResponse(exchange, statusCode);
}
private int testRequestHeadersOrder(HttpExchange exchange) {
var requestHeaders = exchange.getRequestHeaders();
var actualTestRequestHeaders = requestHeaders.get("test_server_handling");
if (actualTestRequestHeaders == null) {
System.out.println("Error: requestHeaders.get(\"test_server_handling\") returned null");
return -1;
}
if (!actualTestRequestHeaders.equals(EXPECTED_HEADER_VALUES)) {
System.out.println("Error: " + String.format(ERROR_MESSAGE_TEMPLATE, EXPECTED_HEADER_VALUES.toString(), actualTestRequestHeaders.toString()));
return -1;
}
return 999;
}
private void sendCustomResponse(HttpExchange exchange, int statusCode) throws IOException {
var responseHeaders = exchange.getResponseHeaders();
responseHeaders.add("test_response", "a");
responseHeaders.add("test_response", "b");
responseHeaders.add("test_response", "c");
var outputStream = exchange.getResponseBody();
var response = "Testing headers";
exchange.sendResponseHeaders(statusCode, response.length());
outputStream.write(response.getBytes());
outputStream.flush();
outputStream.close();
}
}
}

@ -1,5 +1,5 @@
/*
* Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -23,21 +23,23 @@
/*
* @test
* @bug 4644775 6230836
* @summary Test URLConnection Request Proterties
* @bug 4644775 6230836 8133686
* @summary Test URLConnection Request Properties
* @run main RequestPropertyValues
*/
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.*;
/**
* Part1:
* bug 4644775: Unexpected NPE in setRequestProperty(key, null) call
* Part2:
* bug 6230836: A few methods of class URLConnection implemented incorrectly
* Part3:
* bug 8133686: Preserving the insertion-order of getRequestProperties
*/
public class RequestPropertyValues {
@ -45,6 +47,7 @@ public class RequestPropertyValues {
public static void main(String[] args) throws Exception {
part1();
part2();
part3();
}
public static void part1() throws Exception {
@ -99,16 +102,50 @@ public class RequestPropertyValues {
}
}
private static void part3() throws Exception{
List<URL> urls = new ArrayList<>();
urls.add(new URL("http://localhost:8088"));
urls.add(new URL("jar:http://foo.com/bar.html!/foo/bar"));
for(URL url : urls) {
System.out.println("Testing " + url.toString().split(":")[0]);
URLConnection urlConnection = url.openConnection();
addCustomRequestProperties(urlConnection);
testRequestPropertiesOrder(urlConnection);
}
}
private static void addCustomRequestProperties(URLConnection urlConnection) {
urlConnection.addRequestProperty("Testprop", "val1");
urlConnection.addRequestProperty("Testprop", "val2");
urlConnection.addRequestProperty("Testprop", "val3");
}
private static void testRequestPropertiesOrder(URLConnection con) {
List<String> expectedTestRequestProperties = Arrays.asList("val1", "val2", "val3");
Map<String, List<String>> requestProperties = con.getRequestProperties();
List<String> actualTestRequestProperties = requestProperties.get("Testprop");
Objects.requireNonNull(actualTestRequestProperties);
if (!actualTestRequestProperties.equals(expectedTestRequestProperties)) {
System.out.println("expectedTestRequestHeaders = " + expectedTestRequestProperties.toString());
System.out.println("actualTestRequestHeaders = " + actualTestRequestProperties.toString());
String errorMessageTemplate = "expectedTestRequestProperties = %s, actualTestRequestProperties = %s";
throw new RuntimeException("expectedTestRequestProperties != actualTestRequestProperties for URL = " + con.getURL().toString() + String.format(errorMessageTemplate, expectedTestRequestProperties.toString(), actualTestRequestProperties.toString()));
}
}
static URLConnection getConnection(URL url) {
return new DummyURLConnection(url);
}
static class DummyURLConnection extends URLConnection {
DummyURLConnection(URL url) {
super(url);
}
public void connect() {
connected = true;
}

@ -1,5 +1,5 @@
/*
* Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -23,7 +23,7 @@
/*
* @test
* @bug 4143541 4147035 4244362
* @bug 4143541 4147035 4244362 8133686
* @summary URLConnection cannot enumerate request properties,
* and URLConnection can neither get nor set multiple
* request properties w/ same key
@ -104,7 +104,7 @@ public class URLConnectionHeaders {
Map e = uc.getRequestProperties();
if (!((List)e.get("Cookie")).toString().equals("[cookie3, cookie2, cookie1]")) {
if (!((List)e.get("Cookie")).toString().equals("[cookie1, cookie2, cookie3]")) {
throw new RuntimeException("getRequestProperties fails");
}

@ -0,0 +1,84 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @bug 8133686
* @summary Ensuring that multiple header values for a given field-name are returned in
* the order they were added for HttpURLConnection.getRequestProperties
* and HttpURLConnection.getHeaderFields
* @library /test/lib
* @run testng URLConnectionHeadersOrder
*/
import jdk.test.lib.net.URIBuilder;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.io.IOException;
import java.net.InetAddress;
import java.net.URL;
import java.net.URLConnection;
import java.util.Arrays;
public class URLConnectionHeadersOrder {
@Test
public void testRequestPropertiesOrder() throws Exception {
var url = URIBuilder.newBuilder()
.scheme("http")
.host(InetAddress.getLoopbackAddress())
.toURL();
var conn = new DummyHttpURLConnection(url);
conn.addRequestProperty("test", "a");
conn.addRequestProperty("test", "b");
conn.addRequestProperty("test", "c");
conn.connect();
var expectedRequestProps = Arrays.asList("a", "b", "c");
var actualRequestProps = conn.getRequestProperties().get("test");
Assert.assertNotNull(actualRequestProps);
String errorMessageTemplate = "Expected Request Properties = %s, Actual Request Properties = %s";
Assert.assertEquals(actualRequestProps, expectedRequestProps, String.format(errorMessageTemplate, expectedRequestProps.toString(), actualRequestProps.toString()));
}
}
class DummyHttpURLConnection extends URLConnection {
/**
* Constructs a URL connection to the specified URL. A connection to
* the object referenced by the URL is not created.
*
* @param url the specified URL.
*/
protected DummyHttpURLConnection(URL url) {
super(url);
}
@Override
public void connect() throws IOException {
var connected = true;
}
}

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -23,15 +23,65 @@
/**
* @test
* @bug 8003948
* @bug 8003948 8133686
* @modules java.base/sun.net.www
* @run main MessageHeaderTest
* @run testng MessageHeaderTest
*/
import java.io.*;
import java.util.*;
import org.testng.Assert;
import org.testng.annotations.Test;
import sun.net.www.MessageHeader;
public class MessageHeaderTest {
public static void main (String[] args) throws Exception {
/* This test checks to see if the MessageHeader.getHeaders method
returns headers with the same value field in the order they were added
to conform with RFC2616
*/
@Test
public void reverseMessageHeadersTest() throws Exception {
String errorMessageTemplate = "Expected Headers = %s, Actual Headers = %s";
var expectedHeaders = Arrays.asList("a", "b", "c");
MessageHeader testHeader = new MessageHeader();
testHeader.add("test", "a");
testHeader.add("test", "b");
testHeader.add("test", "c");
var actualHeaders = testHeader.getHeaders().get("test");
Assert.assertEquals(expectedHeaders, actualHeaders, String.format(errorMessageTemplate, expectedHeaders.toString(), actualHeaders.toString()));
}
@Test
public void ntlmNegotiateTest () throws Exception {
String expected[] = {
"{null: HTTP/1.1 200 Ok}{Foo: bar}{Bar: foo}{WWW-Authenticate: NTLM sdsds}",
"{null: HTTP/1.1 200 Ok}{Foo: bar}{Bar: foo}{WWW-Authenticate: }",
"{null: HTTP/1.1 200 Ok}{Foo: bar}{Bar: foo}{WWW-Authenticate: NTLM sdsds}",
"{null: HTTP/1.1 200 Ok}{Foo: bar}{Bar: foo}{WWW-Authenticate: NTLM sdsds}",
"{null: HTTP/1.1 200 Ok}{Foo: bar}{Bar: foo}{WWW-Authenticate: NTLM sdsds}{Bar: foo}",
"{null: HTTP/1.1 200 Ok}{WWW-Authenticate: Negotiate}{Foo: bar}{Bar: foo}{WWW-Authenticate: NTLM}{Bar: foo}{WWW-Authenticate: Kerberos}",
"{null: HTTP/1.1 200 Ok}{Foo: foo}{Bar: }{WWW-Authenticate: NTLM blob}{Bar: foo blob}"
};
boolean[] expectedResult = {
false, false, true, true, true, false, false
};
String[] headers = {
"HTTP/1.1 200 Ok\r\nFoo: bar\r\nBar: foo\r\nWWW-Authenticate: NTLM sdsds",
"HTTP/1.1 200 Ok\r\nFoo: bar\r\nBar: foo\r\nWWW-Authenticate:",
"HTTP/1.1 200 Ok\r\nFoo: bar\r\nBar: foo\r\nWWW-Authenticate: NTLM sdsds\r\nWWW-Authenticate: Negotiate",
"HTTP/1.1 200 Ok\r\nFoo: bar\r\nBar: foo\r\nWWW-Authenticate: NTLM sdsds\r\nWWW-Authenticate: Negotiate\r\nWWW-Authenticate: Kerberos",
"HTTP/1.1 200 Ok\r\nWWW-Authenticate: Negotiate\r\nFoo: bar\r\nBar: foo\r\nWWW-Authenticate: NTLM sdsds\r\nBar: foo\r\nWWW-Authenticate: Kerberos",
"HTTP/1.1 200 Ok\r\nWWW-Authenticate: Negotiate\r\nFoo: bar\r\nBar: foo\r\nWWW-Authenticate: NTLM\r\nBar: foo\r\nWWW-Authenticate: Kerberos",
"HTTP/1.1 200 Ok\r\nFoo: foo\r\nBar:\r\nWWW-Authenticate: NTLM blob\r\nBar: foo blob"
};
for (int i=0; i<7; i++) {
ByteArrayInputStream bis = new ByteArrayInputStream(headers[i].getBytes());
MessageHeader h = new MessageHeader(bis);
@ -40,36 +90,8 @@ public class MessageHeaderTest {
boolean result = h.filterNTLMResponses("WWW-Authenticate");
String after = h.toString();
after = after.substring(after.indexOf('{'));
if (!expected[i].equals(after)) {
throw new RuntimeException(Integer.toString(i) + " expected != after");
}
if (result != expectedResult[i]) {
throw new RuntimeException(Integer.toString(i) + " result != expectedResult");
}
Assert.assertEquals(expected[i], after, i + " expected != after");
Assert.assertEquals(result, expectedResult[i], i + " result != expectedResult");
}
}
static String expected[] = {
"{null: HTTP/1.1 200 Ok}{Foo: bar}{Bar: foo}{WWW-Authenticate: NTLM sdsds}",
"{null: HTTP/1.1 200 Ok}{Foo: bar}{Bar: foo}{WWW-Authenticate: }",
"{null: HTTP/1.1 200 Ok}{Foo: bar}{Bar: foo}{WWW-Authenticate: NTLM sdsds}",
"{null: HTTP/1.1 200 Ok}{Foo: bar}{Bar: foo}{WWW-Authenticate: NTLM sdsds}",
"{null: HTTP/1.1 200 Ok}{Foo: bar}{Bar: foo}{WWW-Authenticate: NTLM sdsds}{Bar: foo}",
"{null: HTTP/1.1 200 Ok}{WWW-Authenticate: Negotiate}{Foo: bar}{Bar: foo}{WWW-Authenticate: NTLM}{Bar: foo}{WWW-Authenticate: Kerberos}",
"{null: HTTP/1.1 200 Ok}{Foo: foo}{Bar: }{WWW-Authenticate: NTLM blob}{Bar: foo blob}"
};
static boolean[] expectedResult = {
false, false, true, true, true, false, false
};
static String[] headers = {
"HTTP/1.1 200 Ok\r\nFoo: bar\r\nBar: foo\r\nWWW-Authenticate: NTLM sdsds",
"HTTP/1.1 200 Ok\r\nFoo: bar\r\nBar: foo\r\nWWW-Authenticate:",
"HTTP/1.1 200 Ok\r\nFoo: bar\r\nBar: foo\r\nWWW-Authenticate: NTLM sdsds\r\nWWW-Authenticate: Negotiate",
"HTTP/1.1 200 Ok\r\nFoo: bar\r\nBar: foo\r\nWWW-Authenticate: NTLM sdsds\r\nWWW-Authenticate: Negotiate\r\nWWW-Authenticate: Kerberos",
"HTTP/1.1 200 Ok\r\nWWW-Authenticate: Negotiate\r\nFoo: bar\r\nBar: foo\r\nWWW-Authenticate: NTLM sdsds\r\nBar: foo\r\nWWW-Authenticate: Kerberos",
"HTTP/1.1 200 Ok\r\nWWW-Authenticate: Negotiate\r\nFoo: bar\r\nBar: foo\r\nWWW-Authenticate: NTLM\r\nBar: foo\r\nWWW-Authenticate: Kerberos",
"HTTP/1.1 200 Ok\r\nFoo: foo\r\nBar:\r\nWWW-Authenticate: NTLM blob\r\nBar: foo blob"
};
}