Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/CookieHandler/EmptyCookieHeader.java
41149 views
1
/*
2
* Copyright (c) 2013, 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 8015799
27
* @modules jdk.httpserver
28
* @summary HttpURLConnection.getHeaderFields() throws IllegalArgumentException
29
* @library /test/lib
30
* @run main EmptyCookieHeader
31
* @run main/othervm -Djava.net.preferIPv6Addresses=true EmptyCookieHeader
32
*/
33
34
import com.sun.net.httpserver.*;
35
import java.io.IOException;
36
import java.io.OutputStream;
37
import java.net.*;
38
import java.util.*;
39
import jdk.test.lib.net.URIBuilder;
40
41
public class EmptyCookieHeader {
42
43
public static void main(String[] args) throws Exception {
44
new EmptyCookieHeader().runTest();
45
}
46
47
public void runTest() throws Exception {
48
final CookieHandler oldHandler = CookieHandler.getDefault();
49
CookieHandler.setDefault(new TestCookieHandler());
50
InetAddress loopback = InetAddress.getLoopbackAddress();
51
HttpServer s = HttpServer.create(new InetSocketAddress(loopback, 0), 0);
52
try {
53
startServer(s);
54
URL url = URIBuilder.newBuilder()
55
.scheme("http")
56
.loopback()
57
.port(s.getAddress().getPort())
58
.path("/")
59
.toURL();
60
HttpURLConnection c = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
61
c.getHeaderFields();
62
} finally {
63
CookieHandler.setDefault(oldHandler);
64
s.stop(0);
65
}
66
}
67
68
static void startServer(HttpServer server) throws IOException {
69
server.createContext("/", new EmptyCookieHandler());
70
server.start();
71
}
72
73
static class EmptyCookieHandler implements HttpHandler {
74
75
@Override
76
public void handle(HttpExchange exchange) throws IOException {
77
String requestMethod = exchange.getRequestMethod();
78
if (requestMethod.equalsIgnoreCase("GET")) {
79
Headers responseHeaders = exchange.getResponseHeaders();
80
responseHeaders.set("Content-Type", "text/plain");
81
responseHeaders.set("Date", "June 13th 2012");
82
83
// No domain value set
84
responseHeaders.set("Set-Cookie2", "name=value");
85
responseHeaders.set("Set-Cookie2", "");
86
exchange.sendResponseHeaders(200, 0);
87
try (OutputStream os = exchange.getResponseBody()) {
88
String str = "This is what the server sent!";
89
os.write(str.getBytes());
90
}
91
}
92
}
93
}
94
95
class TestCookieHandler extends CookieHandler {
96
@Override
97
public Map<String,List<String>> get(URI uri,
98
Map<String,List<String>> respH) {
99
return new HashMap<>();
100
}
101
102
@Override
103
public void put(URI uri, Map<String,List<String >> respH) { }
104
}
105
}
106
107