jdk/test/java/net/URLConnection/RedirectLimit.java
changeset 8547 5a1f87621165
parent 7668 d4a77089c587
child 10699 c6b5b935cce7
equal deleted inserted replaced
8546:ded72ac070ec 8547:5a1f87621165
     1 /*
     1 /*
     2  * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
     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.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    34 
    34 
    35 import java.io.*;
    35 import java.io.*;
    36 import java.net.*;
    36 import java.net.*;
    37 
    37 
    38 class RedirLimitServer extends Thread {
    38 class RedirLimitServer extends Thread {
       
    39     static final int TIMEOUT = 10 * 1000;
       
    40     static final int NUM_REDIRECTS = 9;
    39 
    41 
    40     ServerSocket s;
    42     static final String reply1 = "HTTP/1.1 307 Temporary Redirect\r\n" +
    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" +
    43         "Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +
    49         "Server: Apache/1.3.14 (Unix)\r\n" +
    44         "Server: Apache/1.3.14 (Unix)\r\n" +
    50         "Location: http://localhost:";
    45         "Location: http://localhost:";
    51     String reply2 = ".html\r\n" +
    46     static final String reply2 = ".html\r\n" +
    52         "Connection: close\r\n" +
    47         "Connection: close\r\n" +
    53         "Content-Type: text/html; charset=iso-8859-1\r\n\r\n" +
    48         "Content-Type: text/html; charset=iso-8859-1\r\n\r\n" +
    54         "<html>Hello</html>";
    49         "<html>Hello</html>";
    55 
    50     static final String reply3 = "HTTP/1.1 200 Ok\r\n" +
    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" +
    51         "Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +
    63         "Server: Apache/1.3.14 (Unix)\r\n" +
    52         "Server: Apache/1.3.14 (Unix)\r\n" +
    64         "Connection: close\r\n" +
    53         "Connection: close\r\n" +
    65         "Content-Type: text/html; charset=iso-8859-1\r\n\r\n" +
    54         "Content-Type: text/html; charset=iso-8859-1\r\n\r\n" +
    66         "World";
    55         "World";
    67 
    56 
    68     public void run () {
    57     final ServerSocket ss;
       
    58     final int port;
       
    59 
       
    60     RedirLimitServer(ServerSocket ss) {
       
    61         this.ss = ss;
       
    62         port = ss.getLocalPort();
       
    63     }
       
    64 
       
    65     public void run() {
    69         try {
    66         try {
    70             s.setSoTimeout (2000);
    67             ss.setSoTimeout(TIMEOUT);
    71             for (int i=0; i<nredirects; i++) {
    68             for (int i=0; i<NUM_REDIRECTS; i++) {
    72                 s1 = s.accept ();
    69                 try (Socket s = ss.accept()) {
    73                 s1.setSoTimeout (2000);
    70                     s.setSoTimeout(TIMEOUT);
    74                 is = s1.getInputStream ();
    71                     InputStream is = s.getInputStream();
    75                 os = s1.getOutputStream ();
    72                     OutputStream os = s.getOutputStream();
    76                 is.read ();
    73                     is.read();
    77                 String reply = reply1 + port + "/redirect" + i + reply2;
    74                     String reply = reply1 + port + "/redirect" + i + reply2;
    78                 os.write (reply.getBytes());
    75                     os.write(reply.getBytes());
    79                 os.close();
    76                 }
    80             }
    77             }
    81             s1 = s.accept ();
    78             try (Socket s = ss.accept()) {
    82             is = s1.getInputStream ();
    79                 InputStream is = s.getInputStream();
    83             os = s1.getOutputStream ();
    80                 OutputStream os = s.getOutputStream();
    84             is.read ();
    81                 is.read();
    85             os.write (reply3.getBytes());
    82                 os.write(reply3.getBytes());
    86             os.close();
    83             }
    87         }
    84         } catch (Exception e) {
    88         catch (Exception e) {
    85             e.printStackTrace();
    89             /* Just need thread to terminate */
       
    90         } finally {
    86         } finally {
    91             try { s.close(); } catch (IOException unused) {}
    87             try { ss.close(); } catch (IOException unused) {}
    92         }
    88         }
    93     }
    89     }
    94 };
    90 };
    95 
    91 
       
    92 public class RedirectLimit {
       
    93     public static void main(String[] args) throws Exception {
       
    94         ServerSocket ss = new ServerSocket (0);
       
    95         int port = ss.getLocalPort();
       
    96         RedirLimitServer server = new RedirLimitServer(ss);
       
    97         server.start();
    96 
    98 
    97 public class RedirectLimit {
    99         InputStream in = null;
    98 
       
    99     public static final int DELAY = 10;
       
   100 
       
   101     public static void main(String[] args) throws Exception {
       
   102         int nLoops = 1;
       
   103         int nSize = 10;
       
   104         int port, n =0;
       
   105         byte b[] = new byte[nSize];
       
   106         RedirLimitServer server;
       
   107         ServerSocket sock;
       
   108 
       
   109         try {
   100         try {
   110             sock = new ServerSocket (0);
   101             URL url = new URL("http://localhost:" + port);
   111             port = sock.getLocalPort ();
       
   112         }
       
   113         catch (Exception e) {
       
   114             System.out.println ("Exception: " + e);
       
   115             return;
       
   116         }
       
   117 
       
   118         server = new RedirLimitServer(sock);
       
   119         server.start ();
       
   120 
       
   121         try  {
       
   122 
       
   123             String s = "http://localhost:" + port;
       
   124             URL url = new URL(s);
       
   125             URLConnection conURL =  url.openConnection();
   102             URLConnection conURL =  url.openConnection();
   126 
   103 
   127             conURL.setDoInput(true);
   104             conURL.setDoInput(true);
   128             conURL.setAllowUserInteraction(false);
   105             conURL.setAllowUserInteraction(false);
   129             conURL.setUseCaches(false);
   106             conURL.setUseCaches(false);
   130 
   107 
   131             InputStream in = conURL.getInputStream();
   108             in = conURL.getInputStream();
   132             if ((in.read() != (int)'W') || (in.read()!=(int)'o')) {
   109             if ((in.read() != (int)'W') || (in.read()!=(int)'o')) {
   133                 throw new RuntimeException ("Unexpected string read");
   110                 throw new RuntimeException("Unexpected string read");
   134             }
   111             }
   135         }
   112         } finally {
   136         catch(IOException e) {
   113             if ( in != null ) { in.close(); }
   137             throw new RuntimeException ("Exception caught " + e);
       
   138         }
   114         }
   139     }
   115     }
   140 }
   116 }