View | Details | Raw Unified | Return to bug 55006
Collapse All | Expand All

(-)java/org/apache/tomcat/websocket/WsWebSocketContainer.java (-22 / +72 lines)
Lines 249-279 Link Here
249
                    sm.getString("wsWebSocketContainer.pathNoHost"));
249
                    sm.getString("wsWebSocketContainer.pathNoHost"));
250
        }
250
        }
251
        int port = path.getPort();
251
        int port = path.getPort();
252
        if ("ws".equalsIgnoreCase(scheme)){
253
        if (port == -1) port = 80;
254
        } else if ("wss".equalsIgnoreCase(scheme)){
255
        		secure = true;
256
        		if (port == -1) port = 443;
257
        } else{
258
        		//we shouldn't be here as the scheme is already checked above
259
        		 throw new DeploymentException(
260
                         sm.getString("wsWebSocketContainer.invalidScheme"));
261
        }
252
        Map<String,List<String>> reqHeaders = createRequestHeaders(host, port,
262
        Map<String,List<String>> reqHeaders = createRequestHeaders(host, port,
253
                clientEndpointConfiguration.getPreferredSubprotocols());
263
                clientEndpointConfiguration.getPreferredSubprotocols());
254
        clientEndpointConfiguration.getConfigurator().
264
        clientEndpointConfiguration.getConfigurator().
255
                beforeRequest(reqHeaders);
265
                beforeRequest(reqHeaders);
256
266
        
267
        //construct initial HTTP GET request
257
        ByteBuffer request = createRequest(path, reqHeaders);
268
        ByteBuffer request = createRequest(path, reqHeaders);
258
269
        ByteBuffer proxyConnectRequest = null;
259
        SocketAddress sa;
270
        
260
        if (port == -1) {
271
        boolean proxyIsSet = false;
261
            if ("ws".equalsIgnoreCase(scheme)) {
272
        if (System.getProperty("https.proxyHost")!=null && secure){
262
                sa = new InetSocketAddress(host, 80);
273
        	proxyConnectRequest = createProxyConnect(host, port);
263
            } else if ("wss".equalsIgnoreCase(scheme)) {
274
        	host = System.getProperty("https.proxyHost");
264
                sa = new InetSocketAddress(host, 443);
275
        	port = Integer.valueOf(System.getProperty("https.proxyPort"));
265
                secure = true;
276
        	proxyIsSet = true;
266
            } else {
267
                throw new DeploymentException(
268
                        sm.getString("wsWebSocketContainer.invalidScheme"));
269
            }
270
        } else {
271
            if ("wss".equalsIgnoreCase(scheme)) {
272
                secure = true;
273
            }
274
            sa = new InetSocketAddress(host, port);
275
        }
277
        }
278
        if (System.getProperty("http.proxyHost")!=null && !secure){
279
        	proxyConnectRequest = createProxyConnect(host, port);
280
        	host = System.getProperty("https.proxyHost");
281
        	port = Integer.valueOf(System.getProperty("https.proxyPort"));
282
        	proxyIsSet = true;
283
        }
284
        
276
285
286
        SocketAddress sa = new InetSocketAddress(host, port);
287
        
288
277
        AsynchronousSocketChannel socketChannel;
289
        AsynchronousSocketChannel socketChannel;
278
        try {
290
        try {
279
            socketChannel =
291
            socketChannel =
Lines 283-290 Link Here
283
                    "wsWebSocketContainer.asynchronousSocketChannelFail"), ioe);
295
                    "wsWebSocketContainer.asynchronousSocketChannelFail"), ioe);
284
        }
296
        }
285
297
298
        
286
        Future<Void> fConnect = socketChannel.connect(sa);
299
        Future<Void> fConnect = socketChannel.connect(sa);
287
300
        try {
301
			fConnect.get();
302
		
303
            //if we are behind the proxy lets't do initial CONNECT
304
            if (proxyIsSet && proxyConnectRequest != null){ 
305
        		while (proxyConnectRequest.hasRemaining()){
306
					Future<Integer>fwrite = socketChannel.write(proxyConnectRequest);
307
					fwrite.get();
308
					proxyConnectRequest.compact();
309
					proxyConnectRequest.flip();
310
				}
311
        		//let's read the response
312
        	    proxyConnectRequest.clear();
313
        	    socketChannel.read(proxyConnectRequest).get();
314
			    proxyConnectRequest.flip();
315
			    byte[] res = new byte[proxyConnectRequest.limit()];
316
			    proxyConnectRequest.get(res);
317
            }			
318
		} catch (InterruptedException | ExecutionException e) {
319
			throw new DeploymentException(
320
	           sm.getString("wsWebSocketContainer.httpRequestFailed"), e);
321
		}
322
        	
288
        AsyncChannelWrapper channel;
323
        AsyncChannelWrapper channel;
289
        if (secure) {
324
        if (secure) {
290
            SSLEngine sslEngine = createSSLEngine(
325
            SSLEngine sslEngine = createSSLEngine(
Lines 297-304 Link Here
297
        ByteBuffer response;
332
        ByteBuffer response;
298
        String subProtocol;
333
        String subProtocol;
299
        try {
334
        try {
300
            fConnect.get();
301
302
            Future<Void> fHandshake = channel.handshake();
335
            Future<Void> fHandshake = channel.handshake();
303
            fHandshake.get();
336
            fHandshake.get();
304
337
Lines 360-366 Link Here
360
    }
393
    }
361
394
362
395
363
    protected void registerSession(Class<?> endpoint, WsSession wsSession) {
396
    private ByteBuffer createProxyConnect(String host, int port) {
397
        //behind the proxy ('should send CONNECT request')
398
    	
399
        ByteBuffer proxyRequest = ByteBuffer.allocate(4 * 1024);
400
   	    proxyRequest.put(("CONNECT "+host+":"+port+" HTTP/1.1").getBytes(iso88591));
401
   	    proxyRequest.put(crlf);
402
   	    proxyRequest.put(("Host: "+host).getBytes(iso88591));
403
   	    proxyRequest.put(crlf);
404
   	    proxyRequest.put("Proxy-Connection: keep-alive".getBytes(iso88591));
405
   	    proxyRequest.put(crlf);
406
   	    
407
   	    proxyRequest.put(crlf);
408
   	    proxyRequest.flip();
409
		return proxyRequest;
410
	}
411
412
413
	protected void registerSession(Class<?> endpoint, WsSession wsSession) {
364
        if (!wsSession.isOpen()) {
414
        if (!wsSession.isOpen()) {
365
            // The session was closed during onOpen. No need to register it.
415
            // The session was closed during onOpen. No need to register it.
366
            return;
416
            return;

Return to bug 55006