Skip to content

Commit

Permalink
fix possible response with connection closed
Browse files Browse the repository at this point in the history
  • Loading branch information
paulo-ocean committed Aug 1, 2024
1 parent 586cf44 commit 72a43c3
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions src/components/httpRoutes/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ directCommandRoute.post(
return
}

let closedResponse = false

// detect connection closed
res.on('close', () => {
if (!closedResponse) {
HTTP_LOGGER.error('TCP connection was closed before we could send a response!')
}
closedResponse = true
})
let isBinaryContent = false
const sink = async function (source: any) {
let first = true
Expand All @@ -63,18 +72,25 @@ directCommandRoute.post(
} catch (e) {
res.status(500)
res.write(uint8ArrayToString(chunk.subarray()))
closedResponse = true
res.end()
HTTP_LOGGER.error(e.message)
}
} else {
if (isBinaryContent) {
// Binary content, could be encrypted
res.write(chunk.subarray())
} else {
const str = uint8ArrayToString(chunk.subarray())
res.write(str)
try {
if (isBinaryContent) {
// Binary content, could be encrypted
res.write(chunk.subarray())
} else {
const str = uint8ArrayToString(chunk.subarray())
res.write(str)
}
} catch (e) {
HTTP_LOGGER.error(e.message)
}
}
}
closedResponse = true
res.end()
}

Expand Down Expand Up @@ -117,9 +133,11 @@ directCommandRoute.post(
}
}

if (response.stream == null) {
// only if response was not already sent
if (response.stream == null && !closedResponse) {
res.status(response.status.httpStatus)
res.write(response.status.error)
closedResponse = true
res.end()
}
}
Expand Down

0 comments on commit 72a43c3

Please sign in to comment.