author | chegar |
Fri, 27 Sep 2019 09:55:35 +0100 | |
changeset 58365 | 73950479184b |
parent 52554 | 5f1ca46703f9 |
permissions | -rw-r--r-- |
52554
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
1 |
/* |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
2 |
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
4 |
* |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
8 |
* |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
13 |
* accompanied this code). |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
14 |
* |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
18 |
* |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
21 |
* questions. |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
22 |
*/ |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
23 |
|
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
24 |
/* |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
25 |
* @test |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
26 |
* @bug 8178699 |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
27 |
* @modules java.net.http |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
28 |
* @run main/othervm RestrictedHeadersTest |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
29 |
* @run main/othervm -Djdk.httpclient.allowRestrictedHeaders=content-length,connection RestrictedHeadersTest content-length connection |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
30 |
* @run main/othervm -Djdk.httpclient.allowRestrictedHeaders=host,upgrade RestrictedHeadersTest host upgrade |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
31 |
* @run main/othervm -Djdk.httpclient.allowRestrictedHeaders=via RestrictedHeadersTest via |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
32 |
*/ |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
33 |
|
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
34 |
import java.net.URI; |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
35 |
import java.net.http.HttpRequest; |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
36 |
import java.util.Set; |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
37 |
|
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
38 |
public class RestrictedHeadersTest { |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
39 |
public static void main(String[] args) { |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
40 |
if (args.length == 0) { |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
41 |
runDefaultTest(); |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
42 |
} else { |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
43 |
runTest(Set.of(args)); |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
44 |
} |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
45 |
} |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
46 |
|
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
47 |
// This list must be same as impl |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
48 |
|
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
49 |
static Set<String> defaultRestrictedHeaders = |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
50 |
Set.of("connection", "content-length", "expect", "host", "upgrade"); |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
51 |
|
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
52 |
private static void runDefaultTest() { |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
53 |
System.out.println("DEFAULT TEST: no property set"); |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
54 |
for (String header : defaultRestrictedHeaders) { |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
55 |
checkHeader(header, "foo", false); |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
56 |
} |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
57 |
// miscellaneous others that should succeed |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
58 |
checkHeader("foobar", "barfoo", true); |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
59 |
checkHeader("date", "today", true); |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
60 |
} |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
61 |
|
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
62 |
private static void checkHeader(String name, String value, boolean succeed) { |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
63 |
try { |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
64 |
HttpRequest request = HttpRequest.newBuilder(URI.create("https://foo.com/")) |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
65 |
.header(name, value) |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
66 |
.GET() |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
67 |
.build(); |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
68 |
if (!succeed) { |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
69 |
String s = name+"/"+value+" should have failed"; |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
70 |
throw new RuntimeException(s); |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
71 |
} |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
72 |
System.out.printf("%s = %s succeeded as expected\n", name, value); |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
73 |
} catch (IllegalArgumentException iae) { |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
74 |
if (succeed) { |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
75 |
String s = name+"/"+value+" should have succeeded"; |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
76 |
throw new RuntimeException(s); |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
77 |
} |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
78 |
System.out.printf("%s = %s failed as expected\n", name, value); |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
79 |
} |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
80 |
} |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
81 |
|
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
82 |
// args is the Set of allowed restricted headers |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
83 |
private static void runTest(Set<String> args) { |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
84 |
System.out.print("RUNTEST: allowed headers set in property: "); |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
85 |
for (String arg : args) System.out.printf("%s ", arg); |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
86 |
System.out.println(""); |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
87 |
|
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
88 |
for (String header : args) { |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
89 |
checkHeader(header, "val", true); |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
90 |
} |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
91 |
for (String header : defaultRestrictedHeaders) { |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
92 |
if (!args.contains(header)) { |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
93 |
checkHeader(header, "foo", false); |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
94 |
} |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
95 |
} |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
96 |
} |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
diff
changeset
|
97 |
} |