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