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/UserCookie.java
38867 views
1
/*
2
* Copyright (c) 2006, 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 6439651
27
* @run main/othervm UserAuth
28
* @summary Sending "Cookie" header with JRE 1.5.0_07 doesn't work anymore
29
*/
30
31
import java.net.*;
32
import com.sun.net.httpserver.*;
33
import java.util.*;
34
import java.io.*;
35
36
public class UserCookie
37
{
38
com.sun.net.httpserver.HttpServer httpServer;
39
40
public static void main(String[] args) {
41
new UserCookie();
42
}
43
44
public UserCookie() {
45
try {
46
startHttpServer();
47
doClient();
48
} catch (IOException ioe) {
49
ioe.printStackTrace();
50
}
51
}
52
53
void doClient() {
54
try {
55
// set default CookieHandler to accept only accepts cookies from original server.
56
CookieHandler.setDefault(new CookieManager());
57
58
InetSocketAddress address = httpServer.getAddress();
59
60
URL url = new URL("http://" + address.getHostName() + ":" + address.getPort() + "/test/");
61
HttpURLConnection uc = (HttpURLConnection)url.openConnection();
62
uc.setRequestProperty("Cookie", "value=ValueDoesNotMatter");
63
int resp = uc.getResponseCode();
64
65
System.out.println("Response Code is " + resp);
66
if (resp != 200)
67
throw new RuntimeException("Failed: Cookie header was not retained");
68
69
} catch (IOException e) {
70
e.printStackTrace();
71
} finally {
72
httpServer.stop(1);
73
}
74
}
75
76
/**
77
* Http Server
78
*/
79
void startHttpServer() throws IOException {
80
httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);
81
82
// create HttpServer context
83
HttpContext ctx = httpServer.createContext("/test/", new MyHandler());
84
85
httpServer.start();
86
}
87
88
class MyHandler implements HttpHandler {
89
public void handle(HttpExchange t) throws IOException {
90
Headers reqHeaders = t.getRequestHeaders();
91
92
List<String> cookie = reqHeaders.get("Cookie");
93
94
if (cookie == null || !cookie.get(0).equals("value=ValueDoesNotMatter"))
95
t.sendResponseHeaders(400, -1);
96
97
t.sendResponseHeaders(200, -1);
98
t.close();
99
}
100
}
101
102
103
104
}
105
106