0N/A/*
3261N/A * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
0N/A * @bug 4361492
0N/A * @summary HTTPUrlConnection does not receive binary data correctly
0N/A * @run main/timeout=20 ResendPostBody
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.net.*;
0N/A
0N/A/*
0N/A * This test does the following:
0N/A * 1. client opens HTTP connection to server
0N/A * 2. client sends POST with a body containing "ZZZ"
0N/A * 3. server waits for POST and closes connection without replying
0N/A * 4. client should re-open the connection and re-send the POST
0N/A * 5. <bug>The client forgets to re-send the body with the POST
0N/A * The server hangs waiting for the body</bug>
0N/A *
0N/A * <bugfixed>The client sends the body. The server reads it and the
0N/A * test terminates normally </bugfixed>
0N/A */
0N/A
0N/Apublic class ResendPostBody {
0N/A
0N/A static class Server extends Thread {
0N/A
0N/A InputStream in;
0N/A OutputStream out;
0N/A Socket sock;
0N/A StringBuffer response;
0N/A ServerSocket server;
0N/A
0N/A Server (ServerSocket s) throws IOException
0N/A {
0N/A server = s;
0N/A }
0N/A
0N/A void waitFor (String s) throws IOException
0N/A {
0N/A byte[] w = s.getBytes ();
0N/A for(int c=0; c<w.length; c++ ) {
0N/A byte expected = w[c];
0N/A int b = in.read();
0N/A if (b == -1) {
0N/A acceptConn ();
0N/A }
0N/A if ((byte)b != expected) {
0N/A c = 0;
0N/A }
0N/A }
0N/A }
0N/A
0N/A boolean done = false;
0N/A
0N/A public synchronized boolean finished () {
0N/A return done;
0N/A }
0N/A
0N/A public synchronized void setFinished (boolean b) {
0N/A done = b;
0N/A }
0N/A
0N/A void acceptConn () throws IOException
0N/A {
0N/A sock = server.accept ();
0N/A in = sock.getInputStream ();
0N/A out = sock.getOutputStream ();
0N/A }
0N/A
0N/A public void run () {
0N/A try {
0N/A response = new StringBuffer (1024);
0N/A acceptConn ();
0N/A waitFor ("POST");
0N/A waitFor ("ZZZ");
0N/A Thread.sleep (500);
0N/A sock.close ();
0N/A acceptConn ();
0N/A waitFor ("POST");
0N/A waitFor ("ZZZ");
0N/A response.append ("HTTP/1.1 200 OK\r\n");
0N/A response.append ("Server: Microsoft-IIS/5.0");
0N/A response.append ("Date: Wed, 26 Jul 2000 14:17:04 GMT\r\n\r\n");
0N/A out.write (response.toString().getBytes());
0N/A while (!finished()) {
0N/A Thread.sleep (1000);
0N/A }
2612N/A out.close();
0N/A } catch (Exception e) {
0N/A System.err.println ("Server Exception: " + e);
2612N/A } finally {
2612N/A try { server.close(); } catch (IOException unused) {}
0N/A }
0N/A }
0N/A }
0N/A
0N/A ServerSocket ss;
0N/A Server server;
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A try {
0N/A if (args.length == 1 && args[0].equals ("-i")) {
0N/A System.out.println ("Press return when ready");
0N/A System.in.read ();
0N/A System.out.println ("Done");
0N/A }
0N/A ResendPostBody t = new ResendPostBody ();
0N/A t. execute ();
0N/A } catch (IOException e) {
0N/A System.out.println ("IOException");
0N/A }
0N/A }
0N/A
0N/A public void execute () throws Exception {
0N/A
2612N/A byte b[] = "X=ABCDEFGHZZZ".getBytes();
0N/A
0N/A ss = new ServerSocket (0);
0N/A server = new Server (ss);
0N/A server.start ();
0N/A /* Get the URL */
0N/A
0N/A String s = "http://localhost:"+ss.getLocalPort()+"/test";
0N/A URL url = new URL(s);
0N/A HttpURLConnection conURL = (HttpURLConnection)url.openConnection();
0N/A
0N/A conURL.setDoOutput(true);
0N/A conURL.setDoInput(true);
0N/A conURL.setAllowUserInteraction(false);
0N/A conURL.setUseCaches(false);
0N/A conURL.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
0N/A conURL.setRequestProperty("Content-Length", ""+b.length);
0N/A conURL.setRequestProperty("Connection", "Close");
0N/A
0N/A /* POST some data */
0N/A
0N/A DataOutputStream OutStream = new DataOutputStream(conURL.getOutputStream());
0N/A OutStream.write(b, 0, b.length);
0N/A OutStream.flush();
0N/A OutStream.close();
0N/A
0N/A /* Read the response */
0N/A
0N/A int resp = conURL.getResponseCode ();
2612N/A server.setFinished (true);
2612N/A
0N/A if (resp != 200)
0N/A throw new RuntimeException ("Response code was not 200: " + resp);
0N/A }
0N/A}