jdk/test/java/net/URLConnection/ResendPostBody.java
changeset 2 90ce3da70b43
child 5506 202f599c92aa
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright 2001-2002 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /**
       
    25  * @test
       
    26  * @bug 4361492
       
    27  * @summary HTTPUrlConnection does not receive binary data correctly
       
    28  * @run main/timeout=20 ResendPostBody
       
    29  */
       
    30 
       
    31 import java.io.*;
       
    32 import java.net.*;
       
    33 
       
    34 /*
       
    35  * This test does the following:
       
    36  * 1. client opens HTTP connection to server
       
    37  * 2. client sends POST with a body containing "ZZZ"
       
    38  * 3. server waits for POST and closes connection without replying
       
    39  * 4. client should re-open the connection and re-send the POST
       
    40  * 5. <bug>The client forgets to re-send the body with the POST
       
    41  *    The server hangs waiting for the body</bug>
       
    42  *
       
    43  *    <bugfixed>The client sends the body. The server reads it and the
       
    44  *     test terminates normally </bugfixed>
       
    45  */
       
    46 
       
    47 public class ResendPostBody {
       
    48 
       
    49     static class Server extends Thread {
       
    50 
       
    51         InputStream     in;
       
    52         OutputStream out;
       
    53         Socket  sock;
       
    54         StringBuffer response;
       
    55         ServerSocket server;
       
    56 
       
    57         Server (ServerSocket s) throws IOException
       
    58         {
       
    59             server = s;
       
    60         }
       
    61 
       
    62         void waitFor (String s) throws IOException
       
    63         {
       
    64             byte[] w = s.getBytes ();
       
    65             for(int c=0; c<w.length; c++ ) {
       
    66                 byte expected = w[c];
       
    67                 int b = in.read();
       
    68                 if (b == -1) {
       
    69                     acceptConn ();
       
    70                 }
       
    71                 if ((byte)b != expected) {
       
    72                     c = 0;
       
    73                 }
       
    74             }
       
    75         }
       
    76 
       
    77         boolean done = false;
       
    78 
       
    79         public synchronized boolean finished () {
       
    80             return done;
       
    81         }
       
    82 
       
    83         public synchronized void setFinished (boolean b) {
       
    84             done = b;
       
    85         }
       
    86 
       
    87         void acceptConn () throws IOException
       
    88         {
       
    89             sock = server.accept ();
       
    90             in = sock.getInputStream ();
       
    91             out = sock.getOutputStream ();
       
    92         }
       
    93 
       
    94         public void run () {
       
    95             try {
       
    96                 response = new StringBuffer (1024);
       
    97                 acceptConn ();
       
    98                 waitFor ("POST");
       
    99                 waitFor ("ZZZ");
       
   100                 Thread.sleep (500);
       
   101                 sock.close ();
       
   102                 acceptConn ();
       
   103                 waitFor ("POST");
       
   104                 waitFor ("ZZZ");
       
   105                 response.append ("HTTP/1.1 200 OK\r\n");
       
   106                 response.append ("Server: Microsoft-IIS/5.0");
       
   107                 response.append ("Date: Wed, 26 Jul 2000 14:17:04 GMT\r\n\r\n");
       
   108                 out.write (response.toString().getBytes());
       
   109                 while (!finished()) {
       
   110                     Thread.sleep (1000);
       
   111                 }
       
   112             } catch (Exception e) {
       
   113                 System.err.println ("Server Exception: " + e);
       
   114             }
       
   115         }
       
   116     }
       
   117 
       
   118     ServerSocket ss;
       
   119     Server server;
       
   120 
       
   121     public static void main(String[] args) throws Exception {
       
   122         try {
       
   123             if (args.length == 1 && args[0].equals ("-i")) {
       
   124                 System.out.println ("Press return when ready");
       
   125                 System.in.read ();
       
   126                 System.out.println ("Done");
       
   127             }
       
   128             ResendPostBody t = new ResendPostBody ();
       
   129             t. execute ();
       
   130         } catch (IOException  e) {
       
   131             System.out.println ("IOException");
       
   132         }
       
   133     }
       
   134 
       
   135     public void execute () throws Exception {
       
   136 
       
   137      byte b[] = "X=ABCDEFGHZZZ".getBytes();
       
   138 
       
   139         ss = new ServerSocket (0);
       
   140         server = new Server (ss);
       
   141         server.start ();
       
   142         /* Get the URL */
       
   143 
       
   144         String s = "http://localhost:"+ss.getLocalPort()+"/test";
       
   145         URL url = new URL(s);
       
   146         HttpURLConnection conURL =  (HttpURLConnection)url.openConnection();
       
   147 
       
   148         conURL.setDoOutput(true);
       
   149         conURL.setDoInput(true);
       
   150         conURL.setAllowUserInteraction(false);
       
   151         conURL.setUseCaches(false);
       
   152         conURL.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
       
   153         conURL.setRequestProperty("Content-Length", ""+b.length);
       
   154         conURL.setRequestProperty("Connection", "Close");
       
   155 
       
   156         /* POST some data */
       
   157 
       
   158         DataOutputStream OutStream = new DataOutputStream(conURL.getOutputStream());
       
   159                           OutStream.write(b, 0, b.length);
       
   160         OutStream.flush();
       
   161         OutStream.close();
       
   162 
       
   163         /* Read the response */
       
   164 
       
   165         int resp = conURL.getResponseCode ();
       
   166         if (resp != 200)
       
   167             throw new RuntimeException ("Response code was not 200: " + resp);
       
   168       server.setFinished (true);
       
   169   }
       
   170 }