@@ -306,18 +306,66 @@ ServerResponse.prototype.detachSocket = function detachSocket(socket) {
306306 this . socket = null ;
307307} ;
308308
309+ ServerResponse . prototype . writeInformation = function writeInformation (
310+ statusCode , headers , cb ) {
311+ if ( this . _header ) {
312+ throw new ERR_HTTP_HEADERS_SENT ( 'write' ) ;
313+ }
314+
315+ validateInteger ( statusCode , 'statusCode' , 100 , 199 ) ;
316+ if ( statusCode === 101 ) {
317+ throw new ERR_HTTP_INVALID_STATUS_CODE ( statusCode ) ;
318+ }
319+
320+ const statusMessage = STATUS_CODES [ statusCode ] || 'unknown' ;
321+ let head = `HTTP/1.1 ${ statusCode } ${ statusMessage } \r\n` ;
322+
323+ if ( headers !== undefined && headers !== null ) {
324+ if ( ArrayIsArray ( headers ) ) {
325+ if ( headers . length && ArrayIsArray ( headers [ 0 ] ) ) {
326+ for ( let i = 0 ; i < headers . length ; i ++ ) {
327+ const entry = headers [ i ] ;
328+ head += processInformationHeader ( entry [ 0 ] , entry [ 1 ] ) ;
329+ }
330+ } else {
331+ if ( headers . length % 2 !== 0 ) {
332+ throw new ERR_INVALID_ARG_VALUE ( 'headers' , headers ) ;
333+ }
334+ for ( let i = 0 ; i < headers . length ; i += 2 ) {
335+ head += processInformationHeader ( headers [ i ] , headers [ i + 1 ] ) ;
336+ }
337+ }
338+ } else {
339+ validateObject ( headers , 'headers' ) ;
340+ const keys = ObjectKeys ( headers ) ;
341+ for ( let i = 0 ; i < keys . length ; i ++ ) {
342+ const key = keys [ i ] ;
343+ head += processInformationHeader ( key , headers [ key ] ) ;
344+ }
345+ }
346+ }
347+
348+ head += '\r\n' ;
349+
350+ return this . _writeRaw ( head , 'ascii' , cb ) ;
351+ } ;
352+
353+ function processInformationHeader ( name , value ) {
354+ validateHeaderName ( name ) ;
355+ validateHeaderValue ( name , value ) ;
356+ return `${ name } : ${ value } \r\n` ;
357+ }
358+
309359ServerResponse . prototype . writeContinue = function writeContinue ( cb ) {
310- this . _writeRaw ( 'HTTP/1.1 100 Continue\r\n\r\n' , 'ascii' , cb ) ;
360+ this . writeInformation ( 100 , null , cb ) ;
311361 this . _sent100 = true ;
312362} ;
313363
314364ServerResponse . prototype . writeProcessing = function writeProcessing ( cb ) {
315- this . _writeRaw ( 'HTTP/1.1 102 Processing\r\n\r\n' , 'ascii' , cb ) ;
365+ this . writeInformation ( 102 , null , cb ) ;
316366} ;
317367
318368ServerResponse . prototype . writeEarlyHints = function writeEarlyHints ( hints , cb ) {
319- let head = 'HTTP/1.1 103 Early Hints\r\n' ;
320-
321369 validateObject ( hints , 'hints' ) ;
322370
323371 if ( hints . link === null || hints . link === undefined ) {
@@ -334,22 +382,16 @@ ServerResponse.prototype.writeEarlyHints = function writeEarlyHints(hints, cb) {
334382 throw new ERR_INVALID_CHAR ( 'header content' , 'Link' ) ;
335383 }
336384
337- head += 'Link: ' + link + '\r\n' ;
338-
385+ const headers = { __proto__ : null , Link : link } ;
339386 const keys = ObjectKeys ( hints ) ;
340387 for ( let i = 0 ; i < keys . length ; i ++ ) {
341388 const key = keys [ i ] ;
342389 if ( key !== 'link' ) {
343- validateHeaderName ( key ) ;
344- const value = hints [ key ] ;
345- validateHeaderValue ( key , value ) ;
346- head += key + ': ' + value + '\r\n' ;
390+ headers [ key ] = hints [ key ] ;
347391 }
348392 }
349393
350- head += '\r\n' ;
351-
352- this . _writeRaw ( head , 'ascii' , cb ) ;
394+ this . writeInformation ( 103 , headers , cb ) ;
353395} ;
354396
355397ServerResponse . prototype . _implicitHeader = function _implicitHeader ( ) {
0 commit comments