@@ -35,6 +35,7 @@ var buffer = require('buffer');
35
35
var crypto = require ( 'crypto' ) ;
36
36
var events = require ( 'events' ) ;
37
37
var http = require ( 'http' ) ;
38
+ var https = require ( 'https' ) ;
38
39
var net = require ( 'net' ) ;
39
40
var urllib = require ( 'url' ) ;
40
41
var sys = require ( 'sys' ) ;
@@ -178,16 +179,6 @@ var str2hex = function(str) {
178
179
return out . trim ( ) ;
179
180
} ;
180
181
181
- // Get the scheme for a URL, undefined if none is found
182
- var getUrlScheme = function ( url ) {
183
- var i = url . indexOf ( ':' ) ;
184
- if ( i == - 1 ) {
185
- return undefined ;
186
- }
187
-
188
- return url . substring ( 0 , i ) ;
189
- } ;
190
-
191
182
// Set a constant on the given object
192
183
var setConstant = function ( obj , name , value ) {
193
184
Object . defineProperty ( obj , name , {
@@ -501,125 +492,140 @@ var WebSocket = function(url, proto, opts) {
501
492
// that http.Client passes its constructor arguments through,
502
493
// un-inspected to net.Stream.connect(). The latter accepts a
503
494
// string as its first argument to connect to a UNIX socket.
504
- var httpClient = undefined ;
505
- switch ( getUrlScheme ( url ) ) {
506
- case 'ws' :
507
- var u = urllib . parse ( url ) ;
508
- httpClient = http . createClient ( u . port || 80 , u . hostname ) ;
495
+ var protocol , agent , port , u = urllib . parse ( url ) ;
496
+ if ( u . protocol === 'ws:' || u . protocol === 'wss:' ) {
497
+ require ( 'eyes' ) . inspect ( u ) ;
498
+ protocol = u . protocol === 'ws:' ? http : https ;
499
+ port = u . protocol === 'ws:' ? 80 : 443 ;
500
+ agent = u . protocol === 'ws:' ? protocol . getAgent ( u . hostname , u . port || port ) : protocol . getAgent ( {
501
+ host : u . hostname ,
502
+ port : u . port || port
503
+ } ) ;
504
+
509
505
httpPath = ( u . pathname || '/' ) + ( u . search || '' ) ;
510
506
httpHeaders . Host = u . hostname + ( u . port ? ( ":" + u . port ) : "" ) ;
511
- break ;
512
-
513
- case 'ws+unix' :
514
- var sockPath = url . substring ( 'ws+unix://' . length , url . length ) ;
515
- httpClient = http . createClient ( sockPath ) ;
516
- httpHeaders . Host = 'localhost' ;
517
- break ;
518
-
519
- default :
507
+ }
508
+ else if ( urlScheme === 'ws+unix' ) {
509
+ throw new Error ( 'ws+unix is not implemented' ) ;
510
+ // var sockPath = url.substring('ws+unix://'.length, url.length);
511
+ // httpClient = http.createClient(sockPath);
512
+ // httpHeaders.Host = 'localhost';
513
+ }
514
+ else {
520
515
throw new Error ( 'Invalid URL scheme \'' + urlScheme + '\' specified.' ) ;
521
516
}
522
-
523
- httpClient . on ( 'upgrade' , ( function ( ) {
524
- var data = undefined ;
525
-
526
- return function ( res , s , head ) {
527
- stream = s ;
528
-
529
- //
530
- // Emit the `wsupgrade` event to inspect the raw
531
- // arguments returned from the websocket request.
532
- //
533
- self . emit ( 'wsupgrade' , httpHeaders , res , s , head ) ;
517
+
518
+ if ( ! agent . _events || agent . _events [ 'upgrade' ] . length === 0 ) {
519
+ agent . on ( 'upgrade' , ( function ( ) {
520
+ var data = undefined ;
521
+
522
+ return function ( res , s , head ) {
523
+ stream = s ;
524
+
525
+ //
526
+ // Emit the `wsupgrade` event to inspect the raw
527
+ // arguments returned from the websocket request.
528
+ //
529
+ self . emit ( 'wsupgrade' , httpHeaders , res , s , head ) ;
534
530
535
- stream . on ( 'data' , function ( d ) {
536
- if ( d . length <= 0 ) {
537
- return ;
538
- }
531
+ stream . on ( 'data' , function ( d ) {
532
+ if ( d . length <= 0 ) {
533
+ return ;
534
+ }
539
535
540
- if ( ! data ) {
541
- data = d ;
542
- } else {
543
- var data2 = new buffer . Buffer ( data . length + d . length ) ;
536
+ if ( ! data ) {
537
+ data = d ;
538
+ } else {
539
+ var data2 = new buffer . Buffer ( data . length + d . length ) ;
544
540
545
- data . copy ( data2 , 0 , 0 , data . length ) ;
546
- d . copy ( data2 , data . length , 0 , d . length ) ;
541
+ data . copy ( data2 , 0 , 0 , data . length ) ;
542
+ d . copy ( data2 , data . length , 0 , d . length ) ;
547
543
548
- data = data2 ;
549
- }
550
-
551
- if ( data . length >= 16 ) {
552
- var expected = computeSecretKeySignature ( key1 , key2 , challenge ) ;
553
- var actual = data . slice ( 0 , 16 ) . toString ( 'binary' ) ;
544
+ data = data2 ;
545
+ }
554
546
555
- // Handshaking fails; we're donezo
556
- if ( actual != expected ) {
557
- debug (
558
- 'expected=\'' + str2hex ( expected ) + '\'; ' +
559
- 'actual=\'' + str2hex ( actual ) + '\''
560
- ) ;
547
+ if ( data . length >= 16 ) {
548
+ var expected = computeSecretKeySignature ( key1 , key2 , challenge ) ;
549
+ var actual = data . slice ( 0 , 16 ) . toString ( 'binary' ) ;
561
550
562
- process . nextTick ( function ( ) {
563
- // N.B. Emit 'wserror' here, as 'error' is a reserved word in the
564
- // EventEmitter world, and gets thrown.
565
- self . emit (
566
- 'wserror' ,
567
- new Error ( 'Invalid handshake from server:' +
568
- 'expected \'' + str2hex ( expected ) + '\', ' +
569
- 'actual \'' + str2hex ( actual ) + '\''
570
- )
551
+ // Handshaking fails; we're donezo
552
+ if ( actual != expected ) {
553
+ debug (
554
+ 'expected=\'' + str2hex ( expected ) + '\'; ' +
555
+ 'actual=\'' + str2hex ( actual ) + '\''
571
556
) ;
572
557
573
- if ( self . onerror ) {
574
- self . onerror ( ) ;
575
- }
558
+ process . nextTick ( function ( ) {
559
+ // N.B. Emit 'wserror' here, as 'error' is a reserved word in the
560
+ // EventEmitter world, and gets thrown.
561
+ self . emit (
562
+ 'wserror' ,
563
+ new Error ( 'Invalid handshake from server:' +
564
+ 'expected \'' + str2hex ( expected ) + '\', ' +
565
+ 'actual \'' + str2hex ( actual ) + '\''
566
+ )
567
+ ) ;
568
+
569
+ if ( self . onerror ) {
570
+ self . onerror ( ) ;
571
+ }
572
+
573
+ finishClose ( ) ;
574
+ } ) ;
575
+ }
576
576
577
- finishClose ( ) ;
578
- } ) ;
579
- }
577
+ //
578
+ // Un-register our data handler and add the one to be used
579
+ // for the normal, non-handshaking case. If we have extra
580
+ // data left over, manually fire off the handler on
581
+ // whatever remains.
582
+ //
583
+ stream . removeAllListeners ( 'data' ) ;
584
+ stream . on ( 'data' , dataListener ) ;
580
585
581
- // Un-register our data handler and add the one to be used
582
- // for the normal, non-handshaking case. If we have extra
583
- // data left over, manually fire off the handler on
584
- // whatever remains.
585
- //
586
- // XXX: This is lame. We should only remove the listeners
587
- // that we added.
588
- httpClient . removeAllListeners ( 'upgrade' ) ;
589
- stream . removeAllListeners ( 'data' ) ;
590
- stream . on ( 'data' , dataListener ) ;
586
+ readyState = OPEN ;
591
587
592
- readyState = OPEN ;
588
+ process . nextTick ( function ( ) {
589
+ self . emit ( 'open' ) ;
593
590
594
- process . nextTick ( function ( ) {
595
- self . emit ( 'open' ) ;
591
+ if ( self . onopen ) {
592
+ self . onopen ( ) ;
593
+ }
594
+ } ) ;
596
595
597
- if ( self . onopen ) {
598
- self . onopen ( ) ;
596
+ // Consume any leftover data
597
+ if ( data . length > 16 ) {
598
+ stream . emit ( 'data' , data . slice ( 16 , data . length ) ) ;
599
599
}
600
- } ) ;
601
-
602
- // Consume any leftover data
603
- if ( data . length > 16 ) {
604
- stream . emit ( 'data' , data . slice ( 16 , data . length ) ) ;
605
600
}
606
- }
607
- } ) ;
608
- stream . on ( 'fd ' , fdListener ) ;
609
- stream . on ( 'error ' , errorListener ) ;
610
- stream . on ( 'close' , function ( ) {
611
- errorListener ( new Error ( 'Stream closed unexpectedly.' ) ) ;
612
- } ) ;
613
-
614
- stream . emit ( 'data' , head ) ;
615
- } ;
616
- } ) ( ) ) ;
617
- httpClient . on ( 'error' , function ( e ) {
618
- httpClient . end ( ) ;
601
+ } ) ;
602
+ stream . on ( 'fd' , fdListener ) ;
603
+ stream . on ( 'error ' , errorListener ) ;
604
+ stream . on ( 'close ' , function ( ) {
605
+ errorListener ( new Error ( 'Stream closed unexpectedly.' ) ) ;
606
+ } ) ;
607
+
608
+ stream . emit ( 'data' , head ) ;
609
+ } ;
610
+ } ) ( ) ) ;
611
+ }
612
+
613
+ agent . on ( 'error' , function ( e ) {
619
614
errorListener ( e ) ;
620
615
} ) ;
621
616
622
- var httpReq = httpClient . request ( httpPath , httpHeaders ) ;
617
+
618
+ var x = {
619
+ host : u . hostname ,
620
+ method : 'GET' ,
621
+ agent : agent ,
622
+ port : u . port ,
623
+ path : httpPath ,
624
+ headers : httpHeaders
625
+ } ;
626
+ require ( 'eyes' ) . inspect ( x ) ;
627
+ var httpReq = protocol . request ( x ) ;
628
+
623
629
httpReq . write ( challenge , 'binary' ) ;
624
630
httpReq . end ( ) ;
625
631
} ) ( ) ;
0 commit comments