Redirect307Test.java revision 6073:cea72c2bf071
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 4380568 7095949
27 * @summary  HttpURLConnection does not support 307 redirects
28 */
29import java.io.*;
30import java.net.*;
31
32class RedirServer extends Thread {
33
34    static final int TIMEOUT = 10 * 1000;
35
36    ServerSocket ss;
37    int port;
38
39    String reply1Part1 = "HTTP/1.1 307 Temporary Redirect\r\n" +
40        "Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +
41        "Server: Apache/1.3.14 (Unix)\r\n" +
42        "Location: http://localhost:";
43    String reply1Part2 = "/redirected.html\r\n" +
44        "Connection: close\r\n" +
45        "Content-Type: text/html; charset=iso-8859-1\r\n\r\n" +
46        "<html>Hello</html>";
47
48    RedirServer (ServerSocket ss) throws IOException {
49        this.ss = ss;
50        this.ss.setSoTimeout(TIMEOUT);
51        port = this.ss.getLocalPort();
52    }
53
54    String reply2 = "HTTP/1.1 200 Ok\r\n" +
55        "Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +
56        "Server: Apache/1.3.14 (Unix)\r\n" +
57        "Connection: close\r\n" +
58        "Content-Type: text/html; charset=iso-8859-1\r\n\r\n" +
59        "World";
60
61    static final byte[] requestEnd = new byte[] {'\r', '\n', '\r', '\n' };
62
63    // Read until the end of a HTTP request
64    void readOneRequest(InputStream is) throws IOException {
65        int requestEndCount = 0, r;
66        while ((r = is.read()) != -1) {
67            if (r == requestEnd[requestEndCount]) {
68                requestEndCount++;
69                if (requestEndCount == 4) {
70                    break;
71                }
72            } else {
73                requestEndCount = 0;
74            }
75        }
76    }
77
78    public void run () {
79        try {
80            try (Socket s = ss.accept()) {
81                s.setSoTimeout(TIMEOUT);
82                readOneRequest(s.getInputStream());
83                String reply = reply1Part1 + port + reply1Part2;
84                s.getOutputStream().write(reply.getBytes());
85            }
86
87            /* wait for redirected connection */
88            try (Socket s = ss.accept()) {
89                s.setSoTimeout(TIMEOUT);
90                readOneRequest(s.getInputStream());
91                s.getOutputStream().write(reply2.getBytes());
92            }
93        } catch (Exception e) {
94            e.printStackTrace();
95        } finally {
96            try { ss.close(); } catch (IOException unused) {}
97        }
98    }
99};
100
101public class Redirect307Test {
102    public static void main(String[] args) throws Exception {
103        ServerSocket sock = new ServerSocket(0);
104        int port = sock.getLocalPort();
105        RedirServer server = new RedirServer(sock);
106        server.start();
107
108        URL url = new URL("http://localhost:" + port);
109        URLConnection conURL =  url.openConnection();
110        conURL.setDoInput(true);
111        conURL.setAllowUserInteraction(false);
112        conURL.setUseCaches(false);
113
114        try (InputStream in = conURL.getInputStream()) {
115            if ((in.read() != (int)'W') || (in.read()!=(int)'o')) {
116                throw new RuntimeException ("Unexpected string read");
117            }
118        }
119    }
120}
121