Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/www/protocol/http/UserCookie.java
41159 views
1
/*
2
* Copyright (c) 2006, 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 6439651
27
* @modules jdk.httpserver
28
* @run main/othervm UserAuth
29
* @summary Sending "Cookie" header with JRE 1.5.0_07 doesn't work anymore
30
*/
31
32
import java.net.*;
33
import com.sun.net.httpserver.*;
34
import java.util.*;
35
import java.io.*;
36
import static java.net.Proxy.NO_PROXY;
37
38
public class UserCookie
39
{
40
com.sun.net.httpserver.HttpServer httpServer;
41
42
public static void main(String[] args) {
43
new UserCookie();
44
}
45
46
public UserCookie() {
47
try {
48
startHttpServer();
49
doClient();
50
} catch (IOException ioe) {
51
ioe.printStackTrace();
52
}
53
}
54
55
void doClient() {
56
try {
57
// set default CookieHandler to accept only accepts cookies from original server.
58
CookieHandler.setDefault(new CookieManager());
59
60
InetSocketAddress address = httpServer.getAddress();
61
62
URL url = new URL("http://" + address.getHostName() + ":" + address.getPort() + "/test/");
63
HttpURLConnection uc = (HttpURLConnection)url.openConnection(NO_PROXY);
64
uc.setRequestProperty("Cookie", "value=ValueDoesNotMatter");
65
int resp = uc.getResponseCode();
66
67
System.out.println("Response Code is " + resp);
68
if (resp != 200)
69
throw new RuntimeException("Failed: Cookie header was not retained");
70
71
} catch (IOException e) {
72
e.printStackTrace();
73
} finally {
74
httpServer.stop(1);
75
}
76
}
77
78
/**
79
* Http Server
80
*/
81
void startHttpServer() throws IOException {
82
InetAddress address = InetAddress.getLocalHost();
83
if (!InetAddress.getByName(address.getHostName()).equals(address)) {
84
// if this happens then we should possibly change the client
85
// side to use the address literal in its URL instead of
86
// the host name.
87
throw new IOException(address.getHostName()
88
+ " resolves to "
89
+ InetAddress.getByName(address.getHostName())
90
+ " not to "
91
+ address + ": check host configuration.");
92
}
93
94
httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(address, 0), 0);
95
96
// create HttpServer context
97
HttpContext ctx = httpServer.createContext("/test/", new MyHandler());
98
99
httpServer.start();
100
}
101
102
class MyHandler implements HttpHandler {
103
public void handle(HttpExchange t) throws IOException {
104
Headers reqHeaders = t.getRequestHeaders();
105
106
List<String> cookie = reqHeaders.get("Cookie");
107
108
if (cookie == null || !cookie.get(0).equals("value=ValueDoesNotMatter"))
109
t.sendResponseHeaders(400, -1);
110
111
t.sendResponseHeaders(200, -1);
112
t.close();
113
}
114
}
115
116
117
118
}
119
120