@@ -127,6 +127,26 @@ const { StreamPipe } = internalBinding('stream_pipe');
127
127
const { _connectionListener : httpConnectionListener } = http ;
128
128
const debug = util . debuglog ( 'http2' ) ;
129
129
130
+ // TODO(addaleax): See if this can be made more efficient by figuring out
131
+ // whether debugging is enabled before we perform any further steps. Currently,
132
+ // this seems pretty fast, though.
133
+ function debugStream ( id , sessionType , message , ...args ) {
134
+ debug ( 'Http2Stream %s [Http2Session %s]: ' + message ,
135
+ id , sessionName ( sessionType ) , ...args ) ;
136
+ }
137
+
138
+ function debugStreamObj ( stream , message , ...args ) {
139
+ debugStream ( stream [ kID ] , stream [ kSession ] [ kType ] , ...args ) ;
140
+ }
141
+
142
+ function debugSession ( sessionType , message , ...args ) {
143
+ debug ( 'Http2Session %s: ' + message , sessionName ( sessionType ) , ...args ) ;
144
+ }
145
+
146
+ function debugSessionObj ( session , message , ...args ) {
147
+ debugSession ( session [ kType ] , message , ...args ) ;
148
+ }
149
+
130
150
const kMaxFrameSize = ( 2 ** 24 ) - 1 ;
131
151
const kMaxInt = ( 2 ** 32 ) - 1 ;
132
152
const kMaxStreams = ( 2 ** 31 ) - 1 ;
@@ -248,8 +268,7 @@ function onSessionHeaders(handle, id, cat, flags, headers) {
248
268
249
269
const type = session [ kType ] ;
250
270
session [ kUpdateTimer ] ( ) ;
251
- debug ( `Http2Stream ${ id } [Http2Session ` +
252
- `${ sessionName ( type ) } ]: headers received` ) ;
271
+ debugStream ( id , type , 'headers received' ) ;
253
272
const streams = session [ kState ] . streams ;
254
273
255
274
const endOfStream = ! ! ( flags & NGHTTP2_FLAG_END_STREAM ) ;
@@ -309,8 +328,7 @@ function onSessionHeaders(handle, id, cat, flags, headers) {
309
328
const originSet = session [ kState ] . originSet = initOriginSet ( session ) ;
310
329
originSet . delete ( stream [ kOrigin ] ) ;
311
330
}
312
- debug ( `Http2Stream ${ id } [Http2Session ` +
313
- `${ sessionName ( type ) } ]: emitting stream '${ event } ' event` ) ;
331
+ debugStream ( id , type , "emitting stream '%s' event" , event ) ;
314
332
process . nextTick ( emit , stream , event , obj , flags , headers ) ;
315
333
}
316
334
if ( endOfStream ) {
@@ -351,7 +369,7 @@ function onPing(payload) {
351
369
if ( session . destroyed )
352
370
return ;
353
371
session [ kUpdateTimer ] ( ) ;
354
- debug ( `Http2Session ${ sessionName ( session [ kType ] ) } : new ping received` ) ;
372
+ debugSessionObj ( session , ' new ping received' ) ;
355
373
session . emit ( 'ping' , payload ) ;
356
374
}
357
375
@@ -366,8 +384,7 @@ function onStreamClose(code) {
366
384
if ( stream . destroyed )
367
385
return ;
368
386
369
- debug ( `Http2Stream ${ stream [ kID ] } [Http2Session ` +
370
- `${ sessionName ( stream [ kSession ] [ kType ] ) } ]: closed with code ${ code } ` ) ;
387
+ debugStreamObj ( stream , 'closed with code %d' , code ) ;
371
388
372
389
if ( ! stream . closed )
373
390
closeStream ( stream , code , kNoRstStream ) ;
@@ -403,7 +420,7 @@ function onSettings() {
403
420
if ( session . destroyed )
404
421
return ;
405
422
session [ kUpdateTimer ] ( ) ;
406
- debug ( `Http2Session ${ sessionName ( session [ kType ] ) } : new settings received` ) ;
423
+ debugSessionObj ( session , ' new settings received' ) ;
407
424
session [ kRemoteSettings ] = undefined ;
408
425
session . emit ( 'remoteSettings' , session . remoteSettings ) ;
409
426
}
@@ -415,9 +432,9 @@ function onPriority(id, parent, weight, exclusive) {
415
432
const session = this [ kOwner ] ;
416
433
if ( session . destroyed )
417
434
return ;
418
- debug ( `Http2Stream ${ id } [Http2Session ` +
419
- ` ${ sessionName ( session [ kType ] ) } ]: priority [parent: ${ parent } , ` +
420
- `weight: ${ weight } , exclusive: ${ exclusive } ]` ) ;
435
+ debugStream ( id , session [ kType ] ,
436
+ ' priority [parent: %d, weight: %d, exclusive: %s]' ,
437
+ parent , weight , exclusive ) ;
421
438
const emitter = session [ kState ] . streams . get ( id ) || session ;
422
439
if ( ! emitter . destroyed ) {
423
440
emitter [ kUpdateTimer ] ( ) ;
@@ -431,8 +448,8 @@ function onFrameError(id, type, code) {
431
448
const session = this [ kOwner ] ;
432
449
if ( session . destroyed )
433
450
return ;
434
- debug ( `Http2Session ${ sessionName ( session [ kType ] ) } : error sending frame ` +
435
- `type ${ type } on stream ${ id } , code: ${ code } ` ) ;
451
+ debugSessionObj ( session , ' error sending frame type %d on stream %d, code: %d' ,
452
+ type , id , code ) ;
436
453
const emitter = session [ kState ] . streams . get ( id ) || session ;
437
454
emitter [ kUpdateTimer ] ( ) ;
438
455
emitter . emit ( 'frameError' , type , code , id ) ;
@@ -442,8 +459,8 @@ function onAltSvc(stream, origin, alt) {
442
459
const session = this [ kOwner ] ;
443
460
if ( session . destroyed )
444
461
return ;
445
- debug ( `Http2Session ${ sessionName ( session [ kType ] ) } : altsvc received: ` +
446
- `stream: ${ stream } , origin: ${ origin } , alt: ${ alt } ` ) ;
462
+ debugSessionObj ( session , ' altsvc received: stream: %d, origin: %s, alt: %s' ,
463
+ stream , origin , alt ) ;
447
464
session [ kUpdateTimer ] ( ) ;
448
465
session . emit ( 'altsvc' , alt , origin , stream ) ;
449
466
}
@@ -470,8 +487,7 @@ function onOrigin(origins) {
470
487
const session = this [ kOwner ] ;
471
488
if ( session . destroyed )
472
489
return ;
473
- debug ( 'Http2Session %s: origin received: %j' ,
474
- sessionName ( session [ kType ] ) , origins ) ;
490
+ debugSessionObj ( session , 'origin received: %j' , origins ) ;
475
491
session [ kUpdateTimer ] ( ) ;
476
492
if ( ! session . encrypted || session . destroyed )
477
493
return undefined ;
@@ -491,8 +507,8 @@ function onGoawayData(code, lastStreamID, buf) {
491
507
const session = this [ kOwner ] ;
492
508
if ( session . destroyed )
493
509
return ;
494
- debug ( `Http2Session ${ sessionName ( session [ kType ] ) } : goaway ${ code } ` +
495
- `received [last stream id: ${ lastStreamID } ]` ) ;
510
+ debugSessionObj ( session , ' goaway %d received [last stream id: %d]' ,
511
+ code , lastStreamID ) ;
496
512
497
513
const state = session [ kState ] ;
498
514
state . goawayCode = code ;
@@ -545,8 +561,7 @@ function requestOnConnect(headers, options) {
545
561
return ;
546
562
}
547
563
548
- debug ( `Http2Session ${ sessionName ( session [ kType ] ) } : connected, ` +
549
- 'initializing request' ) ;
564
+ debugSessionObj ( session , 'connected, initializing request' ) ;
550
565
551
566
let streamOptions = 0 ;
552
567
if ( options . endStream )
@@ -641,13 +656,13 @@ function settingsCallback(cb, ack, duration) {
641
656
this [ kState ] . pendingAck -- ;
642
657
this [ kLocalSettings ] = undefined ;
643
658
if ( ack ) {
644
- debug ( `Http2Session ${ sessionName ( this [ kType ] ) } : settings received` ) ;
659
+ debugSessionObj ( this , ' settings received' ) ;
645
660
const settings = this . localSettings ;
646
661
if ( typeof cb === 'function' )
647
662
cb ( null , settings , duration ) ;
648
663
this . emit ( 'localSettings' , settings ) ;
649
664
} else {
650
- debug ( `Http2Session ${ sessionName ( this [ kType ] ) } : settings canceled` ) ;
665
+ debugSessionObj ( this , ' settings canceled' ) ;
651
666
if ( typeof cb === 'function' )
652
667
cb ( new ERR_HTTP2_SETTINGS_CANCEL ( ) ) ;
653
668
}
@@ -657,7 +672,7 @@ function settingsCallback(cb, ack, duration) {
657
672
function submitSettings ( settings , callback ) {
658
673
if ( this . destroyed )
659
674
return ;
660
- debug ( `Http2Session ${ sessionName ( this [ kType ] ) } : submitting settings` ) ;
675
+ debugSessionObj ( this , ' submitting settings' ) ;
661
676
this [ kUpdateTimer ] ( ) ;
662
677
updateSettingsBuffer ( settings ) ;
663
678
if ( ! this [ kHandle ] . settings ( settingsCallback . bind ( this , callback ) ) ) {
@@ -691,7 +706,7 @@ function submitPriority(options) {
691
706
function submitGoaway ( code , lastStreamID , opaqueData ) {
692
707
if ( this . destroyed )
693
708
return ;
694
- debug ( `Http2Session ${ sessionName ( this [ kType ] ) } : submitting goaway` ) ;
709
+ debugSessionObj ( this , ' submitting goaway' ) ;
695
710
this [ kUpdateTimer ] ( ) ;
696
711
this [ kHandle ] . goaway ( code , lastStreamID , opaqueData ) ;
697
712
}
@@ -821,7 +836,9 @@ function setupHandle(socket, type, options) {
821
836
process . nextTick ( emit , this , 'connect' , this , socket ) ;
822
837
return ;
823
838
}
824
- debug ( `Http2Session ${ sessionName ( type ) } : setting up session handle` ) ;
839
+
840
+ debugSession ( type , 'setting up session handle' ) ;
841
+
825
842
this [ kState ] . flags |= SESSION_FLAGS_READY ;
826
843
827
844
updateOptionsBuffer ( options ) ;
@@ -983,7 +1000,7 @@ class Http2Session extends EventEmitter {
983
1000
setupFn ( ) ;
984
1001
}
985
1002
986
- debug ( `Http2Session ${ sessionName ( type ) } : created` ) ;
1003
+ debugSession ( type , ' created' ) ;
987
1004
}
988
1005
989
1006
// Returns undefined if the socket is not yet connected, true if the
@@ -1156,7 +1173,7 @@ class Http2Session extends EventEmitter {
1156
1173
1157
1174
if ( callback && typeof callback !== 'function' )
1158
1175
throw new ERR_INVALID_CALLBACK ( ) ;
1159
- debug ( `Http2Session ${ sessionName ( this [ kType ] ) } : sending settings` ) ;
1176
+ debugSessionObj ( this , ' sending settings' ) ;
1160
1177
1161
1178
this [ kState ] . pendingAck ++ ;
1162
1179
@@ -1197,7 +1214,7 @@ class Http2Session extends EventEmitter {
1197
1214
destroy ( error = NGHTTP2_NO_ERROR , code ) {
1198
1215
if ( this . destroyed )
1199
1216
return ;
1200
- debug ( `Http2Session ${ sessionName ( this [ kType ] ) } : destroying` ) ;
1217
+ debugSessionObj ( this , ' destroying' ) ;
1201
1218
1202
1219
if ( typeof error === 'number' ) {
1203
1220
code = error ;
@@ -1258,7 +1275,7 @@ class Http2Session extends EventEmitter {
1258
1275
close ( callback ) {
1259
1276
if ( this . closed || this . destroyed )
1260
1277
return ;
1261
- debug ( `Http2Session ${ sessionName ( this [ kType ] ) } : marking session closed` ) ;
1278
+ debugSessionObj ( this , ' marking session closed' ) ;
1262
1279
this [ kState ] . flags |= SESSION_FLAGS_CLOSED ;
1263
1280
if ( typeof callback === 'function' )
1264
1281
this . once ( 'close' , callback ) ;
@@ -1430,7 +1447,7 @@ class ClientHttp2Session extends Http2Session {
1430
1447
// Submits a new HTTP2 request to the connected peer. Returns the
1431
1448
// associated Http2Stream instance.
1432
1449
request ( headers , options ) {
1433
- debug ( `Http2Session ${ sessionName ( this [ kType ] ) } : initiating request` ) ;
1450
+ debugSessionObj ( this , ' initiating request' ) ;
1434
1451
1435
1452
if ( this . destroyed )
1436
1453
throw new ERR_HTTP2_INVALID_SESSION ( ) ;
@@ -1827,8 +1844,7 @@ class Http2Stream extends Duplex {
1827
1844
if ( this . pending ) {
1828
1845
this . once ( 'ready' , ( ) => this . _final ( cb ) ) ;
1829
1846
} else if ( handle !== undefined ) {
1830
- debug ( `Http2Stream ${ this [ kID ] } [Http2Session ` +
1831
- `${ sessionName ( this [ kSession ] [ kType ] ) } ]: _final shutting down` ) ;
1847
+ debugStreamObj ( this , '_final shutting down' ) ;
1832
1848
const req = new ShutdownWrap ( ) ;
1833
1849
req . oncomplete = afterShutdown ;
1834
1850
req . callback = cb ;
@@ -1887,9 +1903,7 @@ class Http2Stream extends Duplex {
1887
1903
assertIsObject ( headers , 'headers' ) ;
1888
1904
headers = Object . assign ( Object . create ( null ) , headers ) ;
1889
1905
1890
- const session = this [ kSession ] ;
1891
- debug ( `Http2Stream ${ this [ kID ] } [Http2Session ` +
1892
- `${ sessionName ( session [ kType ] ) } ]: sending trailers` ) ;
1906
+ debugStreamObj ( this , 'sending trailers' ) ;
1893
1907
1894
1908
this [ kUpdateTimer ] ( ) ;
1895
1909
@@ -1944,8 +1958,8 @@ class Http2Stream extends Duplex {
1944
1958
const handle = this [ kHandle ] ;
1945
1959
const id = this [ kID ] ;
1946
1960
1947
- debug ( `Http2Stream ${ this [ kID ] || '< pending>' } [Http2Session ` +
1948
- ` ${ sessionName ( session [ kType ] ) } ]: destroying stream` ) ;
1961
+ debugStream ( this [ kID ] || 'pending' , session [ kType ] , 'destroying stream' ) ;
1962
+
1949
1963
const state = this [ kState ] ;
1950
1964
const code = err != null ?
1951
1965
NGHTTP2_INTERNAL_ERROR : ( state . rstCode || NGHTTP2_NO_ERROR ) ;
@@ -2256,8 +2270,7 @@ class ServerHttp2Stream extends Http2Stream {
2256
2270
2257
2271
const session = this [ kSession ] ;
2258
2272
2259
- debug ( `Http2Stream ${ this [ kID ] } [Http2Session ` +
2260
- `${ sessionName ( session [ kType ] ) } ]: initiating push stream` ) ;
2273
+ debugStreamObj ( this , 'initiating push stream' ) ;
2261
2274
2262
2275
this [ kUpdateTimer ] ( ) ;
2263
2276
@@ -2339,9 +2352,7 @@ class ServerHttp2Stream extends Http2Stream {
2339
2352
assertIsObject ( options , 'options' ) ;
2340
2353
options = Object . assign ( { } , options ) ;
2341
2354
2342
- const session = this [ kSession ] ;
2343
- debug ( `Http2Stream ${ this [ kID ] } [Http2Session ` +
2344
- `${ sessionName ( session [ kType ] ) } ]: initiating response` ) ;
2355
+ debugStreamObj ( this , 'initiating response' ) ;
2345
2356
this [ kUpdateTimer ] ( ) ;
2346
2357
2347
2358
options . endStream = ! ! options . endStream ;
@@ -2420,8 +2431,7 @@ class ServerHttp2Stream extends Http2Stream {
2420
2431
2421
2432
validateNumber ( fd , 'fd' ) ;
2422
2433
2423
- debug ( `Http2Stream ${ this [ kID ] } [Http2Session ` +
2424
- `${ sessionName ( session [ kType ] ) } ]: initiating response from fd` ) ;
2434
+ debugStreamObj ( this , 'initiating response from fd' ) ;
2425
2435
this [ kUpdateTimer ] ( ) ;
2426
2436
this . ownsFd = false ;
2427
2437
@@ -2481,8 +2491,7 @@ class ServerHttp2Stream extends Http2Stream {
2481
2491
}
2482
2492
2483
2493
const session = this [ kSession ] ;
2484
- debug ( `Http2Stream ${ this [ kID ] } [Http2Session ` +
2485
- `${ sessionName ( session [ kType ] ) } ]: initiating response from file` ) ;
2494
+ debugStreamObj ( this , 'initiating response from file' ) ;
2486
2495
this [ kUpdateTimer ] ( ) ;
2487
2496
this . ownsFd = true ;
2488
2497
@@ -2515,9 +2524,7 @@ class ServerHttp2Stream extends Http2Stream {
2515
2524
assertIsObject ( headers , 'headers' ) ;
2516
2525
headers = Object . assign ( Object . create ( null ) , headers ) ;
2517
2526
2518
- const session = this [ kSession ] ;
2519
- debug ( `Http2Stream ${ this [ kID ] } [Http2Session ` +
2520
- `${ sessionName ( session [ kType ] ) } ]: sending additional headers` ) ;
2527
+ debugStreamObj ( this , 'sending additional headers' ) ;
2521
2528
2522
2529
if ( headers [ HTTP2_HEADER_STATUS ] != null ) {
2523
2530
const statusCode = headers [ HTTP2_HEADER_STATUS ] |= 0 ;
@@ -2608,8 +2615,7 @@ function socketOnError(error) {
2608
2615
// we can do and the other side is fully within its rights to do so.
2609
2616
if ( error . code === 'ECONNRESET' && session [ kState ] . goawayCode !== null )
2610
2617
return session . destroy ( ) ;
2611
- debug ( `Http2Session ${ sessionName ( session [ kType ] ) } : socket error [` +
2612
- `${ error . message } ]` ) ;
2618
+ debugSessionObj ( this , 'socket error [%s]' , error . message ) ;
2613
2619
session . destroy ( error ) ;
2614
2620
}
2615
2621
}
@@ -2654,7 +2660,8 @@ function connectionListener(socket) {
2654
2660
return httpConnectionListener . call ( this , socket ) ;
2655
2661
}
2656
2662
// Let event handler deal with the socket
2657
- debug ( `Unknown protocol from ${ socket . remoteAddress } :${ socket . remotePort } ` ) ;
2663
+ debug ( 'Unknown protocol from %s:%s' ,
2664
+ socket . remoteAddress , socket . remotePort ) ;
2658
2665
if ( ! this . emit ( 'unknownProtocol' , socket ) ) {
2659
2666
// We don't know what to do, so let's just tell the other side what's
2660
2667
// going on in a format that they *might* understand.
@@ -2779,7 +2786,7 @@ function setupCompat(ev) {
2779
2786
function socketOnClose ( ) {
2780
2787
const session = this [ kSession ] ;
2781
2788
if ( session !== undefined ) {
2782
- debug ( `Http2Session ${ sessionName ( session [ kType ] ) } : socket closed` ) ;
2789
+ debugSessionObj ( session , ' socket closed' ) ;
2783
2790
const err = session . connecting ? new ERR_SOCKET_CLOSED ( ) : null ;
2784
2791
const state = session [ kState ] ;
2785
2792
state . streams . forEach ( ( stream ) => stream . close ( NGHTTP2_CANCEL ) ) ;
0 commit comments