Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/http/B6660405.java
38867 views
/*1* Copyright (c) 2008, 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 666040526* @summary HttpURLConnection returns the wrong InputStream27*/2829import java.net.*;30import java.util.*;31import java.io.*;32import com.sun.net.httpserver.*;33import java.util.concurrent.Executors;34import java.util.concurrent.ExecutorService;3536public class B666040537{38com.sun.net.httpserver.HttpServer httpServer;39ExecutorService executorService;4041static class MyCacheResponse extends CacheResponse {42private byte[] buf = new byte[1024];4344public MyCacheResponse() {45}4647@Override48public Map<String, List<String>> getHeaders() throws IOException49{50Map<String, List<String>> h = new HashMap<String, List<String>>();51ArrayList<String> l = new ArrayList<String>();52l.add("HTTP/1.1 200 OK");53h.put(null, l);54l = new ArrayList<String>();55l.add("1024");56h.put("Content-Length", l);57return h;58}5960@Override61public InputStream getBody() throws IOException62{63return new ByteArrayInputStream(buf);64}6566}67static class MyResponseCache extends ResponseCache {6869public MyResponseCache() {70}7172@Override73public CacheResponse get(URI uri, String rqstMethod, Map<String, List<String>> rqstHeaders) throws IOException74{75if (uri.getPath().equals("/redirect/index.html")) {76return new MyCacheResponse();77}78return null;79}8081@Override82public CacheRequest put(URI uri, URLConnection conn) throws IOException83{84return null;85}8687}8889public static void main(String[] args)90{91new B6660405();92}9394public B6660405()95{96try {97startHttpServer();98doClient();99} catch (IOException ioe) {100System.err.println(ioe);101}102}103104void doClient() {105ResponseCache.setDefault(new MyResponseCache());106try {107InetSocketAddress address = httpServer.getAddress();108109// GET Request110URL url = new URL("http://localhost:" + address.getPort() + "/test/index.html");111HttpURLConnection uc = (HttpURLConnection)url.openConnection();112int code = uc.getResponseCode();113System.err.println("response code = " + code);114int l = uc.getContentLength();115System.err.println("content-length = " + l);116InputStream in = uc.getInputStream();117int i = 0;118// Read till end of stream119do {120i = in.read();121} while (i != -1);122in.close();123} catch (IOException e) {124throw new RuntimeException("Got the wrong InputStream after checking headers");125} finally {126httpServer.stop(1);127executorService.shutdown();128}129}130131/**132* Http Server133*/134public void startHttpServer() throws IOException {135httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);136137// create HttpServer context138HttpContext ctx = httpServer.createContext("/test/", new MyHandler());139140executorService = Executors.newCachedThreadPool();141httpServer.setExecutor(executorService);142httpServer.start();143}144145class MyHandler implements HttpHandler {146public void handle(HttpExchange t) throws IOException {147InputStream is = t.getRequestBody();148Headers reqHeaders = t.getRequestHeaders();149Headers resHeaders = t.getResponseHeaders();150151int i = 0;152// Read till end of stream153do {154i = is.read();155} while (i != -1);156is.close();157resHeaders.add("Location", "http://foo.bar/redirect/index.html");158t.sendResponseHeaders(302, -1);159t.close();160}161}162}163164165