Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/net/httpserver/bugs/HeadTest.java
41155 views
1
/*
2
* Copyright (c) 2010, 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 6886723
27
* @library /test/lib
28
* @run main HeadTest
29
* @run main/othervm -Djava.net.preferIPv6Addresses=true HeadTest
30
* @summary light weight http server doesn't return correct status code for HEAD requests
31
*/
32
33
import java.net.InetAddress;
34
import java.net.InetSocketAddress;
35
import java.net.HttpURLConnection;
36
import java.net.Proxy;
37
import java.net.URL;
38
import java.io.IOException;
39
import java.util.concurrent.ExecutorService;
40
import java.util.concurrent.Executors;
41
import com.sun.net.httpserver.HttpContext;
42
import com.sun.net.httpserver.HttpExchange;
43
import com.sun.net.httpserver.HttpHandler;
44
import com.sun.net.httpserver.HttpServer;
45
import jdk.test.lib.net.URIBuilder;
46
47
public class HeadTest {
48
49
public static void main(String[] args) throws Exception {
50
server();
51
}
52
53
static void server() throws Exception {
54
InetAddress loopback = InetAddress.getLoopbackAddress();
55
InetSocketAddress inetAddress = new InetSocketAddress(loopback, 0);
56
HttpServer server = HttpServer.create(inetAddress, 5);
57
try {
58
server.setExecutor(Executors.newFixedThreadPool(5));
59
HttpContext chunkedContext = server.createContext("/chunked");
60
chunkedContext.setHandler(new HttpHandler() {
61
@Override
62
public void handle(HttpExchange msg) {
63
try {
64
try {
65
if (msg.getRequestMethod().equals("HEAD")) {
66
msg.getRequestBody().close();
67
msg.getResponseHeaders().add("Transfer-encoding", "chunked");
68
msg.sendResponseHeaders(200, -1);
69
}
70
} catch(IOException ioe) {
71
ioe.printStackTrace();
72
}
73
} finally {
74
msg.close();
75
}
76
}
77
});
78
HttpContext clContext = server.createContext("/content");
79
clContext.setHandler(new HttpHandler() {
80
@Override
81
public void handle(HttpExchange msg) {
82
try {
83
try {
84
if (msg.getRequestMethod().equals("HEAD")) {
85
msg.getRequestBody().close();
86
msg.getResponseHeaders().add("Content-length", "1024");
87
msg.sendResponseHeaders(200, -1);
88
}
89
} catch(IOException ioe) {
90
ioe.printStackTrace();
91
}
92
} finally {
93
msg.close();
94
}
95
}
96
});
97
server.start();
98
String urlStr = URIBuilder.newBuilder()
99
.scheme("http")
100
.loopback()
101
.port(server.getAddress().getPort())
102
.path("/")
103
.build()
104
.toString();
105
System.out.println("Server is at " + urlStr);
106
107
// Run the chunked client
108
for(int i=0; i < 10; i++) {
109
runClient(urlStr + "chunked/");
110
}
111
// Run the content length client
112
for(int i=0; i < 10; i++) {
113
runClient(urlStr + "content/");
114
}
115
} finally {
116
// Stop the server
117
((ExecutorService)server.getExecutor()).shutdown();
118
server.stop(0);
119
}
120
}
121
122
static void runClient(String urlStr) throws Exception {
123
HttpURLConnection conn = (HttpURLConnection) new URL(urlStr).openConnection(Proxy.NO_PROXY);
124
conn.setRequestMethod("HEAD");
125
int status = conn.getResponseCode();
126
if (status != 200) {
127
throw new RuntimeException("HEAD request doesn't return 200, but returns " + status);
128
}
129
}
130
}
131
132