author | jjg |
Wed, 04 Jan 2017 18:33:20 -0800 | |
changeset 43029 | 1cd1c816581e |
parent 42460 | 7133f144981a |
child 44854 | 5a486e0acd29 |
permissions | -rw-r--r-- |
36131 | 1 |
/* |
2 |
* Copyright (c) 2015, 2016, 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 |
||
41890
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
24 |
/* |
36131 | 25 |
* @test |
26 |
* @bug 8087112 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
27 |
* @modules jdk.incubator.httpclient |
41890
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
28 |
* java.logging |
38883 | 29 |
* jdk.httpserver |
37720 | 30 |
* @library /lib/testlibrary/ / |
31 |
* @build jdk.testlibrary.SimpleSSLContext ProxyServer EchoHandler |
|
36131 | 32 |
* @compile ../../../com/sun/net/httpserver/LogFilter.java |
33 |
* @compile ../../../com/sun/net/httpserver/FileServerHandler.java |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
34 |
* @run main/othervm -Djdk.httpclient.HttpClient.log=errors,trace SmokeTest |
36131 | 35 |
*/ |
36 |
||
41890
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
37 |
import com.sun.net.httpserver.Headers; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
38 |
import com.sun.net.httpserver.HttpContext; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
39 |
import com.sun.net.httpserver.HttpExchange; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
40 |
import com.sun.net.httpserver.HttpHandler; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
41 |
import com.sun.net.httpserver.HttpServer; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
42 |
import com.sun.net.httpserver.HttpsConfigurator; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
43 |
import com.sun.net.httpserver.HttpsParameters; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
44 |
import com.sun.net.httpserver.HttpsServer; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
45 |
import java.util.concurrent.atomic.AtomicInteger; |
41890
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
46 |
import java.net.InetSocketAddress; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
47 |
import java.net.PasswordAuthentication; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
48 |
import java.net.ProxySelector; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
49 |
import java.net.ServerSocket; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
50 |
import java.net.Socket; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
51 |
import java.net.URI; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
52 |
import jdk.incubator.http.HttpClient; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
53 |
import jdk.incubator.http.HttpRequest; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
54 |
import jdk.incubator.http.HttpResponse; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
55 |
import java.nio.file.StandardOpenOption; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
56 |
import java.io.File; |
41890
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
57 |
import java.io.FileInputStream; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
58 |
import java.io.FileOutputStream; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
59 |
import java.io.FileNotFoundException; |
41890
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
60 |
import java.io.IOException; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
61 |
import java.io.BufferedInputStream; |
41890
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
62 |
import java.io.InputStream; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
63 |
import java.io.OutputStream; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
64 |
import java.io.UncheckedIOException; |
41890
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
65 |
import java.util.concurrent.BlockingQueue; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
66 |
import java.util.concurrent.CompletableFuture; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
67 |
import java.util.concurrent.CompletionException; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
68 |
import java.util.concurrent.CyclicBarrier; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
69 |
import java.util.concurrent.Executors; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
70 |
import java.util.concurrent.ExecutorService; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
71 |
import java.util.concurrent.LinkedBlockingQueue; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
72 |
import java.util.concurrent.TimeUnit; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
73 |
import javax.net.ssl.SSLContext; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
74 |
import javax.net.ssl.SSLParameters; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
75 |
import java.nio.file.Files; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
76 |
import java.nio.file.Path; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
77 |
import java.nio.file.Paths; |
36131 | 78 |
import java.util.HashSet; |
79 |
import java.util.LinkedList; |
|
80 |
import java.util.List; |
|
81 |
import java.util.Random; |
|
82 |
import jdk.testlibrary.SimpleSSLContext; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
83 |
import static jdk.incubator.http.HttpRequest.BodyProcessor.fromFile; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
84 |
import static jdk.incubator.http.HttpRequest.BodyProcessor.fromInputStream; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
85 |
import static jdk.incubator.http.HttpRequest.BodyProcessor.fromString; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
86 |
import static jdk.incubator.http.HttpResponse.*; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
87 |
import static jdk.incubator.http.HttpResponse.BodyHandler.asFile; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
88 |
import static jdk.incubator.http.HttpResponse.BodyHandler.asString; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
89 |
import java.util.concurrent.CountDownLatch; |
36131 | 90 |
import java.util.logging.ConsoleHandler; |
91 |
import java.util.logging.Level; |
|
92 |
import java.util.logging.Logger; |
|
93 |
||
94 |
/** |
|
95 |
* * Basic smoke test for Http/1.1 client |
|
96 |
* - basic request response |
|
97 |
* - request body POST |
|
98 |
* - response body GET |
|
99 |
* - redirect |
|
100 |
* - chunked request/response |
|
101 |
* - SSL |
|
102 |
* - proxies |
|
103 |
* - 100 continue |
|
104 |
* - check keep alive appears to be working |
|
105 |
* - cancel of long request |
|
106 |
* |
|
107 |
* Uses a FileServerHandler serving a couple of known files |
|
108 |
* in docs directory. |
|
109 |
*/ |
|
110 |
public class SmokeTest { |
|
111 |
static SSLContext ctx; |
|
37720 | 112 |
static SSLParameters sslparams; |
36131 | 113 |
static HttpServer s1 ; |
114 |
static HttpsServer s2; |
|
115 |
static ExecutorService executor; |
|
116 |
static int port; |
|
117 |
static int httpsport; |
|
118 |
static String httproot; |
|
119 |
static String httpsroot; |
|
120 |
static HttpClient client; |
|
121 |
static ProxyServer proxy; |
|
122 |
static int proxyPort; |
|
123 |
static RedirectErrorHandler redirectErrorHandler, redirectErrorHandlerSecure; |
|
124 |
static RedirectHandler redirectHandler, redirectHandlerSecure; |
|
125 |
static DelayHandler delayHandler; |
|
126 |
final static String midSizedFilename = "/files/notsobigfile.txt"; |
|
127 |
final static String smallFilename = "/files/smallfile.txt"; |
|
128 |
static Path midSizedFile; |
|
129 |
static Path smallFile; |
|
130 |
static String fileroot; |
|
131 |
||
132 |
static String getFileContent(String path) throws IOException { |
|
133 |
FileInputStream fis = new FileInputStream(path); |
|
134 |
byte[] buf = new byte[2000]; |
|
135 |
StringBuilder sb = new StringBuilder(); |
|
136 |
int n; |
|
137 |
while ((n=fis.read(buf)) != -1) { |
|
138 |
sb.append(new String(buf, 0, n, "US-ASCII")); |
|
139 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
140 |
fis.close(); |
36131 | 141 |
return sb.toString(); |
142 |
} |
|
143 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
144 |
static void cmpFileContent(Path path1, Path path2) throws IOException { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
145 |
InputStream fis1 = new BufferedInputStream(new FileInputStream(path1.toFile())); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
146 |
InputStream fis2 = new BufferedInputStream(new FileInputStream(path2.toFile())); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
147 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
148 |
int n1, n2; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
149 |
while ((n1=fis1.read()) != -1) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
150 |
n2 = fis2.read(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
151 |
if (n1 != n2) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
152 |
throw new IOException("Content not the same"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
153 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
154 |
fis1.close(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
155 |
fis2.close(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
156 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
157 |
|
36131 | 158 |
public static void main(String[] args) throws Exception { |
159 |
initServer(); |
|
160 |
fileroot = System.getProperty ("test.src", ".")+ "/docs"; |
|
161 |
midSizedFile = Paths.get(fileroot + midSizedFilename); |
|
162 |
smallFile = Paths.get(fileroot + smallFilename); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
163 |
ExecutorService e = Executors.newCachedThreadPool(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
164 |
System.out.println(e); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
165 |
client = HttpClient.newBuilder() |
36131 | 166 |
.sslContext(ctx) |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
167 |
.executor(e) |
37720 | 168 |
.sslParameters(sslparams) |
36131 | 169 |
.followRedirects(HttpClient.Redirect.ALWAYS) |
170 |
.build(); |
|
171 |
||
172 |
try { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
173 |
|
36131 | 174 |
test1(httproot + "files/foo.txt", true); |
175 |
test1(httproot + "files/foo.txt", false); |
|
176 |
test1(httpsroot + "files/foo.txt", true); |
|
177 |
test1(httpsroot + "files/foo.txt", false); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
178 |
|
36131 | 179 |
test2(httproot + "echo/foo", "This is a short test"); |
180 |
test2(httpsroot + "echo/foo", "This is a short test"); |
|
181 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
182 |
test2a(httproot + "echo/foo"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
183 |
test2a(httpsroot + "echo/foo"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
184 |
|
36131 | 185 |
test3(httproot + "redirect/foo.txt"); |
186 |
test3(httpsroot + "redirect/foo.txt"); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
187 |
|
36131 | 188 |
test4(httproot + "files/foo.txt"); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
189 |
|
36131 | 190 |
test4(httpsroot + "files/foo.txt"); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
191 |
|
36131 | 192 |
test5(httproot + "echo/foo", true); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
193 |
|
36131 | 194 |
test5(httpsroot + "echo/foo", true); |
195 |
test5(httproot + "echo/foo", false); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
196 |
|
36131 | 197 |
test5(httpsroot + "echo/foo", false); |
198 |
||
199 |
test6(httproot + "echo/foo", true); |
|
200 |
test6(httpsroot + "echo/foo", true); |
|
201 |
test6(httproot + "echo/foo", false); |
|
202 |
test6(httpsroot + "echo/foo", false); |
|
203 |
||
204 |
test7(httproot + "keepalive/foo"); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
205 |
/* |
36131 | 206 |
test10(httproot + "redirecterror/foo.txt"); |
207 |
||
208 |
test10(httpsroot + "redirecterror/foo.txt"); |
|
209 |
||
210 |
test11(httproot + "echo/foo"); |
|
211 |
test11(httpsroot + "echo/foo"); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
212 |
*/ |
36131 | 213 |
//test12(httproot + "delay/foo", delayHandler); |
214 |
||
215 |
} finally { |
|
216 |
s1.stop(0); |
|
217 |
s2.stop(0); |
|
218 |
proxy.close(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
219 |
e.shutdownNow(); |
36131 | 220 |
executor.shutdownNow(); |
221 |
} |
|
222 |
} |
|
223 |
||
224 |
static class Auth extends java.net.Authenticator { |
|
225 |
volatile int count = 0; |
|
226 |
@Override |
|
227 |
protected PasswordAuthentication getPasswordAuthentication() { |
|
228 |
if (count++ == 0) { |
|
229 |
return new PasswordAuthentication("user", "passwd".toCharArray()); |
|
230 |
} else { |
|
231 |
return new PasswordAuthentication("user", "goober".toCharArray()); |
|
232 |
} |
|
233 |
} |
|
234 |
int count() { |
|
235 |
return count; |
|
236 |
} |
|
237 |
} |
|
238 |
||
239 |
// Basic test |
|
240 |
static void test1(String target, boolean fixedLen) throws Exception { |
|
241 |
System.out.print("test1: " + target); |
|
242 |
URI uri = new URI(target); |
|
243 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
244 |
HttpRequest.Builder builder = HttpRequest.newBuilder().uri(uri).GET(); |
36131 | 245 |
|
246 |
if (fixedLen) { |
|
247 |
builder.header("XFixed", "yes"); |
|
248 |
} |
|
249 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
250 |
HttpRequest request = builder.build(); |
36131 | 251 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
252 |
HttpResponse<String> response = client.send(request, asString()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
253 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
254 |
String body = response.body(); |
36131 | 255 |
if (!body.equals("This is foo.txt\r\n")) { |
256 |
throw new RuntimeException(); |
|
257 |
} |
|
258 |
||
259 |
// repeat async |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
260 |
HttpResponse<String> response1 = client.sendAsync(request, asString()) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
261 |
.join(); |
36131 | 262 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
263 |
String body1 = response1.body(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
264 |
if (!body1.equals("This is foo.txt\r\n")) { |
36131 | 265 |
throw new RuntimeException(); |
266 |
} |
|
267 |
System.out.println(" OK"); |
|
268 |
} |
|
269 |
||
270 |
// POST use echo to check reply |
|
271 |
static void test2(String s, String body) throws Exception { |
|
272 |
System.out.print("test2: " + s); |
|
273 |
URI uri = new URI(s); |
|
274 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
275 |
HttpRequest request = HttpRequest.newBuilder(uri) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
276 |
.POST(fromString(body)) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
277 |
.build(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
278 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
279 |
HttpResponse<String> response = client.send(request, asString()); |
36131 | 280 |
|
281 |
if (response.statusCode() != 200) { |
|
282 |
throw new RuntimeException( |
|
283 |
"Expected 200, got [ " + response.statusCode() + " ]"); |
|
284 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
285 |
String reply = response.body(); |
36131 | 286 |
if (!reply.equals(body)) { |
287 |
throw new RuntimeException( |
|
288 |
"Body mismatch: expected [" + body + "], got [" + reply + "]"); |
|
289 |
} |
|
290 |
System.out.println(" OK"); |
|
291 |
} |
|
292 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
293 |
// POST use echo to check reply |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
294 |
static void test2a(String s) throws Exception { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
295 |
System.out.print("test2a: " + s); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
296 |
URI uri = new URI(s); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
297 |
Path p = Util.getTempFile(128 * 1024); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
298 |
//Path p = Util.getTempFile(1 * 1024); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
299 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
300 |
HttpRequest request = HttpRequest.newBuilder(uri) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
301 |
.POST(fromFile(p)) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
302 |
.build(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
303 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
304 |
Path resp = Util.getTempFile(1); // will be overwritten |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
305 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
306 |
HttpResponse<Path> response = |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
307 |
client.send(request, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
308 |
BodyHandler.asFile(resp, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
309 |
StandardOpenOption.TRUNCATE_EXISTING, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
310 |
StandardOpenOption.WRITE)); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
311 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
312 |
if (response.statusCode() != 200) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
313 |
throw new RuntimeException( |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
314 |
"Expected 200, got [ " + response.statusCode() + " ]"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
315 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
316 |
Path reply = response.body(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
317 |
//System.out.println("Reply stored in " + reply.toString()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
318 |
cmpFileContent(reply, p); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
319 |
System.out.println(" OK"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
320 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
321 |
|
36131 | 322 |
// Redirect |
323 |
static void test3(String s) throws Exception { |
|
324 |
System.out.print("test3: " + s); |
|
325 |
URI uri = new URI(s); |
|
326 |
RedirectHandler handler = uri.getScheme().equals("https") |
|
327 |
? redirectHandlerSecure : redirectHandler; |
|
328 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
329 |
HttpRequest request = HttpRequest.newBuilder() |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
330 |
.uri(uri) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
331 |
.GET() |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
332 |
.build(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
333 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
334 |
HttpResponse<Path> response = client.send(request, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
335 |
asFile(Paths.get("redir1.txt"))); |
36131 | 336 |
|
337 |
if (response.statusCode() != 200) { |
|
338 |
throw new RuntimeException( |
|
339 |
"Expected 200, got [ " + response.statusCode() + " ]"); |
|
340 |
} else { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
341 |
response.body(); |
36131 | 342 |
} |
343 |
||
344 |
Path downloaded = Paths.get("redir1.txt"); |
|
345 |
if (Files.size(downloaded) != Files.size(midSizedFile)) { |
|
346 |
throw new RuntimeException("Size mismatch"); |
|
347 |
} |
|
348 |
||
349 |
System.out.printf(" (count: %d) ", handler.count()); |
|
350 |
// repeat with async api |
|
351 |
||
352 |
handler.reset(); |
|
353 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
354 |
request = HttpRequest.newBuilder(uri).build(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
355 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
356 |
response = client.sendAsync(request, asFile(Paths.get("redir2.txt"))).join(); |
36131 | 357 |
|
358 |
if (response.statusCode() != 200) { |
|
359 |
throw new RuntimeException( |
|
360 |
"Expected 200, got [ " + response.statusCode() + " ]"); |
|
361 |
} else { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
362 |
response.body(); |
36131 | 363 |
} |
364 |
||
365 |
downloaded = Paths.get("redir2.txt"); |
|
366 |
if (Files.size(downloaded) != Files.size(midSizedFile)) { |
|
367 |
throw new RuntimeException("Size mismatch 2"); |
|
368 |
} |
|
369 |
System.out.printf(" (count: %d) ", handler.count()); |
|
370 |
System.out.println(" OK"); |
|
371 |
} |
|
372 |
||
373 |
// Proxies |
|
374 |
static void test4(String s) throws Exception { |
|
375 |
System.out.print("test4: " + s); |
|
376 |
URI uri = new URI(s); |
|
377 |
InetSocketAddress proxyAddr = new InetSocketAddress("127.0.0.1", proxyPort); |
|
378 |
String filename = fileroot + uri.getPath(); |
|
379 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
380 |
ExecutorService e = Executors.newCachedThreadPool(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
381 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
382 |
HttpClient cl = HttpClient.newBuilder() |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
383 |
.executor(e) |
36131 | 384 |
.proxy(ProxySelector.of(proxyAddr)) |
385 |
.sslContext(ctx) |
|
37720 | 386 |
.sslParameters(sslparams) |
36131 | 387 |
.build(); |
388 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
389 |
HttpRequest request = HttpRequest.newBuilder(uri).GET().build(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
390 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
391 |
CompletableFuture<String> fut = client.sendAsync(request, asString()) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
392 |
.thenApply((response) -> response.body()); |
36131 | 393 |
|
394 |
String body = fut.get(5, TimeUnit.HOURS); |
|
395 |
||
396 |
String fc = getFileContent(filename); |
|
397 |
||
398 |
if (!body.equals(fc)) { |
|
399 |
throw new RuntimeException( |
|
400 |
"Body mismatch: expected [" + body + "], got [" + fc + "]"); |
|
401 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
402 |
e.shutdownNow(); |
36131 | 403 |
System.out.println(" OK"); |
404 |
} |
|
405 |
||
406 |
// 100 Continue: use echo target |
|
407 |
static void test5(String target, boolean fixedLen) throws Exception { |
|
408 |
System.out.print("test5: " + target); |
|
409 |
URI uri = new URI(target); |
|
410 |
String requestBody = generateString(12 * 1024 + 13); |
|
411 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
412 |
HttpRequest.Builder builder = HttpRequest.newBuilder(uri) |
36131 | 413 |
.expectContinue(true) |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
414 |
.POST(fromString(requestBody)); |
36131 | 415 |
|
416 |
if (fixedLen) { |
|
417 |
builder.header("XFixed", "yes"); |
|
418 |
} |
|
419 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
420 |
HttpRequest request = builder.build(); |
36131 | 421 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
422 |
HttpResponse<String> response = client.send(request, asString()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
423 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
424 |
String body = response.body(); |
36131 | 425 |
|
426 |
if (!body.equals(requestBody)) { |
|
427 |
throw new RuntimeException( |
|
428 |
"Body mismatch: expected [" + body + "], got [" + body + "]"); |
|
429 |
} |
|
430 |
System.out.println(" OK"); |
|
431 |
} |
|
432 |
||
433 |
// use echo |
|
434 |
static void test6(String target, boolean fixedLen) throws Exception { |
|
435 |
System.out.print("test6: " + target); |
|
436 |
URI uri = new URI(target); |
|
437 |
String requestBody = generateString(12 * 1024 + 3); |
|
438 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
439 |
HttpRequest.Builder builder = HttpRequest.newBuilder(uri).GET(); |
36131 | 440 |
|
441 |
if (fixedLen) { |
|
442 |
builder.header("XFixed", "yes"); |
|
443 |
} |
|
444 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
445 |
HttpRequest request = builder.build(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
446 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
447 |
HttpResponse<String> response = client.send(request, asString()); |
36131 | 448 |
|
449 |
if (response.statusCode() != 200) { |
|
450 |
throw new RuntimeException( |
|
451 |
"Expected 200, got [ " + response.statusCode() + " ]"); |
|
452 |
} |
|
453 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
454 |
String responseBody = response.body(); |
36131 | 455 |
|
456 |
if (responseBody.equals(requestBody)) { |
|
457 |
throw new RuntimeException( |
|
458 |
"Body mismatch: expected [" + requestBody + "], got [" + responseBody + "]"); |
|
459 |
} |
|
460 |
System.out.println(" OK"); |
|
461 |
} |
|
462 |
||
463 |
@SuppressWarnings("rawtypes") |
|
464 |
static void test7(String target) throws Exception { |
|
465 |
System.out.print("test7: " + target); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
466 |
Path requestBody = Util.getTempFile(128 * 1024); |
36131 | 467 |
// First test |
468 |
URI uri = new URI(target); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
469 |
HttpRequest request = HttpRequest.newBuilder().uri(uri).GET().build(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
470 |
|
36131 | 471 |
for (int i=0; i<4; i++) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
472 |
HttpResponse<String> r = client.send(request, asString()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
473 |
String body = r.body(); |
36131 | 474 |
if (!body.equals("OK")) { |
475 |
throw new RuntimeException("Expected OK, got: " + body); |
|
476 |
} |
|
477 |
} |
|
478 |
||
479 |
// Second test: 4 x parallel |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
480 |
request = HttpRequest.newBuilder().uri(uri).POST(fromFile(requestBody)).build(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
481 |
List<CompletableFuture<String>> futures = new LinkedList<>(); |
36131 | 482 |
for (int i=0; i<4; i++) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
483 |
futures.add(client.sendAsync(request, asString()) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
484 |
.thenApply((response) -> { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
485 |
if (response.statusCode() == 200) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
486 |
return response.body(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
487 |
else |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
488 |
return "ERROR"; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
489 |
})); |
36131 | 490 |
} |
491 |
// all sent? |
|
492 |
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])) |
|
493 |
.join(); |
|
494 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
495 |
for (CompletableFuture<String> future : futures) { |
36131 | 496 |
String body = future.get(); |
497 |
if (!body.equals("OK")) { |
|
498 |
throw new RuntimeException("Expected OK, got: " + body); |
|
499 |
} |
|
500 |
} |
|
501 |
||
502 |
// Third test: Multiple of 4 parallel requests |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
503 |
request = HttpRequest.newBuilder(uri).GET().build(); |
36131 | 504 |
BlockingQueue<String> q = new LinkedBlockingQueue<>(); |
505 |
for (int i=0; i<4; i++) { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
506 |
client.sendAsync(request, asString()) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
507 |
.thenApply((HttpResponse<String> resp) -> { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
508 |
String body = resp.body(); |
36131 | 509 |
putQ(q, body); |
510 |
return body; |
|
511 |
}); |
|
512 |
} |
|
513 |
// we've sent four requests. Now, just send another request |
|
514 |
// as each response is received. The idea is to ensure that |
|
515 |
// only four sockets ever get used. |
|
516 |
||
517 |
for (int i=0; i<100; i++) { |
|
518 |
// block until response received |
|
519 |
String body = takeQ(q); |
|
520 |
if (!body.equals("OK")) { |
|
521 |
throw new RuntimeException(body); |
|
522 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
523 |
client.sendAsync(request, asString()) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
524 |
.thenApply((resp) -> { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
525 |
if (resp.statusCode() == 200) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
526 |
putQ(q, resp.body()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
527 |
else |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
528 |
putQ(q, "ERROR"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
529 |
return null; |
36131 | 530 |
}); |
531 |
} |
|
532 |
// should be four left |
|
533 |
for (int i=0; i<4; i++) { |
|
534 |
takeQ(q); |
|
535 |
} |
|
536 |
System.out.println(" OK"); |
|
537 |
} |
|
538 |
||
539 |
static String takeQ(BlockingQueue<String> q) { |
|
540 |
String r = null; |
|
541 |
try { |
|
542 |
r = q.take(); |
|
543 |
} catch (InterruptedException e) {} |
|
544 |
||
545 |
return r; |
|
546 |
} |
|
547 |
||
548 |
static void putQ(BlockingQueue<String> q, String o) { |
|
549 |
try { |
|
550 |
q.put(o); |
|
551 |
} catch (InterruptedException e) { |
|
552 |
// can't happen |
|
553 |
} |
|
554 |
} |
|
555 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
556 |
static FileInputStream newStream() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
557 |
try { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
558 |
return new FileInputStream(smallFile.toFile()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
559 |
} catch (FileNotFoundException e) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
560 |
throw new UncheckedIOException(e); |
36131 | 561 |
} |
562 |
} |
|
563 |
// Chunked output stream |
|
564 |
static void test11(String target) throws Exception { |
|
565 |
System.out.print("test11: " + target); |
|
566 |
URI uri = new URI(target); |
|
567 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
568 |
HttpRequest request = HttpRequest.newBuilder(uri) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
569 |
.POST(fromInputStream(SmokeTest::newStream)) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
570 |
.build(); |
36131 | 571 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
572 |
Path download = Paths.get("test11.txt"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
573 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
574 |
HttpResponse<Path> response = client.send(request, asFile(download)); |
36131 | 575 |
|
576 |
if (response.statusCode() != 200) { |
|
577 |
throw new RuntimeException("Wrong response code"); |
|
578 |
} |
|
579 |
||
580 |
download.toFile().delete(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
581 |
response.body(); |
36131 | 582 |
|
583 |
if (Files.size(download) != Files.size(smallFile)) { |
|
584 |
System.out.println("Original size: " + Files.size(smallFile)); |
|
585 |
System.out.println("Downloaded size: " + Files.size(download)); |
|
586 |
throw new RuntimeException("Size mismatch"); |
|
587 |
} |
|
588 |
System.out.println(" OK"); |
|
589 |
} |
|
590 |
||
591 |
static void delay(int seconds) { |
|
592 |
try { |
|
593 |
Thread.sleep(seconds * 1000); |
|
594 |
} catch (InterruptedException e) { |
|
595 |
} |
|
596 |
} |
|
597 |
||
598 |
// Redirect loop: return an error after a certain number of redirects |
|
599 |
static void test10(String s) throws Exception { |
|
600 |
System.out.print("test10: " + s); |
|
601 |
URI uri = new URI(s); |
|
602 |
RedirectErrorHandler handler = uri.getScheme().equals("https") |
|
603 |
? redirectErrorHandlerSecure : redirectErrorHandler; |
|
604 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
605 |
HttpRequest request = HttpRequest.newBuilder(uri).GET().build(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
606 |
CompletableFuture<HttpResponse<String>> cf = |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
607 |
client.sendAsync(request, asString()); |
36131 | 608 |
|
609 |
try { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
610 |
HttpResponse<String> response = cf.join(); |
36131 | 611 |
throw new RuntimeException("Exepected Completion Exception"); |
612 |
} catch (CompletionException e) { |
|
613 |
//System.out.println(e); |
|
614 |
} |
|
615 |
||
616 |
System.out.printf(" (Calls %d) ", handler.count()); |
|
617 |
System.out.println(" OK"); |
|
618 |
} |
|
619 |
||
620 |
static final int NUM = 50; |
|
621 |
||
622 |
static Random random = new Random(); |
|
623 |
static final String alphabet = "ABCDEFGHIJKLMNOPQRST"; |
|
624 |
||
625 |
static char randomChar() { |
|
626 |
return alphabet.charAt(random.nextInt(alphabet.length())); |
|
627 |
} |
|
628 |
||
629 |
static String generateString(int length) { |
|
630 |
StringBuilder sb = new StringBuilder(length); |
|
631 |
for (int i=0; i<length; i++) { |
|
632 |
sb.append(randomChar()); |
|
633 |
} |
|
634 |
return sb.toString(); |
|
635 |
} |
|
636 |
||
637 |
static void initServer() throws Exception { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
638 |
|
36131 | 639 |
Logger logger = Logger.getLogger("com.sun.net.httpserver"); |
640 |
ConsoleHandler ch = new ConsoleHandler(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
641 |
logger.setLevel(Level.SEVERE); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
642 |
ch.setLevel(Level.SEVERE); |
36131 | 643 |
logger.addHandler(ch); |
644 |
||
645 |
String root = System.getProperty ("test.src")+ "/docs"; |
|
646 |
InetSocketAddress addr = new InetSocketAddress (0); |
|
647 |
s1 = HttpServer.create (addr, 0); |
|
648 |
if (s1 instanceof HttpsServer) { |
|
649 |
throw new RuntimeException ("should not be httpsserver"); |
|
650 |
} |
|
651 |
s2 = HttpsServer.create (addr, 0); |
|
652 |
HttpHandler h = new FileServerHandler(root); |
|
653 |
||
654 |
HttpContext c1 = s1.createContext("/files", h); |
|
655 |
HttpContext c2 = s2.createContext("/files", h); |
|
656 |
HttpContext c3 = s1.createContext("/echo", new EchoHandler()); |
|
657 |
redirectHandler = new RedirectHandler("/redirect"); |
|
658 |
redirectHandlerSecure = new RedirectHandler("/redirect"); |
|
659 |
HttpContext c4 = s1.createContext("/redirect", redirectHandler); |
|
660 |
HttpContext c41 = s2.createContext("/redirect", redirectHandlerSecure); |
|
661 |
HttpContext c5 = s2.createContext("/echo", new EchoHandler()); |
|
662 |
HttpContext c6 = s1.createContext("/keepalive", new KeepAliveHandler()); |
|
663 |
redirectErrorHandler = new RedirectErrorHandler("/redirecterror"); |
|
664 |
redirectErrorHandlerSecure = new RedirectErrorHandler("/redirecterror"); |
|
665 |
HttpContext c7 = s1.createContext("/redirecterror", redirectErrorHandler); |
|
666 |
HttpContext c71 = s2.createContext("/redirecterror", redirectErrorHandlerSecure); |
|
667 |
delayHandler = new DelayHandler(); |
|
668 |
HttpContext c8 = s1.createContext("/delay", delayHandler); |
|
669 |
HttpContext c81 = s2.createContext("/delay", delayHandler); |
|
670 |
||
671 |
executor = Executors.newCachedThreadPool(); |
|
672 |
s1.setExecutor(executor); |
|
673 |
s2.setExecutor(executor); |
|
674 |
ctx = new SimpleSSLContext().get(); |
|
37720 | 675 |
sslparams = ctx.getSupportedSSLParameters(); |
676 |
s2.setHttpsConfigurator(new Configurator(ctx)); |
|
36131 | 677 |
s1.start(); |
678 |
s2.start(); |
|
679 |
||
680 |
port = s1.getAddress().getPort(); |
|
681 |
System.out.println("HTTP server port = " + port); |
|
682 |
httpsport = s2.getAddress().getPort(); |
|
683 |
System.out.println("HTTPS server port = " + httpsport); |
|
684 |
httproot = "http://127.0.0.1:" + port + "/"; |
|
685 |
httpsroot = "https://127.0.0.1:" + httpsport + "/"; |
|
686 |
||
687 |
proxy = new ProxyServer(0, false); |
|
688 |
proxyPort = proxy.getPort(); |
|
689 |
System.out.println("Proxy port = " + proxyPort); |
|
690 |
} |
|
691 |
} |
|
692 |
||
37720 | 693 |
class Configurator extends HttpsConfigurator { |
694 |
public Configurator(SSLContext ctx) { |
|
695 |
super(ctx); |
|
696 |
} |
|
697 |
||
698 |
public void configure (HttpsParameters params) { |
|
699 |
params.setSSLParameters (getSSLContext().getSupportedSSLParameters()); |
|
700 |
} |
|
701 |
} |
|
702 |
||
36131 | 703 |
class UploadServer extends Thread { |
704 |
int statusCode; |
|
705 |
ServerSocket ss; |
|
706 |
int port; |
|
707 |
int size; |
|
708 |
Object lock; |
|
709 |
boolean failed = false; |
|
710 |
||
711 |
UploadServer(int size) throws IOException { |
|
712 |
this.statusCode = statusCode; |
|
713 |
this.size = size; |
|
714 |
ss = new ServerSocket(0); |
|
715 |
port = ss.getLocalPort(); |
|
716 |
lock = new Object(); |
|
717 |
} |
|
718 |
||
719 |
int port() { |
|
720 |
return port; |
|
721 |
} |
|
722 |
||
723 |
int size() { |
|
724 |
return size; |
|
725 |
} |
|
726 |
||
727 |
// wait a sec before calling this |
|
728 |
boolean failed() { |
|
729 |
synchronized(lock) { |
|
730 |
return failed; |
|
731 |
} |
|
732 |
} |
|
733 |
||
734 |
@Override |
|
735 |
public void run () { |
|
736 |
int nbytes = 0; |
|
737 |
Socket s = null; |
|
738 |
||
739 |
synchronized(lock) { |
|
740 |
try { |
|
741 |
s = ss.accept(); |
|
742 |
||
743 |
InputStream is = s.getInputStream(); |
|
744 |
OutputStream os = s.getOutputStream(); |
|
745 |
os.write("HTTP/1.1 201 OK\r\nContent-length: 0\r\n\r\n".getBytes()); |
|
746 |
int n; |
|
747 |
byte[] buf = new byte[8000]; |
|
748 |
while ((n=is.read(buf)) != -1) { |
|
749 |
nbytes += n; |
|
750 |
} |
|
751 |
} catch (IOException e) { |
|
752 |
System.out.println ("read " + nbytes); |
|
753 |
System.out.println ("size " + size); |
|
754 |
failed = nbytes >= size; |
|
755 |
} finally { |
|
756 |
try { |
|
757 |
ss.close(); |
|
758 |
if (s != null) |
|
759 |
s.close(); |
|
760 |
} catch (IOException e) {} |
|
761 |
} |
|
762 |
} |
|
763 |
} |
|
764 |
} |
|
765 |
||
766 |
class RedirectHandler implements HttpHandler { |
|
767 |
String root; |
|
768 |
volatile int count = 0; |
|
769 |
||
770 |
RedirectHandler(String root) { |
|
771 |
this.root = root; |
|
772 |
} |
|
773 |
||
774 |
@Override |
|
775 |
public synchronized void handle(HttpExchange t) |
|
776 |
throws IOException |
|
777 |
{ |
|
778 |
byte[] buf = new byte[2048]; |
|
779 |
try (InputStream is = t.getRequestBody()) { |
|
780 |
while (is.read(buf) != -1) ; |
|
781 |
} |
|
782 |
||
783 |
Headers responseHeaders = t.getResponseHeaders(); |
|
784 |
||
785 |
if (count++ < 1) { |
|
786 |
responseHeaders.add("Location", root + "/foo/" + count); |
|
787 |
} else { |
|
788 |
responseHeaders.add("Location", SmokeTest.midSizedFilename); |
|
789 |
} |
|
790 |
t.sendResponseHeaders(301, -1); |
|
791 |
t.close(); |
|
792 |
} |
|
793 |
||
794 |
int count() { |
|
795 |
return count; |
|
796 |
} |
|
797 |
||
798 |
void reset() { |
|
799 |
count = 0; |
|
800 |
} |
|
801 |
} |
|
802 |
||
803 |
class RedirectErrorHandler implements HttpHandler { |
|
804 |
String root; |
|
805 |
volatile int count = 1; |
|
806 |
||
807 |
RedirectErrorHandler(String root) { |
|
808 |
this.root = root; |
|
809 |
} |
|
810 |
||
811 |
synchronized int count() { |
|
812 |
return count; |
|
813 |
} |
|
814 |
||
815 |
synchronized void increment() { |
|
816 |
count++; |
|
817 |
} |
|
818 |
||
819 |
@Override |
|
820 |
public synchronized void handle (HttpExchange t) |
|
821 |
throws IOException |
|
822 |
{ |
|
823 |
byte[] buf = new byte[2048]; |
|
824 |
try (InputStream is = t.getRequestBody()) { |
|
825 |
while (is.read(buf) != -1) ; |
|
826 |
} |
|
827 |
||
828 |
Headers map = t.getResponseHeaders(); |
|
829 |
String redirect = root + "/foo/" + Integer.toString(count); |
|
830 |
increment(); |
|
831 |
map.add("Location", redirect); |
|
832 |
t.sendResponseHeaders(301, -1); |
|
833 |
t.close(); |
|
834 |
} |
|
835 |
} |
|
836 |
||
837 |
class Util { |
|
838 |
static byte[] readAll(InputStream is) throws IOException { |
|
839 |
byte[] buf = new byte[1024]; |
|
840 |
byte[] result = new byte[0]; |
|
841 |
||
842 |
while (true) { |
|
843 |
int n = is.read(buf); |
|
844 |
if (n > 0) { |
|
845 |
byte[] b1 = new byte[result.length + n]; |
|
846 |
System.arraycopy(result, 0, b1, 0, result.length); |
|
847 |
System.arraycopy(buf, 0, b1, result.length, n); |
|
848 |
result = b1; |
|
849 |
} else if (n == -1) { |
|
850 |
return result; |
|
851 |
} |
|
852 |
} |
|
853 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
854 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
855 |
static Path getTempFile(int size) throws IOException { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
856 |
File f = File.createTempFile("test", "txt"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
857 |
f.deleteOnExit(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
858 |
byte[] buf = new byte[2048]; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
859 |
for (int i=0; i<buf.length; i++) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
860 |
buf[i] = (byte)i; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
861 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
862 |
FileOutputStream fos = new FileOutputStream(f); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
863 |
while (size > 0) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
864 |
int amount = Math.min(size, buf.length); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
865 |
fos.write(buf, 0, amount); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
866 |
size -= amount; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
867 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
868 |
fos.close(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
869 |
return f.toPath(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
870 |
} |
36131 | 871 |
} |
872 |
||
873 |
class DelayHandler implements HttpHandler { |
|
874 |
||
875 |
CyclicBarrier bar1 = new CyclicBarrier(2); |
|
876 |
CyclicBarrier bar2 = new CyclicBarrier(2); |
|
877 |
CyclicBarrier bar3 = new CyclicBarrier(2); |
|
878 |
||
879 |
CyclicBarrier barrier1() { |
|
880 |
return bar1; |
|
881 |
} |
|
882 |
||
883 |
CyclicBarrier barrier2() { |
|
884 |
return bar2; |
|
885 |
} |
|
886 |
||
887 |
@Override |
|
888 |
public synchronized void handle(HttpExchange he) throws IOException { |
|
889 |
byte[] buf = Util.readAll(he.getRequestBody()); |
|
890 |
try { |
|
891 |
bar1.await(); |
|
892 |
bar2.await(); |
|
893 |
} catch (Exception e) {} |
|
894 |
he.sendResponseHeaders(200, -1); // will probably fail |
|
895 |
he.close(); |
|
896 |
} |
|
897 |
||
898 |
} |
|
899 |
||
900 |
// check for simple hardcoded sequence and use remote address |
|
901 |
// to check. |
|
902 |
// First 4 requests executed in sequence (should use same connection/address) |
|
903 |
// Next 4 requests parallel (should use different addresses) |
|
904 |
// Then send 4 requests in parallel x 100 times (same four addresses used all time) |
|
905 |
||
906 |
class KeepAliveHandler implements HttpHandler { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
907 |
AtomicInteger counter = new AtomicInteger(0); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
908 |
AtomicInteger nparallel = new AtomicInteger(0); |
36131 | 909 |
|
910 |
HashSet<Integer> portSet = new HashSet<>(); |
|
911 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
912 |
int[] ports = new int[8]; |
36131 | 913 |
|
914 |
void sleep(int n) { |
|
915 |
try { |
|
916 |
Thread.sleep(n); |
|
917 |
} catch (InterruptedException e) {} |
|
918 |
} |
|
919 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
920 |
synchronized void setPort(int index, int value) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
921 |
ports[index] = value; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
922 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
923 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
924 |
synchronized int getPort(int index) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
925 |
return ports[index]; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
926 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
927 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
928 |
synchronized void getPorts(int[] dest, int from) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
929 |
dest[0] = ports[from+0]; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
930 |
dest[1] = ports[from+1]; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
931 |
dest[2] = ports[from+2]; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
932 |
dest[3] = ports[from+3]; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
933 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
934 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
935 |
static CountDownLatch latch = new CountDownLatch(4); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
936 |
|
36131 | 937 |
@Override |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
938 |
public void handle (HttpExchange t) |
36131 | 939 |
throws IOException |
940 |
{ |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
941 |
int np = nparallel.incrementAndGet(); |
36131 | 942 |
int remotePort = t.getRemoteAddress().getPort(); |
943 |
String result = "OK"; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
944 |
int[] lports = new int[4]; |
36131 | 945 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
946 |
int n = counter.getAndIncrement(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
947 |
|
36131 | 948 |
/// First test |
949 |
if (n < 4) { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
950 |
setPort(n, remotePort); |
36131 | 951 |
} |
952 |
if (n == 3) { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
953 |
getPorts(lports, 0); |
36131 | 954 |
// check all values in ports[] are the same |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
955 |
if (lports[0] != lports[1] || lports[2] != lports[3] |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
956 |
|| lports[0] != lports[2]) { |
36131 | 957 |
result = "Error " + Integer.toString(n); |
958 |
System.out.println(result); |
|
959 |
} |
|
960 |
} |
|
961 |
// Second test |
|
962 |
if (n >=4 && n < 8) { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
963 |
// delay so that this connection doesn't get reused |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
964 |
// before all 4 requests sent |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
965 |
setPort(n, remotePort); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
966 |
latch.countDown(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
967 |
try {latch.await();} catch (InterruptedException e) {} |
36131 | 968 |
} |
969 |
if (n == 7) { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
970 |
getPorts(lports, 4); |
36131 | 971 |
// should be all different |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
972 |
if (lports[0] == lports[1] || lports[2] == lports[3] |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
973 |
|| lports[0] == lports[2]) { |
36131 | 974 |
result = "Error " + Integer.toString(n); |
975 |
System.out.println(result); |
|
976 |
} |
|
977 |
// setup for third test |
|
978 |
for (int i=0; i<4; i++) { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
979 |
portSet.add(lports[i]); |
36131 | 980 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
981 |
System.out.printf("Ports: %d, %d, %d, %d\n", lports[0], lports[1], lports[2], lports[3]); |
36131 | 982 |
} |
983 |
// Third test |
|
984 |
if (n > 7) { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
985 |
if (np > 4) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
986 |
System.err.println("XXX np = " + np); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
987 |
} |
36131 | 988 |
// just check that port is one of the ones in portSet |
989 |
if (!portSet.contains(remotePort)) { |
|
990 |
System.out.println ("UNEXPECTED REMOTE PORT " + remotePort); |
|
991 |
result = "Error " + Integer.toString(n); |
|
992 |
System.out.println(result); |
|
993 |
} |
|
994 |
} |
|
995 |
byte[] buf = new byte[2048]; |
|
996 |
||
997 |
try (InputStream is = t.getRequestBody()) { |
|
998 |
while (is.read(buf) != -1) ; |
|
999 |
} |
|
1000 |
t.sendResponseHeaders(200, result.length()); |
|
1001 |
OutputStream o = t.getResponseBody(); |
|
1002 |
o.write(result.getBytes("US-ASCII")); |
|
1003 |
t.close(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
1004 |
nparallel.getAndDecrement(); |
36131 | 1005 |
} |
1006 |
} |