jdk/test/java/net/URLConnection/ResendPostBody.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 6115 7c523cf2bc8a
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 6115
diff changeset
     2
 * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4361492
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary HTTPUrlConnection does not receive binary data correctly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @run main/timeout=20 ResendPostBody
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * This test does the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * 1. client opens HTTP connection to server
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * 2. client sends POST with a body containing "ZZZ"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * 3. server waits for POST and closes connection without replying
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * 4. client should re-open the connection and re-send the POST
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * 5. <bug>The client forgets to re-send the body with the POST
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *    The server hangs waiting for the body</bug>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *    <bugfixed>The client sends the body. The server reads it and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *     test terminates normally </bugfixed>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
public class ResendPostBody {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    static class Server extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        InputStream     in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        OutputStream out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        Socket  sock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        StringBuffer response;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        ServerSocket server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        Server (ServerSocket s) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            server = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        void waitFor (String s) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            byte[] w = s.getBytes ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            for(int c=0; c<w.length; c++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                byte expected = w[c];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                int b = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                if (b == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                    acceptConn ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                if ((byte)b != expected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                    c = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        boolean done = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        public synchronized boolean finished () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            return done;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        public synchronized void setFinished (boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            done = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        void acceptConn () throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            sock = server.accept ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            in = sock.getInputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            out = sock.getOutputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        public void run () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                response = new StringBuffer (1024);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                acceptConn ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                waitFor ("POST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                waitFor ("ZZZ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                Thread.sleep (500);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                sock.close ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                acceptConn ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                waitFor ("POST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                waitFor ("ZZZ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                response.append ("HTTP/1.1 200 OK\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                response.append ("Server: Microsoft-IIS/5.0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                response.append ("Date: Wed, 26 Jul 2000 14:17:04 GMT\r\n\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                out.write (response.toString().getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                while (!finished()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    Thread.sleep (1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                }
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   112
                out.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                System.err.println ("Server Exception: " + e);
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   115
            } finally {
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   116
                try { server.close(); } catch (IOException unused) {}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    ServerSocket ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    Server server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            if (args.length == 1 && args[0].equals ("-i")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                System.out.println ("Press return when ready");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                System.in.read ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                System.out.println ("Done");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            ResendPostBody t = new ResendPostBody ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            t. execute ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        } catch (IOException  e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            System.out.println ("IOException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public void execute () throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   140
        byte b[] = "X=ABCDEFGHZZZ".getBytes();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        ss = new ServerSocket (0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        server = new Server (ss);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        server.start ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        /* Get the URL */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        String s = "http://localhost:"+ss.getLocalPort()+"/test";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        URL url = new URL(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        HttpURLConnection conURL =  (HttpURLConnection)url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        conURL.setDoOutput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        conURL.setDoInput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        conURL.setAllowUserInteraction(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        conURL.setUseCaches(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        conURL.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        conURL.setRequestProperty("Content-Length", ""+b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        conURL.setRequestProperty("Connection", "Close");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        /* POST some data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        DataOutputStream OutStream = new DataOutputStream(conURL.getOutputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                          OutStream.write(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        OutStream.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        OutStream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        /* Read the response */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        int resp = conURL.getResponseCode ();
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   169
        server.setFinished (true);
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   170
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        if (resp != 200)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            throw new RuntimeException ("Response code was not 200: " + resp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
}