完成2.0版重构

v2
yutent 2023-10-30 16:41:37 +08:00
parent 5e827928ba
commit 30f0ca48e3
6 changed files with 240 additions and 232 deletions

2
.gitignore vendored
View File

@ -7,7 +7,7 @@
._* ._*
.idea .idea
.vscode .vscode
.tmp
.tmp/
node_modules/ node_modules/

View File

@ -39,6 +39,8 @@ export default class Request {
#req = null #req = null
#res = null #res = null
#opts = {}
#query = null #query = null
#body = null #body = null
#cookies = Object.create(null) #cookies = Object.create(null)
@ -49,7 +51,7 @@ export default class Request {
url = '' url = ''
host = '127.0.0.1' host = '127.0.0.1'
constructor(req, res) { constructor(req, res, opts = {}) {
this.method = req.method.toUpperCase() this.method = req.method.toUpperCase()
this.#req = req this.#req = req
@ -58,6 +60,8 @@ export default class Request {
this.host = req.headers['host'] this.host = req.headers['host']
this.#cookies = parseCookie(this.headers['cookie'] || '') this.#cookies = parseCookie(this.headers['cookie'] || '')
Object.assign(this.#opts, opts)
this.#init() this.#init()
} }
@ -114,7 +118,7 @@ export default class Request {
contentType = this.header('content-type') || DEFAULT_FORM_TYPE contentType = this.header('content-type') || DEFAULT_FORM_TYPE
form = new Parser(this.#req, { uploadDir: tmpdir }) form = new Parser(this.#req, { ...this.#opts, uploadDir: tmpdir })
form form
.on('field', (name, value) => { .on('field', (name, value) => {

View File

@ -16,6 +16,20 @@ function randomPath(uploadDir) {
return join(uploadDir, name) return join(uploadDir, name)
} }
function parseFilename(headerValue) {
let matches = headerValue.match(/\bfilename="(.*?)"($|; )/i)
if (!matches) {
return
}
let filename = matches[1].slice(matches[1].lastIndexOf('\\') + 1)
filename = filename.replace(/%22/g, '"')
filename = filename.replace(/&#([\d]{4});/g, function (m, code) {
return String.fromCharCode(code)
})
return filename
}
/* ------------------------------------- */ /* ------------------------------------- */
export default class IncomingForm extends EventEmitter { export default class IncomingForm extends EventEmitter {
@ -31,7 +45,7 @@ export default class IncomingForm extends EventEmitter {
bytesExpected = null bytesExpected = null
#parser = null #parser = null
#pending = true #pending = 0
#openedFiles = [] #openedFiles = []
@ -42,7 +56,6 @@ export default class IncomingForm extends EventEmitter {
this.uploadDir = opts.uploadDir this.uploadDir = opts.uploadDir
this.encoding = opts.encoding || 'utf-8' this.encoding = opts.encoding || 'utf-8'
this.multiples = opts.multiples || false
// Parse headers and setup the parser, ready to start listening for data. // Parse headers and setup the parser, ready to start listening for data.
this.writeHeaders(req.headers) this.writeHeaders(req.headers)
@ -134,8 +147,8 @@ export default class IncomingForm extends EventEmitter {
file.open() file.open()
this.#openedFiles.push(file) this.#openedFiles.push(file)
// 表单解析完的时候文件写入不一定完成了, 所以需要加入pending计数
this.#pending = true this.#pending++
part part
.on('data', buffer => { .on('data', buffer => {
@ -145,12 +158,13 @@ export default class IncomingForm extends EventEmitter {
file.write(buffer) file.write(buffer)
}) })
.on('end', () => { .on('end', () => {
console.log('file part end...') if (part.ended) {
return
}
part.ended = true
file.end(() => { file.end(() => {
console.log('<><><><>', part.name, file)
this.emit('file', part.name, file) this.emit('file', part.name, file)
this.#pending = false this.#pending--
// this.#handleEnd()
}) })
}) })
} }
@ -199,118 +213,99 @@ export default class IncomingForm extends EventEmitter {
} }
#createMultipartParser(boundary) { #createMultipartParser(boundary) {
let parser = new MultipartParser(boundary)
let headerField, headerValue, part let headerField, headerValue, part
parser this.#parser = new MultipartParser(boundary)
.on('partBegin', function () {
part = new Stream()
part.readable = true
part.headers = {}
part.name = null
part.filename = null
part.mime = null
part.transferEncoding = 'binary' this.#parser.$partBegin = function () {
part.transferBuffer = '' part = new Stream()
part.readable = true
part.headers = {}
part.name = null
part.filename = null
part.mime = null
headerField = '' part.transferEncoding = 'binary'
headerValue = '' part.transferBuffer = ''
})
.on('headerField', (b, start, end) => {
headerField += b.toString(this.encoding, start, end)
})
.on('headerValue', (b, start, end) => {
headerValue += b.toString(this.encoding, start, end)
})
.on('headerEnd', () => {
headerField = headerField.toLowerCase()
part.headers[headerField] = headerValue
var m = headerValue.match(/\bname="([^"]+)"/i) headerField = ''
if (headerField == 'content-disposition') { headerValue = ''
if (m) { }
part.name = m[1]
this.#parser.$headerField = b => {
headerField += b.toString(this.encoding)
}
this.#parser.$headerValue = b => {
headerValue += b.toString(this.encoding)
}
this.#parser.$headerEnd = () => {
headerField = headerField.toLowerCase()
part.headers[headerField] = headerValue
let matches = headerValue.match(/\bname="([^"]+)"/i)
if (headerField == 'content-disposition') {
if (matches) {
part.name = matches[1]
}
part.filename = parseFilename(headerValue)
} else if (headerField == 'content-type') {
part.mime = headerValue
} else if (headerField == 'content-transfer-encoding') {
part.transferEncoding = headerValue.toLowerCase()
}
headerField = ''
headerValue = ''
}
this.#parser.$headersEnd = () => {
switch (part.transferEncoding) {
case 'binary':
case '7bit':
case '8bit':
this.#parser.$partData = function (b) {
part.emit('data', b)
} }
this.#parser.$partEnd = function () {
part.emit('end')
}
break
part.filename = this._fileName(headerValue) case 'base64':
} else if (headerField == 'content-type') { this.#parser.$partData = function (b) {
part.mime = headerValue part.transferBuffer += b.toString('ascii')
} else if (headerField == 'content-transfer-encoding') {
part.transferEncoding = headerValue.toLowerCase()
}
headerField = '' // 确保offset的值能被4整除
headerValue = '' let offset = ~~(part.transferBuffer.length / 4) * 4
}) part.emit(
.on('headersEnd', () => { 'data',
switch (part.transferEncoding) { Buffer.from(part.transferBuffer.slice(0, offset), 'base64')
case 'binary': )
case '7bit': part.transferBuffer = part.transferBuffer.slice(offset)
case '8bit': }
parser this.#parser.$partEnd = function () {
.on('partData', function (b, start, end) { part.emit('data', Buffer.from(part.transferBuffer, 'base64'))
part.emit('data', b.slice(start, end)) part.emit('end')
}) }
.on('partEnd', function () { break
part.emit('end')
})
break
case 'base64': default:
parser return this.#handleError(new Error('unknown transfer-encoding'))
.on('partData', function (b, start, end) { }
part.transferBuffer += b.slice(start, end).toString('ascii')
/* this.#handlePart(part)
four bytes (chars) in base64 converts to three bytes in binary }
encoding. So we should always work with a number of bytes that
can be divided by 4, it will result in a number of buytes that
can be divided vy 3.
*/
var offset = parseInt(part.transferBuffer.length / 4, 10) * 4
part.emit(
'data',
Buffer.from(
part.transferBuffer.substring(0, offset),
'base64'
)
)
part.transferBuffer = part.transferBuffer.substring(offset)
})
.on('partEnd', function () {
part.emit('data', Buffer.from(part.transferBuffer, 'base64'))
part.emit('end')
})
break
default: this.#parser.$end = () => {
return this.#handleError(new Error('unknown transfer-encoding')) if (this.#pending > 0) {
} setTimeout(_ => this.#parser.$end())
} else {
this.#handlePart(part) this.#handleEnd()
}) }
.on('end', () => { }
if (this.#pending) {
setTimeout(_ => parser.emit('end'))
} else {
this.#handleEnd()
}
})
this.#parser = parser
}
_fileName(headerValue) {
var m = headerValue.match(/\bfilename="(.*?)"($|; )/i)
if (!m) return
var filename = m[1].substr(m[1].lastIndexOf('\\') + 1)
filename = filename.replace(/%22/g, '"')
filename = filename.replace(/&#([\d]{4});/g, function (m, code) {
return String.fromCharCode(code)
})
return filename
} }
#createUrlencodedParser() { #createUrlencodedParser() {

