/* * 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 this test verifies that a client may provides authorization * headers directly when connecting with a server. * @bug 8087112 * @library /test/lib http2/server * @build jdk.test.lib.net.SimpleSSLContext HttpServerAdapters DigestEchoServer * ReferenceTracker DigestEchoClient * @modules java.net.http/jdk.internal.net.http.common * java.net.http/jdk.internal.net.http.frame * java.net.http/jdk.internal.net.http.hpack * java.logging * java.base/sun.net.www.http * java.base/sun.net.www * java.base/sun.net * @run main/othervm DigestEchoClient * @run main/othervm -Djdk.http.auth.proxying.disabledSchemes= * -Djdk.http.auth.tunneling.disabledSchemes= * DigestEchoClient
*/
publicclass DigestEchoClient {
staticfinal String data[] = { "Lorem ipsum", "dolor sit amet", "consectetur adipiscing elit, sed do eiusmod tempor", "quis nostrud exercitation ullamco", "laboris nisi", "ut", "aliquip ex ea commodo consequat." + "Duis aute irure dolor in reprehenderit in voluptate velit esse" + "cillum dolore eu fugiat nulla pariatur.", "Excepteur sint occaecat cupidatat non proident."
};
staticfinal AtomicLong serverCount = new AtomicLong(); staticfinalclass EchoServers { final DigestEchoServer.HttpAuthType authType; final DigestEchoServer.HttpAuthSchemeType authScheme; final String protocolScheme; final String key; final DigestEchoServer server; final Version serverVersion;
publicstatic List<Boolean> expectContinue(Version serverVersion) { if (serverVersion == Version.HTTP_1_1) { return BOOLEANS;
} else { // our test HTTP/2 server does not support Expect: 100-Continue return List.of(Boolean.FALSE);
}
}
publicstaticvoid main(String[] args) throws Exception {
HttpServerAdapters.enableServerLogging(); boolean useSSL = false;
EnumSet<DigestEchoServer.HttpAuthType> types =
EnumSet.complementOf(EnumSet.of(DigestEchoServer.HttpAuthType.PROXY305));
Throwable failed = null; if (args != null && args.length >= 1) {
useSSL = "SSL".equals(args[0]); if (args.length > 1) {
List<DigestEchoServer.HttpAuthType> httpAuthTypes =
Stream.of(Arrays.copyOfRange(args, 1, args.length))
.map(DigestEchoServer.HttpAuthType::valueOf)
.collect(Collectors.toList());
types = EnumSet.copyOf(httpAuthTypes);
}
} try { for (DigestEchoServer.HttpAuthType authType : types) { // The test server does not support PROXY305 properly if (authType == DigestEchoServer.HttpAuthType.PROXY305) continue;
EnumSet<DigestEchoServer.HttpAuthSchemeType> basics =
EnumSet.of(DigestEchoServer.HttpAuthSchemeType.BASICSERVER,
DigestEchoServer.HttpAuthSchemeType.BASIC); for (DigestEchoServer.HttpAuthSchemeType authScheme : basics) {
DigestEchoClient dec = new DigestEchoClient(useSSL,
authScheme,
authType); for (Version clientVersion : clientVersions()) { for (Version serverVersion : serverVersions(clientVersion)) { for (boolean expectContinue : expectContinue(serverVersion)) { for (boolean async : BOOLEANS) { for (boolean preemptive : BOOLEANS) {
dec.testBasic(clientVersion,
serverVersion, async,
expectContinue, preemptive);
}
}
}
}
}
}
EnumSet<DigestEchoServer.HttpAuthSchemeType> digests =
EnumSet.of(DigestEchoServer.HttpAuthSchemeType.DIGEST); for (DigestEchoServer.HttpAuthSchemeType authScheme : digests) {
DigestEchoClient dec = new DigestEchoClient(useSSL,
authScheme,
authType); for (Version clientVersion : clientVersions()) { for (Version serverVersion : serverVersions(clientVersion)) { for (boolean expectContinue : expectContinue(serverVersion)) { for (boolean async : BOOLEANS) {
dec.testDigest(clientVersion, serverVersion,
async, expectContinue);
}
}
}
}
}
}
} catch(Throwable t) {
out.println(DigestEchoServer.now()
+ ": Unexpected exception: " + t);
t.printStackTrace();
failed = t; throw t;
} finally { Thread.sleep(100);
AssertionError trackFailed = TRACKER.check(500);
EchoServers.stop();
System.out.println(" ---------------------------------------------------------- ");
System.out.println(String.format("DigestEchoClient %s %s", useSSL ? "SSL" : "CLEAR", types));
System.out.println(String.format("Created %d clients and %d servers",
clientCount.get(), serverCount.get()));
System.out.println(String.format("basics: %d requests sent, %d ns / req",
basicCount.get(), basics.get()));
System.out.println(String.format("digests: %d requests sent, %d ns / req",
digestCount.get(), digests.get()));
System.out.println(" ---------------------------------------------------------- "); if (trackFailed != null) { if (failed != null) {
failed.addSuppressed(trackFailed); if (failed instanceof Error) throw (Error) failed; if (failed instanceof Exception) throw (Exception) failed;
} throw trackFailed;
}
}
}
// In case of a tunnel connection only the first request // which establishes the tunnel needs to authenticate with // the proxy. if (challenge != null && (!isTunnel || isSchemeDisabled())) { assert cnonceStr != null;
String auth = digestResponse(uri, digestMethod, challenge, cnonceStr); try {
reqBuilder = reqBuilder.header(authorizationKey(authType), auth);
} catch (IllegalArgumentException x) { throw x;
}
}
long start = System.nanoTime();
HttpRequest request = reqBuilder.build();
HttpResponse<Stream<String>> resp; if (async) {
resp = client.sendAsync(request, BodyHandlers.ofLines()).join();
} else {
resp = client.send(request, BodyHandlers.ofLines());
}
System.out.println(resp); assert challenge != null || resp.statusCode() == 401 || resp.statusCode() == 407
: "challenge=" + challenge + ", resp=" + resp + ", test=[" + test + "]"; if (resp.statusCode() == 401 || resp.statusCode() == 407) { // This assert may need to be relaxed if our server happened to // decide to close the tunnel connection, in which case we would // receive 407 again... assert challenge == null || !isTunnel || isSchemeDisabled()
: "No proxy auth should be required after establishing an SSL tunnel";
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.