/* * Copyright (c) 2015, 2019, 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 8151441 * @summary Request body of incorrect (larger or smaller) sizes than that * reported by the body publisher * @run main/othervm ShortRequestBody
*/
// Some body types ( sources ) for testing. staticfinal String STRING_BODY = "Hello world"; staticfinalbyte[] BYTE_ARRAY_BODY = newbyte[] {
(byte)0xCA, (byte)0xFE, (byte)0xBA, (byte)0xBE }; staticfinal Path FILE_BODY = testSrc.resolve("docs").resolve("files").resolve("foo.txt");
// Body lengths and offsets ( amount to be wrong by ), to make coordination // between client and server easier. staticfinalint[] BODY_LENGTHS = newint[] { STRING_BODY.length(),
BYTE_ARRAY_BODY.length,
fileSize(FILE_BODY) }; staticfinalint[] BODY_OFFSETS = newint[] { 0, +1, -1, +2, -2, +3, -3 }; staticfinal String MARKER = "ShortRequestBody";
// A delegating Body Publisher. Subtypes will have a concrete body type. staticabstractclass AbstractDelegateRequestBody implements HttpRequest.BodyPublisher {
final HttpRequest.BodyPublisher delegate; finallong contentLength;
try (Server server = new Server()) { for (Supplier<HttpClient> cs : clientSuppliers) {
err.println("\n---- next supplier ----\n");
URI uri = new URI("http://localhost:" + server.getPort() + "/" + MARKER);
// sanity ( 6 requests to keep client and server offsets easy to workout )
success(cs, uri, new StringRequestBody(STRING_BODY, 0));
success(cs, uri, new ByteArrayRequestBody(BYTE_ARRAY_BODY, 0));
success(cs, uri, new FileRequestBody(FILE_BODY, 0));
success(cs, uri, new StringRequestBody(STRING_BODY, 0));
success(cs, uri, new ByteArrayRequestBody(BYTE_ARRAY_BODY, 0));
success(cs, uri, new FileRequestBody(FILE_BODY, 0));
for (int i = 1; i < BODY_OFFSETS.length; i++) {
failureBlocking(cs, uri, new StringRequestBody(STRING_BODY, BODY_OFFSETS[i]));
failureBlocking(cs, uri, new ByteArrayRequestBody(BYTE_ARRAY_BODY, BODY_OFFSETS[i]));
failureBlocking(cs, uri, new FileRequestBody(FILE_BODY, BODY_OFFSETS[i]));
failureNonBlocking(cs, uri, new StringRequestBody(STRING_BODY, BODY_OFFSETS[i]));
failureNonBlocking(cs, uri, new ByteArrayRequestBody(BYTE_ARRAY_BODY, BODY_OFFSETS[i]));
failureNonBlocking(cs, uri, new FileRequestBody(FILE_BODY, BODY_OFFSETS[i]));
}
}
}
}
Server() throws IOException { super("Test-Server");
ss = new ServerSocket();
ss.setReuseAddress(false);
ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0)); this.start();
}
int getPort() { return ss.getLocalPort(); }
@Override publicvoid run() { int count = 0; int offset = 0;
while (!closed) {
err.println("Server: waiting for connection"); try (Socket s = ss.accept()) {
err.println("Server: got connection");
InputStream is = s.getInputStream(); try {
String headers = readRequestHeaders(is); if (headers == null) continue;
} catch (SocketException ex) {
err.println("Ignoring unexpected exception while reading headers: " + ex);
ex.printStackTrace(err); // proceed in order to update count etc..., even though // we know that read() will fail;
} byte[] ba = newbyte[1024];
int length = BODY_LENGTHS[count % 3];
length += BODY_OFFSETS[offset];
err.println("Server: count=" + count + ", offset=" + offset);
err.println("Server: expecting " +length+ " bytes"); int read = 0; try {
read = is.readNBytes(ba, 0, length);
err.println("Server: actually read " + read + " bytes");
} finally { // Update the counts before replying, to prevent the // client-side racing reset with this thread.
count++; if (count % 6 == 0) // 6 is the number of failure requests per offset
offset++; if (count % 42 == 0) {
count = 0; // reset, for second iteration
offset = 0;
}
} if (read < length) { // no need to reply, client has already closed // ensure closed if (is.read() != -1) new AssertionError("Unexpected read: " + read);
} else {
OutputStream os = s.getOutputStream();
err.println("Server: writing "
+ RESPONSE.getBytes(US_ASCII).length + " bytes");
os.write(RESPONSE.getBytes(US_ASCII));
}
} catch (Throwable e) { if (!closed) {
err.println("Unexpected: " + e);
e.printStackTrace();
}
}
}
}
staticboolean check(boolean cond, Throwable t, Object... failedArgs) { if (cond) returntrue; // We are going to fail...
StringBuilder sb = new StringBuilder(); for (Object o : failedArgs)
sb.append(o); thrownew RuntimeException(sb.toString(), t);
}
}
¤ Dauer der Verarbeitung: 0.16 Sekunden
(vorverarbeitet)
¤
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.