@@ -308,18 +308,66 @@ ServerResponse.prototype.detachSocket = function detachSocket(socket) {
308308 this . socket = null ;
309309} ;
310310
311+ ServerResponse . prototype . writeInformation = function writeInformation (
312+ statusCode , headers , cb ) {
313+ if ( this . _header ) {
314+ throw new ERR_HTTP_HEADERS_SENT ( 'write' ) ;
315+ }
316+
317+ validateInteger ( statusCode , 'statusCode' , 100 , 199 ) ;
318+ if ( statusCode === 101 ) {
319+ throw new ERR_HTTP_INVALID_STATUS_CODE ( statusCode ) ;
320+ }
321+
322+ const statusMessage = STATUS_CODES [ statusCode ] || 'unknown' ;
323+ let head = `HTTP/1.1 ${ statusCode } ${ statusMessage } \r\n` ;
324+
325+ if ( headers !== undefined && headers !== null ) {
326+ if ( ArrayIsArray ( headers ) ) {
327+ if ( headers . length && ArrayIsArray ( headers [ 0 ] ) ) {
328+ for ( let i = 0 ; i < headers . length ; i ++ ) {
329+ const entry = headers [ i ] ;
330+ head += processInformationHeader ( entry [ 0 ] , entry [ 1 ] ) ;
331+ }
332+ } else {
333+ if ( headers . length % 2 !== 0 ) {
334+ throw new ERR_INVALID_ARG_VALUE ( 'headers' , headers ) ;
335+ }
336+ for ( let i = 0 ; i < headers . length ; i += 2 ) {
337+ head += processInformationHeader ( headers [ i ] , headers [ i + 1 ] ) ;
338+ }
339+ }
340+ } else {
341+ validateObject ( headers , 'headers' ) ;
342+ const keys = ObjectKeys ( headers ) ;
343+ for ( let i = 0 ; i < keys . length ; i ++ ) {
344+ const key = keys [ i ] ;
345+ head += processInformationHeader ( key , headers [ key ] ) ;
346+ }
347+ }
348+ }
349+
350+ head += '\r\n' ;
351+
352+ return this . _writeRaw ( head , 'ascii' , cb ) ;
353+ } ;
354+
355+ function processInformationHeader ( name , value ) {
356+ validateHeaderName ( name ) ;
357+ validateHeaderValue ( name , value ) ;
358+ return `${ name } : ${ value } \r\n` ;
359+ }
360+
311361ServerResponse . prototype . writeContinue = function writeContinue ( cb ) {
312- this . _writeRaw ( 'HTTP/1.1 100 Continue\r\n\r\n' , 'ascii' , cb ) ;
362+ this . writeInformation ( 100 , null , cb ) ;
313363 this . _sent100 = true ;
314364} ;
315365
316366ServerResponse . prototype . writeProcessing = function writeProcessing ( cb ) {
317- this . _writeRaw ( 'HTTP/1.1 102 Processing\r\n\r\n' , 'ascii' , cb ) ;
367+ this . writeInformation ( 102 , null , cb ) ;
318368} ;
319369
320370ServerResponse . prototype . writeEarlyHints = function writeEarlyHints ( hints , cb ) {
321- let head = 'HTTP/1.1 103 Early Hints\r\n' ;
322-
323371 validateObject ( hints , 'hints' ) ;
324372
325373 if ( hints . link === null || hints . link === undefined ) {
@@ -336,22 +384,16 @@ ServerResponse.prototype.writeEarlyHints = function writeEarlyHints(hints, cb) {
336384 throw new ERR_INVALID_CHAR ( 'header content' , 'Link' ) ;
337385 }
338386
339- head += 'Link: ' + link + '\r\n' ;
340-
387+ const headers = { __proto__ : null , Link : link } ;
341388 const keys = ObjectKeys ( hints ) ;
342389 for ( let i = 0 ; i < keys . length ; i ++ ) {
343390 const key = keys [ i ] ;
344391 if ( key !== 'link' ) {
345- validateHeaderName ( key ) ;
346- const value = hints [ key ] ;
347- validateHeaderValue ( key , value ) ;
348- head += key + ': ' + value + '\r\n' ;
392+ headers [ key ] = hints [ key ] ;
349393 }
350394 }
351395
352- head += '\r\n' ;
353-
354- this . _writeRaw ( head , 'ascii' , cb ) ;
396+ this . writeInformation ( 103 , headers , cb ) ;
355397} ;
356398
357399ServerResponse . prototype . _implicitHeader = function _implicitHeader ( ) {
0 commit comments