优化content-type判断;增加protocol属性
parent
53b0e2443e
commit
5ecf94b5f3
44
index.js
44
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
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue