Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/URLConnection/URLConnectionHeaders.java
41149 views
1
/*
2
* Copyright (c) 2001, 2019, 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
* @library /test/lib
31
*
32
*/
33
34
import java.net.*;
35
import java.util.*;
36
import java.io.*;
37
import jdk.test.lib.net.URIBuilder;
38
import static java.net.Proxy.NO_PROXY;
39
40
public class URLConnectionHeaders {
41
42
static class XServer extends Thread {
43
ServerSocket srv;
44
Socket s;
45
InputStream is;
46
OutputStream os;
47
48
XServer (ServerSocket s) {
49
srv = s;
50
}
51
52
Socket getSocket () {
53
return (s);
54
}
55
56
public void run() {
57
try {
58
String x;
59
s = srv.accept ();
60
is = s.getInputStream ();
61
BufferedReader r = new BufferedReader(new InputStreamReader(is));
62
os = s.getOutputStream ();
63
BufferedWriter w = new BufferedWriter(new OutputStreamWriter(os));
64
w.write("HTTP/1.1 200 OK\r\n");
65
w.write("Content-Type: text/plain\r\n");
66
while ((x=r.readLine()).length() != 0) {
67
System.out.println("request: "+x);
68
if (!x.startsWith("GET")) {
69
w.write(x);
70
w.newLine();
71
}
72
}
73
w.newLine();
74
w.flush();
75
s.close ();
76
} catch (IOException e) { e.printStackTrace();
77
} finally {
78
try { srv.close(); } catch (IOException unused) {}
79
}
80
}
81
}
82
83
public static void main(String[] args) throws Exception {
84
try {
85
InetAddress loopback = InetAddress.getLoopbackAddress();
86
ServerSocket serversocket = new ServerSocket();
87
serversocket.bind(new InetSocketAddress(loopback, 0));
88
int port = serversocket.getLocalPort();
89
XServer server = new XServer(serversocket);
90
server.start();
91
Thread.sleep(200);
92
URL url = URIBuilder.newBuilder()
93
.scheme("http")
94
.loopback()
95
.port(port)
96
.path("/index.html")
97
.toURL();
98
URLConnection uc = url.openConnection(NO_PROXY);
99
100
// add request properties
101
uc.addRequestProperty("Cookie", "cookie1");
102
uc.addRequestProperty("Cookie", "cookie2");
103
uc.addRequestProperty("Cookie", "cookie3");
104
105
Map e = uc.getRequestProperties();
106
107
if (!((List)e.get("Cookie")).toString().equals("[cookie3, cookie2, cookie1]")) {
108
throw new RuntimeException("getRequestProperties fails");
109
}
110
111
e = uc.getHeaderFields();
112
113
if ((e.get("Content-Type") == null) || (e.get(null) == null)) {
114
throw new RuntimeException("getHeaderFields fails");
115
}
116
117
} catch (Exception e) {
118
e.printStackTrace();
119
throw e;
120
}
121
}
122
}
123
124