Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/www/http/HttpClient/B7025238.java
41154 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
import com.sun.net.httpserver.*;
25
import java.io.IOException;
26
import java.io.OutputStream;
27
import java.net.*;
28
import java.util.concurrent.Executors;
29
import jdk.test.lib.net.URIBuilder;
30
31
/*
32
* @test
33
* @bug 7025238
34
* @modules jdk.httpserver
35
* @library /test/lib
36
* @summary HttpURLConnection does not handle URLs with an empty path component
37
*/
38
public class B7025238 {
39
40
public static void main(String[] args) throws Exception {
41
new B7025238().runTest();
42
}
43
44
public void runTest() throws Exception {
45
Server s = null;
46
try {
47
s = new Server();
48
s.startServer();
49
URL url = URIBuilder.newBuilder()
50
.scheme("http")
51
.loopback()
52
.port(s.getPort())
53
.query("q=test")
54
.toURL();
55
System.out.println("Connecting to: " + url);
56
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
57
urlConnection.setRequestMethod("GET");
58
urlConnection.connect();
59
int code = urlConnection.getResponseCode();
60
61
if (code != 200) {
62
throw new RuntimeException("Test failed!");
63
}
64
} finally {
65
s.stopServer();
66
}
67
}
68
69
class Server {
70
HttpServer server;
71
72
public void startServer() {
73
InetAddress loopback = InetAddress.getLoopbackAddress();
74
InetSocketAddress addr = new InetSocketAddress(loopback, 0);
75
try {
76
server = HttpServer.create(addr, 0);
77
} catch (IOException ioe) {
78
throw new RuntimeException("Server could not be created");
79
}
80
81
server.createContext("/", new EmptyPathHandler());
82
server.start();
83
}
84
85
public int getPort() {
86
return server.getAddress().getPort();
87
}
88
89
public void stopServer() {
90
server.stop(0);
91
}
92
}
93
94
class EmptyPathHandler implements HttpHandler {
95
96
@Override
97
public void handle(HttpExchange exchange) throws IOException {
98
String requestMethod = exchange.getRequestMethod();
99
100
if (requestMethod.equalsIgnoreCase("GET")) {
101
Headers responseHeaders = exchange.getResponseHeaders();
102
responseHeaders.set("Content-Type", "text/plain");
103
exchange.sendResponseHeaders(200, 0);
104
OutputStream os = exchange.getResponseBody();
105
String str = "Hello from server!";
106
os.write(str.getBytes());
107
os.flush();
108
os.close();
109
}
110
}
111
}
112
}
113
114