From ef41691e408ef1c1c7a921cc1050bdd533651331 Mon Sep 17 00:00:00 2001 From: Saad Jutt Date: Sun, 20 Feb 2022 06:18:44 +0500 Subject: [PATCH] fix(file): fixes response headers --- api/src/controllers/code.ts | 5 ++++- api/src/controllers/internal/Execution.ts | 2 +- api/src/controllers/stp.ts | 10 +++++++--- api/src/routes/api/code.ts | 6 ++++++ api/src/routes/api/stp.ts | 12 ++++++++++++ api/src/utils/extractHeaders.ts | 4 ++-- api/src/utils/specs/extractHeaders.spec.ts | 6 +++--- 7 files changed, 35 insertions(+), 10 deletions(-) diff --git a/api/src/controllers/code.ts b/api/src/controllers/code.ts index b2a3c49..85e3d54 100644 --- a/api/src/controllers/code.ts +++ b/api/src/controllers/code.ts @@ -41,7 +41,10 @@ const executeSASCode = async (req: any, { code }: ExecuteSASCodePayload) => { true )) as ExecuteReturnJson - if (webout instanceof Buffer) return webout + if (webout instanceof Buffer) { + ;(req as any).sasHeaders = httpHeaders + return webout + } return { status: 'success', diff --git a/api/src/controllers/internal/Execution.ts b/api/src/controllers/internal/Execution.ts index 04e5084..6856923 100644 --- a/api/src/controllers/internal/Execution.ts +++ b/api/src/controllers/internal/Execution.ts @@ -148,7 +148,7 @@ ${program}` ? await readFile(headersPath) : '' const httpHeaders: HTTPHeaders = extractHeaders(headersContent) - const fileResponse: boolean = httpHeaders.hasOwnProperty('Content-Type') + const fileResponse: boolean = httpHeaders.hasOwnProperty('content-type') const webout = (await fileExists(weboutPath)) ? fileResponse diff --git a/api/src/controllers/stp.ts b/api/src/controllers/stp.ts index b5390ca..2335e5e 100644 --- a/api/src/controllers/stp.ts +++ b/api/src/controllers/stp.ts @@ -113,8 +113,9 @@ const executeReturnRaw = async ( req.res?.set(httpHeaders) - // if (result instanceof Buffer) - // req.res?.writeHead(200, httpHeaders) + if (result instanceof Buffer) { + ;(req as any).sasHeaders = httpHeaders + } return result } catch (err: any) { @@ -148,7 +149,10 @@ const executeReturnJson = async ( true )) as ExecuteReturnJson - if (webout instanceof Buffer) return webout + if (webout instanceof Buffer) { + ;(req as any).sasHeaders = httpHeaders + return webout + } return { status: 'success', diff --git a/api/src/routes/api/code.ts b/api/src/routes/api/code.ts index fb751b5..efeaccd 100644 --- a/api/src/routes/api/code.ts +++ b/api/src/routes/api/code.ts @@ -12,6 +12,12 @@ runRouter.post('/execute', async (req, res) => { try { const response = await controller.executeSASCode(req, body) + + if (response instanceof Buffer) { + res.writeHead(200, (req as any).sasHeaders) + return res.end(response) + } + res.send(response) } catch (err: any) { const statusCode = err.code diff --git a/api/src/routes/api/stp.ts b/api/src/routes/api/stp.ts index 9879a7c..425208e 100644 --- a/api/src/routes/api/stp.ts +++ b/api/src/routes/api/stp.ts @@ -14,6 +14,12 @@ stpRouter.get('/execute', async (req, res) => { try { const response = await controller.executeReturnRaw(req, query._program) + + if (response instanceof Buffer) { + res.writeHead(200, (req as any).sasHeaders) + return res.end(response) + } + res.send(response) } catch (err: any) { const statusCode = err.code @@ -40,6 +46,12 @@ stpRouter.post( body, query?._program ) + + if (response instanceof Buffer) { + res.writeHead(200, (req as any).sasHeaders) + return res.end(response) + } + res.send(response) } catch (err: any) { const statusCode = err.code diff --git a/api/src/utils/extractHeaders.ts b/api/src/utils/extractHeaders.ts index bba76c0..bf64a14 100644 --- a/api/src/utils/extractHeaders.ts +++ b/api/src/utils/extractHeaders.ts @@ -15,9 +15,9 @@ export const extractHeaders = (content?: string): HTTPHeaders => { const [key, value] = headerStr.split(':').map((data) => data.trim()) if (value && headerUtils.validateHeader(key, value)) { - headersObj[key] = value + headersObj[key.toLowerCase()] = value } else { - delete headersObj[key] + delete headersObj[key.toLowerCase()] } }) diff --git a/api/src/utils/specs/extractHeaders.spec.ts b/api/src/utils/specs/extractHeaders.spec.ts index 4d5f5d0..9d29f04 100644 --- a/api/src/utils/specs/extractHeaders.spec.ts +++ b/api/src/utils/specs/extractHeaders.spec.ts @@ -12,8 +12,8 @@ describe('extractHeaders', () => { `) expect(headers).toEqual({ - 'Content-type': 'application/zip', - 'Cache-Control': 'public, max-age=1000' + 'content-type': 'application/zip', + 'cache-control': 'public, max-age=1000' }) }) @@ -25,7 +25,7 @@ describe('extractHeaders', () => { Content-type: `) - expect(headers).toEqual({ 'Cache-Control': 'public, max-age=1000' }) + expect(headers).toEqual({ 'cache-control': 'public, max-age=1000' }) }) it('should return only valid http headers', () => {