UNPKG

web-dev-server

Version:

Node.js simple http server for common development or training purposes.

322 lines 12.8 kB
Object.defineProperty(exports, "__esModule", { value: true }); exports.Url = void 0; var Constants_1 = require("./Constants"); var StringHelper_1 = require("../Tools/Helpers/StringHelper"); var Url = /** @class */ (function () { function Url() { /** * Http scheme: `"http:" | "https:"` * Example: `"http:"` * @todo: implement https requesting. */ this.scheme = 'http:'; /** * `TRUE` if http port defined in requested URI (parsed by `url.parse()`). */ this.portDefined = false; } Url.prototype.SetScriptName = function (scriptName) { this.scriptName = scriptName; return this; }; Url.prototype.GetScriptName = function () { return this.scriptName; }; Url.prototype.SetAppRoot = function (appRoot) { this.appRoot = appRoot; return this; }; Url.prototype.GetAppRoot = function () { return this.appRoot; }; Url.prototype.SetScheme = function (rawProtocol) { this.scheme = rawProtocol; this.domainUrl = undefined; this.baseUrl = undefined; this.requestUrl = undefined; this.fullUrl = undefined; return this; }; Url.prototype.GetScheme = function () { return this.scheme; }; Url.prototype.IsSecure = function () { if (this.secure == null) { this.secure = [ Constants_1.Constants.SCHEME_HTTPS, Constants_1.Constants.SCHEME_FTPS, Constants_1.Constants.SCHEME_IRCS, Constants_1.Constants.SCHEME_SSH ].indexOf(this.GetScheme()) != -1; } return this.secure; }; Url.prototype.SetHostName = function (rawHostName) { if (this.hostName !== rawHostName) this.domainParts = undefined; this.hostName = rawHostName; this.domainUrl = undefined; this.baseUrl = undefined; this.requestUrl = undefined; this.fullUrl = undefined; if (rawHostName && this.portDefined) this.host = rawHostName + ':' + this.port; return this; }; Url.prototype.GetHostName = function () { if (this.hostName == null) this.SetHost(StringHelper_1.StringHelper.HtmlSpecialChars( // @ts-ignore this.GetHeader('host'), false)); return this.hostName; }; Url.prototype.SetHost = function (rawHost) { this.host = rawHost; this.domainUrl = undefined; this.baseUrl = undefined; this.requestUrl = undefined; this.fullUrl = undefined; var hostName; var doubleDotPos = rawHost.indexOf(':'); if (doubleDotPos != -1) { hostName = rawHost.substr(0, doubleDotPos); this.SetPort(rawHost.substr(doubleDotPos + 1)); } else { hostName = rawHost; this.port = ''; this.portDefined = false; } return this.SetHostName(hostName); }; Url.prototype.GetHost = function () { if (this.host == null) this.SetHost(StringHelper_1.StringHelper.HtmlSpecialChars( // @ts-ignore this.GetHeader('host'), false)); return this.host; }; Url.prototype.SetPort = function (rawPort) { this.port = rawPort; this.domainUrl = undefined; this.baseUrl = undefined; this.requestUrl = undefined; this.fullUrl = undefined; if (rawPort.length > 0) { this.host = this.hostName + ':' + rawPort; this.portDefined = true; } else { this.host = this.hostName; this.portDefined = false; } return this; }; Url.prototype.GetPort = function () { if (this.port == null) this.SetHost(StringHelper_1.StringHelper.HtmlSpecialChars( // @ts-ignore this.GetHeader('host'), false)); return this.port; }; Url.prototype.SetTopLevelDomain = function (topLevelDomain) { if (this.domainParts == null) this.initDomainSegments(); this.domainParts[2] = topLevelDomain; this.hostName = this.domainParts.join('.').trim(); if (this.hostName && this.portDefined) this.host = this.hostName + ':' + this.port; this.domainUrl = undefined; this.baseUrl = undefined; this.requestUrl = undefined; this.fullUrl = undefined; return this; }; Url.prototype.GetTopLevelDomain = function () { if (this.domainParts == null) this.initDomainSegments(); return this.domainParts[2]; }; Url.prototype.SetSecondLevelDomain = function (secondLevelDomain) { if (this.domainParts == null) this.initDomainSegments(); this.domainParts[1] = secondLevelDomain; this.hostName = this.domainParts.join('.').trim(); if (this.hostName && this.portDefined) this.host = this.hostName + ':' + this.port; this.domainUrl = undefined; this.baseUrl = undefined; this.requestUrl = undefined; this.fullUrl = undefined; return this; }; Url.prototype.GetSecondLevelDomain = function () { if (this.domainParts == null) this.initDomainSegments(); return this.domainParts[1]; }; Url.prototype.SetThirdLevelDomain = function (thirdLevelDomain) { if (this.domainParts == null) this.initDomainSegments(); this.domainParts[0] = thirdLevelDomain; this.hostName = this.domainParts.join('.').trim(); if (this.hostName && this.portDefined) this.host = this.hostName + ':' + this.port; this.domainUrl = undefined; this.baseUrl = undefined; this.requestUrl = undefined; this.fullUrl = undefined; return this; }; Url.prototype.GetThirdLevelDomain = function () { if (this.domainParts == null) this.initDomainSegments(); return this.domainParts[0]; }; Url.prototype.SetBasePath = function (rawBasePath) { this.basePath = rawBasePath; this.baseUrl = undefined; this.requestUrl = undefined; this.fullUrl = undefined; return this; }; Url.prototype.GetBasePath = function () { return this.basePath; }; Url.prototype.SetPath = function (rawPathValue) { this.path = rawPathValue; this.requestUrl = undefined; this.requestPath = undefined; this.fullUrl = undefined; return this; }; Url.prototype.GetPath = function (rawInput) { if (rawInput === void 0) { rawInput = false; } return rawInput ? this.path : StringHelper_1.StringHelper.HtmlSpecialChars(this.path, false); }; Url.prototype.SetQuery = function (rawQuery) { this.query = StringHelper_1.StringHelper.TrimLeft(rawQuery, '?'); this.fullUrl = undefined; this.requestPath = undefined; return this; }; Url.prototype.GetQuery = function (withQuestionMark, rawInput) { if (withQuestionMark === void 0) { withQuestionMark = false; } if (rawInput === void 0) { rawInput = false; } var result = (withQuestionMark && this.query.length > 0) ? '?' + this.query : this.query; return rawInput ? result : StringHelper_1.StringHelper.HtmlSpecialChars(result, false); }; Url.prototype.GetRequestPath = function (rawInput) { if (rawInput === void 0) { rawInput = false; } if (this.requestPath == null) this.requestPath = this.GetPath(true) + this.GetQuery(true, true); return rawInput ? this.requestPath : StringHelper_1.StringHelper.HtmlSpecialChars(this.requestPath, false); }; Url.prototype.GetDomainUrl = function (rawInput) { if (rawInput === void 0) { rawInput = false; } if (this.domainUrl == null) this.domainUrl = this.GetScheme() + '//' + this.GetHost(); return rawInput ? this.domainUrl : StringHelper_1.StringHelper.HtmlSpecialChars(this.domainUrl, false); }; Url.prototype.GetBaseUrl = function (rawInput) { if (rawInput === void 0) { rawInput = false; } if (this.baseUrl == null) this.baseUrl = this.GetDomainUrl(true) + this.GetBasePath(); return rawInput ? this.baseUrl : StringHelper_1.StringHelper.HtmlSpecialChars(this.baseUrl, false); }; Url.prototype.GetRequestUrl = function (rawInput) { if (rawInput === void 0) { rawInput = false; } if (this.requestUrl == null) this.requestUrl = this.GetBaseUrl(true) + this.GetPath(true); return rawInput ? this.requestUrl : StringHelper_1.StringHelper.HtmlSpecialChars(this.requestUrl, false); }; Url.prototype.GetFullUrl = function (rawInput) { if (rawInput === void 0) { rawInput = false; } if (this.fullUrl == null) this.fullUrl = this.GetRequestUrl(true) + this.GetQuery(true, true); return rawInput ? this.fullUrl : StringHelper_1.StringHelper.HtmlSpecialChars(this.fullUrl, false); }; /** * @summary Request set up method called before `index.js` script execution. * @param serverDocRoot * @param appRootFullPath * @param indexScript * @param serverBasePath */ Url.prototype.setUpIndexScriptExec = function (serverDocRoot, appRootFullPath, indexScript, serverBasePath, response) { // @ts-ignore this.response = response; var basePath = appRootFullPath.substr(serverDocRoot.length); var requestPath = this.path; if (basePath.length > 0 && requestPath.indexOf(basePath) == 0) requestPath = requestPath.substr(basePath.length); if (this.query && this.query.length > 0) requestPath += '?' + this.query; if (serverBasePath != null) basePath = serverBasePath + basePath; this.initUrlSegments(appRootFullPath, basePath, indexScript, requestPath); }; /** * Initialize URI segments parsed by `url.parse()`: path, query and fragment. */ Url.prototype.initUrlSegments = function (appRoot, basePath, scriptName, requestPath) { this.scheme = 'http:'; this.appRoot = appRoot; this.basePath = basePath; this.scriptName = scriptName; var qmPos = requestPath.indexOf('?'); if (qmPos !== -1) { this.path = requestPath.substr(0, qmPos); this.query = requestPath.substr(qmPos + 1); } else { this.path = requestPath; this.query = ''; } }; /** * Initialize domain parts from server name property. * If you need to add exceptional top-level domain names, use method * `Request.AddTwoSegmentTlds('co.uk');` * Example: * `'any.content.example.co.uk' => ['any.content', 'example', 'co.uk']` */ Url.prototype.initDomainSegments = function () { var hostName = this.GetHostName(), // without port lastDotPos = hostName.lastIndexOf('.'), twoSegmentTlds, first, second, third, firstTmp; this.domainParts = []; if (lastDotPos == -1) { this.domainParts = [null, null, hostName]; } else { first = hostName.substr(lastDotPos + 1); second = hostName.substr(0, lastDotPos); twoSegmentTlds = this.constructor['twoSegmentTlds']; // check co.uk and other... if (twoSegmentTlds && twoSegmentTlds.size > 0) { lastDotPos = second.lastIndexOf('.'); if (lastDotPos != -1) { firstTmp = second.substr(lastDotPos + 1) + '.' + first; if (twoSegmentTlds.has(firstTmp)) { first = firstTmp; second = firstTmp = second.substr(0, lastDotPos); } } } lastDotPos = second.lastIndexOf('.'); if (lastDotPos == -1) { this.domainParts = [null, second, first]; } else { third = second.substr(0, lastDotPos); second = second.substr(lastDotPos + 1); this.domainParts = [third, second, first]; } } }; return Url; }()); exports.Url = Url; //# sourceMappingURL=Url.js.map