UNPKG

node-web-mvc

Version:
156 lines (155 loc) 4.8 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); /** * @module HttpServletRequest * @description Http请求类 */ const querystring_1 = __importDefault(require("querystring")); const MediaType_1 = __importDefault(require("./MediaType")); const HttpMethod_1 = __importDefault(require("./HttpMethod")); const RequestMemoryStream_1 = __importDefault(require("./RequestMemoryStream")); const ApiUtils_1 = require("../util/ApiUtils"); const Javascript_1 = __importDefault(require("../../interface/Javascript")); const servletContextSymbol = Symbol('servletContext'); const createDispatcherSymbol = Symbol('createDispatcher'); class HttpServletRequest { get cookies() { return this._cookies; } /** * 当前请求上下文 */ get servletContext() { return this[servletContextSymbol]; } /** * 获取当前node原生请求对象 */ get nativeRequest() { return this.request; } /** * 获取完整协议域名 */ get fdomain() { const port = (this.port ? ':' + this.port : this.port); return this.protocol + '//' + this.host + port; } /** * 获取完整的url */ get url() { return this.nativeRequest.url; } /** * 获取当前请求完整path 不包含参数 */ get baseUrl() { return this.fdomain + this.path; } get requestUrl() { return this.fdomain + this.url; } /** * 设置属性值 * @param name 属性名 * @param value 属性值 */ setAttribute(name, value) { this.params.set(name, value); } /** * 获取属性值 * @param name 属性名称 */ getAttribute(name) { return this.params.get(name); } /** * 将可读流输送传入写出流 */ pipe(writeStream, options) { this.nativeRequest.pipe(writeStream, options); } /** * 判断是否存在body */ get hasBody() { const method = this.method; return !(method === HttpMethod_1.default.HEAD || method === HttpMethod_1.default.GET); } constructor(request, contextPath, createDispatcher, reader) { const protocol = request.socket.encrypted ? 'https' : 'http'; const uRL = new URL(request.url, `${protocol}://${request.headers.host}`); this.headers = request.headers; this.method = HttpMethod_1.default[(request.method).toUpperCase()]; this.protocol = protocol; this.request = request; this.query = querystring_1.default.parse(uRL.search.slice(1)); this.host = uRL.hostname; this.port = uRL.port; this.path = uRL.pathname; this.contextPath = contextPath; this.mediaType = new MediaType_1.default(this.headers['content-type']); this._cookies = this.parseCookie(request.headers['cookie']); this.params = new Map(); Javascript_1.default.defineHiddenProperty(this, createDispatcherSymbol, createDispatcher); this.bodyReader = reader; } setServletContext(context) { Javascript_1.default.defineHiddenProperty(this, servletContextSymbol, context); } /** * 解析cookie * @param cookieStr */ parseCookie(cookieStr) { const cookies = {}; (cookieStr || '').split(/; */).forEach((kv) => { const pairs = kv.split('='); const name = pairs[0].trim(); if (undefined === cookies[name]) { const value = (pairs[1] || '').trim(); const values = value[0] === '"' ? value.slice(1, -1) : value; cookies[name] = values; } }); return cookies; } getHeader(name) { return this.nativeRequest.headers[(name || '').toLowerCase()]; } getHeaderValue(name) { const v = this.getHeader(name); return v instanceof Array ? v : (0, ApiUtils_1.isEmpty)(v) ? [] : [v]; } getHeaderSingleValue(name) { const v = this.getHeader(name); if (v instanceof Array) { return v[0]; } return v; } getDateHeader(name) { const v = this.getHeader(name); if (v) { return Date.parse(v); } return null; } getRequestDispatcher(path) { const createDispatcher = this[createDispatcherSymbol]; return createDispatcher(path); } /** * 读取body内容为buffer * @returns */ readBodyAsBuffer() { return RequestMemoryStream_1.default.readBody(this.nativeRequest); } } exports.default = HttpServletRequest;