Path: blob/master/test/jdk/com/sun/net/httpserver/bugs/8199849/ParamTest.java
41161 views
/*1* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.*;24import java.net.*;25import java.net.http.HttpClient;26import java.net.http.HttpRequest;27import java.net.http.HttpResponse;28import java.util.*;29import java.nio.charset.StandardCharsets;30import jdk.test.lib.net.URIBuilder;3132/**33* @test34* @bug 8199849 823597635* @summary36* @library /test/lib37* @run main/othervm ParamTest38* @run main/othervm -Djava.net.preferIPv6Addresses=true ParamTest39*/4041public class ParamTest {4243static final String[] variants = {44" ,charset=utf-8",45" ,charset=UtF-8",46" ,charset=\"utF-8\"",47" ,charset=\"UtF-8\""48};4950static final int LOOPS = variants.length;5152volatile static boolean error = false;5354static class BasicServer extends Thread {5556final ServerSocket server;5758Socket s;59InputStream is;60OutputStream os;6162static final String realm = "wallyworld";6364String reply1 = "HTTP/1.1 401 Unauthorized\r\n"+65"WWW-Authenticate: Basic realm=\""+realm+"\"\r\n";6667String reply2 = "HTTP/1.1 200 OK\r\n"+68"Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +69"Server: Apache/1.3.14 (Unix)\r\n" +70"Connection: close\r\n" +71"Content-Type: text/html; charset=iso-8859-1\r\n" +72"Content-Length: 10\r\n\r\n";7374BasicServer(ServerSocket s) {75server = s;76}7778String readHeaders(Socket sock) throws IOException {79InputStream is = sock.getInputStream();80String s = "";81byte[] buf = new byte[1024];82while (!s.endsWith("\r\n\r\n")) {83int c = is.read(buf);84if (c == -1)85return s;86String f = new String(buf, 0, c, StandardCharsets.ISO_8859_1);87s = s + f;88}89return s;90}9192void check(String s, int iteration) {93if (s.indexOf(encodedAuthString) == -1) {94System.err.printf("On iteration %d, wrong auth string received %s\n", iteration, s);95error = true;96} else {97System.err.println("check: correct auth string received");98}99}100101public void run() {102try {103for (int j = 0; j < 2; j++)104for (int i = 0; i < LOOPS; i++) {105System.out.println("Server 1: accept");106s = server.accept();107readHeaders(s);108System.out.println("accepted");109os = s.getOutputStream();110String str = reply1 + variants[i] + "\r\n\r\n";111os.write(str.getBytes());112113System.out.println("Server 2: accept");114Socket s1 = server.accept();115String request = readHeaders(s1);116check(request, i);117System.out.println("accepted");118os = s1.getOutputStream();119os.write((reply2 + "HelloWorld").getBytes());120os.flush();121s.close();122s1.close();123finished();124}125} catch (Exception e) {126System.out.println(e);127error = true;128}129}130131public synchronized void finished() {132notifyAll();133}134135}136137static final String password = "Selam D\u00fcnya.";138139// "user : <password above>" encoded in UTF-8 and converted to Base 64140141static final String encodedAuthString = "dXNlcjpTZWxhbSBEw7xueWEu";142143static class MyAuthenticator extends Authenticator {144MyAuthenticator() {145super();146}147148public PasswordAuthentication getPasswordAuthentication()149{150System.out.println("Auth called");151return (new PasswordAuthentication ("user", password.toCharArray()));152}153}154155156static void read(InputStream is) throws IOException {157int c;158System.out.println("reading");159while ((c=is.read()) != -1) {160System.out.write(c);161}162System.out.println("");163System.out.println("finished reading");164}165166public static void main(String args[]) throws Exception {167MyAuthenticator auth = new MyAuthenticator();168Authenticator.setDefault(auth);169InetAddress loopback = InetAddress.getLoopbackAddress();170ServerSocket ss = new ServerSocket();171ss.bind(new InetSocketAddress(loopback, 0));172int port = ss.getLocalPort();173BasicServer server = new BasicServer(ss);174synchronized (server) {175server.start();176System.out.println("client 1");177String base = URIBuilder.newBuilder()178.scheme("http")179.loopback()180.port(port)181.path("/")182.build()183.toString();184URL url = new URL(base + "d1/d2/d3/foo.html");185186for (int i = 0; i < LOOPS; i++) {187URLConnection urlc = url.openConnection(Proxy.NO_PROXY);188InputStream is = urlc.getInputStream();189read(is);190System.out.println("Client: waiting for notify");191server.wait();192System.out.println("Client: continue");193// check if authenticator was called once (ok) or twice (not)194if (error) {195System.err.println("Error old client iteration " + i);196}197}198199URI uri = url.toURI();200HttpClient client = HttpClient.newBuilder()201.authenticator(auth)202.proxy(ProxySelector.of(null))203.build();204205HttpRequest request = HttpRequest206.newBuilder(uri)207.GET()208.build();209210for (int i = 0; i < LOOPS; i++) {211HttpResponse<Void> response = client.send(request, HttpResponse.BodyHandlers.discarding());212int status = response.statusCode();213if (status != 200) {214System.err.printf("Error new client (%d) iteration ",215status, i);216error = true;217} else218System.err.println("New client ok iteration " + i);219System.out.println("New Client: waiting for notify");220server.wait();221System.out.println("New Client: continue");222}223224if (error) {225throw new RuntimeException("Test failed");226}227}228}229}230231232