Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/net/httpserver/Test11.java
41152 views
1
/*
2
* Copyright (c) 2005, 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 6270015
27
* @summary Light weight HTTP server
28
* @library /test/lib
29
* @run main Test11
30
* @run main/othervm -Djava.net.preferIPv6Addresses=true Test11
31
*/
32
33
import java.net.*;
34
import java.util.concurrent.*;
35
import java.io.*;
36
import com.sun.net.httpserver.*;
37
import jdk.test.lib.net.URIBuilder;
38
39
public class Test11 {
40
static class Handler implements HttpHandler {
41
public void handle(HttpExchange t) throws IOException {
42
read (t.getRequestBody());
43
String response = "response";
44
t.sendResponseHeaders (200, response.length());
45
OutputStream os = t.getResponseBody();
46
os.write (response.getBytes ("ISO8859_1"));
47
t.close();
48
}
49
50
void read (InputStream is ) throws IOException {
51
byte[] b = new byte [8096];
52
while (is.read (b) != -1) {}
53
}
54
}
55
56
public static void main (String[] args) throws Exception {
57
System.out.print ("Test 11: ");
58
InetAddress loopback = InetAddress.getLoopbackAddress();
59
HttpServer server = HttpServer.create(new InetSocketAddress(loopback, 0), 0);
60
ExecutorService s = Executors.newCachedThreadPool();
61
try {
62
HttpContext ctx = server.createContext (
63
"/foo/bar/", new Handler ()
64
);
65
s = Executors.newCachedThreadPool();
66
server.start ();
67
URL url = URIBuilder.newBuilder()
68
.scheme("http")
69
.loopback()
70
.port(server.getAddress().getPort())
71
.path("/Foo/bar/test.html")
72
.toURL();
73
HttpURLConnection urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
74
int r = urlc.getResponseCode();
75
if (r == 200) {
76
throw new RuntimeException ("wrong response received");
77
}
78
System.out.println ("OK");
79
} finally {
80
s.shutdown();
81
server.stop(2);
82
}
83
}
84
}
85
86