优化content-type的读取

v1 1.2.4
宇天 2020-12-17 10:36:48 +08:00
parent bf74d08d44
commit e526f32b4a
2 changed files with 9 additions and 4 deletions

View File

@ -12,6 +12,8 @@ import URL from 'url'
import QS from 'querystring' import QS from 'querystring'
import PATH from 'path' import PATH from 'path'
const DEFAULT_FORM_TYPE = 'application/x-www-form-urlencoded'
var __dirname = PATH.dirname(URL.fileURLToPath(import.meta.url)) var __dirname = PATH.dirname(URL.fileURLToPath(import.meta.url))
var tmpdir = PATH.resolve(__dirname, './.tmp/') var tmpdir = PATH.resolve(__dirname, './.tmp/')
@ -147,6 +149,7 @@ export default class Request {
post(key = '', xss = true) { post(key = '', xss = true) {
let para = {} let para = {}
let out = Promise.defer() let out = Promise.defer()
let form, contentType
xss = !!xss xss = !!xss
//如果之前已经缓存过,则直接从缓存读取 //如果之前已经缓存过,则直接从缓存读取
@ -158,7 +161,9 @@ export default class Request {
} }
} }
let form = new Parser() contentType = this.header('content-type') || DEFAULT_FORM_TYPE
form = new Parser()
form.uploadDir = tmpdir form.uploadDir = tmpdir
form.parse(this.origin.req) form.parse(this.origin.req)
@ -167,7 +172,7 @@ export default class Request {
para = value para = value
return return
} }
if (~this.header('content-type').indexOf('urlencoded')) { if (~contentType.indexOf('urlencoded')) {
if ( if (
name.slice(0, 2) === '{"' && name.slice(0, 2) === '{"' &&
(name.slice(-2) === '"}' || value.slice(-2) === '"}') (name.slice(-2) === '"}' || value.slice(-2) === '"}')
@ -237,7 +242,7 @@ export default class Request {
form.on('error', out.reject) form.on('error', out.reject)
form.on('end', err => { form.on('end', err => {
if (~this.header('content-type').indexOf('urlencoded')) { if (~contentType.indexOf('urlencoded')) {
for (let i in para) { for (let i in para) {
if (typeof para[i] === 'string') { if (typeof para[i] === 'string') {
if (!para[i]) { if (!para[i]) {

View File

@ -1,6 +1,6 @@
{ {
"name": "@gm5/request", "name": "@gm5/request",
"version": "1.2.3", "version": "1.2.4",
"description": "对Http的request进一步封装, 提供常用的API", "description": "对Http的request进一步封装, 提供常用的API",
"main": "index.js", "main": "index.js",
"author": "yutent", "author": "yutent",