Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/ResponseCache/B6181108.java
38812 views
/*1* Copyright (c) 2004, 2010, 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* @summary double encoded URL passed to ResponseCache27* @author Edward Wang28*/2930import java.net.*;31import java.util.*;32import java.io.*;333435public class B6181108 implements Runnable {36ServerSocket ss;37static String urlWithSpace;3839/*40* "Our" http server just return 20041*/42public void run() {43try {44Socket s = ss.accept();4546InputStream is = s.getInputStream ();47BufferedReader r = new BufferedReader(new InputStreamReader(is));48String x;49while ((x=r.readLine()) != null) {50if (x.length() ==0) {51break;52}53}54PrintStream out = new PrintStream(55new BufferedOutputStream(56s.getOutputStream() ));5758/* response 200 */59out.print("HTTP/1.1 200 OK\r\n");60out.print("Content-Type: text/html; charset=iso-8859-1\r\n");61out.print("Content-Length: 0\r\n");62out.print("Connection: close\r\n");63out.print("\r\n");64out.print("\r\n");6566out.flush();6768s.close();69} catch (Exception e) {70e.printStackTrace();71} finally {72try { ss.close(); } catch (IOException unused) {}73}74}7576static class ResponseCache extends java.net.ResponseCache {77public CacheResponse get (URI uri, String method, Map<String,List<String>> hdrs) {78System.out.println ("get uri = " + uri);79if (!urlWithSpace.equals(uri.toString())) {80throw new RuntimeException("test failed");81}82return null;83}84public CacheRequest put (URI uri, URLConnection urlc) {85System.out.println ("put uri = " + uri);86return null;87}88}8990B6181108() throws Exception {91/* start the server */92ss = new ServerSocket(0);93(new Thread(this)).start();9495ResponseCache.setDefault (new ResponseCache());96urlWithSpace = "http://localhost:" +97Integer.toString(ss.getLocalPort()) +98"/space%20test/page1.html";99URL url = new URL (urlWithSpace);100URLConnection urlc = url.openConnection();101int i = ((HttpURLConnection)(urlc)).getResponseCode();102System.out.println ("response code = " + i);103ResponseCache.setDefault(null);104}105106public static void main(String args[]) throws Exception {107new B6181108();108}109110}111112113