From 5ecf94b5f36d92e4f81a73d4fe736ab861f42235 Mon Sep 17 00:00:00 2001 From: yutent Date: Tue, 31 Oct 2023 14:28:15 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96content-type=E5=88=A4?= =?UTF-8?q?=E6=96=AD;=E5=A2=9E=E5=8A=A0protocol=E5=B1=9E=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 44 ++++++++++++++++++++++++-------------------- lib/index.js | 2 +- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/index.js b/index.js index 4e2b386..965778a 100644 --- a/index.js +++ b/index.js @@ -45,11 +45,13 @@ export default class Request { #body = null #cookies = Object.create(null) + controller = 'index' method = 'GET' path = [] url = '' host = '127.0.0.1' + protocol = 'http' constructor(req, res, opts = {}) { this.method = req.method.toUpperCase() @@ -57,7 +59,9 @@ export default class Request { this.#req = req this.#res = res - this.host = req.headers['host'] + this.host = req.headers['host'] || '127.0.0.1' + this.protocol = req.headers['x-forwarded-proto'] || 'http' + this.#cookies = parseCookie(this.headers['cookie'] || '') Object.assign(this.#opts, opts) @@ -67,44 +71,44 @@ export default class Request { // 修正请求的url #init() { - let _url = parse(this.#req.url) + let url = parse(this.#req.url) .pathname.slice(1) .replace(/[\/]+$/, '') - let app = '' // 将作为主控制器(即apps目录下的应用) - let pathArr = [] + let controller = '' // 将作为主控制器(即apps目录下的应用) + let path = [] // URL上不允许有非法字符 - if (/[^\w-/.,@~!$&:+'"=]/.test(decode(_url))) { + if (/[^\w-/.,@~!$&:+'"=]/.test(decode(url))) { this.#res.rendered = true this.#res.writeHead(400, { - 'X-debug': `url [/${encode(_url)}] contains invalid characters` + 'X-debug': `url [/${encode(url)}] contains invalid characters` }) - return this.#res.end(`Invalid characters: /${_url}`) + return this.#res.end(`Invalid characters: /${url}`) } // 修正url中可能出现的"多斜杠" - _url = _url.replace(/[\/]+/g, '/').replace(/^\//, '') + url = url.replace(/[\/]+/g, '/').replace(/^\//, '') - pathArr = _url.split('/') - if (!pathArr[0] || pathArr[0] === '') { - pathArr[0] = 'index' + path = url.split('/') + if (!path[0] || path[0] === '') { + path[0] = 'index' } - if (pathArr[0].indexOf('.') !== -1) { - app = pathArr[0].slice(0, pathArr[0].indexOf('.')) + if (path[0].indexOf('.') !== -1) { + controller = path[0].slice(0, path[0].indexOf('.')) // 如果app为空(这种情况一般是url前面带了个"."造成的),则自动默认为index - if (!app || app === '') { - app = 'index' + if (!controller || controller === '') { + controller = 'index' } } else { - app = pathArr[0] + controller = path[0] } - pathArr.shift() + path.shift() - this.app = app - this.url = _url - this.path = pathArr + this.controller = controller + this.url = url + this.path = path } /** diff --git a/lib/index.js b/lib/index.js index 6646fee..5647007 100644 --- a/lib/index.js +++ b/lib/index.js @@ -138,7 +138,7 @@ export default class IncomingForm extends EventEmitter { } #parseContentType() { - let contentType = this.headers['content-type'] + let contentType = this.headers['content-type'] || '' let lower = contentType.toLowerCase() if (this.bytesExpected === 0) {