UNPKG

web-dev-server

Version:

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

112 lines 4.93 kB
Object.defineProperty(exports, "__esModule", { value: true }); exports.Request = void 0; var http_1 = require("http"); var url_1 = require("url"); var ObjectHelper_1 = require("./Tools/Helpers/ObjectHelper"); var Static_1 = require("./Requests/Static"); var Constants_1 = require("./Requests/Constants"); var Localization_1 = require("./Requests/Localization"); var Headers_1 = require("./Requests/Headers"); var Params_1 = require("./Requests/Params"); var Stream_1 = require("./Requests/Stream"); var Cookies_1 = require("./Requests/Cookies"); var Url_1 = require("./Requests/Url"); var Other_1 = require("./Requests/Other"); var StringHelper_1 = require("./Tools/Helpers/StringHelper"); Object.defineProperty(exports, "__esModule", { value: true }); exports.Request = /** @class */ (function () { var Request = /** @class */ (function () { function Request(socket) { var _this = this; //super(socket); // @ts-ignore http_1.IncomingMessage.call(this, socket); this.http = this; // @ts-ignore this.startTime = (new Date()).getTime(); var urlInitLocal = false; var bodyArr = []; this.http.on('data', function (chunk) { bodyArr.push(chunk); }).on('end', function () { // @ts-ignore _this.body = Buffer.concat(bodyArr).toString(); _this.http.complete = true; _this.http.emit('body-loaded'); }); Object.defineProperty(this, 'url', { get: function () { return _this._url; }, set: function (url) { _this._url = url; if (!urlInitLocal) { urlInitLocal = true; _this._urlInit(socket['server']['__wds']); } } }); } Request.prototype.GetHttpRequest = function () { return this.http; }; Request.prototype._urlInit = function (server) { var serverDocumentRoot = server.GetDocumentRoot(); var parsedUrl = url_1.parse(this._url, false); var requestPath = parsedUrl.pathname.replace(/[\/]+/g, '/'); if (typeof parsedUrl.query == 'string' && parsedUrl.query.length > 0) requestPath += '?' + parsedUrl.query.replace(/\+/g, '%20'); requestPath = StringHelper_1.StringHelper.DecodeUri(requestPath); var basePath = ''; var appRootPathAndScript = server.TryToFindIndexPath(requestPath); if (appRootPathAndScript.length > 0) { var appRootFullPath = appRootPathAndScript[0]; var indexScript = appRootPathAndScript[1]; basePath = appRootFullPath.substr(serverDocumentRoot.length); if (basePath.length > 0 && requestPath.indexOf(basePath) == 0) requestPath = requestPath.substr(basePath.length); } var serverBasePath = server.GetBasePath(); if (serverBasePath != null) basePath = serverBasePath + basePath; // @ts-ignore this.initUrlSegments(appRootFullPath, basePath, indexScript, requestPath); }; Request.prototype.AddListener = function () { return this.http.addListener.apply(this, [].slice.apply(arguments)); }; Request.prototype.Emit = function () { return this.http.emit.apply(this, [].slice.apply(arguments)); }; Request.prototype.On = function () { return this.http.on.apply(this, [].slice.apply(arguments)); }; Request.prototype.Once = function () { return this.http.once.apply(this, [].slice.apply(arguments)); }; Request.prototype.PrependListener = function () { return this.http.prependListener.apply(this, [].slice.apply(arguments)); }; Request.prototype.PrependOnceListener = function () { return this.http.prependOnceListener.apply(this, [].slice.apply(arguments)); }; Request.prototype.RemoveListener = function () { return this.http.removeListener.apply(this, [].slice.apply(arguments)); }; return Request; }()); ObjectHelper_1.ObjectHelper.Extend(Request, http_1.IncomingMessage); ObjectHelper_1.ObjectHelper.Mixins(Request, [ Constants_1.Constants, Static_1.Static, Localization_1.Localization, Headers_1.Headers, Params_1.Params, Stream_1.Stream, Cookies_1.Cookies, Url_1.Url, Other_1.Other ]); return Request; }()); //# sourceMappingURL=Request.js.map