web-dev-server
Version:
Node.js simple http server for common development or training purposes.
255 lines • 9.72 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.Headers = void 0;
var http_1 = require("http");
var Constants_1 = require("./Constants");
var Headers = /** @class */ (function () {
function Headers() {
/**
* Response HTTP protocol version by request.
* Example: `HTTP/1.0 | HTTP/1.1 | HTTP/2 | SPDY`
*/
this.httpVersion = null;
/**
* Response HTTP headers as `key => value` array.
* Example:
* `array(
* 'content-type' => 'text/html',
* 'content-encoding' => 'utf-8'
* );`
*/
this.headers = {};
/**
* Response content encoding.
* Example: `"utf-8" | "windows-1250" | "ISO-8859-2"`
* @var \string|NULL
*/
this.encoding = null;
/**
* Disabled headers, never sent except if there is
* rendered exception in development environment.
*/
this.disabledHeaders = new Map();
}
Headers.prototype.IsSentHeaders = function () {
var httpRes = this['http'];
return httpRes.headersSent;
};
Headers.prototype.SetHeaders = function (headers, cleanAllPrevious) {
if (headers === void 0) { headers = {}; }
if (cleanAllPrevious === void 0) { cleanAllPrevious = false; }
var httpRes = this['http'];
if (cleanAllPrevious) {
var allHeaders = httpRes.getHeaders();
for (var name in allHeaders)
httpRes.removeHeader(name);
}
for (var name in headers)
this.SetHeader(name, headers[name]);
return this;
};
Headers.prototype.SetHeader = function (name, value) {
name = name.toLowerCase();
if (this.disabledHeaders.has(name))
return this;
var httpRes = this['http'];
httpRes.setHeader(name, value);
this.headers[name] = value;
if (name === 'content-type') {
var valueStr = value.toString();
var charsetPos = valueStr.indexOf('charset');
if (charsetPos !== -1) {
var equalPos = valueStr.indexOf('=', charsetPos);
if (equalPos !== -1)
this.SetEncoding(valueStr.substr(equalPos + 1).trim());
}
}
if (name === 'content-encoding')
this.SetEncoding(value.toString());
return this;
};
Headers.prototype.GetHeader = function (name) {
this.UpdateHeaders();
name = name.toLowerCase();
return name in this.headers
? this.headers[name]
: null;
};
Headers.prototype.HasHeader = function (name) {
this.UpdateHeaders();
name = name.toLowerCase();
return name in this.headers;
};
Headers.prototype.UpdateHeaders = function () {
var httpRes = this['http'];
this.headers = httpRes.getHeaders();
return this;
};
Headers.prototype.SetDisabledHeaders = function () {
var disabledHeaders = [];
for (var _i = 0; _i < arguments.length; _i++) {
disabledHeaders[_i] = arguments[_i];
}
this.disabledHeaders = new Map();
for (var i = 0, l = disabledHeaders.length; i < l; i++)
this.disabledHeaders.set(disabledHeaders[i], true);
return this;
};
Headers.prototype.GetDisabledHeaders = function () {
var result = [];
this.disabledHeaders.forEach(function (value, key) { return result.push(key); });
return result;
};
Headers.prototype.GetHttpVersion = function () {
if (this.httpVersion == null) {
var httpReq = this['req'];
this.httpVersion = httpReq.httpVersion
? 'HTTP/' + httpReq.httpVersion
: 'HTTP/1.1';
}
return this.httpVersion;
};
Headers.prototype.SetHttpVersion = function (httpVersion) {
this.httpVersion = httpVersion;
return this;
};
Headers.prototype.SetCode = function (code, codeMessage) {
if (codeMessage === void 0) { codeMessage = null; }
var httpRes = this['http'];
httpRes.statusCode = code;
if (codeMessage != null)
httpRes.statusMessage = codeMessage;
return this;
};
Headers.prototype.GetCode = function () {
var httpRes = this['http'];
if (httpRes.statusCode == null)
httpRes.statusCode == Constants_1.Constants.CODES.OK;
return httpRes.statusCode;
};
Headers.prototype.GetEncoding = function () {
if (this.encoding == null) {
this.UpdateHeaders();
if ('content-encoding' in this.headers) {
this.encoding = this.headers['content-encoding'];
}
else if ('content-type' in this.headers) {
var valueStr = this.headers['content-type'].toString();
var charsetPos = valueStr.indexOf('charset');
if (charsetPos !== -1) {
var equalPos = valueStr.indexOf('=', charsetPos);
if (equalPos !== -1)
this.encoding = valueStr.substr(equalPos + 1).trim();
}
}
if (!this.encoding)
this.encoding = 'utf-8';
}
return this.encoding;
};
Headers.prototype.SetEncoding = function (encoding) {
if (encoding === void 0) { encoding = 'utf-8'; }
var httpRes = this['http'];
httpRes.setHeader('content-encoding', encoding);
this.encoding = encoding;
this.headers['content-encoding'] = encoding;
return this;
};
Headers.prototype.IsUpgrading = function () {
var httpRes = this['http'];
if (httpRes['upgrading'] != null)
return httpRes['upgrading'];
return this.HasHeader("upgrading");
};
Headers.prototype.IsRedirect = function () {
return this.HasHeader('location');
};
Headers.prototype.IsSent = function () {
var httpRes = this['http'];
return httpRes.finished && httpRes.headersSent;
};
Headers.prototype.SendHeaders = function (code, end) {
if (end === void 0) { end = false; }
var httpRes = this['http'];
if (httpRes.headersSent)
return this;
httpRes.statusCode = code
? code
: this.GetCode();
var codeStr = httpRes.statusCode.toString();
if (httpRes.statusMessage == null)
httpRes.statusMessage = codeStr in http_1.STATUS_CODES
? http_1.STATUS_CODES[codeStr]
: '';
this.UpdateHeaders();
if (!('content-encoding' in this.headers))
this.headers['content-encoding'] = this.GetEncoding();
var httpReq = this['req'];
httpRes.setHeader('Host', httpReq.GetHost());
var charsetMatched, charsetPos, equalPos, value, separator, nameExploded;
for (var name in this.headers) {
value = this.headers[name];
if (name == 'content-type') {
charsetMatched = false;
charsetPos = value.toString().indexOf('charset');
if (charsetPos !== -1) {
equalPos = value.indexOf('=', charsetPos);
if (equalPos != -1)
charsetMatched = true;
}
if (!charsetMatched)
value += '; charset=' + this.GetEncoding();
}
if (this.disabledHeaders.has(name)) {
httpRes.removeHeader(name);
}
else {
nameExploded = name.split('-');
name = '';
separator = '';
nameExploded.forEach(function (part, i) {
if (part.length == 0)
return;
name += separator + part.substr(0, 1).toUpperCase();
if (part.length > 1)
name += part.substr(1);
separator = '-';
});
httpRes.setHeader(name, value);
}
}
var cookiesHeaders = this['getCookiesHeaders']();
for (var i = 0, l = cookiesHeaders.length; i < l; i++)
httpRes.setHeader('Set-Cookie', cookiesHeaders[i]);
this.disabledHeaders.forEach(function (bool, name) {
httpRes.removeHeader(name);
});
httpRes.writeHead(httpRes.statusCode);
if (end)
this.endHttpRequest();
return this;
};
Headers.prototype.Redirect = function (location, code, reason, end) {
if (code === void 0) { code = Constants_1.Constants.CODES.SEE_OTHER; }
if (end === void 0) { end = true; }
this.SetHeader('Location', location);
if (reason && reason.length > 0)
this.SetHeader('X-Redirect-Reason', reason);
this.SendHeaders(code);
if (end)
this.endHttpRequest();
return this;
};
Headers.prototype.endHttpRequest = function (cb) {
var httpRes = this['http'];
httpRes.end(function () {
httpRes.emit('session-unlock');
httpRes.finished = true;
if (cb)
cb();
});
return this;
};
return Headers;
}());
exports.Headers = Headers;
//# sourceMappingURL=Headers.js.map