Path: blob/master/test/jdk/java/net/URLConnection/ResendPostBody.java
41149 views
/*1* Copyright (c) 2001, 2019, 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* @library /test/lib28* @run main/timeout=20 ResendPostBody29*/3031import java.io.*;32import java.net.*;3334import jdk.test.lib.net.URIBuilder;3536/*37* This test does the following:38* 1. client opens HTTP connection to server39* 2. client sends POST with a body containing "ZZZ"40* 3. server waits for POST and closes connection without replying41* 4. client should re-open the connection and re-send the POST42* 5. <bug>The client forgets to re-send the body with the POST43* The server hangs waiting for the body</bug>44*45* <bugfixed>The client sends the body. The server reads it and the46* test terminates normally </bugfixed>47*/4849public class ResendPostBody {5051static class Server extends Thread {5253private InputStream in;54private OutputStream out;55private Socket sock;56private StringBuffer response;57private ServerSocket server;5859Server(ServerSocket s) throws IOException {60server = s;61}6263void waitFor(String s) throws IOException {64byte[] w = s.getBytes();65for (int c = 0; c < w.length; c++) {66byte expected = w[c];67int b = in.read();68if (b == -1) {69acceptConn();70}71if ((byte) b != expected) {72c = 0;73}74}75}7677private boolean done = false;7879public synchronized boolean finished() {80return done;81}8283public synchronized void setFinished(boolean b) throws IOException {84done = b;85this.closeConn();86server.close();87}8889void acceptConn() throws IOException {90sock = server.accept();91in = sock.getInputStream();92out = sock.getOutputStream();93}9495void closeConn() throws IOException {96in.close();97out.close();98sock.close();99}100101public void run() {102try {103response = new StringBuffer(1024);104acceptConn();105waitFor("POST");106waitFor("ZZZ");107Thread.sleep(500);108sock.close();109acceptConn();110waitFor("POST");111waitFor("ZZZ");112response.append("HTTP/1.1 200 OK\r\n");113response.append("Server: Microsoft-IIS/5.0");114response.append("Date: Wed, 26 Jul 2000 14:17:04 GMT\r\n\r\n");115out.write(response.toString().getBytes());116out.flush();117while (!finished()) {118Thread.sleep(1000);119}120} catch (Exception e) {121if (!done) {122System.err.println("Server Exception: " + e);123}124} finally {125try {126closeConn();127} catch (IOException ioe) {128if (!done) {129ioe.printStackTrace();130}131}132}133}134}135136private ServerSocket ss;137private Server server;138139public static void main(String[] args) throws Exception {140if (args.length == 1 && args[0].equals("-i")) {141System.out.println("Press return when ready");142System.in.read();143System.out.println("Done");144}145ResendPostBody t = new ResendPostBody();146t.execute();147}148149public void execute() throws Exception {150byte b[] = "X=ABCDEFGHZZZ".getBytes();151152ss = new ServerSocket(0, 0, InetAddress.getLoopbackAddress());153server = new Server(ss);154server.start();155156/* Get the URL */157URL url = URIBuilder.newBuilder()158.scheme("http")159.loopback()160.port(ss.getLocalPort())161.path("/test")162.toURL();163HttpURLConnection conURL = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);164165conURL.setDoOutput(true);166conURL.setDoInput(true);167conURL.setAllowUserInteraction(false);168conURL.setUseCaches(false);169conURL.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");170conURL.setRequestProperty("Content-Length", "" + b.length);171conURL.setRequestProperty("Connection", "Close");172173/* POST some data */174DataOutputStream OutStream = new DataOutputStream(conURL.getOutputStream());175OutStream.write(b, 0, b.length);176OutStream.flush();177OutStream.close();178179/* Read the response */180int resp = conURL.getResponseCode();181182server.setFinished(true); // Set finished and close ServerSocket183server.join(); // Join server thread184185if (resp != 200)186throw new RuntimeException("Response code was not 200: " + resp);187}188}189190191