@@ -311,18 +311,66 @@ ServerResponse.prototype.detachSocket = function detachSocket(socket) {
311311 this . socket = null ;
312312} ;
313313
314+ ServerResponse . prototype . writeInformation = function writeInformation (
315+ statusCode , headers , cb ) {
316+ if ( this . _header ) {
317+ throw new ERR_HTTP_HEADERS_SENT ( 'write' ) ;
318+ }
319+
320+ validateInteger ( statusCode , 'statusCode' , 100 , 199 ) ;
321+ if ( statusCode === 101 ) {
322+ throw new ERR_HTTP_INVALID_STATUS_CODE ( statusCode ) ;
323+ }
324+
325+ const statusMessage = STATUS_CODES [ statusCode ] || 'unknown' ;
326+ let head = `HTTP/1.1 ${ statusCode } ${ statusMessage } \r\n` ;
327+
328+ if ( headers !== undefined && headers !== null ) {
329+ if ( ArrayIsArray ( headers ) ) {
330+ if ( headers . length && ArrayIsArray ( headers [ 0 ] ) ) {
331+ for ( let i = 0 ; i < headers . length ; i ++ ) {
332+ const entry = headers [ i ] ;
333+ head += processInformationHeader ( entry [ 0 ] , entry [ 1 ] ) ;
334+ }
335+ } else {
336+ if ( headers . length % 2 !== 0 ) {
337+ throw new ERR_INVALID_ARG_VALUE ( 'headers' , headers ) ;
338+ }
339+ for ( let i = 0 ; i < headers . length ; i += 2 ) {
340+ head += processInformationHeader ( headers [ i ] , headers [ i + 1 ] ) ;
341+ }
342+ }
343+ } else {
344+ validateObject ( headers , 'headers' ) ;
345+ const keys = ObjectKeys ( headers ) ;
346+ for ( let i = 0 ; i < keys . length ; i ++ ) {
347+ const key = keys [ i ] ;
348+ head += processInformationHeader ( key , headers [ key ] ) ;
349+ }
350+ }
351+ }
352+
353+ head += '\r\n' ;
354+
355+ return this . _writeRaw ( head , 'ascii' , cb ) ;
356+ } ;
357+
358+ function processInformationHeader ( name , value ) {
359+ validateHeaderName ( name ) ;
360+ validateHeaderValue ( name , value ) ;
361+ return `${ name } : ${ value } \r\n` ;
362+ }
363+
314364ServerResponse . prototype . writeContinue = function writeContinue ( cb ) {
315- this . _writeRaw ( 'HTTP/1.1 100 Continue\r\n\r\n' , 'ascii' , cb ) ;
365+ this . writeInformation ( 100 , null , cb ) ;
316366 this . _sent100 = true ;
317367} ;
318368
319369ServerResponse . prototype . writeProcessing = function writeProcessing ( cb ) {
320- this . _writeRaw ( 'HTTP/1.1 102 Processing\r\n\r\n' , 'ascii' , cb ) ;
370+ this . writeInformation ( 102 , null , cb ) ;
321371} ;
322372
323373ServerResponse . prototype . writeEarlyHints = function writeEarlyHints ( hints , cb ) {
324- let head = 'HTTP/1.1 103 Early Hints\r\n' ;
325-
326374 validateObject ( hints , 'hints' ) ;
327375
328376 if ( hints . link === null || hints . link === undefined ) {
@@ -339,22 +387,16 @@ ServerResponse.prototype.writeEarlyHints = function writeEarlyHints(hints, cb) {
339387 throw new ERR_INVALID_CHAR ( 'header content' , 'Link' ) ;
340388 }
341389
342- head += 'Link: ' + link + '\r\n' ;
343-
390+ const headers = { __proto__ : null , Link : link } ;
344391 const keys = ObjectKeys ( hints ) ;
345392 for ( let i = 0 ; i < keys . length ; i ++ ) {
346393 const key = keys [ i ] ;
347394 if ( key !== 'link' ) {
348- validateHeaderName ( key ) ;
349- const value = hints [ key ] ;
350- validateHeaderValue ( key , value ) ;
351- head += key + ': ' + value + '\r\n' ;
395+ headers [ key ] = hints [ key ] ;
352396 }
353397 }
354398
355- head += '\r\n' ;
356-
357- this . _writeRaw ( head , 'ascii' , cb ) ;
399+ this . writeInformation ( 103 , headers , cb ) ;
358400} ;
359401
360402ServerResponse . prototype . _implicitHeader = function _implicitHeader ( ) {
0 commit comments