web addon frontend fix breakpoint and fix send message

addon-dailer
lqqyt2423 4 years ago
parent 20d3f26411
commit 6bb96b85ff

@ -22,13 +22,16 @@ const stringifyRequest = (request: IRequest) => {
}
const parseRequest = (content: string): IRequest | undefined => {
const sections = content.split('\n\n')
if (sections.length !== 3) return
const firstIndex = content.indexOf('\n\n')
if (firstIndex <= 0) return
const [firstLine, headerLines, bodyLines] = sections
const firstLine = content.slice(0, firstIndex)
const [method, url] = firstLine.split(' ')
if (!method || !url) return
const secondIndex = content.indexOf('\n\n', firstIndex + 2)
if (secondIndex <= 0) return
const headerLines = content.slice(firstIndex + 2, secondIndex)
const header: Header = {}
for (const line of headerLines.split('\n')) {
const [key, vals] = line.split(': ')
@ -36,6 +39,7 @@ const parseRequest = (content: string): IRequest | undefined => {
header[key] = vals.split(' \t ')
}
const bodyLines = content.slice(secondIndex + 2)
let body: ArrayBuffer | undefined
if (bodyLines) body = new TextEncoder().encode(bodyLines)
@ -62,13 +66,16 @@ const stringifyResponse = (response: IResponse) => {
}
const parseResponse = (content: string): IResponse | undefined => {
const sections = content.split('\n\n')
if (sections.length !== 3) return
const firstIndex = content.indexOf('\n\n')
if (firstIndex <= 0) return
const [firstLine, headerLines, bodyLines] = sections
const firstLine = content.slice(0, firstIndex)
const statusCode = parseInt(firstLine)
if (isNaN(statusCode)) return
const secondIndex = content.indexOf('\n\n', firstIndex + 2)
if (secondIndex <= 0) return
const headerLines = content.slice(firstIndex + 2, secondIndex)
const header: Header = {}
for (const line of headerLines.split('\n')) {
const [key, vals] = line.split(': ')
@ -76,6 +83,7 @@ const parseResponse = (content: string): IResponse | undefined => {
header[key] = vals.split(' \t ')
}
const bodyLines = content.slice(secondIndex + 2)
let body: ArrayBuffer | undefined
if (bodyLines) body = new TextEncoder().encode(bodyLines)

@ -102,7 +102,7 @@ export const buildMessageEdit = (messageType: SendMessageType, flow: IFlow) => {
}
let header: Omit<IRequest, 'body'> | Omit<IResponse, 'body'>
let body: ArrayBuffer | undefined
let body: ArrayBuffer | Uint8Array | undefined
if (messageType === SendMessageType.CHANGE_REQUEST) {
({ body, ...header } = flow.request)
@ -112,7 +112,13 @@ export const buildMessageEdit = (messageType: SendMessageType, flow: IFlow) => {
throw new Error('invalid message type')
}
if (body instanceof ArrayBuffer) body = new Uint8Array(body)
const bodyLen = (body && body.byteLength) ? body.byteLength : 0
if ('Content-Encoding' in header.header) delete header.header['Content-Encoding']
if ('Transfer-Encoding' in header.header) delete header.header['Transfer-Encoding']
header.header['Content-Length'] = [String(bodyLen)]
const headerBytes = new TextEncoder().encode(JSON.stringify(header))
const len = 2 + 36 + 4 + headerBytes.byteLength + 4 + bodyLen
const data = new ArrayBuffer(len)
@ -121,7 +127,7 @@ export const buildMessageEdit = (messageType: SendMessageType, flow: IFlow) => {
view[1] = messageType
view.set(new TextEncoder().encode(flow.id), 2)
view.set(headerBytes, 2 + 36 + 4)
if (bodyLen) view.set(body as any, 2 + 36 + 4 + headerBytes.byteLength + 4)
if (bodyLen) view.set(body as Uint8Array, 2 + 36 + 4 + headerBytes.byteLength + 4)
const view2 = new DataView(data)
view2.setUint32(2 + 36, headerBytes.byteLength)

Loading…
Cancel
Save