jdk/test/java/net/URLConnection/RedirectLimit.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 6115 7c523cf2bc8a
child 8547 5a1f87621165
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 4458085
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary  Redirects Limited to 5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * Simulate a server that redirects ( to a different URL) 9 times
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * and see if the client correctly follows the trail
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
class RedirLimitServer extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    ServerSocket s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    Socket   s1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    InputStream  is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    OutputStream os;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    int nredirects = 9;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    String reply1 = "HTTP/1.1 307 Temporary Redirect\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        "Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        "Server: Apache/1.3.14 (Unix)\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        "Location: http://localhost:";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    String reply2 = ".html\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        "Connection: close\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        "Content-Type: text/html; charset=iso-8859-1\r\n\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        "<html>Hello</html>";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    RedirLimitServer (ServerSocket y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        s = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        port = s.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    String reply3 = "HTTP/1.1 200 Ok\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        "Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        "Server: Apache/1.3.14 (Unix)\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        "Connection: close\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        "Content-Type: text/html; charset=iso-8859-1\r\n\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        "World";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public void run () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            s.setSoTimeout (2000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            for (int i=0; i<nredirects; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                s1 = s.accept ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                s1.setSoTimeout (2000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                is = s1.getInputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                os = s1.getOutputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                is.read ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                String reply = reply1 + port + "/redirect" + i + reply2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                os.write (reply.getBytes());
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    79
                os.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            s1 = s.accept ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            is = s1.getInputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            os = s1.getOutputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            is.read ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            os.write (reply3.getBytes());
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    86
            os.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            /* Just need thread to terminate */
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    90
        } finally {
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    91
            try { s.close(); } catch (IOException unused) {}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
public class RedirectLimit {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public static final int DELAY = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        int nLoops = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        int nSize = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        int port, n =0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        byte b[] = new byte[nSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        RedirLimitServer server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        ServerSocket sock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            sock = new ServerSocket (0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            port = sock.getLocalPort ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            System.out.println ("Exception: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        server = new RedirLimitServer(sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        server.start ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        try  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            String s = "http://localhost:" + port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            URL url = new URL(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            URLConnection conURL =  url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            conURL.setDoInput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            conURL.setAllowUserInteraction(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            conURL.setUseCaches(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            InputStream in = conURL.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            if ((in.read() != (int)'W') || (in.read()!=(int)'o')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                throw new RuntimeException ("Unexpected string read");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        catch(IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            throw new RuntimeException ("Exception caught " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
}