Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/URLConnection/ResendPostBody.java
38812 views
/*1* Copyright (c) 2001, 2010, 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 436149226* @summary HTTPUrlConnection does not receive binary data correctly27* @run main/timeout=20 ResendPostBody28*/2930import java.io.*;31import java.net.*;3233/*34* This test does the following:35* 1. client opens HTTP connection to server36* 2. client sends POST with a body containing "ZZZ"37* 3. server waits for POST and closes connection without replying38* 4. client should re-open the connection and re-send the POST39* 5. <bug>The client forgets to re-send the body with the POST40* The server hangs waiting for the body</bug>41*42* <bugfixed>The client sends the body. The server reads it and the43* test terminates normally </bugfixed>44*/4546public class ResendPostBody {4748static class Server extends Thread {4950InputStream in;51OutputStream out;52Socket sock;53StringBuffer response;54ServerSocket server;5556Server (ServerSocket s) throws IOException57{58server = s;59}6061void waitFor (String s) throws IOException62{63byte[] w = s.getBytes ();64for(int c=0; c<w.length; c++ ) {65byte expected = w[c];66int b = in.read();67if (b == -1) {68acceptConn ();69}70if ((byte)b != expected) {71c = 0;72}73}74}7576boolean done = false;7778public synchronized boolean finished () {79return done;80}8182public synchronized void setFinished (boolean b) {83done = b;84}8586void acceptConn () throws IOException87{88sock = server.accept ();89in = sock.getInputStream ();90out = sock.getOutputStream ();91}9293public void run () {94try {95response = new StringBuffer (1024);96acceptConn ();97waitFor ("POST");98waitFor ("ZZZ");99Thread.sleep (500);100sock.close ();101acceptConn ();102waitFor ("POST");103waitFor ("ZZZ");104response.append ("HTTP/1.1 200 OK\r\n");105response.append ("Server: Microsoft-IIS/5.0");106response.append ("Date: Wed, 26 Jul 2000 14:17:04 GMT\r\n\r\n");107out.write (response.toString().getBytes());108while (!finished()) {109Thread.sleep (1000);110}111out.close();112} catch (Exception e) {113System.err.println ("Server Exception: " + e);114} finally {115try { server.close(); } catch (IOException unused) {}116}117}118}119120ServerSocket ss;121Server server;122123public static void main(String[] args) throws Exception {124try {125if (args.length == 1 && args[0].equals ("-i")) {126System.out.println ("Press return when ready");127System.in.read ();128System.out.println ("Done");129}130ResendPostBody t = new ResendPostBody ();131t. execute ();132} catch (IOException e) {133System.out.println ("IOException");134}135}136137public void execute () throws Exception {138139byte b[] = "X=ABCDEFGHZZZ".getBytes();140141ss = new ServerSocket (0);142server = new Server (ss);143server.start ();144/* Get the URL */145146String s = "http://localhost:"+ss.getLocalPort()+"/test";147URL url = new URL(s);148HttpURLConnection conURL = (HttpURLConnection)url.openConnection();149150conURL.setDoOutput(true);151conURL.setDoInput(true);152conURL.setAllowUserInteraction(false);153conURL.setUseCaches(false);154conURL.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");155conURL.setRequestProperty("Content-Length", ""+b.length);156conURL.setRequestProperty("Connection", "Close");157158/* POST some data */159160DataOutputStream OutStream = new DataOutputStream(conURL.getOutputStream());161OutStream.write(b, 0, b.length);162OutStream.flush();163OutStream.close();164165/* Read the response */166167int resp = conURL.getResponseCode ();168server.setFinished (true);169170if (resp != 200)171throw new RuntimeException ("Response code was not 200: " + resp);172}173}174175176