define([ "dojo/_base/declare", "dojo/Deferred", "url", "implab/safe", "./Cookie", "./ContentType", "implab/log/trace!"], function (declare, Deferred, url, safe, Cookie, ContentType, trace) { return declare([], { // http.IncommingMessage message: null, response: null, session: null, cookieFormat: "base64+json", _contentType: null, _container: null, _initialised: false, constructor: function (options) { safe.argumentNotNull(options, "options"); safe.argumentNotNull(options.container, "options.container"); this._container = options.container; trace.log("Request created"); }, init: function (message, response) { safe.argumentNotNull(message, "message"); this.message = message; this.method = message.method; this.path = url.parse(message.url, true, true).pathname; this.response = response; this._initialised = true; }, _checkInit: function () { if(!this._initialised){ throw new Error("Request is not initialised. 'init' function should be called"); } }, getService: function (service) { return this._container.getService(service); }, getContainer: function () { return this._container; }, getRemoteAddress: function () { return this.message.socket.remoteAddress; }, readAllText: function (encoding) { this._checkInit(); if (!encoding) encoding = this.getContentType().parameters.charset || 'utf8'; this.message.setEncoding(encoding); let d = new Deferred(); let chunks = []; this.message.on('data', function (chunk) { chunks.push(chunk); }); this.message.on('end', function () { d.resolve(chunks.join('')); }); this.message.on('error', function (e) { d.reject(e); }); return d; }, header: function (name) { safe.argumentNotEmptyString(name, "name"); this._checkInit(); return this.message.headers[name.toLowerCase()]; }, getContentType: function () { this._checkInit(); if (safe.isNull(this._contentType)) this._contentType = ContentType.parse(this.header('Content-Type')); return this._contentType; }, /** Возвращает значение печеньки из запроса. * @name Имя печеньки * @format Формат значения, например json+base64 * @return Объект со значением печеньки */ cookie: function (name, format) { safe.argumentNotEmptyString(name, "name"); this._checkInit(); if (!format) format = this.cookieFormat; if (safe.isNull(this._cookies)) { let cookiesHeader = this.header("Cookie"); if (!safe.isNullOrEmptyString(cookiesHeader)) this._cookies = Cookie.parseHeaderValue(cookiesHeader); else this._cookies = {}; } let cookie = this._cookies[name]; if (safe.isNull(cookie)) return null; return cookie.decode(format); } }); });