View File

@ -19,10 +19,10 @@ export class JSONParser extends EventEmitter {
try { try {
fields = JSON.parse(data) fields = JSON.parse(data)
} catch (e) { } catch (e) {
try{ try {
// 非标准的json语法,尝试用 Function 解析 // 非标准的json语法,尝试用 Function 解析
fields = Function(`try{return ${data}}catch(e){}`)() fields = Function(`try{return ${data}}catch(e){}`)()
}catch(err){} } catch (err) {}
} }
this.emit('field', false, fields) this.emit('field', false, fields)
@ -30,7 +30,14 @@ export class JSONParser extends EventEmitter {
this.#buff = null this.#buff = null
} else { } else {
this.emit('error', new Error(`The uploaded data is incomplete. Expected ${this.#byteLen}, Received ${this.#buff.length} .`)) this.emit(
'error',
new Error(
`The uploaded data is incomplete. Expected ${
this.#byteLen
}, Received ${this.#buff.length} .`
)
)
} }
} }
} }

View File

@ -1,47 +1,49 @@
import { EventEmitter } from 'node:events' import { EventEmitter } from 'node:events'
var s = 0, let s = 0
STATE_DICT = { const STATE_DICT = {
PARSER_UNINITIALIZED: s++, PARSER_UNINITIALIZED: s++,
START: s++, START: s++,
START_BOUNDARY: s++, START_BOUNDARY: s++,
HEADER_FIELD_START: s++, HEADER_FIELD_START: s++,
HEADER_FIELD: s++, HEADER_FIELD: s++,
HEADER_VALUE_START: s++, HEADER_VALUE_START: s++,
HEADER_VALUE: s++, HEADER_VALUE: s++,
HEADER_VALUE_ALMOST_DONE: s++, HEADER_VALUE_ALMOST_DONE: s++,
HEADERS_ALMOST_DONE: s++, HEADERS_ALMOST_DONE: s++,
PART_DATA_START: s++, PART_DATA_START: s++,
PART_DATA: s++, PART_DATA: s++,
PART_END: s++, PART_END: s++,
END: s++ END: s++
}, }
f = 1, let f = 1
F = { const FLAG_DICT = {
PART_BOUNDARY: f, PART_BOUNDARY: f,
LAST_BOUNDARY: (f *= 2) LAST_BOUNDARY: (f *= 2)
}, }
LF = 10,
CR = 13,
SPACE = 32,
HYPHEN = 45,
COLON = 58,
A = 97,
Z = 122,
lower = function (c) {
return c | 0x20
}
function stateToString(stateNumber) { const LF = 10
for (let state in STATE_DICT) { const CR = 13
let number = STATE_DICT[state] const SPACE = 32
if (number === stateNumber) { const HYPHEN = 45
return state const COLON = 58
} const LETTER_A = 97
const LETTER_Z = 122
function lower(c) {
return c | 0x20
}
function stateToString(value) {
for (let key in STATE_DICT) {
let number = STATE_DICT[key]
if (number === value) {
return key
} }
} }
}
export class MultipartParser extends EventEmitter { export class MultipartParser {
boundary = null boundary = null
boundaryChars = null boundaryChars = null
lookbehind = null lookbehind = null
@ -50,11 +52,7 @@ export class MultipartParser extends EventEmitter {
index = null index = null
flags = 0 flags = 0
constructor(str) { constructor(str) {
super()
this.boundary = Buffer.alloc(str.length + 4) this.boundary = Buffer.alloc(str.length + 4)
this.boundary.write('\r\n--', 0) this.boundary.write('\r\n--', 0)
this.boundary.write(str, 4) this.boundary.write(str, 4)
@ -67,8 +65,32 @@ export class MultipartParser extends EventEmitter {
} }
} }
#mark(k, v) {
this[k + 'Mark'] = v
}
#emit(name, buff, idx, cleanup) {
let mark = name + 'Mark'
if (this[mark] !== void 0) {
let start = this[mark]
let end = buff.length
if (cleanup) {
end = idx
delete this[mark]
} else {
this[mark] = 0
}
if (start === end) {
return
}
this['$' + name](buff.slice(start, end))
}
}
write(buffer) { write(buffer) {
var i = 0, let idx = 0,
len = buffer.length, len = buffer.length,
prevIndex = this.index, prevIndex = this.index,
index = this.index, index = this.index,
@ -83,55 +105,37 @@ export class MultipartParser extends EventEmitter {
c, c,
cl cl
let mark = (name) => { for (idx = 0; idx < len; idx++) {
this[name + 'Mark'] = i c = buffer[idx]
},
dataCallback = (name, clear) => {
var markSymbol = name + 'Mark'
if ((markSymbol in this)) {
if (clear) {
this.emit(name, buffer, this[markSymbol], i)
delete this[markSymbol]
} else {
this.emit(name, buffer, this[markSymbol], buffer.length)
this[markSymbol] = 0
}
}
}
// console.log('???? ', state, 'len: ', len);
for (i = 0; i < len; i++) {
c = buffer[i]
switch (state) { switch (state) {
case STATE_DICT.PARSER_UNINITIALIZED: case STATE_DICT.PARSER_UNINITIALIZED:
return i return
case STATE_DICT.START: case STATE_DICT.START:
index = 0 index = 0
state = STATE_DICT.START_BOUNDARY state = STATE_DICT.START_BOUNDARY
case STATE_DICT.START_BOUNDARY: case STATE_DICT.START_BOUNDARY:
// console.log('=====>>>', index, c, boundary);
if (index == boundary.length - 2) { if (index == boundary.length - 2) {
if (c == HYPHEN) { if (c == HYPHEN) {
flags |= F.LAST_BOUNDARY flags |= FLAG_DICT.LAST_BOUNDARY
} else if (c != CR) { } else if (c != CR) {
return i return
} }
index++ index++
break break
} else if (index - 1 == boundary.length - 2) { } else if (index - 1 == boundary.length - 2) {
if (flags & F.LAST_BOUNDARY && c == HYPHEN) { if (flags & FLAG_DICT.LAST_BOUNDARY && c == HYPHEN) {
this.emit('end') this.$end()
state = STATE_DICT.END state = STATE_DICT.END
flags = 0 flags = 0
} else if (!(flags & F.LAST_BOUNDARY) && c == LF) { } else if (!(flags & FLAG_DICT.LAST_BOUNDARY) && c == LF) {
index = 0 index = 0
this.emit('partBegin') this.$partBegin()
state = STATE_DICT.HEADER_FIELD_START state = STATE_DICT.HEADER_FIELD_START
} else { } else {
return i return
} }
break break
} }
@ -146,7 +150,7 @@ export class MultipartParser extends EventEmitter {
case STATE_DICT.HEADER_FIELD_START: case STATE_DICT.HEADER_FIELD_START:
state = STATE_DICT.HEADER_FIELD state = STATE_DICT.HEADER_FIELD
mark('headerField') this.#mark('headerField', idx)
index = 0 index = 0
case STATE_DICT.HEADER_FIELD: case STATE_DICT.HEADER_FIELD:
@ -164,16 +168,16 @@ export class MultipartParser extends EventEmitter {
if (c == COLON) { if (c == COLON) {
if (index == 1) { if (index == 1) {
// empty header field // empty header field
return i return
} }
dataCallback('headerField', true) this.#emit('headerField', buffer, idx, true)
state = STATE_DICT.HEADER_VALUE_START state = STATE_DICT.HEADER_VALUE_START
break break
} }
cl = lower(c) cl = lower(c)
if (cl < A || cl > Z) { if (cl < LETTER_A || cl > LETTER_Z) {
return i return
} }
break break
@ -182,54 +186,54 @@ export class MultipartParser extends EventEmitter {
break break
} }
mark('headerValue') this.#mark('headerValue', idx)
state = STATE_DICT.HEADER_VALUE state = STATE_DICT.HEADER_VALUE
case STATE_DICT.HEADER_VALUE: case STATE_DICT.HEADER_VALUE:
if (c == CR) { if (c == CR) {
dataCallback('headerValue', true) this.#emit('headerValue', buffer, idx, true)
this.emit('headerEnd') this.$headerEnd()
state = STATE_DICT.HEADER_VALUE_ALMOST_DONE state = STATE_DICT.HEADER_VALUE_ALMOST_DONE
} }
break break
case STATE_DICT.HEADER_VALUE_ALMOST_DONE: case STATE_DICT.HEADER_VALUE_ALMOST_DONE:
if (c != LF) { if (c != LF) {
return i return
} }
state = STATE_DICT.HEADER_FIELD_START state = STATE_DICT.HEADER_FIELD_START
break break
case STATE_DICT.HEADERS_ALMOST_DONE: case STATE_DICT.HEADERS_ALMOST_DONE:
if (c != LF) { if (c != LF) {
return i return
} }
this.emit('headersEnd') this.$headersEnd()
state = STATE_DICT.PART_DATA_START state = STATE_DICT.PART_DATA_START
break break
case STATE_DICT.PART_DATA_START: case STATE_DICT.PART_DATA_START:
state = STATE_DICT.PART_DATA state = STATE_DICT.PART_DATA
mark('partData') this.#mark('partData', idx)
case STATE_DICT.PART_DATA: case STATE_DICT.PART_DATA:
prevIndex = index prevIndex = index
if (index === 0) { if (index === 0) {
// boyer-moore derrived algorithm to safely skip non-boundary data // boyer-moore derrived algorithm to safely skip non-boundary data
i += boundaryEnd idx += boundaryEnd
while (i < bufferLength && !(buffer[i] in boundaryChars)) { while (idx < bufferLength && !(buffer[idx] in boundaryChars)) {
i += boundaryLength idx += boundaryLength
} }
i -= boundaryEnd idx -= boundaryEnd
c = buffer[i] c = buffer[idx]
} }
if (index < boundary.length) { if (index < boundary.length) {
if (boundary[index] == c) { if (boundary[index] == c) {
if (index === 0) { if (index === 0) {
dataCallback('partData', true) this.#emit('partData', buffer, idx, true)
} }
index++ index++
} else { } else {
@ -239,28 +243,28 @@ export class MultipartParser extends EventEmitter {
index++ index++
if (c == CR) { if (c == CR) {
// CR = part boundary // CR = part boundary
flags |= F.PART_BOUNDARY flags |= FLAG_DICT.PART_BOUNDARY
} else if (c == HYPHEN) { } else if (c == HYPHEN) {
// HYPHEN = end boundary // HYPHEN = end boundary
flags |= F.LAST_BOUNDARY flags |= FLAG_DICT.LAST_BOUNDARY
} else { } else {
index = 0 index = 0
} }
} else if (index - 1 == boundary.length) { } else if (index - 1 == boundary.length) {
if (flags & F.PART_BOUNDARY) { if (flags & FLAG_DICT.PART_BOUNDARY) {
index = 0 index = 0
if (c == LF) { if (c == LF) {
// unset the PART_BOUNDARY flag // unset the PART_BOUNDARY flag
flags &= ~F.PART_BOUNDARY flags &= ~FLAG_DICT.PART_BOUNDARY
this.emit('partEnd') this.$partEnd()
this.emit('partBegin') this.$partBegin()
state = STATE_DICT.HEADER_FIELD_START state = STATE_DICT.HEADER_FIELD_START
break break
} }
} else if (flags & F.LAST_BOUNDARY) { } else if (flags & FLAG_DICT.LAST_BOUNDARY) {
if (c == HYPHEN) { if (c == HYPHEN) {
this.emit('partEnd') this.$partEnd()
this.emit('end') this.$end()
state = STATE_DICT.END state = STATE_DICT.END
flags = 0 flags = 0
} else { } else {
@ -278,13 +282,14 @@ export class MultipartParser extends EventEmitter {
} else if (prevIndex > 0) { } else if (prevIndex > 0) {
// if our boundary turned out to be rubbish, the captured lookbehind // if our boundary turned out to be rubbish, the captured lookbehind
// belongs to partData // belongs to partData
this.emit('partData', lookbehind, 0, prevIndex)
this.$partData(lookbehind.slice(0, prevIndex))
prevIndex = 0 prevIndex = 0
mark('partData') this.#mark('partData', idx)
// reconsider the current character even so it interrupted the sequence // reconsider the current character even so it interrupted the sequence
// it could be the beginning of a new sequence // it could be the beginning of a new sequence
i-- idx--
} }
break break
@ -293,28 +298,26 @@ export class MultipartParser extends EventEmitter {
break break
default: default:
return i return
} }
} }
dataCallback('headerField') this.#emit('headerField', buffer, idx)
dataCallback('headerValue') this.#emit('headerValue', buffer, idx)
dataCallback('partData') this.#emit('partData', buffer, idx)
this.index = index this.index = index
this.state = state this.state = state
this.flags = flags this.flags = flags
} }
end() { end() {
if ( if (
(this.state === STATE_DICT.HEADER_FIELD_START && this.index === 0) || (this.state === STATE_DICT.HEADER_FIELD_START && this.index === 0) ||
(this.state === STATE_DICT.PART_DATA && this.index == this.boundary.length) (this.state === STATE_DICT.PART_DATA &&
this.index == this.boundary.length)
) { ) {
this.emit('end') this.$end()
} else if (this.state !== STATE_DICT.END) { } else if (this.state !== STATE_DICT.END) {
return new Error( return new Error(
'MultipartParser.end(): stream ended unexpectedly: ' + this.explain() 'MultipartParser.end(): stream ended unexpectedly: ' + this.explain()

View File

@ -22,6 +22,5 @@ export class UrlencodedParser extends EventEmitter {
this.emit('field', fields) this.emit('field', fields)
this.emit('end') this.emit('end')
} }
} }