1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8164941
  27  * @modules jdk.incubator.httpclient java.logging jdk.httpserver
  28  * @run main/othervm ZeroRedirects
  29  */
  30 
  31 import com.sun.net.httpserver.Headers;
  32 import com.sun.net.httpserver.HttpContext;
  33 import com.sun.net.httpserver.HttpExchange;
  34 import com.sun.net.httpserver.HttpHandler;
  35 import com.sun.net.httpserver.HttpServer;
  36 import java.io.IOException;
  37 import java.io.OutputStream;
  38 import java.net.URI;
  39 import java.util.concurrent.Executors;
  40 import java.util.concurrent.ExecutorService;
  41 import java.net.InetSocketAddress;
  42 import jdk.incubator.http.HttpClient;
  43 import jdk.incubator.http.HttpRequest;
  44 import jdk.incubator.http.HttpResponse;
  45 import static jdk.incubator.http.HttpRequest.BodyPublisher.fromString;
  46 import static jdk.incubator.http.HttpResponse.BodyHandler.asString;
  47 import static jdk.incubator.http.HttpResponse.BodyHandler.discard;
  48 import static jdk.incubator.http.HttpClient.Version.HTTP_1_1;
  49 import static jdk.incubator.http.HttpClient.Version.HTTP_2;
  50 
  51 /**
  52  */
  53 public class ZeroRedirects {
  54     static HttpServer s1 ;
  55     static ExecutorService executor;
  56     static int port;
  57     static HttpClient client;
  58     static URI uri;
  59 
  60     public static void main(String[] args) throws Exception {
  61         initServer();
  62 
  63         client = HttpClient.newBuilder()
  64                            .executor(executor)
  65                            .followRedirects(HttpClient.Redirect.ALWAYS)
  66                            .build();
  67         try {
  68             test();
  69         } finally {
  70             s1.stop(0);
  71             executor.shutdownNow();
  72         }
  73     }
  74 
  75     public static void test() throws Exception {
  76         System.setProperty("java.net.httpclient.redirects.retrylimit", "0");
  77         HttpRequest r = HttpRequest.newBuilder(uri)
  78                 .GET()
  79                 .build();
  80         HttpResponse<Void> resp = client.send(r, discard(null));
  81         System.out.printf("Client: response is %d\n", resp.statusCode());
  82         if (resp.statusCode() != 200)
  83             throw new RuntimeException();
  84     }
  85 
  86     static void initServer() throws Exception {
  87         InetSocketAddress addr = new InetSocketAddress (0);
  88         s1 = HttpServer.create (addr, 0);
  89         HttpHandler h = new Handler();
  90 
  91         HttpContext c1 = s1.createContext("/", h);
  92 
  93         executor = Executors.newCachedThreadPool();
  94         s1.setExecutor(executor);
  95         s1.start();
  96 
  97         port = s1.getAddress().getPort();
  98         uri = new URI("http://127.0.0.1:" + Integer.toString(port) + "/foo");
  99         System.out.println("HTTP server port = " + port);
 100     }
 101 }
 102 
 103 class Handler implements HttpHandler {
 104 
 105     @Override
 106     public synchronized void handle(HttpExchange t)
 107         throws IOException
 108     {
 109         String reply = "Hello world";
 110         int len = reply.length();
 111         System.out.printf("Sending response 200\n");
 112         t.sendResponseHeaders(200, len);
 113         OutputStream o = t.getResponseBody();
 114         o.write(reply.getBytes());
 115         t.close();
 116     }
 117 }