/* * Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/* * @test * @summary Tests that our client deals correctly with servers that * close the connection right after sending the last byte. * @library /test/lib http2/server * @build jdk.test.lib.net.SimpleSSLContext HttpServerAdapters EncodedCharsInURI * @modules java.base/sun.net.www.http * java.net.http/jdk.internal.net.http.common * java.net.http/jdk.internal.net.http.frame * java.net.http/jdk.internal.net.http.hpack * @run testng/othervm -Djdk.tls.acknowledgeCloseNotify=true ServerCloseTest
*/ //* -Djdk.internal.httpclient.debug=true
staticfinalint ITERATION_COUNT = 3; // a shared executor helps reduce the amount of threads created by the test staticfinal Executor executor = new TestExecutor(Executors.newCachedThreadPool()); staticfinal ConcurrentMap<String, Throwable> FAILURES = new ConcurrentHashMap<>(); staticvolatileboolean tasksFailed; staticfinal AtomicLong serverCount = new AtomicLong(); staticfinal AtomicLong clientCount = new AtomicLong(); staticfinallong start = System.nanoTime(); publicstatic String now() { long now = System.nanoTime() - start; long secs = now / 1000_000_000; long mill = (now % 1000_000_000) / 1000_000; long nan = now % 1000_000; return String.format("[%d s, %d ms, %d ns] ", secs, mill, nan);
}
privatevolatile HttpClient sharedClient;
staticclass TestExecutor implements Executor { final AtomicLong tasks = new AtomicLong();
Executor executor;
TestExecutor(Executor executor) { this.executor = executor;
}
// This is a bit shaky. It doesn't handle continuation // lines, but our client shouldn't send any. // Read a line from the input stream, swallowing the final // \r\n sequence. Stops at the first \n, doesn't complain // if it wasn't preceded by '\r'. //
String readLine(InputStream r) throws IOException {
StringBuilder b = new StringBuilder(); int c; while ((c = r.read()) != -1) { if (c == '\n') break;
b.appendCodePoint(c);
} if (b.codePointAt(b.length() -1) == '\r') {
b.delete(b.length() -1, b.length());
} return b.toString();
}
URI uri; try {
String hostport = serverAuthority();
uri = new URI((secure ? "https" : "http") + "://" + hostport + path);
} catch (Throwable x) {
System.err.printf(now() + getName() + ": Bad target address: \"%s\" in \"%s\"%n",
path, requestLine);
clientConnection.close(); continue;
}
// Method, path and URI are valid. Add to connections list
connections.add(clientConnection); // Read all headers until we find the empty line that // signals the end of all headers.
String line = requestLine; while (!line.equals("")) {
System.out.println(now() + getName() + ": Reading header: "
+ (line = readLine(ccis)));
headers.append(line).append("\r\n");
}
StringBuilder response = new StringBuilder();
int index = headers.toString()
.toLowerCase(Locale.US)
.indexOf("content-length: "); byte[] b = uri.toString().getBytes(UTF_8); if (index >= 0) {
index = index + "content-length: ".length();
String cl = headers.toString().substring(index);
StringTokenizer tk = new StringTokenizer(cl); int len = Integer.parseInt(tk.nextToken()); assert len < b.length * 2;
System.out.println(now() + getName()
+ ": received body: "
+ new String(ccis.readNBytes(len), UTF_8));
}
System.out.println(now()
+ getName() + ": sending back " + uri);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.