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