修复换行问题
This commit is contained in:
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "yizhi-multipart-reader",
|
"name": "yizhi-multipart-reader",
|
||||||
"version": "1.0.2",
|
"version": "1.0.3",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"types": "typing/index.d.ts",
|
"types": "typing/index.d.ts",
|
||||||
|
@ -181,24 +181,13 @@ export class MultipartReader {
|
|||||||
if (this.#boundary.compare(data, 0, this.#boundary.length) == 0) data = data.slice(this.#boundary.length)
|
if (this.#boundary.compare(data, 0, this.#boundary.length) == 0) data = data.slice(this.#boundary.length)
|
||||||
else throw new Error('multipart body error')
|
else throw new Error('multipart body error')
|
||||||
//去除\r\n
|
//去除\r\n
|
||||||
if (data[0] == 13) {
|
if (data[0] == 13 && data[1] == 10) data = data.slice(2)
|
||||||
data = data.slice(1)
|
//遇到 --\r\n,表示读取完毕
|
||||||
if (data[0] == 10) data = data.slice(1) // \r
|
else if (data[0] == 45 && data[1] == 45 && data[2] == 13 && data[3] == 10) {
|
||||||
else throw new Error('multipart body error') // \r后面必须时\n
|
|
||||||
}
|
|
||||||
//去除\n
|
|
||||||
else if (data[0] == 10) data = data.slice(1)
|
|
||||||
//遇到 -- , 可能就要结束了
|
|
||||||
else if (data[0] == 45 && data[1] == 45) {
|
|
||||||
//看看是否有换行,有换行表示结束
|
|
||||||
if (data[2] == 10 || (data[2] == 13 && data[3] == 10)) {
|
|
||||||
this.#saveHeader()
|
this.#saveHeader()
|
||||||
this.#state = ReadState.finish
|
this.#state = ReadState.finish
|
||||||
return { left: null, exit: true }
|
return { left: null, exit: true }
|
||||||
}
|
}
|
||||||
//否则就当出错
|
|
||||||
else throw new Error('multipart body error')
|
|
||||||
}
|
|
||||||
//上面的情况都不是,表示出错了
|
//上面的情况都不是,表示出错了
|
||||||
else throw new Error('multipart body error')
|
else throw new Error('multipart body error')
|
||||||
//开始读取头
|
//开始读取头
|
||||||
@ -213,26 +202,20 @@ export class MultipartReader {
|
|||||||
let endAt = 0
|
let endAt = 0
|
||||||
for (let i = 0; i < data.length; ++i) {
|
for (let i = 0; i < data.length; ++i) {
|
||||||
// 检测是不是两个连续换行
|
// 检测是不是两个连续换行
|
||||||
if (data[i] == 10) {
|
if (data[i] == 13 && data[i + 1] == 10 && data[i + 2] == 13 && data[i + 3] == 10) {
|
||||||
if (data[i + 1] == 10) {
|
endAt = i + 4
|
||||||
endAt = i + 1
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
else if (data[i + 1] == 13 && data[i + 2] == 10) {
|
|
||||||
endAt = i + 2
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
//如果头读取完成,则读取body
|
//如果头读取完成,则读取body
|
||||||
if (endAt) {
|
if (endAt) {
|
||||||
//取得头
|
//取得头
|
||||||
const header = this.#headBuffer ? Buffer.concat([this.#headBuffer, data.slice(0, endAt + 1)]) : data.slice(0, endAt + 1)
|
const header = this.#headBuffer ? Buffer.concat([this.#headBuffer, data.slice(0, endAt)]) : data.slice(0, endAt)
|
||||||
this.#headBuffer = null
|
this.#headBuffer = null
|
||||||
await this.#resolveHead((header + '').trim())
|
await this.#resolveHead((header + '').trim())
|
||||||
//读取内容
|
//读取内容
|
||||||
this.#state = ReadState.content
|
this.#state = ReadState.content
|
||||||
return { left: data.slice(endAt + 1), exit: false }
|
return { left: data.slice(endAt), exit: false }
|
||||||
}
|
}
|
||||||
//否则将内容缓存起来,下次处理
|
//否则将内容缓存起来,下次处理
|
||||||
else {
|
else {
|
||||||
@ -244,15 +227,10 @@ export class MultipartReader {
|
|||||||
//读取内容(头读取完后,就要读取内容了)
|
//读取内容(头读取完后,就要读取内容了)
|
||||||
async #readContent(data: Buffer): Promise<IReadResult> {
|
async #readContent(data: Buffer): Promise<IReadResult> {
|
||||||
for (let i = 0; i < data.length; ++i) {
|
for (let i = 0; i < data.length; ++i) {
|
||||||
//处理换行
|
|
||||||
let gotbr = 0
|
|
||||||
if (data[i] == 10) gotbr = 1
|
|
||||||
else if (data[i] == 13 && data[i + 1] == 10) gotbr = 2
|
|
||||||
|
|
||||||
//遇到换行,那么很有可能就遇到了分隔符
|
//遇到换行,那么很有可能就遇到了分隔符
|
||||||
if (gotbr) {
|
if (data[i] == 13 && data[i + 1] == 10) {
|
||||||
//看看内容够不够,不够啦?先保存起来,剩下的下次处理
|
//看看内容够不够,不够啦?先保存起来,剩下的下次处理
|
||||||
if (data.length - i - gotbr - 2 < this.#boundary.length) { //-gotbr表示减去换行符,-2表示减去分隔符开始的--
|
if (data.length - i - 4 < this.#boundary.length) { //-4表示减去分隔符开始的--以及换行符
|
||||||
// 保存数据
|
// 保存数据
|
||||||
await this.#resolveData(data.slice(0, i))
|
await this.#resolveData(data.slice(0, i))
|
||||||
//剩下的可能时分隔符的内容留着下次处理
|
//剩下的可能时分隔符的内容留着下次处理
|
||||||
@ -261,13 +239,13 @@ export class MultipartReader {
|
|||||||
//内容充足,处理内容
|
//内容充足,处理内容
|
||||||
else {
|
else {
|
||||||
//跳过换行符
|
//跳过换行符
|
||||||
i += gotbr
|
i += 2
|
||||||
//遇到了 -- ,后面很有可能时分隔符,瞧一眼
|
//遇到了 -- ,后面很有可能时分隔符,瞧一眼
|
||||||
if (data[i] == 45 && data[i + 1] == 45) {
|
if (data[i] == 45 && data[i + 1] == 45) {
|
||||||
//看看是不是遇到了分隔符
|
//看看是不是遇到了分隔符
|
||||||
if (this.#boundary.compare(data, i + 2, i + 2 + this.#boundary.length) == 0) {
|
if (this.#boundary.compare(data, i + 2, i + 2 + this.#boundary.length) == 0) {
|
||||||
//保存数据
|
//保存数据
|
||||||
await this.#resolveData(data.slice(0, i - gotbr))
|
await this.#resolveData(data.slice(0, i - 2)) //去除最后的\r\n进行读取
|
||||||
//接下来读取分隔符
|
//接下来读取分隔符
|
||||||
this.#state = ReadState.boundary
|
this.#state = ReadState.boundary
|
||||||
return { left: data.slice(i), exit: false }
|
return { left: data.slice(i), exit: false }
|
||||||
|
Reference in New Issue
Block a user