1 /* 2 * Copyright (c) 2010, 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 6672144 8050983 27 * @summary Do not retry failed request with a streaming body. 28 */ 29 30 import java.net.HttpURLConnection; 31 import java.net.ServerSocket; 32 import java.net.URL; 33 import java.io.IOException; 34 import java.io.InputStream; 35 import java.io.OutputStream; 36 import static java.lang.System.out; 37 38 public class StreamingRetry implements Runnable { 39 static final int ACCEPT_TIMEOUT = 20 * 1000; // 20 seconds 40 volatile ServerSocket ss; 41 main(String[] args)42 public static void main(String[] args) throws Exception { 43 (new StreamingRetry()).instanceMain(); 44 } 45 instanceMain()46 void instanceMain() throws Exception { 47 out.println("Test with default method"); 48 test(null); 49 out.println("Test with POST method"); 50 test("POST"); 51 out.println("Test with PUT method"); 52 test("PUT"); 53 54 if (failed > 0) throw new RuntimeException("Some tests failed"); 55 } 56 test(String method)57 void test(String method) throws Exception { 58 ss = new ServerSocket(0); 59 ss.setSoTimeout(ACCEPT_TIMEOUT); 60 int port = ss.getLocalPort(); 61 62 Thread otherThread = new Thread(this); 63 otherThread.start(); 64 65 try { 66 URL url = new URL("http://localhost:" + port + "/"); 67 HttpURLConnection uc = (HttpURLConnection) url.openConnection(); 68 uc.setDoOutput(true); 69 if (method != null) 70 uc.setRequestMethod(method); 71 uc.setChunkedStreamingMode(4096); 72 OutputStream os = uc.getOutputStream(); 73 os.write("Hello there".getBytes()); 74 75 InputStream is = uc.getInputStream(); 76 is.close(); 77 } catch (IOException expected) { 78 //expected.printStackTrace(); 79 } finally { 80 ss.close(); 81 otherThread.join(); 82 } 83 } 84 85 // Server run()86 public void run() { 87 try { 88 (ss.accept()).close(); 89 (ss.accept()).close(); 90 ss.close(); 91 fail("The server shouldn't accept a second connection"); 92 } catch (IOException e) { 93 //OK, the client will close the server socket if successful 94 } 95 } 96 97 volatile int failed = 0; fail()98 void fail() {failed++; Thread.dumpStack();} fail(String msg)99 void fail(String msg) {System.err.println(msg); fail();} 100 } 101