@@ -303,6 +303,9 @@ function OutgoingMessage() {
303
303
this . _trailer = '' ;
304
304
305
305
this . finished = false ;
306
+
307
+ this . _headers = { } ;
308
+ this . _headerNames = { } ;
306
309
}
307
310
util . inherits ( OutgoingMessage , stream . Stream ) ;
308
311
@@ -432,7 +435,6 @@ OutgoingMessage.prototype._storeHeader = function(firstLine, headers) {
432
435
433
436
} else if ( expectExpression . test ( field ) ) {
434
437
sentExpect = true ;
435
-
436
438
}
437
439
}
438
440
@@ -495,9 +497,68 @@ OutgoingMessage.prototype._storeHeader = function(firstLine, headers) {
495
497
} ;
496
498
497
499
500
+ OutgoingMessage . prototype . setHeader = function ( name , value ) {
501
+ if ( arguments . length < 2 ) {
502
+ throw new Error ( "`name` and `value` are required for setHeader()." ) ;
503
+ }
504
+
505
+ if ( this . _header ) {
506
+ throw new Error ( "Can't set headers after they are sent." ) ;
507
+ }
508
+
509
+ var key = name . toLowerCase ( ) ;
510
+ this . _headers [ key ] = value ;
511
+ this . _headerNames [ key ] = name ;
512
+ } ;
513
+
514
+
515
+ OutgoingMessage . prototype . getHeader = function ( name ) {
516
+ if ( arguments . length < 1 ) {
517
+ throw new Error ( "`name` is required for getHeader()." ) ;
518
+ }
519
+
520
+ if ( this . _header ) {
521
+ throw new Error ( "Can't use mutable header APIs after sent." ) ;
522
+ }
523
+
524
+ var key = name . toLowerCase ( ) ;
525
+ return this . _headers [ key ] ;
526
+ } ;
527
+
528
+
529
+ OutgoingMessage . prototype . removeHeader = function ( name ) {
530
+ if ( arguments . length < 1 ) {
531
+ throw new Error ( "`name` is required for removeHeader()." ) ;
532
+ }
533
+
534
+ if ( this . _header ) {
535
+ throw new Error ( "Can't remove headers after they are sent." ) ;
536
+ }
537
+
538
+ var key = name . toLowerCase ( ) ;
539
+ delete this . _headers [ key ] ;
540
+ delete this . _headerNames [ key ] ;
541
+ } ;
542
+
543
+
544
+ OutgoingMessage . prototype . _renderHeaders = function ( ) {
545
+ if ( this . _header ) {
546
+ throw new Error ( "Can't render headers after they are sent to the client." ) ;
547
+ }
548
+ var headers = { } ;
549
+ var keys = Object . keys ( this . _headers ) ;
550
+ for ( var i = 0 , l = keys . length ; i < l ; i ++ ) {
551
+ var key = keys [ i ] ;
552
+ headers [ this . _headerNames [ key ] ] = this . _headers [ key ] ;
553
+ }
554
+ return headers ;
555
+ } ;
556
+
557
+
558
+
498
559
OutgoingMessage . prototype . write = function ( chunk , encoding ) {
499
560
if ( ! this . _header ) {
500
- throw new Error ( 'You have to call writeHead() before write()' ) ;
561
+ this . _implicitHeader ( ) ;
501
562
}
502
563
503
564
if ( ! this . _hasBody ) {
@@ -557,6 +618,10 @@ OutgoingMessage.prototype.addTrailers = function(headers) {
557
618
558
619
559
620
OutgoingMessage . prototype . end = function ( data , encoding ) {
621
+ if ( ! this . _header ) {
622
+ this . _implicitHeader ( ) ;
623
+ }
624
+
560
625
var ret ;
561
626
562
627
var hot = this . _headerSent === false &&
@@ -681,12 +746,16 @@ util.inherits(ServerResponse, OutgoingMessage);
681
746
682
747
exports . ServerResponse = ServerResponse ;
683
748
749
+ ServerResponse . prototype . statusCode = 200 ;
684
750
685
751
ServerResponse . prototype . writeContinue = function ( ) {
686
752
this . _writeRaw ( 'HTTP/1.1 100 Continue' + CRLF + CRLF , 'ascii' ) ;
687
753
this . _sent100 = true ;
688
754
} ;
689
755
756
+ ServerResponse . prototype . _implicitHeader = function ( ) {
757
+ this . writeHead ( this . statusCode , this . _renderHeaders ( ) ) ;
758
+ } ;
690
759
691
760
ServerResponse . prototype . writeHead = function ( statusCode ) {
692
761
var reasonPhrase , headers , headerIndex ;
@@ -742,12 +811,21 @@ function ClientRequest(options) {
742
811
OutgoingMessage . call ( this ) ;
743
812
744
813
var method = this . method = ( options . method || 'GET' ) . toUpperCase ( ) ;
745
- var path = options . path || '/' ;
746
- var headers = options . headers || { } ;
747
-
748
- // Host header set by default.
749
- if ( options . host && ! ( headers . host || headers . Host || headers . HOST ) ) {
750
- headers . Host = options . host ;
814
+ this . path = options . path || '/' ;
815
+
816
+ if ( ! Array . isArray ( headers ) ) {
817
+ if ( options . headers ) {
818
+ var headers = options . headers ;
819
+ var keys = Object . keys ( headers ) ;
820
+ for ( var i = 0 , l = keys . length ; i < l ; i ++ ) {
821
+ var key = keys [ i ] ;
822
+ this . setHeader ( key , headers [ key ] ) ;
823
+ }
824
+ }
825
+ // Host header set by default.
826
+ if ( options . host && ! this . getHeader ( 'host' ) ) {
827
+ this . setHeader ( "Host" , options . host ) ;
828
+ }
751
829
}
752
830
753
831
this . shouldKeepAlive = false ;
@@ -761,13 +839,21 @@ function ClientRequest(options) {
761
839
// specified.
762
840
this . _last = true ;
763
841
764
- this . _storeHeader ( method + ' ' + path + ' HTTP/1.1\r\n' , headers ) ;
842
+ if ( Array . isArray ( headers ) ) {
843
+ this . _storeHeader ( this . method + ' ' + this . path + ' HTTP/1.1\r\n' , headers ) ;
844
+ } else if ( this . getHeader ( 'expect' ) ) {
845
+ this . _storeHeader ( this . method + ' ' + this . path + ' HTTP/1.1\r\n' , this . _renderHeaders ( ) ) ;
846
+ }
847
+
765
848
}
766
849
util . inherits ( ClientRequest , OutgoingMessage ) ;
767
850
768
851
769
852
exports . ClientRequest = ClientRequest ;
770
853
854
+ ClientRequest . prototype . _implicitHeader = function ( ) {
855
+ this . _storeHeader ( this . method + ' ' + this . path + ' HTTP/1.1\r\n' , this . _renderHeaders ( ) ) ;
856
+ }
771
857
772
858
ClientRequest . prototype . abort = function ( ) {
773
859
if ( this . _queue ) {
0 commit comments