优化content-type判断;增加protocol属性

master
yutent 2023-10-31 14:28:15 +08:00
parent 53b0e2443e
commit 5ecf94b5f3
2 changed files with 25 additions and 21 deletions

View File

@ -45,11 +45,13 @@ export default class Request {
#body = null #body = null
#cookies = Object.create(null) #cookies = Object.create(null)
controller = 'index'
method = 'GET' method = 'GET'
path = [] path = []
url = '' url = ''
host = '127.0.0.1' host = '127.0.0.1'
protocol = 'http'
constructor(req, res, opts = {}) { constructor(req, res, opts = {}) {
this.method = req.method.toUpperCase() this.method = req.method.toUpperCase()
@ -57,7 +59,9 @@ export default class Request {
this.#req = req this.#req = req
this.#res = res 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'] || '') this.#cookies = parseCookie(this.headers['cookie'] || '')
Object.assign(this.#opts, opts) Object.assign(this.#opts, opts)
@ -67,44 +71,44 @@ export default class Request {
// 修正请求的url // 修正请求的url
#init() { #init() {
let _url = parse(this.#req.url) let url = parse(this.#req.url)
.pathname.slice(1) .pathname.slice(1)
.replace(/[\/]+$/, '') .replace(/[\/]+$/, '')
let app = '' // 将作为主控制器(即apps目录下的应用) let controller = '' // 将作为主控制器(即apps目录下的应用)
let pathArr = [] let path = []
// URL上不允许有非法字符 // URL上不允许有非法字符
if (/[^\w-/.,@~!$&:+'"=]/.test(decode(_url))) { if (/[^\w-/.,@~!$&:+'"=]/.test(decode(url))) {
this.#res.rendered = true this.#res.rendered = true
this.#res.writeHead(400, { 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 = _url.replace(/[\/]+/g, '/').replace(/^\//, '') url = url.replace(/[\/]+/g, '/').replace(/^\//, '')
pathArr = _url.split('/') path = url.split('/')
if (!pathArr[0] || pathArr[0] === '') { if (!path[0] || path[0] === '') {
pathArr[0] = 'index' path[0] = 'index'
} }
if (pathArr[0].indexOf('.') !== -1) { if (path[0].indexOf('.') !== -1) {
app = pathArr[0].slice(0, pathArr[0].indexOf('.')) controller = path[0].slice(0, path[0].indexOf('.'))
// 如果app为空(这种情况一般是url前面带了个"."造成的),则自动默认为index // 如果app为空(这种情况一般是url前面带了个"."造成的),则自动默认为index
if (!app || app === '') { if (!controller || controller === '') {
app = 'index' controller = 'index'
} }
} else { } else {
app = pathArr[0] controller = path[0]
} }
pathArr.shift() path.shift()
this.app = app this.controller = controller
this.url = _url this.url = url
this.path = pathArr this.path = path
} }
/** /**

View File

@ -138,7 +138,7 @@ export default class IncomingForm extends EventEmitter {
} }
#parseContentType() { #parseContentType() {
let contentType = this.headers['content-type'] let contentType = this.headers['content-type'] || ''
let lower = contentType.toLowerCase() let lower = contentType.toLowerCase()
if (this.bytesExpected === 0) { if (this.bytesExpected === 0) {