2
|
1 |
/*
|
|
2 |
* Copyright 2005-2006 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 |
* have any questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
/**
|
|
25 |
* @test
|
|
26 |
* @bug 6270015
|
|
27 |
* @summary Light weight HTTP server
|
|
28 |
* @run main/othervm -Dsun.net.httpserver.idleInterval=4 Test4
|
|
29 |
*/
|
|
30 |
|
|
31 |
import com.sun.net.httpserver.*;
|
|
32 |
|
|
33 |
import java.util.*;
|
|
34 |
import java.util.concurrent.*;
|
|
35 |
import java.util.regex.*;
|
|
36 |
import java.io.*;
|
|
37 |
import java.net.*;
|
|
38 |
import java.security.*;
|
|
39 |
import javax.net.ssl.*;
|
|
40 |
|
|
41 |
/**
|
|
42 |
* Test pipe-lining (block after read)
|
|
43 |
*/
|
|
44 |
public class Test4 extends Test {
|
|
45 |
static int count = 1;
|
|
46 |
public static void main (String[] args) throws Exception {
|
|
47 |
System.out.print ("Test4: ");
|
|
48 |
Handler handler = new Handler();
|
|
49 |
InetSocketAddress addr = new InetSocketAddress (0);
|
|
50 |
HttpServer server = HttpServer.create (addr, 0);
|
|
51 |
int port = server.getAddress().getPort();
|
|
52 |
HttpContext c2 = server.createContext ("/test", handler);
|
|
53 |
c2.getAttributes().put ("name", "This is the http handler");
|
|
54 |
|
|
55 |
ExecutorService exec = Executors.newCachedThreadPool();
|
|
56 |
server.setExecutor (exec);
|
|
57 |
try {
|
|
58 |
server.start ();
|
|
59 |
doClient(port);
|
|
60 |
System.out.println ("OK");
|
|
61 |
} finally {
|
|
62 |
delay();
|
|
63 |
server.stop(2);
|
|
64 |
exec.shutdown();
|
|
65 |
}
|
|
66 |
}
|
|
67 |
|
|
68 |
static class Handler implements HttpHandler {
|
|
69 |
volatile int invocation = 0;
|
|
70 |
public void handle (HttpExchange t)
|
|
71 |
throws IOException
|
|
72 |
{
|
|
73 |
InputStream is = t.getRequestBody();
|
|
74 |
Headers map = t.getRequestHeaders();
|
|
75 |
Headers rmap = t.getResponseHeaders();
|
|
76 |
int x = invocation ++;
|
|
77 |
rmap.set ("XTest", Integer.toString (x));
|
|
78 |
|
|
79 |
switch (x) {
|
|
80 |
case 0:
|
|
81 |
checkBody (is, body1);
|
|
82 |
try {Thread.sleep (2000); } catch (Exception e) {}
|
|
83 |
break;
|
|
84 |
case 1:
|
|
85 |
checkBody (is, body2);
|
|
86 |
try {Thread.sleep (1000); } catch (Exception e) {}
|
|
87 |
break;
|
|
88 |
case 2:
|
|
89 |
checkBody (is, body3);
|
|
90 |
break;
|
|
91 |
case 3:
|
|
92 |
checkBody (is, body4);
|
|
93 |
break;
|
|
94 |
}
|
|
95 |
t.sendResponseHeaders (200, -1);
|
|
96 |
t.close();
|
|
97 |
}
|
|
98 |
}
|
|
99 |
|
|
100 |
static void checkBody (InputStream is, String cmp) throws IOException {
|
|
101 |
byte [] b = new byte [1024];
|
|
102 |
int count = 0, c;
|
|
103 |
while ((c=is.read(b, count, b.length-count)) != -1) {
|
|
104 |
count+=c;
|
|
105 |
}
|
|
106 |
is.close();
|
|
107 |
String s = new String (b, 0, count, "ISO8859_1");
|
|
108 |
if (!s.equals (cmp)) {
|
|
109 |
throw new RuntimeException ("strings not equal");
|
|
110 |
}
|
|
111 |
}
|
|
112 |
|
|
113 |
static String body1 = "1234567890abcdefghij";
|
|
114 |
static String body2 = "2234567890abcdefghij0123456789";
|
|
115 |
static String body3 = "3wertyuiop";
|
|
116 |
static String body4 = "4234567890";
|
|
117 |
|
|
118 |
static String result =
|
|
119 |
"HTTP/1.1 200 OK.*Xtest: 0.*"+
|
|
120 |
"HTTP/1.1 200 OK.*Xtest: 1.*"+
|
|
121 |
"HTTP/1.1 200 OK.*Xtest: 2.*"+
|
|
122 |
"HTTP/1.1 200 OK.*Xtest: 3.*";
|
|
123 |
|
|
124 |
public static void doClient (int port) throws Exception {
|
|
125 |
String s = "GET /test/1.html HTTP/1.1\r\nContent-length: 20\r\n"+
|
|
126 |
"\r\n" +body1 +
|
|
127 |
"GET /test/2.html HTTP/1.1\r\nContent-length: 30\r\n"+
|
|
128 |
"\r\n"+ body2 +
|
|
129 |
"GET /test/3.html HTTP/1.1\r\nContent-length: 10\r\n"+
|
|
130 |
"\r\n"+ body3 +
|
|
131 |
"GET /test/4.html HTTP/1.1\r\nContent-length: 10\r\n"+
|
|
132 |
"\r\n"+body4;
|
|
133 |
|
|
134 |
Socket socket = new Socket ("localhost", port);
|
|
135 |
OutputStream os = socket.getOutputStream();
|
|
136 |
os.write (s.getBytes());
|
|
137 |
InputStream is = socket.getInputStream();
|
|
138 |
int c, count=0;
|
|
139 |
byte[] b = new byte [1024];
|
|
140 |
while ((c=is.read(b, count, b.length-count)) != -1) {
|
|
141 |
count +=c;
|
|
142 |
}
|
|
143 |
is.close();
|
|
144 |
socket.close();
|
|
145 |
s = new String (b,0,count, "ISO8859_1");
|
|
146 |
if (!compare (s, result)) {
|
|
147 |
throw new RuntimeException ("wrong string result");
|
|
148 |
}
|
|
149 |
}
|
|
150 |
|
|
151 |
static boolean compare (String s, String result) {
|
|
152 |
Pattern pattern = Pattern.compile (result,
|
|
153 |
Pattern.DOTALL|Pattern.CASE_INSENSITIVE
|
|
154 |
);
|
|
155 |
Matcher matcher = pattern.matcher (s);
|
|
156 |
return matcher.matches();
|
|
157 |
}
|
|
158 |
|
|
159 |
}
|