Path: blob/master/test/jdk/java/net/ResponseCache/B6181108.java
41152 views
/*1* Copyright (c) 2004, 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*/2223/**24* @test25* @bug 618110826* @library /test/lib27* @summary double encoded URL passed to ResponseCache28* @author Edward Wang29*/3031import java.net.*;32import java.util.*;33import java.io.*;34import jdk.test.lib.net.URIBuilder;35import static java.net.Proxy.NO_PROXY;3637public class B6181108 implements Runnable {38ServerSocket ss;39static String urlWithSpace;4041/*42* "Our" http server just return 20043*/44public void run() {45try {46Socket s = ss.accept();4748InputStream is = s.getInputStream();49BufferedReader r = new BufferedReader(new InputStreamReader(is));50String x;51while ((x=r.readLine()) != null) {52if (x.length() ==0) {53break;54}55}56PrintStream out = new PrintStream(57new BufferedOutputStream(58s.getOutputStream() ));5960/* response 200 */61out.print("HTTP/1.1 200 OK\r\n");62out.print("Content-Type: text/html; charset=iso-8859-1\r\n");63out.print("Content-Length: 0\r\n");64out.print("Connection: close\r\n");65out.print("\r\n");66out.print("\r\n");6768out.flush();6970s.close();71} catch (Exception e) {72e.printStackTrace();73} finally {74try { ss.close(); } catch (IOException unused) {}75}76}7778static class ResponseCache extends java.net.ResponseCache {79public CacheResponse get(URI uri, String method, Map<String,List<String>> hdrs) {80System.out.println("get uri = " + uri);81if (!urlWithSpace.equals(uri.toString())) {82throw new RuntimeException("test failed");83}84return null;85}86public CacheRequest put(URI uri, URLConnection urlc) {87System.out.println("put uri = " + uri);88return null;89}90}9192B6181108() throws Exception {93/* start the server */94InetAddress loopback = InetAddress.getLoopbackAddress();95ss = new ServerSocket();96ss.bind(new InetSocketAddress(loopback, 0));97(new Thread(this)).start();9899ResponseCache.setDefault(new ResponseCache());100String base = URIBuilder.newBuilder()101.scheme("http")102.loopback()103.port(ss.getLocalPort())104.build()105.toString();106urlWithSpace = base + "/space%20test/page1.html";107URL url = new URL(urlWithSpace);108URLConnection urlc = url.openConnection(NO_PROXY);109int i = ((HttpURLConnection)(urlc)).getResponseCode();110System.out.println("response code = " + i);111ResponseCache.setDefault(null);112}113114public static void main(String args[]) throws Exception {115new B6181108();116}117118}119120121