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
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
31
31
32
32
public class ProtocolRedirect {
33
33
public static void main (String [] args ) throws Exception {
34
- int localPort ;
35
- new Thread (new Redirect ()).start ();
36
- while ((localPort = Redirect .listenPort ) == -1 ) {
37
- Thread .sleep (1000 );
38
- }
34
+ try (Redirect server = new Redirect (new ServerSocket ())) {
35
+ ServerSocket ss = server .ssock ;
36
+ ss .bind (new InetSocketAddress (InetAddress .getLoopbackAddress (), 0 ));
37
+ new Thread (server ).start ();
39
38
40
- String page = "http://localhost:" +localPort +"/" ;
41
- URL url = new URL (page );
42
- HttpURLConnection conn = (HttpURLConnection )url .openConnection ();
43
- conn .connect ();
44
- if (conn .getResponseCode () != 302 ) {
45
- throw new RuntimeException ("Test failed. Should get RespCode: 302. Got:" +conn .getResponseCode ());
39
+ URL url = new URL ("http" , ss .getInetAddress ().getHostAddress (), ss .getLocalPort (), "/" );
40
+ String page = url .toString ();
41
+ HttpURLConnection conn = (HttpURLConnection ) url .openConnection ();
42
+ conn .connect ();
43
+ if (conn .getResponseCode () != 302 ) {
44
+ System .err .println ("Bad response code received from: " + page );
45
+ throw new RuntimeException ("Test failed. Should get RespCode: 302. Got:" + conn .getResponseCode ());
46
+ }
47
+ System .out .println ("Received expected response code from: " + page );
46
48
}
47
49
}
48
50
}
49
51
50
- class Redirect implements Runnable {
51
- public static int listenPort = -1 ; // port to listen for connections on
52
+ class Redirect implements Runnable , Closeable {
53
+ final ServerSocket ssock ;
54
+ volatile boolean stopped ;
55
+ Redirect (ServerSocket ss ) {
56
+ ssock = ss ;
57
+ }
52
58
53
59
// Send a header redirect to the peer telling it to go to the
54
60
// https server on the host it sent the connection request to.
@@ -61,18 +67,31 @@ private void sendReply() throws IOException {
61
67
out .write (reply .toString ().getBytes ());
62
68
}
63
69
64
- Socket sock ;
70
+ volatile Socket sock ;
65
71
public void run () {
66
72
try {
67
- ServerSocket ssock = new ServerSocket ();
68
- ssock .bind (null );
69
- listenPort = ssock .getLocalPort ();
70
- sock = ssock .accept ();
71
- sock .setTcpNoDelay (true );
73
+ Socket s = sock = ssock .accept ();
74
+ s .setTcpNoDelay (true );
72
75
sendReply ();
73
- sock .shutdownOutput ();
74
- } catch (IOException io ) {
75
- throw new RuntimeException (io .getCause ());
76
+ s .shutdownOutput ();
77
+ } catch (Throwable t ) {
78
+ if (!stopped ) {
79
+ t .printStackTrace ();
80
+ throw new RuntimeException (String .valueOf (t ), t );
81
+ }
82
+ }
83
+ }
84
+
85
+ public void close () {
86
+ Socket s = sock ;
87
+ boolean done = stopped ;
88
+ if (done ) return ;
89
+ stopped = true ;
90
+ try {
91
+ if (s != null ) s .close ();
92
+ } catch (Throwable x ) {
93
+ } finally {
94
+ try { ssock .close (); } catch (Throwable x ) {}
76
95
}
77
96
}
78
97
0 commit comments