Skip to content

Commit

Permalink
bypass GET request in sending content-length
Browse files Browse the repository at this point in the history
  • Loading branch information
pxue committed Oct 17, 2023
1 parent 741b811 commit e428cf8
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 38 deletions.
5 changes: 2 additions & 3 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1624,10 +1624,9 @@ function write (client, request) {
if (channels.sendHeaders.hasSubscribers) {
channels.sendHeaders.publish({ request, headers: header, socket })
}

/* istanbul ignore else: assertion */
if (!body) {
if (contentLength === 0) {
if (!body || !shouldSendContentLength(method)) {
if (contentLength !== null) {
socket.write(`${header}content-length: 0\r\n\r\n`, 'latin1')
} else {
assert(contentLength === null, 'no body must not have content length')
Expand Down
16 changes: 12 additions & 4 deletions test/client-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1148,18 +1148,22 @@ test('invalid signal', (t) => {
})

test('invalid body chunk does not crash', (t) => {
t.plan(1)
t.plan(3)

const server = createServer()
server.on('request', (req, res) => {
res.end()
res.end('hello')
})
t.teardown(server.close.bind(server))

server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`)
t.teardown(client.destroy.bind(client))

let dumped = false
client.on('disconnect', () => {
t.equal(dumped, true)
})
client.request({
path: '/',
body: new Readable({
Expand All @@ -1169,8 +1173,12 @@ test('invalid body chunk does not crash', (t) => {
}
}),
method: 'GET'
}, (err) => {
t.equal(err.code, 'ERR_INVALID_ARG_TYPE')
}, (err, { body }) => {
t.error(err)
body.dump().then(() => {
dumped = true
t.pass()
})
})
})
})
Expand Down
4 changes: 2 additions & 2 deletions test/client-post.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ test('request post blob', { skip: !Blob }, (t) => {

client.request({
path: '/',
method: 'GET',
method: 'POST',
body: new Blob(['asd'], {
type: 'application/json'
})
Expand Down Expand Up @@ -61,7 +61,7 @@ test('request post arrayBuffer', { skip: !Blob }, (t) => {

client.request({
path: '/',
method: 'GET',
method: 'POST',
body: dst
}, (err, data) => {
t.error(err)
Expand Down
4 changes: 2 additions & 2 deletions test/client-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ test('request post body no missing data', { skip: nodeMajor < 16 }, (t) => {

const { body } = await client.request({
path: '/',
method: 'GET',
method: 'POST',
body: new Readable({
read () {
this.push('asd')
Expand Down Expand Up @@ -520,7 +520,7 @@ test('request post body no extra data handler', { skip: nodeMajor < 16 }, (t) =>
})
const { body } = await client.request({
path: '/',
method: 'GET',
method: 'POST',
body: reqBody,
maxRedirections: 0
})
Expand Down
4 changes: 2 additions & 2 deletions test/client-timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ test('start headers timeout after request body', (t) => {
client.dispatch({
path: '/',
body,
method: 'GET'
method: 'POST'
}, {
onConnect () {
process.nextTick(() => {
Expand Down Expand Up @@ -134,7 +134,7 @@ test('start headers timeout after async iterator request body', (t) => {
client.dispatch({
path: '/',
body,
method: 'GET'
method: 'POST'
}, {
onConnect () {
process.nextTick(() => {
Expand Down
51 changes: 27 additions & 24 deletions test/content-length.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const { Readable } = require('stream')
const { maybeWrapStream, consts } = require('./utils/async-iterators')

test('request invalid content-length', (t) => {
t.plan(7)
t.plan(5)

const server = createServer((req, res) => {
res.end()
Expand Down Expand Up @@ -61,28 +61,6 @@ test('request invalid content-length', (t) => {
t.type(err, errors.RequestContentLengthMismatchError)
})

client.request({
path: '/',
method: 'GET',
headers: {
'content-length': 4
},
body: ['asd']
}, (err, data) => {
t.type(err, errors.RequestContentLengthMismatchError)
})

client.request({
path: '/',
method: 'GET',
headers: {
'content-length': 4
},
body: ['asasdasdasdd']
}, (err, data) => {
t.type(err, errors.RequestContentLengthMismatchError)
})

client.request({
path: '/',
method: 'DELETE',
Expand Down Expand Up @@ -336,7 +314,7 @@ test('request DELETE, content-length=0, with body', (t) => {
})

test('content-length shouldSendContentLength=false', (t) => {
t.plan(15)
t.plan(16)
const server = createServer((req, res) => {
res.end()
})
Expand All @@ -351,6 +329,9 @@ test('content-length shouldSendContentLength=false', (t) => {
case '/get':
t.equal(req.headers['content-length'], undefined)
break
case '/getbody':
t.equal(req.headers['content-length'], '0')
break
}
})
t.teardown(server.close.bind(server))
Expand Down Expand Up @@ -396,6 +377,28 @@ test('content-length shouldSendContentLength=false', (t) => {
headers: {
'content-length': 4
},
body: ['asd']
}, (err, data) => {
t.error(err)
})

client.request({
path: '/',
method: 'GET',
headers: {
'content-length': 4
},
body: ['asasdasdasdd']
}, (err, data) => {
t.error(err)
})

client.request({
path: '/getbody',
method: 'GET',
headers: {
'content-length': 4
},
body: new Readable({
read () {
this.push('asd')
Expand Down
2 changes: 1 addition & 1 deletion test/get-head-body.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const { kBusy } = require('../lib/core/symbols')
const { wrapWithAsyncIterable } = require('./utils/async-iterators')

test('GET and HEAD with body should reset connection', (t) => {
t.plan(8 + 2)
t.plan(6 + 2)

const server = createServer((req, res) => {
res.end('asd')
Expand Down

0 comments on commit e428cf8

Please sign in to comment.