1/*
2 * Copyright (c) 2001, 2011, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/**
25 * @test
26 * @bug 4458085 7095949
27 * @summary  Redirects Limited to 5
28 */
29
30/*
31 * Simulate a server that redirects ( to a different URL) 9 times
32 * and see if the client correctly follows the trail
33 */
34
35import java.io.*;
36import java.net.*;
37
38class RedirLimitServer extends Thread {
39    static final int TIMEOUT = 10 * 1000;
40    static final int NUM_REDIRECTS = 9;
41
42    static final String reply1 = "HTTP/1.1 307 Temporary Redirect\r\n" +
43        "Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +
44        "Server: Apache/1.3.14 (Unix)\r\n" +
45        "Location: http://localhost:";
46    static final String reply2 = ".html\r\n" +
47        "Connection: close\r\n" +
48        "Content-Type: text/html; charset=iso-8859-1\r\n\r\n" +
49        "<html>Hello</html>";
50    static final String reply3 = "HTTP/1.1 200 Ok\r\n" +
51        "Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +
52        "Server: Apache/1.3.14 (Unix)\r\n" +
53        "Connection: close\r\n" +
54        "Content-Type: text/html; charset=iso-8859-1\r\n\r\n" +
55        "World";
56
57    final ServerSocket ss;
58    final int port;
59
60    RedirLimitServer(ServerSocket ss) throws IOException {
61        this.ss = ss;
62        port = this.ss.getLocalPort();
63        this.ss.setSoTimeout(TIMEOUT);
64    }
65
66    static final byte[] requestEnd = new byte[] {'\r', '\n', '\r', '\n' };
67
68    // Read until the end of a HTTP request
69    void readOneRequest(InputStream is) throws IOException {
70        int requestEndCount = 0, r;
71        while ((r = is.read()) != -1) {
72            if (r == requestEnd[requestEndCount]) {
73                requestEndCount++;
74                if (requestEndCount == 4) {
75                    break;
76                }
77            } else {
78                requestEndCount = 0;
79            }
80        }
81    }
82
83    public void run() {
84        try {
85            for (int i=0; i<NUM_REDIRECTS; i++) {
86                try (Socket s = ss.accept()) {
87                    s.setSoTimeout(TIMEOUT);
88                    readOneRequest(s.getInputStream());
89                    String reply = reply1 + port + "/redirect" + i + reply2;
90                    s.getOutputStream().write(reply.getBytes());
91                }
92            }
93            try (Socket s = ss.accept()) {
94                s.setSoTimeout(TIMEOUT);
95                readOneRequest(s.getInputStream());
96                s.getOutputStream().write(reply3.getBytes());
97            }
98        } catch (Exception e) {
99            e.printStackTrace();
100        } finally {
101            try { ss.close(); } catch (IOException unused) {}
102        }
103    }
104};
105
106public class RedirectLimit {
107    public static void main(String[] args) throws Exception {
108        ServerSocket ss = new ServerSocket (0);
109        int port = ss.getLocalPort();
110        RedirLimitServer server = new RedirLimitServer(ss);
111        server.start();
112
113        URL url = new URL("http://localhost:" + port);
114        URLConnection conURL =  url.openConnection();
115
116        conURL.setDoInput(true);
117        conURL.setAllowUserInteraction(false);
118        conURL.setUseCaches(false);
119
120        try (InputStream in = conURL.getInputStream()) {
121            if ((in.read() != (int)'W') || (in.read()!=(int)'o')) {
122                throw new RuntimeException("Unexpected string read");
123            }
124        }
125    }
126}
127