Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/URLConnection/URLConnectionHeaders.java
38821 views
1
/*
2
* Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/*
25
* @test
26
* @bug 4143541 4147035 4244362
27
* @summary URLConnection cannot enumerate request properties,
28
* and URLConnection can neither get nor set multiple
29
* request properties w/ same key
30
*
31
*/
32
33
import java.net.*;
34
import java.util.*;
35
import java.io.*;
36
37
public class URLConnectionHeaders {
38
39
static class XServer extends Thread {
40
ServerSocket srv;
41
Socket s;
42
InputStream is;
43
OutputStream os;
44
45
XServer (ServerSocket s) {
46
srv = s;
47
}
48
49
Socket getSocket () {
50
return (s);
51
}
52
53
public void run() {
54
try {
55
String x;
56
s = srv.accept ();
57
is = s.getInputStream ();
58
BufferedReader r = new BufferedReader(new InputStreamReader(is));
59
os = s.getOutputStream ();
60
BufferedWriter w = new BufferedWriter(new OutputStreamWriter(os));
61
w.write("HTTP/1.1 200 OK\r\n");
62
w.write("Content-Type: text/plain\r\n");
63
while ((x=r.readLine()).length() != 0) {
64
System.out.println("request: "+x);
65
if (!x.startsWith("GET")) {
66
w.write(x);
67
w.newLine();
68
}
69
}
70
w.newLine();
71
w.flush();
72
s.close ();
73
} catch (IOException e) { e.printStackTrace();
74
} finally {
75
try { srv.close(); } catch (IOException unused) {}
76
}
77
}
78
}
79
80
public static void main(String[] args) {
81
try {
82
ServerSocket serversocket = new ServerSocket (0);
83
int port = serversocket.getLocalPort ();
84
XServer server = new XServer (serversocket);
85
server.start ();
86
Thread.sleep (200);
87
URL url = new URL ("http://localhost:"+port+"/index.html");
88
URLConnection uc = url.openConnection ();
89
90
// add request properties
91
uc.addRequestProperty("Cookie", "cookie1");
92
uc.addRequestProperty("Cookie", "cookie2");
93
uc.addRequestProperty("Cookie", "cookie3");
94
95
Map e = uc.getRequestProperties();
96
97
if (!((List)e.get("Cookie")).toString().equals("[cookie3, cookie2, cookie1]")) {
98
throw new RuntimeException("getRequestProperties fails");
99
}
100
101
e = uc.getHeaderFields();
102
103
if ((e.get("Content-Type") == null) || (e.get(null) == null)) {
104
throw new RuntimeException("getHeaderFields fails");
105
}
106
107
} catch (Exception e) {
108
e.printStackTrace();
109
}
110
}
111
}
112
113