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