blob: a2b96addd1bf6be23bf2a72e55620ecca33d5db2 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2002-2003 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 4726087
27 * @library ../../httptest/
28 * @build HttpCallback HttpServer ClosedChannelList HttpTransaction
29 * @run main RelativeRedirect
30 * @summary URLConnection cannot handle redirects
31 */
32
33import java.io.*;
34import java.net.*;
35
36public class RelativeRedirect implements HttpCallback {
37 static int count = 0;
38 static HttpServer server;
39
40 static class MyAuthenticator extends Authenticator {
41 public MyAuthenticator () {
42 super ();
43 }
44
45 public PasswordAuthentication getPasswordAuthentication ()
46 {
47 return (new PasswordAuthentication ("user", "Wrongpassword".toCharArray()));
48 }
49 }
50
51 void firstReply (HttpTransaction req) throws IOException {
52 req.addResponseHeader ("Connection", "close");
53 req.addResponseHeader ("Location", "/redirect/file.html");
54 req.sendResponse (302, "Moved Permamently");
55 req.orderlyClose();
56 }
57
58 void secondReply (HttpTransaction req) throws IOException {
59 if (req.getRequestURI().toString().equals("/redirect/file.html") &&
60 req.getRequestHeader("Host").equals("localhost:"+server.getLocalPort())) {
61 req.setResponseEntityBody ("Hello .");
62 req.sendResponse (200, "Ok");
63 } else {
64 req.setResponseEntityBody (req.getRequestURI().toString());
65 req.sendResponse (400, "Bad request");
66 }
67 req.orderlyClose();
68
69 }
70 public void request (HttpTransaction req) {
71 try {
72 switch (count) {
73 case 0:
74 // server redirect to /redirect/file.html
75 firstReply (req);
76 break;
77 case 1:
78 // client retry to /redirect/file.html on same server
79 secondReply (req);
80 break;
81 }
82 count ++;
83 } catch (IOException e) {
84 e.printStackTrace();
85 }
86 }
87
88 public static void main (String[] args) throws Exception {
89 MyAuthenticator auth = new MyAuthenticator ();
90 Authenticator.setDefault (auth);
91 try {
92 server = new HttpServer (new RelativeRedirect(), 1, 10, 0);
93 System.out.println ("Server: listening on port: " + server.getLocalPort());
94 URL url = new URL("http://localhost:"+server.getLocalPort());
95 System.out.println ("client opening connection to: " + url);
96 HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
97 InputStream is = urlc.getInputStream ();
98 is.close();
99 } catch (Exception e) {
100 throw new RuntimeException(e);
101 } finally {
102 if (server != null) {
103 server.terminate();
104 }
105 }
106 }
107}