J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2001 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /** |
| 25 | * @test |
| 26 | * @bug 4458085 |
| 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 | |
| 35 | import java.io.*; |
| 36 | import java.net.*; |
| 37 | |
| 38 | class RedirLimitServer extends Thread { |
| 39 | |
| 40 | ServerSocket s; |
| 41 | Socket s1; |
| 42 | InputStream is; |
| 43 | OutputStream os; |
| 44 | int port; |
| 45 | int nredirects = 9; |
| 46 | |
| 47 | String reply1 = "HTTP/1.1 307 Temporary Redirect\r\n" + |
| 48 | "Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" + |
| 49 | "Server: Apache/1.3.14 (Unix)\r\n" + |
| 50 | "Location: http://localhost:"; |
| 51 | String reply2 = ".html\r\n" + |
| 52 | "Connection: close\r\n" + |
| 53 | "Content-Type: text/html; charset=iso-8859-1\r\n\r\n" + |
| 54 | "<html>Hello</html>"; |
| 55 | |
| 56 | RedirLimitServer (ServerSocket y) { |
| 57 | s = y; |
| 58 | port = s.getLocalPort(); |
| 59 | } |
| 60 | |
| 61 | String reply3 = "HTTP/1.1 200 Ok\r\n" + |
| 62 | "Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" + |
| 63 | "Server: Apache/1.3.14 (Unix)\r\n" + |
| 64 | "Connection: close\r\n" + |
| 65 | "Content-Type: text/html; charset=iso-8859-1\r\n\r\n" + |
| 66 | "World"; |
| 67 | |
| 68 | public void run () { |
| 69 | try { |
| 70 | s.setSoTimeout (2000); |
| 71 | for (int i=0; i<nredirects; i++) { |
| 72 | s1 = s.accept (); |
| 73 | s1.setSoTimeout (2000); |
| 74 | is = s1.getInputStream (); |
| 75 | os = s1.getOutputStream (); |
| 76 | is.read (); |
| 77 | String reply = reply1 + port + "/redirect" + i + reply2; |
| 78 | os.write (reply.getBytes()); |
| 79 | } |
| 80 | s1 = s.accept (); |
| 81 | is = s1.getInputStream (); |
| 82 | os = s1.getOutputStream (); |
| 83 | is.read (); |
| 84 | os.write (reply3.getBytes()); |
| 85 | } |
| 86 | catch (Exception e) { |
| 87 | /* Just need thread to terminate */ |
| 88 | } |
| 89 | } |
| 90 | }; |
| 91 | |
| 92 | |
| 93 | public class RedirectLimit { |
| 94 | |
| 95 | public static final int DELAY = 10; |
| 96 | |
| 97 | public static void main(String[] args) throws Exception { |
| 98 | int nLoops = 1; |
| 99 | int nSize = 10; |
| 100 | int port, n =0; |
| 101 | byte b[] = new byte[nSize]; |
| 102 | RedirLimitServer server; |
| 103 | ServerSocket sock; |
| 104 | |
| 105 | try { |
| 106 | sock = new ServerSocket (0); |
| 107 | port = sock.getLocalPort (); |
| 108 | } |
| 109 | catch (Exception e) { |
| 110 | System.out.println ("Exception: " + e); |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | server = new RedirLimitServer(sock); |
| 115 | server.start (); |
| 116 | |
| 117 | try { |
| 118 | |
| 119 | String s = "http://localhost:" + port; |
| 120 | URL url = new URL(s); |
| 121 | URLConnection conURL = url.openConnection(); |
| 122 | |
| 123 | conURL.setDoInput(true); |
| 124 | conURL.setAllowUserInteraction(false); |
| 125 | conURL.setUseCaches(false); |
| 126 | |
| 127 | InputStream in = conURL.getInputStream(); |
| 128 | if ((in.read() != (int)'W') || (in.read()!=(int)'o')) { |
| 129 | throw new RuntimeException ("Unexpected string read"); |
| 130 | } |
| 131 | } |
| 132 | catch(IOException e) { |
| 133 | throw new RuntimeException ("Exception caught " + e); |
| 134 | } |
| 135 | } |
| 136 | } |