171 out.flush(); 172 } catch (IOException ie) { 173 ie.printStackTrace(); 174 return; 175 } 176 177 } catch (Exception e) { 178 e.printStackTrace(); 179 // write out error response 180 out.writeBytes("HTTP/1.0 400 " + e.getMessage() + "\r\n"); 181 out.writeBytes("Content-Type: text/html\r\n\r\n"); 182 out.flush(); 183 } finally { 184 // close the socket 185 System.out.println("Server closing socket"); 186 sslSocket.close(); 187 serverReady = false; 188 } 189 } 190 191 /* 192 * Define the client side of the test. 193 * 194 * If the server prematurely exits, serverReady will be set to true 195 * to avoid infinite hangs. 196 */ 197 void doClientSide() throws Exception { 198 /* 199 * Wait for server to get started. 200 */ 201 while (!serverReady) { 202 Thread.sleep(50); 203 } 204 205 HostnameVerifier reservedHV = 206 HttpsURLConnection.getDefaultHostnameVerifier(); 207 try { 208 System.setProperty("java.protocol.handler.pkgs", 209 "com.sun.net.ssl.internal.www.protocol"); 210 HttpsURLConnection.setDefaultHostnameVerifier(new NameVerifier()); 211 212 URL url = new URL("https://" + "localhost:" + serverPort + 213 "/etc/hosts"); 214 URLConnection urlc = url.openConnection(); 215 216 if (!(urlc instanceof com.sun.net.ssl.HttpsURLConnection)) { 217 throw new Exception( 218 "URLConnection ! instanceof " + 219 "com.sun.net.ssl.HttpsURLConnection"); 220 } 221 222 BufferedReader in = null; 223 try { 224 in = new BufferedReader(new InputStreamReader( 225 urlc.getInputStream())); 226 String inputLine; 227 System.out.print("Client reading... "); 228 while ((inputLine = in.readLine()) != null) 229 System.out.println(inputLine); | 171 out.flush(); 172 } catch (IOException ie) { 173 ie.printStackTrace(); 174 return; 175 } 176 177 } catch (Exception e) { 178 e.printStackTrace(); 179 // write out error response 180 out.writeBytes("HTTP/1.0 400 " + e.getMessage() + "\r\n"); 181 out.writeBytes("Content-Type: text/html\r\n\r\n"); 182 out.flush(); 183 } finally { 184 // close the socket 185 System.out.println("Server closing socket"); 186 sslSocket.close(); 187 serverReady = false; 188 } 189 } 190 191 private static class ComSunHTTPSHandlerFactory implements URLStreamHandlerFactory { 192 private static String SUPPORTED_PROTOCOL = "https"; 193 194 public URLStreamHandler createURLStreamHandler(String protocol) { 195 if (!protocol.equalsIgnoreCase(SUPPORTED_PROTOCOL)) 196 return null; 197 198 return new com.sun.net.ssl.internal.www.protocol.https.Handler(); 199 } 200 } 201 202 /* 203 * Define the client side of the test. 204 * 205 * If the server prematurely exits, serverReady will be set to true 206 * to avoid infinite hangs. 207 */ 208 void doClientSide() throws Exception { 209 /* 210 * Wait for server to get started. 211 */ 212 while (!serverReady) { 213 Thread.sleep(50); 214 } 215 216 HostnameVerifier reservedHV = 217 HttpsURLConnection.getDefaultHostnameVerifier(); 218 try { 219 URL.addURLStreamHandlerFactory(new ComSunHTTPSHandlerFactory()); 220 HttpsURLConnection.setDefaultHostnameVerifier(new NameVerifier()); 221 222 URL url = new URL("https://" + "localhost:" + serverPort + 223 "/etc/hosts"); 224 URLConnection urlc = url.openConnection(); 225 226 if (!(urlc instanceof com.sun.net.ssl.HttpsURLConnection)) { 227 throw new Exception( 228 "URLConnection ! instanceof " + 229 "com.sun.net.ssl.HttpsURLConnection"); 230 } 231 232 BufferedReader in = null; 233 try { 234 in = new BufferedReader(new InputStreamReader( 235 urlc.getInputStream())); 236 String inputLine; 237 System.out.print("Client reading... "); 238 while ((inputLine = in.readLine()) != null) 239 System.out.println(inputLine); |