1 /* 2 * Copyright (c) 2006, 2016, 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 6427251 6382788 27 * @modules jdk.httpserver 28 * @run main RetryPost 29 * @run main/othervm -Dsun.net.http.retryPost=false RetryPost noRetry 30 * @summary HttpURLConnection automatically retries non-idempotent method POST 31 */ 32 33 import com.sun.net.httpserver.HttpContext; 34 import com.sun.net.httpserver.HttpExchange; 35 import com.sun.net.httpserver.HttpHandler; 36 import java.io.IOException; 37 import java.io.OutputStream; 38 import java.net.HttpURLConnection; 39 import java.net.InetSocketAddress; 40 import java.net.Proxy; 41 import java.net.SocketException; 42 import java.net.URL; 43 import java.util.concurrent.Executors; 44 import java.util.concurrent.ExecutorService; 45 46 public class RetryPost 47 { 48 static boolean shouldRetry = true; 49 50 com.sun.net.httpserver.HttpServer httpServer; 51 MyHandler httpHandler; 52 ExecutorService executorService; 53 main(String[] args)54 public static void main(String[] args) { 55 if (args.length == 1 && args[0].equals("noRetry")) 56 shouldRetry = false; 57 58 new RetryPost(); 59 } 60 RetryPost()61 public RetryPost() { 62 try { 63 startHttpServer(shouldRetry); 64 doClient(); 65 } catch (IOException ioe) { 66 System.err.println(ioe); 67 } 68 } 69 doClient()70 void doClient() { 71 try { 72 InetSocketAddress address = httpServer.getAddress(); 73 URL url = new URL("http://localhost:" + address.getPort() + "/test/"); 74 HttpURLConnection uc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY); 75 uc.setDoOutput(true); 76 uc.setRequestMethod("POST"); 77 uc.getResponseCode(); 78 79 // if we reach here then we have failed 80 throw new RuntimeException("Failed: POST request being retried"); 81 82 } catch (SocketException se) { 83 // this is what we expect to happen and is OK. 84 if (shouldRetry && httpHandler.getCallCount() != 2) 85 throw new RuntimeException("Failed: Handler should have been called twice. " + 86 "It was called "+ httpHandler.getCallCount() + " times"); 87 else if (!shouldRetry && httpHandler.getCallCount() != 1) 88 throw new RuntimeException("Failed: Handler should have only been called once" + 89 "It was called "+ httpHandler.getCallCount() + " times"); 90 } catch (IOException e) { 91 e.printStackTrace(); 92 } finally { 93 httpServer.stop(1); 94 executorService.shutdown(); 95 } 96 } 97 98 /** 99 * Http Server 100 */ startHttpServer(boolean shouldRetry)101 public void startHttpServer(boolean shouldRetry) throws IOException { 102 httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0); 103 httpHandler = new MyHandler(shouldRetry); 104 105 HttpContext ctx = httpServer.createContext("/test/", httpHandler); 106 107 executorService = Executors.newCachedThreadPool(); 108 httpServer.setExecutor(executorService); 109 httpServer.start(); 110 } 111 112 class MyHandler implements HttpHandler { 113 int callCount = 0; 114 boolean shouldRetry; 115 MyHandler(boolean shouldRetry)116 public MyHandler(boolean shouldRetry) { 117 this.shouldRetry = shouldRetry; 118 } 119 handle(HttpExchange t)120 public void handle(HttpExchange t) throws IOException { 121 callCount++; 122 123 if (callCount > 1 && !shouldRetry) { 124 // if this bug has been fixed then this method will not be called twice 125 // when -Dhttp.retryPost=false 126 t.sendResponseHeaders(400, -1); // indicate failure by returning 400 127 } else { 128 // simply close out the stream without sending any data. 129 OutputStream os = t.getResponseBody(); 130 os.close(); 131 } 132 } 133 getCallCount()134 public int getCallCount() { 135 return callCount; 136 } 137 } 138 139 } 140