7271
|
1 |
/*
|
|
2 |
* Copyright (c) 2005, 2006, 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 6725892
|
|
27 |
* @run main/othervm -Dsun.net.httpserver.maxReqTime=2 Test
|
|
28 |
* @summary
|
|
29 |
*/
|
|
30 |
|
|
31 |
import com.sun.net.httpserver.*;
|
|
32 |
|
|
33 |
import java.util.concurrent.*;
|
|
34 |
import java.util.logging.*;
|
|
35 |
import java.io.*;
|
|
36 |
import java.net.*;
|
|
37 |
import javax.net.ssl.*;
|
|
38 |
|
|
39 |
public class Test {
|
|
40 |
|
|
41 |
static HttpServer s1;
|
|
42 |
static int port;
|
|
43 |
static URL url;
|
|
44 |
static final String RESPONSE_BODY = "response";
|
|
45 |
static boolean failed = false;
|
|
46 |
|
|
47 |
static class Handler implements HttpHandler {
|
|
48 |
|
|
49 |
public void handle (HttpExchange t)
|
|
50 |
throws IOException
|
|
51 |
{
|
|
52 |
InputStream is = t.getRequestBody();
|
|
53 |
InetSocketAddress rem = t.getRemoteAddress();
|
|
54 |
System.out.println ("Request from: " + rem);
|
|
55 |
while (is.read () != -1) ;
|
|
56 |
is.close();
|
|
57 |
String requrl = t.getRequestURI().toString();
|
|
58 |
OutputStream os = t.getResponseBody();
|
|
59 |
t.sendResponseHeaders (200, RESPONSE_BODY.length());
|
|
60 |
os.write (RESPONSE_BODY.getBytes());
|
|
61 |
t.close();
|
|
62 |
}
|
|
63 |
}
|
|
64 |
|
|
65 |
public static void main (String[] args) throws Exception {
|
|
66 |
|
|
67 |
ExecutorService exec = Executors.newCachedThreadPool();
|
|
68 |
//Logger log = Logger.getLogger ("com.sun.net.httpserver");
|
|
69 |
//log.setLevel(Level.ALL);
|
|
70 |
//ConsoleHandler hg = new ConsoleHandler();
|
|
71 |
//hg.setLevel (Level.ALL);
|
|
72 |
//log.addHandler(hg);
|
|
73 |
|
|
74 |
sun.net.httpserver.HttpServerImpl x = null;
|
|
75 |
try {
|
|
76 |
InetSocketAddress addr = new InetSocketAddress (0);
|
|
77 |
s1 = HttpServer.create (addr, 0);
|
|
78 |
HttpHandler h = new Handler ();
|
|
79 |
HttpContext c1 = s1.createContext ("/", h);
|
|
80 |
s1.setExecutor(exec);
|
|
81 |
s1.start();
|
|
82 |
|
|
83 |
port = s1.getAddress().getPort();
|
|
84 |
System.out.println ("Server on port " + port);
|
|
85 |
url = new URL ("http://rialto.ireland:"+port+"/foo");
|
|
86 |
test1();
|
|
87 |
test2();
|
|
88 |
test3();
|
|
89 |
x = (sun.net.httpserver.HttpServerImpl)s1;
|
|
90 |
Thread.sleep (2000);
|
|
91 |
} catch (Exception e) {
|
|
92 |
e.printStackTrace();
|
|
93 |
System.out.println ("FAIL");
|
|
94 |
throw new RuntimeException ();
|
|
95 |
} finally {
|
|
96 |
s1.stop(0);
|
|
97 |
System.out.println ("After Shutdown");
|
|
98 |
exec.shutdown();
|
|
99 |
}
|
|
100 |
}
|
|
101 |
|
|
102 |
// open TCP connection without sending anything. Check server closes it.
|
|
103 |
|
|
104 |
static void test1() throws IOException {
|
|
105 |
failed = false;
|
|
106 |
Socket s = new Socket ("127.0.0.1", port);
|
|
107 |
InputStream is = s.getInputStream();
|
|
108 |
// server should close connection after 2 seconds. We wait up to 10
|
|
109 |
s.setSoTimeout (10000);
|
|
110 |
try {
|
|
111 |
is.read();
|
|
112 |
} catch (SocketTimeoutException e) {
|
|
113 |
failed = true;
|
|
114 |
}
|
|
115 |
s.close();
|
|
116 |
if (failed) {
|
|
117 |
System.out.println ("test1: FAIL");
|
|
118 |
throw new RuntimeException ();
|
|
119 |
} else {
|
|
120 |
System.out.println ("test1: OK");
|
|
121 |
}
|
|
122 |
}
|
|
123 |
|
|
124 |
// send request and don't read response. Check server closes connection
|
|
125 |
|
|
126 |
static void test2() throws IOException {
|
|
127 |
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
|
|
128 |
urlc.setReadTimeout (20 * 1000);
|
|
129 |
InputStream is = urlc.getInputStream();
|
|
130 |
// we won't read response and check if it times out
|
|
131 |
// on server. If it timesout at client then there is a problem
|
|
132 |
try {
|
|
133 |
Thread.sleep (10 * 1000);
|
|
134 |
while (is.read() != -1) ;
|
|
135 |
} catch (InterruptedException e) {
|
|
136 |
System.out.println (e);
|
|
137 |
System.out.println ("test2: FAIL");
|
|
138 |
throw new RuntimeException ("unexpected error");
|
|
139 |
} catch (SocketTimeoutException e1) {
|
|
140 |
System.out.println (e1);
|
|
141 |
System.out.println ("test2: FAIL");
|
|
142 |
throw new RuntimeException ("client timedout");
|
|
143 |
} finally {
|
|
144 |
is.close();
|
|
145 |
}
|
|
146 |
System.out.println ("test2: OK");
|
|
147 |
}
|
|
148 |
|
|
149 |
// same as test2, but repeated with multiple connections
|
|
150 |
// including a number of valid request/responses
|
|
151 |
|
|
152 |
// Worker: a thread opens a connection to the server in one of three modes.
|
|
153 |
// NORMAL - sends a request, waits for response, and checks valid response
|
|
154 |
// REQUEST - sends a partial request, and blocks, to see if
|
|
155 |
// server closes the connection.
|
|
156 |
// RESPONSE - sends a request, partially reads response and blocks,
|
|
157 |
// to see if server closes the connection.
|
|
158 |
|
|
159 |
static class Worker extends Thread {
|
|
160 |
CountDownLatch latch;
|
|
161 |
Mode mode;
|
|
162 |
|
|
163 |
enum Mode {
|
|
164 |
REQUEST, // block during sending of request
|
|
165 |
RESPONSE, // block during reading of response
|
|
166 |
NORMAL // don't block
|
|
167 |
};
|
|
168 |
|
|
169 |
Worker (CountDownLatch latch, Mode mode) {
|
|
170 |
this.latch = latch;
|
|
171 |
this.mode = mode;
|
|
172 |
}
|
|
173 |
|
|
174 |
void fail(String msg) {
|
|
175 |
System.out.println (msg);
|
|
176 |
failed = true;
|
|
177 |
}
|
|
178 |
|
|
179 |
public void run () {
|
|
180 |
HttpURLConnection urlc;
|
|
181 |
InputStream is = null;
|
|
182 |
|
|
183 |
try {
|
|
184 |
urlc = (HttpURLConnection) url.openConnection();
|
|
185 |
urlc.setReadTimeout (20 * 1000);
|
|
186 |
urlc.setDoOutput(true);
|
|
187 |
} catch (IOException e) {
|
|
188 |
fail("Worker: failed to connect to server");
|
|
189 |
latch.countDown();
|
|
190 |
return;
|
|
191 |
}
|
|
192 |
try {
|
|
193 |
OutputStream os = urlc.getOutputStream();
|
|
194 |
os.write ("foo".getBytes());
|
|
195 |
if (mode == Mode.REQUEST) {
|
|
196 |
Thread.sleep (3000);
|
|
197 |
}
|
|
198 |
os.close();
|
|
199 |
is = urlc.getInputStream();
|
|
200 |
if (mode == Mode.RESPONSE) {
|
|
201 |
Thread.sleep (3000);
|
|
202 |
}
|
|
203 |
if (!checkResponse (is, RESPONSE_BODY)) {
|
|
204 |
fail ("Worker: response");
|
|
205 |
}
|
|
206 |
is.close();
|
|
207 |
return;
|
|
208 |
} catch (InterruptedException e0) {
|
|
209 |
fail("Worker: timedout");
|
|
210 |
} catch (SocketTimeoutException e1) {
|
|
211 |
fail("Worker: timedout");
|
|
212 |
} catch (IOException e2) {
|
|
213 |
switch (mode) {
|
|
214 |
case NORMAL:
|
|
215 |
fail ("Worker: " + e2.getMessage());
|
|
216 |
break;
|
|
217 |
case RESPONSE:
|
|
218 |
if (is == null) {
|
|
219 |
fail ("Worker: " + e2.getMessage());
|
|
220 |
break;
|
|
221 |
}
|
|
222 |
// default: is ok
|
|
223 |
}
|
|
224 |
} finally {
|
|
225 |
latch.countDown();
|
|
226 |
}
|
|
227 |
}
|
|
228 |
}
|
|
229 |
|
|
230 |
static final int NUM = 20;
|
|
231 |
|
|
232 |
static void test3() throws Exception {
|
|
233 |
failed = false;
|
|
234 |
CountDownLatch l = new CountDownLatch (NUM*3);
|
|
235 |
Worker[] workers = new Worker[NUM*3];
|
|
236 |
for (int i=0; i<NUM; i++) {
|
|
237 |
workers[i*3] = new Worker (l, Worker.Mode.NORMAL);
|
|
238 |
workers[i*3+1] = new Worker (l, Worker.Mode.REQUEST);
|
|
239 |
workers[i*3+2] = new Worker (l, Worker.Mode.RESPONSE);
|
|
240 |
workers[i*3].start();
|
|
241 |
workers[i*3+1].start();
|
|
242 |
workers[i*3+2].start();
|
|
243 |
}
|
|
244 |
l.await();
|
|
245 |
for (int i=0; i<NUM*3; i++) {
|
|
246 |
workers[i].join();
|
|
247 |
}
|
|
248 |
if (failed) {
|
|
249 |
throw new RuntimeException ("test3: failed");
|
|
250 |
}
|
|
251 |
System.out.println ("test3: OK");
|
|
252 |
}
|
|
253 |
|
|
254 |
static boolean checkResponse (InputStream is, String resp) {
|
|
255 |
try {
|
|
256 |
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
257 |
byte[] buf = new byte [64];
|
|
258 |
int c;
|
|
259 |
while ((c=is.read(buf)) != -1) {
|
|
260 |
bos.write (buf, 0, c);
|
|
261 |
}
|
|
262 |
bos.close();
|
|
263 |
if (!bos.toString().equals(resp)) {
|
|
264 |
System.out.println ("Wrong response: " + bos.toString());
|
|
265 |
return false;
|
|
266 |
}
|
|
267 |
} catch (IOException e) {
|
|
268 |
System.out.println (e);
|
|
269 |
return false;
|
|
270 |
}
|
|
271 |
return true;
|
|
272 |
}
|
|
273 |
}
|