1 /* |
1 /* |
2 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2002, 2019, 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. |
192 } |
202 } |
193 |
203 |
194 public static void main (String args[]) throws Exception { |
204 public static void main (String args[]) throws Exception { |
195 MyAuthenticator auth = new MyAuthenticator (); |
205 MyAuthenticator auth = new MyAuthenticator (); |
196 Authenticator.setDefault (auth); |
206 Authenticator.setDefault (auth); |
197 ServerSocket ss = new ServerSocket (0); |
207 InetAddress loopback = InetAddress.getLoopbackAddress(); |
|
208 ServerSocket ss = new ServerSocket(); |
|
209 ss.bind(new InetSocketAddress(loopback, 0)); |
198 int port = ss.getLocalPort (); |
210 int port = ss.getLocalPort (); |
199 BasicServer server = new BasicServer (ss); |
211 BasicServer server = new BasicServer (ss); |
200 synchronized (server) { |
212 synchronized (server) { |
201 server.start(); |
213 server.start(); |
202 System.out.println ("client 1"); |
214 System.out.println ("client 1"); |
203 URL url = new URL ("http://localhost:"+port+"/d1/d3/foo.html"); |
215 String base = URIBuilder.newBuilder() |
204 URLConnection urlc = url.openConnection (); |
216 .scheme("http") |
|
217 .loopback() |
|
218 .port(port) |
|
219 .path("/d1/") |
|
220 .build() |
|
221 .toString(); |
|
222 System.out.println("Base URL: " + base); |
|
223 URL url = new URL (base + "d3/foo.html"); |
|
224 URLConnection urlc = url.openConnection(Proxy.NO_PROXY); |
205 InputStream is = urlc.getInputStream (); |
225 InputStream is = urlc.getInputStream (); |
206 read (is); |
226 read (is); |
207 System.out.println ("client 2"); |
227 System.out.println ("client 2"); |
208 url = new URL ("http://localhost:"+port+"/d1/d2/bar.html"); |
228 url = new URL (base + "d2/bar.html"); |
209 urlc = url.openConnection (); |
229 urlc = url.openConnection(Proxy.NO_PROXY); |
210 is = urlc.getInputStream (); |
230 is = urlc.getInputStream (); |
211 System.out.println ("client 3"); |
231 System.out.println ("client 3"); |
212 url = new URL ("http://localhost:"+port+"/d1/d4/foobar.html"); |
232 url = new URL (base + "d4/foobar.html"); |
213 urlc = url.openConnection (); |
233 urlc = url.openConnection(Proxy.NO_PROXY); |
214 is = urlc.getInputStream (); |
234 is = urlc.getInputStream (); |
215 read (is); |
235 read (is); |
216 server.wait (); |
236 server.wait (); |
217 if (!server.success) { |
237 if (!server.success) { |
218 throw new RuntimeException ("3rd request did not use pre-emptive authorization"); |
238 throw new RuntimeException ("3rd request did not use pre-emptive authorization"); |