author | never |
Mon, 12 Jul 2010 22:27:18 -0700 | |
changeset 5926 | a36f90d986b6 |
parent 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
5460
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
1 |
/* |
5506 | 2 |
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. |
5460
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
4 |
* |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
8 |
* |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
13 |
* accompanied this code). |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
14 |
* |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
18 |
* |
5506 | 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. |
|
5460
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
22 |
*/ |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
23 |
|
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
24 |
/** |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
25 |
* @test |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
26 |
* @bug 6886723 |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
27 |
* @summary light weight http server doesn't return correct status code for HEAD requests |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
28 |
*/ |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
29 |
|
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
30 |
import java.net.InetSocketAddress; |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
31 |
import java.net.HttpURLConnection; |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
32 |
import java.net.URL; |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
33 |
import java.io.IOException; |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
34 |
import java.util.concurrent.ExecutorService; |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
35 |
import java.util.concurrent.Executors; |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
36 |
import com.sun.net.httpserver.HttpContext; |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
37 |
import com.sun.net.httpserver.HttpExchange; |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
38 |
import com.sun.net.httpserver.HttpHandler; |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
39 |
import com.sun.net.httpserver.HttpServer; |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
40 |
|
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
41 |
public class HeadTest { |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
42 |
|
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
43 |
public static void main(String[] args) throws Exception { |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
44 |
server(); |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
45 |
} |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
46 |
|
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
47 |
static void server() throws Exception { |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
48 |
InetSocketAddress inetAddress = new InetSocketAddress(0); |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
49 |
HttpServer server = HttpServer.create(inetAddress, 5); |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
50 |
try { |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
51 |
server.setExecutor(Executors.newFixedThreadPool(5)); |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
52 |
HttpContext chunkedContext = server.createContext("/chunked"); |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
53 |
chunkedContext.setHandler(new HttpHandler() { |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
54 |
@Override |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
55 |
public void handle(HttpExchange msg) { |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
56 |
try { |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
57 |
try { |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
58 |
if (msg.getRequestMethod().equals("HEAD")) { |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
59 |
msg.getRequestBody().close(); |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
60 |
msg.getResponseHeaders().add("Transfer-encoding", "chunked"); |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
61 |
msg.sendResponseHeaders(200, -1); |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
62 |
} |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
63 |
} catch(IOException ioe) { |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
64 |
ioe.printStackTrace(); |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
65 |
} |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
66 |
} finally { |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
67 |
msg.close(); |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
68 |
} |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
69 |
} |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
70 |
}); |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
71 |
HttpContext clContext = server.createContext("/content"); |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
72 |
clContext.setHandler(new HttpHandler() { |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
73 |
@Override |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
74 |
public void handle(HttpExchange msg) { |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
75 |
try { |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
76 |
try { |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
77 |
if (msg.getRequestMethod().equals("HEAD")) { |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
78 |
msg.getRequestBody().close(); |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
79 |
msg.getResponseHeaders().add("Content-length", "1024"); |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
80 |
msg.sendResponseHeaders(200, -1); |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
81 |
} |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
82 |
} catch(IOException ioe) { |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
83 |
ioe.printStackTrace(); |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
84 |
} |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
85 |
} finally { |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
86 |
msg.close(); |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
87 |
} |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
88 |
} |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
89 |
}); |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
90 |
server.start(); |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
91 |
String urlStr = "http://localhost:" + server.getAddress().getPort() + "/"; |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
92 |
System.out.println("Server is at " + urlStr); |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
93 |
|
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
94 |
// Run the chunked client |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
95 |
for(int i=0; i < 10; i++) { |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
96 |
runClient(urlStr + "chunked/"); |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
97 |
} |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
98 |
// Run the content length client |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
99 |
for(int i=0; i < 10; i++) { |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
100 |
runClient(urlStr + "content/"); |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
101 |
} |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
102 |
} finally { |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
103 |
// Stop the server |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
104 |
((ExecutorService)server.getExecutor()).shutdown(); |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
105 |
server.stop(0); |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
106 |
} |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
107 |
} |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
108 |
|
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
109 |
static void runClient(String urlStr) throws Exception { |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
110 |
HttpURLConnection conn = (HttpURLConnection) new URL(urlStr).openConnection(); |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
111 |
conn.setRequestMethod("HEAD"); |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
112 |
int status = conn.getResponseCode(); |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
113 |
if (status != 200) { |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
114 |
throw new RuntimeException("HEAD request doesn't return 200, but returns " + status); |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
115 |
} |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
116 |
} |
0682ab146d05
6886723: light weight http server doesn't return correct status code for HEAD requests
chegar
parents:
diff
changeset
|
117 |
} |