Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/www/protocol/http/B6641309.java
41159 views
1
/*
2
* Copyright (c) 2008, 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 6641309
27
* @modules jdk.httpserver
28
* @library /test/lib
29
* @run main/othervm B6641309
30
* @run main/othervm -Djava.net.preferIPv6Addresses=true B6641309
31
* @summary Wrong Cookie separator used in HttpURLConnection B6641309
32
*/
33
34
import java.net.*;
35
import java.util.*;
36
import java.io.*;
37
import com.sun.net.httpserver.*;
38
import java.util.concurrent.Executors;
39
import java.util.concurrent.ExecutorService;
40
41
import jdk.test.lib.net.URIBuilder;
42
43
public class B6641309
44
{
45
com.sun.net.httpserver.HttpServer httpServer;
46
ExecutorService executorService;
47
48
public static void main(String[] args) throws Exception {
49
new B6641309();
50
}
51
52
public B6641309() throws Exception {
53
startHttpServer();
54
doClient();
55
}
56
57
void doClient() throws Exception {
58
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
59
ProxySelector.setDefault(ProxySelector.of(null));
60
61
InetSocketAddress address = httpServer.getAddress();
62
63
// GET Request
64
URL url = URIBuilder.newBuilder()
65
.scheme("http")
66
.host(address.getAddress())
67
.port(address.getPort())
68
.path("/test/")
69
.toURL();
70
71
CookieHandler ch = CookieHandler.getDefault();
72
Map<String,List<String>> header = new HashMap<String,List<String>>();
73
List<String> values = new LinkedList<String>();
74
values.add("Test1Cookie=TEST1; path=/test/");
75
values.add("Test2Cookie=TEST2; path=/test/");
76
header.put("Set-Cookie", values);
77
78
// preload the CookieHandler with a cookie for our URL
79
// so that it will be sent during the first request
80
ch.put(url.toURI(), header);
81
HttpURLConnection uc = (HttpURLConnection)url.openConnection();
82
int resp = uc.getResponseCode();
83
if (resp != 200) {
84
throw new RuntimeException("Failed: Response code from GET is not 200: "
85
+ resp);
86
}
87
System.out.println("Response code from GET = 200 OK");
88
89
httpServer.stop(1);
90
executorService.shutdown();
91
}
92
93
/**
94
* Http Server
95
*/
96
public void startHttpServer() throws IOException {
97
InetAddress loopback = InetAddress.getLoopbackAddress();
98
InetSocketAddress address = new InetSocketAddress(loopback, 0);
99
httpServer = com.sun.net.httpserver.HttpServer.create(address, 0);
100
101
// create HttpServer context
102
HttpContext ctx = httpServer.createContext("/test/", new MyHandler());
103
104
executorService = Executors.newCachedThreadPool();
105
httpServer.setExecutor(executorService);
106
httpServer.start();
107
}
108
109
class MyHandler implements HttpHandler {
110
public void handle(HttpExchange t) throws IOException {
111
InputStream is = t.getRequestBody();
112
Headers reqHeaders = t.getRequestHeaders();
113
int i = 0;
114
// Read till end of stream
115
do {
116
i = is.read();
117
} while (i != -1);
118
is.close();
119
120
List<String> cookies = reqHeaders.get("Cookie");
121
if (cookies != null) {
122
for (String str : cookies) {
123
// The separator between the 2 cookies should be
124
// a semi-colon AND a space
125
if (str.equals("Test1Cookie=TEST1; Test2Cookie=TEST2"))
126
t.sendResponseHeaders(200, -1);
127
}
128
}
129
t.sendResponseHeaders(400, -1);
130
t.close();
131
}
132
}
133
}
134
135