diff --git a/addon/web/client/src/components/EditFlow.tsx b/addon/web/client/src/components/EditFlow.tsx index 8be70c3..144ae58 100644 --- a/addon/web/client/src/components/EditFlow.tsx +++ b/addon/web/client/src/components/EditFlow.tsx @@ -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) diff --git a/addon/web/client/src/message.ts b/addon/web/client/src/message.ts index 07d829a..5fe6a55 100644 --- a/addon/web/client/src/message.ts +++ b/addon/web/client/src/message.ts @@ -102,7 +102,7 @@ export const buildMessageEdit = (messageType: SendMessageType, flow: IFlow) => { } let header: Omit | Omit - 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)