37 import java.util.*; |
37 import java.util.*; |
38 import com.sun.net.httpserver.*; |
38 import com.sun.net.httpserver.*; |
39 import java.util.concurrent.*; |
39 import java.util.concurrent.*; |
40 import javax.net.ssl.*; |
40 import javax.net.ssl.*; |
41 import jdk.test.lib.net.SimpleSSLContext; |
41 import jdk.test.lib.net.SimpleSSLContext; |
|
42 import jdk.test.lib.net.URIBuilder; |
42 |
43 |
43 public class RedirectOnPost { |
44 public class RedirectOnPost { |
44 |
45 |
45 |
46 |
46 public static void main(String[] args) throws Exception { |
47 public static void main(String[] args) throws Exception { |
53 // take the keystore from elsewhere in test hierarchy |
54 // take the keystore from elsewhere in test hierarchy |
54 int port = httpServer.getAddress().getPort(); |
55 int port = httpServer.getAddress().getPort(); |
55 int sslPort = httpsServer.getAddress().getPort(); |
56 int sslPort = httpsServer.getAddress().getPort(); |
56 httpServer.start(); |
57 httpServer.start(); |
57 httpsServer.start(); |
58 httpsServer.start(); |
58 runTest("http://127.0.0.1:"+port+"/test/", null); |
59 runTest("http", port, null); |
59 runTest("https://127.0.0.1:"+sslPort+"/test/", ctx); |
60 runTest("https", sslPort, ctx); |
60 System.out.println("Main thread waiting"); |
61 System.out.println("Main thread waiting"); |
61 } finally { |
62 } finally { |
62 httpServer.stop(0); |
63 httpServer.stop(0); |
63 httpsServer.stop(0); |
64 httpsServer.stop(0); |
64 e.shutdownNow(); |
65 e.shutdownNow(); |
65 } |
66 } |
66 } |
67 } |
67 |
68 |
68 public static void runTest(String baseURL, SSLContext ctx) throws Exception |
69 public static void runTest(String scheme, int port, SSLContext ctx) throws Exception |
69 { |
70 { |
70 byte[] buf = "Hello world".getBytes(); |
71 byte[] buf = "Hello world".getBytes(); |
71 URL url = new URL(baseURL + "a"); |
72 |
|
73 URL url = URIBuilder.newBuilder() |
|
74 .scheme(scheme) |
|
75 .loopback() |
|
76 .port(port) |
|
77 .path("/test/a") |
|
78 .toURL(); |
|
79 System.out.println("URL: " + url); |
72 HttpURLConnection con = (HttpURLConnection)url.openConnection(); |
80 HttpURLConnection con = (HttpURLConnection)url.openConnection(); |
73 if (con instanceof HttpsURLConnection) { |
81 if (con instanceof HttpsURLConnection) { |
74 HttpsURLConnection ssl = (HttpsURLConnection)con; |
82 HttpsURLConnection ssl = (HttpsURLConnection)con; |
75 ssl.setHostnameVerifier(new HostnameVerifier() { |
83 ssl.setHostnameVerifier(new HostnameVerifier() { |
76 public boolean verify(String host, SSLSession sess) { |
84 public boolean verify(String host, SSLSession sess) { |
105 } |
113 } |
106 |
114 |
107 |
115 |
108 static class Handler implements HttpHandler { |
116 static class Handler implements HttpHandler { |
109 |
117 |
110 String baseURL; |
118 String scheme; |
|
119 int port; |
111 |
120 |
112 Handler(String baseURL) { |
121 Handler(String scheme, int port) { |
113 this.baseURL = baseURL; |
122 this.scheme = scheme; |
|
123 this.port = port; |
114 } |
124 } |
115 |
125 |
116 int calls = 0; |
126 int calls = 0; |
117 |
127 |
118 public void handle(HttpExchange msg) { |
128 public void handle(HttpExchange msg) { |
119 try { |
129 try { |
120 String method = msg.getRequestMethod(); |
130 String method = msg.getRequestMethod(); |
|
131 URL baseURL = URIBuilder.newBuilder() |
|
132 .scheme(scheme) |
|
133 .loopback() |
|
134 .path("/test/b") |
|
135 .port(port) |
|
136 .toURLUnchecked(); |
121 System.out.println ("Server: " + baseURL); |
137 System.out.println ("Server: " + baseURL); |
122 if (calls++ == 0) { |
138 if (calls++ == 0) { |
123 System.out.println ("Server: redirecting"); |
139 System.out.println ("Server: redirecting"); |
124 InputStream is = msg.getRequestBody(); |
140 InputStream is = msg.getRequestBody(); |
125 byte[] buf = readFully(is); |
141 byte[] buf = readFully(is); |
126 is.close(); |
142 is.close(); |
127 Headers h = msg.getResponseHeaders(); |
143 Headers h = msg.getResponseHeaders(); |
128 h.add("Location", baseURL + "b"); |
144 h.add("Location", baseURL.toString()); |
129 msg.sendResponseHeaders(302, -1); |
145 msg.sendResponseHeaders(302, -1); |
130 msg.close(); |
146 msg.close(); |
131 } else { |
147 } else { |
132 System.out.println ("Server: second call"); |
148 System.out.println ("Server: second call"); |
133 InputStream is = msg.getRequestBody(); |
149 InputStream is = msg.getRequestBody(); |
151 { |
167 { |
152 InetSocketAddress inetAddress = new InetSocketAddress(0); |
168 InetSocketAddress inetAddress = new InetSocketAddress(0); |
153 HttpServer testServer = HttpServer.create(inetAddress, 15); |
169 HttpServer testServer = HttpServer.create(inetAddress, 15); |
154 int port = testServer.getAddress().getPort(); |
170 int port = testServer.getAddress().getPort(); |
155 testServer.setExecutor(execs); |
171 testServer.setExecutor(execs); |
156 String base = "http://127.0.0.1:"+port+"/test"; |
172 |
157 HttpContext context = testServer.createContext("/test"); |
173 HttpContext context = testServer.createContext("/test"); |
158 context.setHandler(new Handler(base)); |
174 context.setHandler(new Handler("http", port)); |
159 return testServer; |
175 return testServer; |
160 } |
176 } |
161 |
177 |
162 private static HttpsServer getHttpsServer( |
178 private static HttpsServer getHttpsServer( |
163 ExecutorService execs, SSLContext ctx |
179 ExecutorService execs, SSLContext ctx |
167 InetSocketAddress inetAddress = new InetSocketAddress(0); |
183 InetSocketAddress inetAddress = new InetSocketAddress(0); |
168 HttpsServer testServer = HttpsServer.create(inetAddress, 15); |
184 HttpsServer testServer = HttpsServer.create(inetAddress, 15); |
169 int port = testServer.getAddress().getPort(); |
185 int port = testServer.getAddress().getPort(); |
170 testServer.setExecutor(execs); |
186 testServer.setExecutor(execs); |
171 testServer.setHttpsConfigurator(new HttpsConfigurator (ctx)); |
187 testServer.setHttpsConfigurator(new HttpsConfigurator (ctx)); |
172 String base = "https://127.0.0.1:"+port+"/test"; |
188 |
173 HttpContext context = testServer.createContext("/test"); |
189 HttpContext context = testServer.createContext("/test"); |
174 context.setHandler(new Handler(base)); |
190 context.setHandler(new Handler("https", port)); |
175 return testServer; |
191 return testServer; |
176 } |
192 } |
177 } |
193 } |