Skip to content

Commit

Permalink
fix: empty streaming body
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Jul 11, 2020
1 parent 93d3d9c commit 0ccbaff
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions lib/client-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,17 @@ function write (client, request) {
if (method !== 'PUT' && method !== 'POST' && method !== 'PATCH') {
contentLength = undefined
}

if (streaming && typeof body.destroy === 'function' && !body.destroyed) {
// Wait for 'end' before destroying to avoid close before end errors.
body
.on('end', function () {
this.destroy()
})
.resume()
}

body = null
}

if (request.contentLength !== undefined && request.contentLength !== contentLength) {
Expand All @@ -752,20 +763,18 @@ function write (client, request) {
socket.write(`content-length: ${contentLength}\r\n`, 'ascii')
}

// TODO: What if HEAD and body?
// TODO: What if not HEAD and headers['content-length'] !== contentLength?

if (contentLength === 0) {
if (!body) {
socket.write(CRLF)
socket.write(CRLF)
if (body && typeof body.destroy === 'function' && !body.destroyed) {
body.destroy()
if (contentLength === 0 && method !== 'HEAD') {
socket.write(CRLF)
}
} else if (!streaming) {
socket.write(CRLF)
if (body && contentLength !== undefined) {
socket.write(body)
socket.write(CRLF)
}
socket.write(body)
socket.write(CRLF)
} else {
const chunked = contentLength === undefined

Expand Down

0 comments on commit 0ccbaff

Please sign in to comment.