jdk/test/java/net/URLConnection/RedirectLimit.java
author mikael
Fri, 04 Dec 2015 15:08:49 -0800
changeset 35090 1f5b6aa795d0
parent 10699 c6b5b935cce7
permissions -rw-r--r--
8144748: Move assembler/macroAssembler inline function definitions to corresponding inline.hpp files Reviewed-by: kvn, coleenp
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
8547
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
     2
 * Copyright (c) 2001, 2011, 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
10699
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    26
 * @bug 4458085 7095949
2
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 {
8547
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
    39
    static final int TIMEOUT = 10 * 1000;
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
    40
    static final int NUM_REDIRECTS = 9;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
8547
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
    42
    static final String reply1 = "HTTP/1.1 307 Temporary Redirect\r\n" +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        "Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        "Server: Apache/1.3.14 (Unix)\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        "Location: http://localhost:";
8547
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
    46
    static final String reply2 = ".html\r\n" +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        "Connection: close\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        "Content-Type: text/html; charset=iso-8859-1\r\n\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        "<html>Hello</html>";
8547
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
    50
    static final String reply3 = "HTTP/1.1 200 Ok\r\n" +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        "Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        "Server: Apache/1.3.14 (Unix)\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        "Connection: close\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        "Content-Type: text/html; charset=iso-8859-1\r\n\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        "World";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
8547
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
    57
    final ServerSocket ss;
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
    58
    final int port;
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
    59
10699
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    60
    RedirLimitServer(ServerSocket ss) throws IOException {
8547
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
    61
        this.ss = ss;
10699
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    62
        port = this.ss.getLocalPort();
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    63
        this.ss.setSoTimeout(TIMEOUT);
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    64
    }
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    65
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    66
    static final byte[] requestEnd = new byte[] {'\r', '\n', '\r', '\n' };
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    67
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    68
    // Read until the end of a HTTP request
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    69
    void readOneRequest(InputStream is) throws IOException {
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    70
        int requestEndCount = 0, r;
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    71
        while ((r = is.read()) != -1) {
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    72
            if (r == requestEnd[requestEndCount]) {
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    73
                requestEndCount++;
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    74
                if (requestEndCount == 4) {
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    75
                    break;
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    76
                }
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    77
            } else {
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    78
                requestEndCount = 0;
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    79
            }
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    80
        }
8547
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
    81
    }
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
    82
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
    83
    public void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        try {
8547
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
    85
            for (int i=0; i<NUM_REDIRECTS; i++) {
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
    86
                try (Socket s = ss.accept()) {
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
    87
                    s.setSoTimeout(TIMEOUT);
10699
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    88
                    readOneRequest(s.getInputStream());
8547
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
    89
                    String reply = reply1 + port + "/redirect" + i + reply2;
10699
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    90
                    s.getOutputStream().write(reply.getBytes());
8547
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
    91
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            }
8547
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
    93
            try (Socket s = ss.accept()) {
10699
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    94
                s.setSoTimeout(TIMEOUT);
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    95
                readOneRequest(s.getInputStream());
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    96
                s.getOutputStream().write(reply3.getBytes());
8547
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
    97
            }
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
    98
        } catch (Exception e) {
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
    99
            e.printStackTrace();
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   100
        } finally {
8547
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
   101
            try { ss.close(); } catch (IOException unused) {}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
public class RedirectLimit {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public static void main(String[] args) throws Exception {
8547
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
   108
        ServerSocket ss = new ServerSocket (0);
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
   109
        int port = ss.getLocalPort();
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
   110
        RedirLimitServer server = new RedirLimitServer(ss);
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
   111
        server.start();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
10699
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
   113
        URL url = new URL("http://localhost:" + port);
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
   114
        URLConnection conURL =  url.openConnection();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
10699
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
   116
        conURL.setDoInput(true);
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
   117
        conURL.setAllowUserInteraction(false);
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
   118
        conURL.setUseCaches(false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
10699
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
   120
        try (InputStream in = conURL.getInputStream()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            if ((in.read() != (int)'W') || (in.read()!=(int)'o')) {
8547
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
   122
                throw new RuntimeException("Unexpected string read");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
}