Skip to content

Commit

Permalink
http: remove CRLF variable
Browse files Browse the repository at this point in the history
  • Loading branch information
shfshanyue committed Sep 14, 2021
1 parent ed556c0 commit a624067
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 26 deletions.
1 change: 0 additions & 1 deletion lib/_http_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,6 @@ module.exports = {
_checkIsHttpToken: checkIsHttpToken,
chunkExpression: /(?:^|\W)chunked(?:$|\W)/i,
continueExpression: /(?:^|\W)100-continue(?:$|\W)/i,
CRLF: '\r\n',
freeParser,
methods,
parsers,
Expand Down
30 changes: 15 additions & 15 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ const Stream = require('stream');
const internalUtil = require('internal/util');
const { kOutHeaders, utcDate, kNeedDrain } = require('internal/http');
const { Buffer } = require('buffer');
const common = require('_http_common');
const checkIsHttpToken = common._checkIsHttpToken;
const checkInvalidHeaderChar = common._checkInvalidHeaderChar;
const {
_checkIsHttpToken: checkIsHttpToken,
_checkInvalidHeaderChar: checkInvalidHeaderChar,
chunkExpression: RE_TE_CHUNKED,
} = require('_http_common');
const {
defaultTriggerAsyncIdScope,
symbols: { async_id_symbol }
Expand Down Expand Up @@ -78,14 +80,12 @@ let debug = require('internal/util/debuglog').debuglog('http', (fn) => {
});

const HIGH_WATER_MARK = getDefaultHighWaterMark();
const { CRLF } = common;

const kCorked = Symbol('corked');

const nop = () => {};

const RE_CONN_CLOSE = /(?:^|\W)close(?:$|\W)/i;
const RE_TE_CHUNKED = common.chunkExpression;

// isCookieField performs a case-insensitive comparison of a provided string
// against the word "cookie." As of V8 6.6 this is faster than handrolling or
Expand Down Expand Up @@ -417,7 +417,7 @@ function _storeHeader(firstLine, headers) {

// Date header
if (this.sendDate && !state.date) {
header += 'Date: ' + utcDate() + CRLF;
header += 'Date: ' + utcDate() + '\r\n';
}

// Force the connection to close when the response is a 204 No Content or
Expand Down Expand Up @@ -447,14 +447,14 @@ function _storeHeader(firstLine, headers) {
const shouldSendKeepAlive = this.shouldKeepAlive &&
(state.contLen || this.useChunkedEncodingByDefault || this.agent);
if (shouldSendKeepAlive) {
header += 'Connection: keep-alive' + CRLF;
header += 'Connection: keep-alive\r\n';
if (this._keepAliveTimeout && this._defaultKeepAlive) {
const timeoutSeconds = MathFloor(this._keepAliveTimeout / 1000);
header += `Keep-Alive: timeout=${timeoutSeconds}${CRLF}`;
header += `Keep-Alive: timeout=${timeoutSeconds}\r\n`;
}
} else {
this._last = true;
header += 'Connection: close' + CRLF;
header += 'Connection: close\r\n';
}
}

Expand All @@ -467,9 +467,9 @@ function _storeHeader(firstLine, headers) {
} else if (!state.trailer &&
!this._removedContLen &&
typeof this._contentLength === 'number') {
header += 'Content-Length: ' + this._contentLength + CRLF;
header += 'Content-Length: ' + this._contentLength + '\r\n';
} else if (!this._removedTE) {
header += 'Transfer-Encoding: chunked' + CRLF;
header += 'Transfer-Encoding: chunked\r\n';
this.chunkedEncoding = true;
} else {
// We should only be able to get here if both Content-Length and
Expand All @@ -487,7 +487,7 @@ function _storeHeader(firstLine, headers) {
throw new ERR_HTTP_TRAILER_INVALID();
}

this._header = header + CRLF;
this._header = header + '\r\n';
this._headerSent = false;

// Wait until the first body chunk, or close(), is sent to flush,
Expand All @@ -514,7 +514,7 @@ function processHeader(self, state, key, value, validate) {
function storeHeader(self, state, key, value, validate) {
if (validate)
validateHeaderValue(key, value);
state.header += key + ': ' + value + CRLF;
state.header += key + ': ' + value + '\r\n';
matchHeader(self, state, key, value);
}

Expand Down Expand Up @@ -694,7 +694,7 @@ ObjectDefineProperty(OutgoingMessage.prototype, 'writableNeedDrain', {
}
});

const crlf_buf = Buffer.from(CRLF);
const crlf_buf = Buffer.from('\r\n');
OutgoingMessage.prototype.write = function write(chunk, encoding, callback) {
if (typeof encoding === 'function') {
callback = encoding;
Expand Down Expand Up @@ -818,7 +818,7 @@ OutgoingMessage.prototype.addTrailers = function addTrailers(headers) {
debug('Trailer "%s" contains invalid characters', field);
throw new ERR_INVALID_CHAR('trailer content', field);
}
this._trailer += field + ': ' + value + CRLF;
this._trailer += field + ': ' + value + '\r\n';
}
};

Expand Down
19 changes: 9 additions & 10 deletions lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ const assert = require('internal/assert');
const {
parsers,
freeParser,
CRLF,
continueExpression,
chunkExpression,
kIncomingMessage,
Expand Down Expand Up @@ -252,12 +251,12 @@ ServerResponse.prototype.detachSocket = function detachSocket(socket) {
};

ServerResponse.prototype.writeContinue = function writeContinue(cb) {
this._writeRaw(`HTTP/1.1 100 Continue${CRLF}${CRLF}`, 'ascii', cb);
this._writeRaw('HTTP/1.1 100 Continue\r\n\r\n', 'ascii', cb);
this._sent100 = true;
};

ServerResponse.prototype.writeProcessing = function writeProcessing(cb) {
this._writeRaw(`HTTP/1.1 102 Processing${CRLF}${CRLF}`, 'ascii', cb);
this._writeRaw('HTTP/1.1 102 Processing\r\n\r\n', 'ascii', cb);
};

ServerResponse.prototype._implicitHeader = function _implicitHeader() {
Expand Down Expand Up @@ -320,7 +319,7 @@ function writeHead(statusCode, reason, obj) {
if (checkInvalidHeaderChar(this.statusMessage))
throw new ERR_INVALID_CHAR('statusMessage');

const statusLine = `HTTP/1.1 ${statusCode} ${this.statusMessage}${CRLF}`;
const statusLine = `HTTP/1.1 ${statusCode} ${this.statusMessage}\r\n`;

if (statusCode === 204 || statusCode === 304 ||
(statusCode >= 100 && statusCode <= 199)) {
Expand Down Expand Up @@ -646,16 +645,16 @@ function onParserTimeout(server, socket) {

const noop = () => {};
const badRequestResponse = Buffer.from(
`HTTP/1.1 400 ${STATUS_CODES[400]}${CRLF}` +
`Connection: close${CRLF}${CRLF}`, 'ascii'
`HTTP/1.1 400 ${STATUS_CODES[400]}\r\n` +
`Connection: close\r\n\r\n`, 'ascii'
);
const requestTimeoutResponse = Buffer.from(
`HTTP/1.1 408 ${STATUS_CODES[408]}${CRLF}` +
`Connection: close${CRLF}${CRLF}`, 'ascii'
`HTTP/1.1 408 ${STATUS_CODES[408]}\r\n` +
'Connection: close\r\n\r\n', 'ascii'
);
const requestHeaderFieldsTooLargeResponse = Buffer.from(
`HTTP/1.1 431 ${STATUS_CODES[431]}${CRLF}` +
`Connection: close${CRLF}${CRLF}`, 'ascii'
`HTTP/1.1 431 ${STATUS_CODES[431]}\r\n` +
`Connection: close\r\n\r\n`, 'ascii'
);
function socketOnError(e) {
// Ignore further errors
Expand Down

0 comments on commit a624067

Please sign in to comment.