/* * Copyright (c) 2021, 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 * @bug 8276774 * @summary Test that user-supplied cookies are appended to * server-cookies for HTTP/2 vs HTTP/1.1 * @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 * java.logging * jdk.httpserver * @library /test/lib http2/server * @build Http2TestServer * @build jdk.test.lib.net.SimpleSSLContext * @run testng/othervm * -Djdk.tls.acknowledgeCloseNotify=true * -Djdk.httpclient.HttpClient.log=trace,headers,requests * UserCookieTest
*/
String uuid = uuids.get(0); // retrying if (closedRequests.putIfAbsent(uuid, t.getRequestURI().toString()) == null) { if (t.getExchangeVersion() == HttpClient.Version.HTTP_1_1) { // Throwing an exception here only causes a retry // with HTTP_1_1 - where it forces the server to close // the connection. // For HTTP/2 then throwing an IOE would cause the server // to close the stream, and throwing anything else would // cause it to close the connection, but neither would // cause the client to retry. // So we simply do not try to retry with HTTP/2 and just verify // we have received the expected cookie thrownew IOException("Closing on first request");
}
}
// Check whether this request was upgraded. // An upgraded request will have a version of HTTP_2 and // an Upgrade: h2c header
HttpClient.Version version = t.getExchangeVersion();
List<String> upgrade = t.getRequestHeaders().get("Upgrade"); if (upgrade == null) upgrade = List.of(); boolean upgraded = version == HttpClient.Version.HTTP_2
&& upgrade.stream().anyMatch("h2c"::equalsIgnoreCase);
// 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();
}
// Read all headers until we find the empty line that // signals the end of all headers.
String line = requestLine;
String cookies = null; while (!line.equals("")) {
System.out.println(now() + getName() + ": Reading header: "
+ (line = readLine(ccis))); if (line.startsWith("Cookie:")) { if (cookies == null) cookies = line; else cookies = cookies + "\n" + line;
}
headers.append(line).append("\r\n");
}
StringBuilder response = new StringBuilder();
StringBuilder xheaders = new StringBuilder();
int index = headers.toString()
.toLowerCase(Locale.US)
.indexOf("content-length: "); 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());
System.out.println(now() + getName()
+ ": received body: "
+ new String(ccis.readNBytes(len), UTF_8));
}
String resp = MESSAGE;
String status = "200 OK"; if (cookies == null) {
resp = "No cookies found in headers";
status = "500 Internal Server Error";
} elseif (cookies.contains("\n")) {
resp = "More than one 'Cookie:' line found: "
+ Arrays.asList(cookies.split("\n"));
status = "500 Internal Server Error";
} else {
List<String> values =
Stream.of(cookies.substring("Cookie:".length()).trim().split("; "))
.map(String::trim)
.collect(Collectors.toList());
Collections.sort(values); if (values.size() != 3) {
resp = "Bad cookie list: " + values;
status = "500 Internal Server Error";
} elseif (!values.get(0).equals("CUSTOMER=ARTHUR_DENT")) {
resp = "Unexpected cookie: " + values.get(0) + " in " + values;
status = "500 Internal Server Error";
} elseif (!values.get(1).equals("ORDER=BISCUITS")) {
resp = "Unexpected cookie: " + values.get(1) + " in " + values;
status = "500 Internal Server Error";
} elseif (!values.get(2).equals("PRICE=42")) {
resp = "Unexpected cookie: " + values.get(1) + " in " + values;
status = "500 Internal Server Error";
} else { for (String cookie : values) {
xheaders.append("X-Request-Cookie: ")
.append(cookie)
.append("\r\n");
}
}
} byte[] b = resp.getBytes(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.