55N/A/*
2362N/A * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
55N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
55N/A *
55N/A * This code is free software; you can redistribute it and/or modify it
55N/A * under the terms of the GNU General Public License version 2 only, as
55N/A * published by the Free Software Foundation.
55N/A *
55N/A * This code is distributed in the hope that it will be useful, but WITHOUT
55N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
55N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
55N/A * version 2 for more details (a copy is included in the LICENSE file that
55N/A * accompanied this code).
55N/A *
55N/A * You should have received a copy of the GNU General Public License version
55N/A * 2 along with this work; if not, write to the Free Software Foundation,
55N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
55N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
55N/A */
55N/A
55N/A/*
55N/A * @test
55N/A * @bug 6660405
55N/A * @summary HttpURLConnection returns the wrong InputStream
55N/A */
55N/A
55N/Aimport java.net.*;
55N/Aimport java.util.*;
55N/Aimport java.io.*;
55N/Aimport com.sun.net.httpserver.*;
55N/Aimport java.util.concurrent.Executors;
55N/Aimport java.util.concurrent.ExecutorService;
55N/A
55N/Apublic class B6660405
55N/A{
55N/A com.sun.net.httpserver.HttpServer httpServer;
55N/A ExecutorService executorService;
55N/A
55N/A static class MyCacheResponse extends CacheResponse {
55N/A private byte[] buf = new byte[1024];
55N/A
55N/A public MyCacheResponse() {
55N/A }
55N/A
55N/A @Override
55N/A public Map<String, List<String>> getHeaders() throws IOException
55N/A {
55N/A Map<String, List<String>> h = new HashMap<String, List<String>>();
55N/A ArrayList<String> l = new ArrayList<String>();
55N/A l.add("HTTP/1.1 200 OK");
55N/A h.put(null, l);
55N/A l = new ArrayList<String>();
55N/A l.add("1024");
55N/A h.put("Content-Length", l);
55N/A return h;
55N/A }
55N/A
55N/A @Override
55N/A public InputStream getBody() throws IOException
55N/A {
55N/A return new ByteArrayInputStream(buf);
55N/A }
55N/A
55N/A }
55N/A static class MyResponseCache extends ResponseCache {
55N/A
55N/A public MyResponseCache() {
55N/A }
55N/A
55N/A @Override
55N/A public CacheResponse get(URI uri, String rqstMethod, Map<String, List<String>> rqstHeaders) throws IOException
55N/A {
55N/A if (uri.getPath().equals("/redirect/index.html")) {
55N/A return new MyCacheResponse();
55N/A }
55N/A return null;
55N/A }
55N/A
55N/A @Override
55N/A public CacheRequest put(URI uri, URLConnection conn) throws IOException
55N/A {
55N/A return null;
55N/A }
55N/A
55N/A }
55N/A
55N/A public static void main(String[] args)
55N/A {
55N/A new B6660405();
55N/A }
55N/A
55N/A public B6660405()
55N/A {
55N/A try {
55N/A startHttpServer();
55N/A doClient();
55N/A } catch (IOException ioe) {
55N/A System.err.println(ioe);
55N/A }
55N/A }
55N/A
55N/A void doClient() {
55N/A ResponseCache.setDefault(new MyResponseCache());
55N/A try {
55N/A InetSocketAddress address = httpServer.getAddress();
55N/A
55N/A // GET Request
55N/A URL url = new URL("http://localhost:" + address.getPort() + "/test/index.html");
55N/A HttpURLConnection uc = (HttpURLConnection)url.openConnection();
55N/A int code = uc.getResponseCode();
55N/A System.err.println("response code = " + code);
55N/A int l = uc.getContentLength();
55N/A System.err.println("content-length = " + l);
55N/A InputStream in = uc.getInputStream();
55N/A int i = 0;
55N/A // Read till end of stream
55N/A do {
55N/A i = in.read();
55N/A } while (i != -1);
55N/A in.close();
55N/A } catch (IOException e) {
55N/A throw new RuntimeException("Got the wrong InputStream after checking headers");
55N/A } finally {
55N/A httpServer.stop(1);
55N/A executorService.shutdown();
55N/A }
55N/A }
55N/A
55N/A /**
55N/A * Http Server
55N/A */
55N/A public void startHttpServer() throws IOException {
55N/A httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);
55N/A
55N/A // create HttpServer context
55N/A HttpContext ctx = httpServer.createContext("/test/", new MyHandler());
55N/A
55N/A executorService = Executors.newCachedThreadPool();
55N/A httpServer.setExecutor(executorService);
55N/A httpServer.start();
55N/A }
55N/A
55N/A class MyHandler implements HttpHandler {
55N/A public void handle(HttpExchange t) throws IOException {
55N/A InputStream is = t.getRequestBody();
55N/A Headers reqHeaders = t.getRequestHeaders();
55N/A Headers resHeaders = t.getResponseHeaders();
55N/A
55N/A int i = 0;
55N/A // Read till end of stream
55N/A do {
55N/A i = is.read();
55N/A } while (i != -1);
55N/A is.close();
55N/A resHeaders.add("Location", "http://foo.bar/redirect/index.html");
55N/A t.sendResponseHeaders(302, -1);
55N/A t.close();
55N/A }
55N/A }
55N/A}