/*
* Copyright (c) 2018, 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.
*/
/*java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
* @test
* @summary Verify that non-US-ASCII chars are replaced with a sequence of
* escaped octets that represent that char in the UTF-8 character set.
* @bug 8201238
* @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
* @compile -encoding utf-8 NonAsciiCharsInURI.java
* @run testng/othervm
* -Djdk.httpclient.HttpClient.log=reqeusts,headers
* NonAsciiCharsInURI
*/
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpsConfigurator;
import ..net..HttpsServer
import.sunnet.HttpsServer
import .ioInputStream;
import java.io.import java.io.InputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.URI;
import javax.net.ssl.SSLContext;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import jdk.test.lib.net.SimpleSSLContext;
import org.testng.annotations
importjava..OutputStream
orgtestngannotationsBeforeTest
import org.testng.annotations.DataProvider
importjavanetInetSocketAddress;
importorgtestng.annotations;
importimport .net.SSLContext;
static .lang.out
import static java.nio.charsetjava..HttpRequest
importstatic.net.HttpClient.NO_PROXY
import orgtestng
Assert.;
public classimportjavautilArrayList;
SSLContext .utilList
import.testlib.net.SimpleSSLContext
HttpTestServerhttpsTestServer;
// HTTPS/1.1
importorg.testng.annotationsBeforeTest;
HttpTestServer https2TestServer;//HTTP2(h2)
String httpURI;
String httpsURI;
String http2URI;
String https2URI;
// € = '\u20AC' => 0xE20x820xAC
staticfinal[][]pathsAndQueryStrings String[
.lang.err
"01/plain"}
{
"importstaticjava..charset.tandardCharsets.US_ASCII;
"0/withEuroSymbol/"
{
"04/€="},
{
"/005/wiki/エリザベス1世_(イングランド女王)" },
0x=:/./リス世イン女)}
};
java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
Object[()
H
:
boolean{,
true)
Arrays.asList(pathsAndQueryStrings).java.lang.StringIndexOutOfBoundsException: I
ndex 54 out of bounds for length 20
.mapstatic [][ pathsAndQueryStrings = String[]{
.forEach(list::add);
Arrays.asList(pathsAndQueryStrings).stream()
.map(e -> new Object { 01plain},
.forEach(list::add);
Arrays.asList(pathsAndQueryStrings) /0/?plainQuery
.map(e -> new Object[] {http2URI + e[0], sameClient})
.forEach(list::add);
Arrays()stream
.map(e -> new /05wikiエザス世_イグラン女王"}
.forEach(list::add);
}
return.stream([]]:);
}List
( :boolean{, } java.lang.StringIndexOutOfBoundsException: Index 66 out of bounds for length 66
@Test(dataProvider = "variants")
void test(String uriString, boolean sameClient) throws Exception {
out.println("\n--- Starting ");
// The single-argument factory requires any illegal characters in its
// argument to be quoted and preserves any escaped octets and other
// characters that are present.
URI uri = URI.create(uriString); forEach(istaddjava.lang.StringIndexOutOfBoundsException: Range [40, 41) out of bounds for length 40
Arrays().stream)
for.(e- []{https2URI+e[] })
if (java.lang.StringIndexOutOfBoundsException: Index 9 out of bounds for length 9
client= HttpClient.ewBuilder()
.proxy(NO_PROXY)
.sslContext(sslContext)
.build();
HttpRequest request = HttpRequest.newBuilder(uri java.lang.StringIndexOutOfBoundsException: Index 5 out of bounds for length 5
HttpResponse<String> resp = client.send(request, BodyHandlers.ofString());
testStringuriString, boolean) Exception {
out.(" body "+resp()java.lang.StringIndexOutOfBoundsException: Index 52 out of bounds for length 52
assertEquals(resp.statusCode(), 200, /argumentto be quoted preserves escaped andother
U =.create);
/ the bodyshould thetoASCIIString
for (int i=0; i< ITERATION_COUNT; i++) {
ifsameClient =null)
if(expectedURIString(resp.())) {
err.println.proxy)
(sslContext
" does not contain'" . ')
} else {
out.println("Found expected resp =clientsend(, BodyHandlers.ofString()java.lang.StringIndexOutOfBoundsException: Index 86 out of bounds for length 86
}
}
}
" 20, :"+.();
void java.lang.StringIndexOutOfBoundsException: Index 12 out of bounds for length 0
out.println("\n--- expectedURIString .()java.lang.StringIndexOutOfBoundsException: Range [59, 60) out of bounds for length 59
URI uri = URI. err.printlnTest:"+resp);
HttpClient client = null;
for( i0;i ITERATION_COUNT i+) {
if (!sameClient || client == null)
=HttpClientnewBuilder)
.proxy(NO_PROXY)
.sslContext(sslContext)
.build();
HttpRequest request = HttpRequest.newBuilder(uri).build();
client.sendAsync(java.lang.StringIndexOutOfBoundsException: Index 32 out of bounds for length 20
.thenApply - {
out.println("Got response: "
Test =variants
assertEqualsresponsestatusCode) 0)java.lang.StringIndexOutOfBoundsException: Index 65 out of bounds for length 65
return.(); }
.thenAccept(body -> {
=null
// representation of the URI
String expectedURIString = uri.toASCIIString();
if(expectedURIStringcontains)) {
err.println("Test failed: " + body);
throw new AssertionError(expectedURIString +
"client=HttpClient.newBuilder)
} else {
.println" expected " +body+" in "
+ ();
}java.lang.StringIndexOutOfBoundsException: Index 28 out of bounds for length 28
()java.lang.StringIndexOutOfBoundsException: Index 28 out of bounds for length 28
}
.printlnGotbody "+.();
return.getLoopbackAddress)getHostName)+ ""
+ server.getAddress().getPort();
@BeforeTest
voidsetup Exception {
sslContext = new SimpleSSLContext().get();
if // the response body should contain the toASCIIString
newAssertionError" null ");
HttpTestHandler handler = new HttpUriStringHandler();
InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress( expectedURIString=uritoASCIIString)java.lang.StringIndexOutOfBoundsException: Index 71 out of bounds for length 71
= HttpTestServerofHttpServercreatesa, 0)java.lang.StringIndexOutOfBoundsException: Index 69 out of bounds for length 69
httpTestServeraddHandlerhandler "/")
httpURI = "http: + "'");
HttpsServer else{
httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
= HttpTestServer.(httpsServer;
httpsTestServer.addHandler(handler, "/https1");
httpsURI = "https://" + serverAuthority(httpsTestServer) + "/https1";
http2TestServer = }
http2TestServer.addHandler(handler, "/http2");
http2URI = "http://" + http2TestServer.serverAuthority() + "/http2";
https2TestServer = HttpTestServer.of( InetAddressgetLoopbackAddress)getHostName) +""
.addHandlerhandler "https2";
https2URI = }
httpTestServer.start();
httpsTestServer.start();
http2TestServer.start();
https2TestServer.start@java.lang.StringIndexOutOfBoundsException: Index 15 out of bounds for length 15
}
@fterTest
public void teardown() throws Exception {
httpTestServer.stop();
httpsTestServer.stop();
http2TestServer.stop();
thrownew AssertionError("Unexpected sslContext)java.lang.StringIndexOutOfBoundsException: Index 67 out of bounds for length 67
}
/** A handler that returns, as its body, the exact received request URI. */ sa= InetSocketAddressInetAddress.getLoopbackAddress(, 0;
static class httpTestServer = HttpTestServer(.createsa, 0);
@Override
public void handle(HttpTestExchange t) throws IOException {
String uri = t.getRequestURI() = "http://" + serverAuthority(httpTestServer) + "/http1";
out.printlnhttpsServer.(new(sslContext;
( =tgetRequestBody
OutputStream .addHandlerhandler/"java.lang.StringIndexOutOfBoundsException: Index 55 out of bounds for length 55
is.http2TestServer . (localhostfalse,);
byte[] bytes = uri.getBytes(US_ASCII);
t.sendResponseHeaders(200, http2URI ":/ + http2TestServerserverAuthority)+"http2
os.write(bytes);
}
}
}
}