request/lib/multipart_parser.js

329 lines
8.3 KiB
JavaScript
Raw Normal View History

2020-09-16 20:07:28 +08:00
var s = 0,
S = {
PARSER_UNINITIALIZED: s++,
START: s++,
START_BOUNDARY: s++,
HEADER_FIELD_START: s++,
HEADER_FIELD: s++,
HEADER_VALUE_START: s++,
HEADER_VALUE: s++,
HEADER_VALUE_ALMOST_DONE: s++,
HEADERS_ALMOST_DONE: s++,
PART_DATA_START: s++,
PART_DATA: s++,
PART_END: s++,
END: s++
},
f = 1,
F = {
PART_BOUNDARY: f,
LAST_BOUNDARY: (f *= 2)
},
LF = 10,
CR = 13,
SPACE = 32,
HYPHEN = 45,
COLON = 58,
A = 97,
Z = 122,
2023-10-26 19:02:46 +08:00
lower = function (c) {
2020-09-16 20:07:28 +08:00
return c | 0x20
}
2023-10-25 18:45:16 +08:00
export class MultipartParser {
boundary = null
boundaryChars = null
lookbehind = null
state = S.PARSER_UNINITIALIZED
2020-09-16 20:07:28 +08:00
2023-10-25 18:45:16 +08:00
index = null
flags = 0
2020-09-16 20:07:28 +08:00
2023-10-26 19:02:46 +08:00
static stateToString(stateNumber) {
2023-10-25 18:45:16 +08:00
for (var state in S) {
var number = S[state]
if (number === stateNumber) return state
}
2020-09-16 20:07:28 +08:00
}
2023-10-25 18:45:16 +08:00
initWithBoundary(str) {
this.boundary = Buffer.alloc(str.length + 4)
this.boundary.write('\r\n--', 0)
this.boundary.write(str, 4)
this.lookbehind = Buffer.alloc(this.boundary.length + 8)
this.state = S.START
2023-10-26 19:02:46 +08:00
2023-10-25 18:45:16 +08:00
this.boundaryChars = {}
for (var i = 0; i < this.boundary.length; i++) {
this.boundaryChars[this.boundary[i]] = true
2020-09-16 20:07:28 +08:00
}
2023-10-25 18:45:16 +08:00
}
2023-10-26 19:02:46 +08:00
2023-10-25 18:45:16 +08:00
write(buffer) {
var self = this,
i = 0,
len = buffer.length,
prevIndex = this.index,
index = this.index,
state = this.state,
flags = this.flags,
lookbehind = this.lookbehind,
boundary = this.boundary,
boundaryChars = this.boundaryChars,
boundaryLength = this.boundary.length,
boundaryEnd = boundaryLength - 1,
bufferLength = buffer.length,
c,
cl,
2023-10-26 19:02:46 +08:00
mark = function (name) {
2023-10-25 18:45:16 +08:00
self[name + 'Mark'] = i
},
2023-10-26 19:02:46 +08:00
clear = function (name) {
2023-10-25 18:45:16 +08:00
delete self[name + 'Mark']
},
2023-10-26 19:02:46 +08:00
callback = function (name, buffer, start, end) {
2023-10-25 18:45:16 +08:00
if (start !== undefined && start === end) {
return
}
2023-10-26 19:02:46 +08:00
2023-10-25 18:45:16 +08:00
var callbackSymbol =
'on' + name.substr(0, 1).toUpperCase() + name.substr(1)
if (callbackSymbol in self) {
self[callbackSymbol](buffer, start, end)
}
},
2023-10-26 19:02:46 +08:00
dataCallback = function (name, clear) {
2023-10-25 18:45:16 +08:00
var markSymbol = name + 'Mark'
if (!(markSymbol in self)) {
return
}
2023-10-26 19:02:46 +08:00
2023-10-25 18:45:16 +08:00
if (!clear) {
callback(name, buffer, self[markSymbol], buffer.length)
self[markSymbol] = 0
} else {
callback(name, buffer, self[markSymbol], i)
delete self[markSymbol]
}
}
2023-10-26 19:02:46 +08:00
2023-10-25 18:45:16 +08:00
for (i = 0; i < len; i++) {
c = buffer[i]
switch (state) {
case S.PARSER_UNINITIALIZED:
return i
case S.START:
index = 0
state = S.START_BOUNDARY
case S.START_BOUNDARY:
if (index == boundary.length - 2) {
if (c == HYPHEN) {
flags |= F.LAST_BOUNDARY
} else if (c != CR) {
return i
}
index++
break
} else if (index - 1 == boundary.length - 2) {
if (flags & F.LAST_BOUNDARY && c == HYPHEN) {
callback('end')
state = S.END
flags = 0
} else if (!(flags & F.LAST_BOUNDARY) && c == LF) {
index = 0
callback('partBegin')
state = S.HEADER_FIELD_START
} else {
return i
}
break
2020-09-16 20:07:28 +08:00
}
2023-10-26 19:02:46 +08:00
2023-10-25 18:45:16 +08:00
if (c != boundary[index + 2]) {
index = -2
}
if (c == boundary[index + 2]) {
index++
2020-09-16 20:07:28 +08:00
}
break
2023-10-25 18:45:16 +08:00
case S.HEADER_FIELD_START:
state = S.HEADER_FIELD
mark('headerField')
index = 0
case S.HEADER_FIELD:
if (c == CR) {
clear('headerField')
state = S.HEADERS_ALMOST_DONE
break
}
2023-10-26 19:02:46 +08:00
2020-09-16 20:07:28 +08:00
index++
2023-10-25 18:45:16 +08:00
if (c == HYPHEN) {
break
}
2023-10-26 19:02:46 +08:00
2023-10-25 18:45:16 +08:00
if (c == COLON) {
if (index == 1) {
// empty header field
return i
}
dataCallback('headerField', true)
state = S.HEADER_VALUE_START
break
}
2023-10-26 19:02:46 +08:00
2023-10-25 18:45:16 +08:00
cl = lower(c)
if (cl < A || cl > Z) {
return i
}
2020-09-16 20:07:28 +08:00
break
2023-10-25 18:45:16 +08:00
case S.HEADER_VALUE_START:
if (c == SPACE) {
break
}
2023-10-26 19:02:46 +08:00
2023-10-25 18:45:16 +08:00
mark('headerValue')
state = S.HEADER_VALUE
case S.HEADER_VALUE:
if (c == CR) {
dataCallback('headerValue', true)
callback('headerEnd')
state = S.HEADER_VALUE_ALMOST_DONE
}
2020-09-16 20:07:28 +08:00
break
2023-10-25 18:45:16 +08:00
case S.HEADER_VALUE_ALMOST_DONE:
if (c != LF) {
2020-09-16 20:07:28 +08:00
return i
}
2023-10-25 18:45:16 +08:00
state = S.HEADER_FIELD_START
2020-09-16 20:07:28 +08:00
break
2023-10-25 18:45:16 +08:00
case S.HEADERS_ALMOST_DONE:
if (c != LF) {
return i
}
2023-10-26 19:02:46 +08:00
2023-10-25 18:45:16 +08:00
callback('headersEnd')
state = S.PART_DATA_START
2020-09-16 20:07:28 +08:00
break
2023-10-25 18:45:16 +08:00
case S.PART_DATA_START:
state = S.PART_DATA
mark('partData')
case S.PART_DATA:
prevIndex = index
2023-10-26 19:02:46 +08:00
2023-10-25 18:45:16 +08:00
if (index === 0) {
// boyer-moore derrived algorithm to safely skip non-boundary data
i += boundaryEnd
while (i < bufferLength && !(buffer[i] in boundaryChars)) {
i += boundaryLength
}
i -= boundaryEnd
c = buffer[i]
2020-09-16 20:07:28 +08:00
}
2023-10-26 19:02:46 +08:00
2023-10-25 18:45:16 +08:00
if (index < boundary.length) {
if (boundary[index] == c) {
if (index === 0) {
dataCallback('partData', true)
}
index++
} else {
index = 0
2020-09-16 20:07:28 +08:00
}
2023-10-25 18:45:16 +08:00
} else if (index == boundary.length) {
2020-09-16 20:07:28 +08:00
index++
2023-10-25 18:45:16 +08:00
if (c == CR) {
// CR = part boundary
flags |= F.PART_BOUNDARY
} else if (c == HYPHEN) {
// HYPHEN = end boundary
flags |= F.LAST_BOUNDARY
} else {
index = 0
2020-09-16 20:07:28 +08:00
}
2023-10-25 18:45:16 +08:00
} else if (index - 1 == boundary.length) {
if (flags & F.PART_BOUNDARY) {
index = 0
if (c == LF) {
// unset the PART_BOUNDARY flag
flags &= ~F.PART_BOUNDARY
callback('partEnd')
callback('partBegin')
state = S.HEADER_FIELD_START
break
}
} else if (flags & F.LAST_BOUNDARY) {
if (c == HYPHEN) {
callback('partEnd')
callback('end')
state = S.END
flags = 0
} else {
index = 0
}
2020-09-16 20:07:28 +08:00
} else {
index = 0
}
}
2023-10-26 19:02:46 +08:00
2023-10-25 18:45:16 +08:00
if (index > 0) {
// when matching a possible boundary, keep a lookbehind reference
// in case it turns out to be a false lead
lookbehind[index - 1] = c
} else if (prevIndex > 0) {
// if our boundary turned out to be rubbish, the captured lookbehind
// belongs to partData
callback('partData', lookbehind, 0, prevIndex)
prevIndex = 0
mark('partData')
2023-10-26 19:02:46 +08:00
2023-10-25 18:45:16 +08:00
// reconsider the current character even so it interrupted the sequence
// it could be the beginning of a new sequence
i--
}
2023-10-26 19:02:46 +08:00
2023-10-25 18:45:16 +08:00
break
case S.END:
break
default:
return i
}
2020-09-16 20:07:28 +08:00
}
2023-10-26 19:02:46 +08:00
2023-10-25 18:45:16 +08:00
dataCallback('headerField')
dataCallback('headerValue')
dataCallback('partData')
2023-10-26 19:02:46 +08:00
2023-10-25 18:45:16 +08:00
this.index = index
this.state = state
this.flags = flags
2023-10-26 19:02:46 +08:00
2023-10-25 18:45:16 +08:00
return len
2020-09-16 20:07:28 +08:00
}
2023-10-26 19:02:46 +08:00
2023-10-25 18:45:16 +08:00
end() {
2023-10-26 19:02:46 +08:00
var callback = function (self, name) {
var callbackSymbol =
'on' + name.substr(0, 1).toUpperCase() + name.substr(1)
2023-10-25 18:45:16 +08:00
if (callbackSymbol in self) {
self[callbackSymbol]()
}
}
if (
(this.state == S.HEADER_FIELD_START && this.index === 0) ||
(this.state == S.PART_DATA && this.index == this.boundary.length)
) {
callback(this, 'partEnd')
callback(this, 'end')
} else if (this.state != S.END) {
return new Error(
'MultipartParser.end(): stream ended unexpectedly: ' + this.explain()
)
2020-09-16 20:07:28 +08:00
}
}
2023-10-26 19:02:46 +08:00
2023-10-25 18:45:16 +08:00
explain() {
return 'state = ' + MultipartParser.stateToString(this.state)
2020-09-16 20:07:28 +08:00
}
}