@@ -64,6 +64,26 @@ const { createPromise, promiseResolve } = process.binding('util');
64
64
const { _connectionListener : httpConnectionListener } = http ;
65
65
const debug = util . debuglog ( 'http2' ) ;
66
66
67
+ // TODO(addaleax): See if this can be made more efficient by figuring out
68
+ // whether debugging is enabled before we perform any further steps. Currently,
69
+ // this seems pretty fast, though.
70
+ function debugStream ( id , sessionType , message , ...args ) {
71
+ debug ( 'Http2Stream %s [Http2Session %s]: ' + message ,
72
+ id , sessionName ( sessionType ) , ...args ) ;
73
+ }
74
+
75
+ function debugStreamObj ( stream , message , ...args ) {
76
+ debugStream ( stream [ kID ] , stream [ kSession ] [ kType ] , ...args ) ;
77
+ }
78
+
79
+ function debugSession ( sessionType , message , ...args ) {
80
+ debug ( 'Http2Session %s: ' + message , sessionName ( sessionType ) , ...args ) ;
81
+ }
82
+
83
+ function debugSessionObj ( session , message , ...args ) {
84
+ debugSession ( session [ kType ] , message , ...args ) ;
85
+ }
86
+
67
87
const kMaxFrameSize = ( 2 ** 24 ) - 1 ;
68
88
const kMaxInt = ( 2 ** 32 ) - 1 ;
69
89
const kMaxStreams = ( 2 ** 31 ) - 1 ;
@@ -189,8 +209,7 @@ function onSessionHeaders(handle, id, cat, flags, headers) {
189
209
190
210
const type = session [ kType ] ;
191
211
session [ kUpdateTimer ] ( ) ;
192
- debug ( `Http2Stream ${ id } [Http2Session ` +
193
- `${ sessionName ( type ) } ]: headers received` ) ;
212
+ debugStream ( id , type , 'headers received' ) ;
194
213
const streams = session [ kState ] . streams ;
195
214
196
215
const endOfStream = ! ! ( flags & NGHTTP2_FLAG_END_STREAM ) ;
@@ -250,8 +269,7 @@ function onSessionHeaders(handle, id, cat, flags, headers) {
250
269
const originSet = session [ kState ] . originSet = initOriginSet ( session ) ;
251
270
originSet . delete ( stream [ kOrigin ] ) ;
252
271
}
253
- debug ( `Http2Stream ${ id } [Http2Session ` +
254
- `${ sessionName ( type ) } ]: emitting stream '${ event } ' event` ) ;
272
+ debugStream ( id , type , "emitting stream '%s' event" , event ) ;
255
273
process . nextTick ( emit , stream , event , obj , flags , headers ) ;
256
274
}
257
275
if ( endOfStream ) {
@@ -292,7 +310,7 @@ function onPing(payload) {
292
310
if ( session . destroyed )
293
311
return ;
294
312
session [ kUpdateTimer ] ( ) ;
295
- debug ( `Http2Session ${ sessionName ( session [ kType ] ) } : new ping received` ) ;
313
+ debugSessionObj ( session , ' new ping received' ) ;
296
314
session . emit ( 'ping' , payload ) ;
297
315
}
298
316
@@ -307,8 +325,7 @@ function onStreamClose(code) {
307
325
if ( stream . destroyed )
308
326
return ;
309
327
310
- debug ( `Http2Stream ${ stream [ kID ] } [Http2Session ` +
311
- `${ sessionName ( stream [ kSession ] [ kType ] ) } ]: closed with code ${ code } ` ) ;
328
+ debugStreamObj ( stream , 'closed with code %d' , code ) ;
312
329
313
330
if ( ! stream . closed )
314
331
closeStream ( stream , code , kNoRstStream ) ;
@@ -376,7 +393,7 @@ function onSettings() {
376
393
if ( session . destroyed )
377
394
return ;
378
395
session [ kUpdateTimer ] ( ) ;
379
- debug ( `Http2Session ${ sessionName ( session [ kType ] ) } : new settings received` ) ;
396
+ debugSessionObj ( session , ' new settings received' ) ;
380
397
session [ kRemoteSettings ] = undefined ;
381
398
session . emit ( 'remoteSettings' , session . remoteSettings ) ;
382
399
}
@@ -388,9 +405,9 @@ function onPriority(id, parent, weight, exclusive) {
388
405
const session = this [ kOwner ] ;
389
406
if ( session . destroyed )
390
407
return ;
391
- debug ( `Http2Stream ${ id } [Http2Session ` +
392
- ` ${ sessionName ( session [ kType ] ) } ]: priority [parent: ${ parent } , ` +
393
- `weight: ${ weight } , exclusive: ${ exclusive } ]` ) ;
408
+ debugStream ( id , session [ kType ] ,
409
+ ' priority [parent: %d, weight: %d, exclusive: %s]' ,
410
+ parent , weight , exclusive ) ;
394
411
const emitter = session [ kState ] . streams . get ( id ) || session ;
395
412
if ( ! emitter . destroyed ) {
396
413
emitter [ kUpdateTimer ] ( ) ;
@@ -404,8 +421,8 @@ function onFrameError(id, type, code) {
404
421
const session = this [ kOwner ] ;
405
422
if ( session . destroyed )
406
423
return ;
407
- debug ( `Http2Session ${ sessionName ( session [ kType ] ) } : error sending frame ` +
408
- `type ${ type } on stream ${ id } , code: ${ code } ` ) ;
424
+ debugSessionObj ( session , ' error sending frame type %d on stream %d, code: %d' ,
425
+ type , id , code ) ;
409
426
const emitter = session [ kState ] . streams . get ( id ) || session ;
410
427
emitter [ kUpdateTimer ] ( ) ;
411
428
emitter . emit ( 'frameError' , type , code , id ) ;
@@ -415,8 +432,8 @@ function onAltSvc(stream, origin, alt) {
415
432
const session = this [ kOwner ] ;
416
433
if ( session . destroyed )
417
434
return ;
418
- debug ( `Http2Session ${ sessionName ( session [ kType ] ) } : altsvc received: ` +
419
- `stream: ${ stream } , origin: ${ origin } , alt: ${ alt } ` ) ;
435
+ debugSessionObj ( session , ' altsvc received: stream: %d, origin: %s, alt: %s' ,
436
+ stream , origin , alt ) ;
420
437
session [ kUpdateTimer ] ( ) ;
421
438
session . emit ( 'altsvc' , alt , origin , stream ) ;
422
439
}
@@ -443,8 +460,7 @@ function onOrigin(origins) {
443
460
const session = this [ kOwner ] ;
444
461
if ( session . destroyed )
445
462
return ;
446
- debug ( `Http2Session ${ sessionName ( session [ kType ] ) } : origin received: ` +
447
- `${ origins . join ( ', ' ) } ` ) ;
463
+ debugSessionObj ( session , 'origin received: %j' , origins ) ;
448
464
session [ kUpdateTimer ] ( ) ;
449
465
if ( ! session . encrypted || session . destroyed )
450
466
return undefined ;
@@ -464,8 +480,8 @@ function onGoawayData(code, lastStreamID, buf) {
464
480
const session = this [ kOwner ] ;
465
481
if ( session . destroyed )
466
482
return ;
467
- debug ( `Http2Session ${ sessionName ( session [ kType ] ) } : goaway ${ code } ` +
468
- `received [last stream id: ${ lastStreamID } ]` ) ;
483
+ debugSessionObj ( session , ' goaway %d received [last stream id: %d]' ,
484
+ code , lastStreamID ) ;
469
485
470
486
const state = session [ kState ] ;
471
487
state . goawayCode = code ;
@@ -519,8 +535,7 @@ function requestOnConnect(headers, options) {
519
535
return ;
520
536
}
521
537
522
- debug ( `Http2Session ${ sessionName ( session [ kType ] ) } : connected, ` +
523
- 'initializing request' ) ;
538
+ debugSessionObj ( session , 'connected, initializing request' ) ;
524
539
525
540
let streamOptions = 0 ;
526
541
if ( options . endStream )
@@ -623,13 +638,13 @@ function settingsCallback(cb, ack, duration) {
623
638
this [ kState ] . pendingAck -- ;
624
639
this [ kLocalSettings ] = undefined ;
625
640
if ( ack ) {
626
- debug ( `Http2Session ${ sessionName ( this [ kType ] ) } : settings received` ) ;
641
+ debugSessionObj ( this , ' settings received' ) ;
627
642
const settings = this . localSettings ;
628
643
if ( typeof cb === 'function' )
629
644
cb ( null , settings , duration ) ;
630
645
this . emit ( 'localSettings' , settings ) ;
631
646
} else {
632
- debug ( `Http2Session ${ sessionName ( this [ kType ] ) } : settings canceled` ) ;
647
+ debugSessionObj ( this , ' settings canceled' ) ;
633
648
if ( typeof cb === 'function' )
634
649
cb ( new errors . Error ( 'ERR_HTTP2_SETTINGS_CANCEL' ) ) ;
635
650
}
@@ -639,7 +654,7 @@ function settingsCallback(cb, ack, duration) {
639
654
function submitSettings ( settings , callback ) {
640
655
if ( this . destroyed )
641
656
return ;
642
- debug ( `Http2Session ${ sessionName ( this [ kType ] ) } : submitting settings` ) ;
657
+ debugSessionObj ( this , ' submitting settings' ) ;
643
658
this [ kUpdateTimer ] ( ) ;
644
659
updateSettingsBuffer ( settings ) ;
645
660
if ( ! this [ kHandle ] . settings ( settingsCallback . bind ( this , callback ) ) ) {
@@ -673,7 +688,7 @@ function submitPriority(options) {
673
688
function submitGoaway ( code , lastStreamID , opaqueData ) {
674
689
if ( this . destroyed )
675
690
return ;
676
- debug ( `Http2Session ${ sessionName ( this [ kType ] ) } : submitting goaway` ) ;
691
+ debugSessionObj ( this , ' submitting goaway' ) ;
677
692
this [ kUpdateTimer ] ( ) ;
678
693
this [ kHandle ] . goaway ( code , lastStreamID , opaqueData ) ;
679
694
}
@@ -803,7 +818,9 @@ function setupHandle(socket, type, options) {
803
818
process . nextTick ( emit , this , 'connect' , this , socket ) ;
804
819
return ;
805
820
}
806
- debug ( `Http2Session ${ sessionName ( type ) } : setting up session handle` ) ;
821
+
822
+ debugSession ( type , 'setting up session handle' ) ;
823
+
807
824
this [ kState ] . flags |= SESSION_FLAGS_READY ;
808
825
809
826
updateOptionsBuffer ( options ) ;
@@ -964,7 +981,7 @@ class Http2Session extends EventEmitter {
964
981
setupFn ( ) ;
965
982
}
966
983
967
- debug ( `Http2Session ${ sessionName ( type ) } : created` ) ;
984
+ debugSession ( type , ' created' ) ;
968
985
}
969
986
970
987
// Returns undefined if the socket is not yet connected, true if the
@@ -1138,7 +1155,7 @@ class Http2Session extends EventEmitter {
1138
1155
1139
1156
if ( callback && typeof callback !== 'function' )
1140
1157
throw new errors . TypeError ( 'ERR_INVALID_CALLBACK' ) ;
1141
- debug ( `Http2Session ${ sessionName ( this [ kType ] ) } : sending settings` ) ;
1158
+ debugSessionObj ( this , ' sending settings' ) ;
1142
1159
1143
1160
this [ kState ] . pendingAck ++ ;
1144
1161
@@ -1184,7 +1201,7 @@ class Http2Session extends EventEmitter {
1184
1201
destroy ( error = NGHTTP2_NO_ERROR , code ) {
1185
1202
if ( this . destroyed )
1186
1203
return ;
1187
- debug ( `Http2Session ${ sessionName ( this [ kType ] ) } : destroying` ) ;
1204
+ debugSessionObj ( this , ' destroying' ) ;
1188
1205
1189
1206
if ( typeof error === 'number' ) {
1190
1207
code = error ;
@@ -1245,7 +1262,7 @@ class Http2Session extends EventEmitter {
1245
1262
close ( callback ) {
1246
1263
if ( this . closed || this . destroyed )
1247
1264
return ;
1248
- debug ( `Http2Session ${ sessionName ( this [ kType ] ) } : marking session closed` ) ;
1265
+ debugSessionObj ( this , ' marking session closed' ) ;
1249
1266
this [ kState ] . flags |= SESSION_FLAGS_CLOSED ;
1250
1267
if ( typeof callback === 'function' )
1251
1268
this . once ( 'close' , callback ) ;
@@ -1415,7 +1432,7 @@ class ClientHttp2Session extends Http2Session {
1415
1432
// Submits a new HTTP2 request to the connected peer. Returns the
1416
1433
// associated Http2Stream instance.
1417
1434
request ( headers , options ) {
1418
- debug ( `Http2Session ${ sessionName ( this [ kType ] ) } : initiating request` ) ;
1435
+ debugSessionObj ( this , ' initiating request' ) ;
1419
1436
1420
1437
if ( this . destroyed )
1421
1438
throw new errors . Error ( 'ERR_HTTP2_INVALID_SESSION' ) ;
@@ -1843,8 +1860,7 @@ class Http2Stream extends Duplex {
1843
1860
if ( this [ kID ] === undefined ) {
1844
1861
this . once ( 'ready' , ( ) => this . _final ( cb ) ) ;
1845
1862
} else if ( handle !== undefined ) {
1846
- debug ( `Http2Stream ${ this [ kID ] } [Http2Session ` +
1847
- `${ sessionName ( this [ kSession ] [ kType ] ) } ]: _final shutting down` ) ;
1863
+ debugStreamObj ( this , '_final shutting down' ) ;
1848
1864
const req = new ShutdownWrap ( ) ;
1849
1865
req . oncomplete = afterShutdown ;
1850
1866
req . callback = cb ;
@@ -1901,9 +1917,7 @@ class Http2Stream extends Duplex {
1901
1917
assertIsObject ( headers , 'headers' ) ;
1902
1918
headers = Object . assign ( Object . create ( null ) , headers ) ;
1903
1919
1904
- const session = this [ kSession ] ;
1905
- debug ( `Http2Stream ${ this [ kID ] } [Http2Session ` +
1906
- `${ sessionName ( session [ kType ] ) } ]: sending trailers` ) ;
1920
+ debugStreamObj ( this , 'sending trailers' ) ;
1907
1921
1908
1922
this [ kUpdateTimer ] ( ) ;
1909
1923
@@ -1959,8 +1973,8 @@ class Http2Stream extends Duplex {
1959
1973
const handle = this [ kHandle ] ;
1960
1974
const id = this [ kID ] ;
1961
1975
1962
- debug ( `Http2Stream ${ this [ kID ] || '< pending>' } [Http2Session ` +
1963
- ` ${ sessionName ( session [ kType ] ) } ]: destroying stream` ) ;
1976
+ debugStream ( this [ kID ] || 'pending' , session [ kType ] , 'destroying stream' ) ;
1977
+
1964
1978
const state = this [ kState ] ;
1965
1979
const code = err != null ?
1966
1980
NGHTTP2_INTERNAL_ERROR : ( state . rstCode || NGHTTP2_NO_ERROR ) ;
@@ -2232,8 +2246,7 @@ class ServerHttp2Stream extends Http2Stream {
2232
2246
2233
2247
const session = this [ kSession ] ;
2234
2248
2235
- debug ( `Http2Stream ${ this [ kID ] } [Http2Session ` +
2236
- `${ sessionName ( session [ kType ] ) } ]: initiating push stream` ) ;
2249
+ debugStreamObj ( this , 'initiating push stream' ) ;
2237
2250
2238
2251
this [ kUpdateTimer ] ( ) ;
2239
2252
@@ -2315,9 +2328,7 @@ class ServerHttp2Stream extends Http2Stream {
2315
2328
assertIsObject ( options , 'options' ) ;
2316
2329
options = Object . assign ( { } , options ) ;
2317
2330
2318
- const session = this [ kSession ] ;
2319
- debug ( `Http2Stream ${ this [ kID ] } [Http2Session ` +
2320
- `${ sessionName ( session [ kType ] ) } ]: initiating response` ) ;
2331
+ debugStreamObj ( this , 'initiating response' ) ;
2321
2332
this [ kUpdateTimer ] ( ) ;
2322
2333
2323
2334
options . endStream = ! ! options . endStream ;
@@ -2404,8 +2415,7 @@ class ServerHttp2Stream extends Http2Stream {
2404
2415
throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' ,
2405
2416
'fd' , 'number' ) ;
2406
2417
2407
- debug ( `Http2Stream ${ this [ kID ] } [Http2Session ` +
2408
- `${ sessionName ( session [ kType ] ) } ]: initiating response` ) ;
2418
+ debugStreamObj ( this , 'initiating response from fd' ) ;
2409
2419
this [ kUpdateTimer ] ( ) ;
2410
2420
2411
2421
headers = processHeaders ( headers ) ;
@@ -2470,8 +2480,7 @@ class ServerHttp2Stream extends Http2Stream {
2470
2480
}
2471
2481
2472
2482
const session = this [ kSession ] ;
2473
- debug ( `Http2Stream ${ this [ kID ] } [Http2Session ` +
2474
- `${ sessionName ( session [ kType ] ) } ]: initiating response` ) ;
2483
+ debugStreamObj ( this , 'initiating response from file' ) ;
2475
2484
this [ kUpdateTimer ] ( ) ;
2476
2485
2477
2486
@@ -2504,9 +2513,7 @@ class ServerHttp2Stream extends Http2Stream {
2504
2513
assertIsObject ( headers , 'headers' ) ;
2505
2514
headers = Object . assign ( Object . create ( null ) , headers ) ;
2506
2515
2507
- const session = this [ kSession ] ;
2508
- debug ( `Http2Stream ${ this [ kID ] } [Http2Session ` +
2509
- `${ sessionName ( session [ kType ] ) } ]: sending additional headers` ) ;
2516
+ debugStreamObj ( this , 'sending additional headers' ) ;
2510
2517
2511
2518
if ( headers [ HTTP2_HEADER_STATUS ] != null ) {
2512
2519
const statusCode = headers [ HTTP2_HEADER_STATUS ] |= 0 ;
@@ -2595,8 +2602,7 @@ function socketOnError(error) {
2595
2602
// we can do and the other side is fully within its rights to do so.
2596
2603
if ( error . code === 'ECONNRESET' && session [ kState ] . goawayCode !== null )
2597
2604
return session . destroy ( ) ;
2598
- debug ( `Http2Session ${ sessionName ( session [ kType ] ) } : socket error [` +
2599
- `${ error . message } ]` ) ;
2605
+ debugSessionObj ( this , 'socket error [%s]' , error . message ) ;
2600
2606
session . destroy ( error ) ;
2601
2607
}
2602
2608
}
@@ -2641,7 +2647,8 @@ function connectionListener(socket) {
2641
2647
return httpConnectionListener . call ( this , socket ) ;
2642
2648
}
2643
2649
// Let event handler deal with the socket
2644
- debug ( `Unknown protocol from ${ socket . remoteAddress } :${ socket . remotePort } ` ) ;
2650
+ debug ( 'Unknown protocol from %s:%s' ,
2651
+ socket . remoteAddress , socket . remotePort ) ;
2645
2652
if ( ! this . emit ( 'unknownProtocol' , socket ) ) {
2646
2653
// We don't know what to do, so let's just tell the other side what's
2647
2654
// going on in a format that they *might* understand.
@@ -2766,7 +2773,7 @@ function setupCompat(ev) {
2766
2773
function socketOnClose ( ) {
2767
2774
const session = this [ kSession ] ;
2768
2775
if ( session !== undefined ) {
2769
- debug ( `Http2Session ${ sessionName ( session [ kType ] ) } : socket closed` ) ;
2776
+ debugSessionObj ( session , ' socket closed' ) ;
2770
2777
const err = session . connecting ?
2771
2778
new errors . Error ( 'ERR_SOCKET_CLOSED' ) : null ;
2772
2779
const state = session [ kState ] ;
0 commit comments