Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: update method matching #5419

Merged
merged 9 commits into from May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 16 additions & 21 deletions lib/handleRequest.js
@@ -1,5 +1,6 @@
'use strict'

const { bodylessMethods, bodyMethods } = require('./httpMethods')
const { validate: validateSchema } = require('./validation')
const { preValidationHookRunner, preHandlerHookRunner } = require('./hooks')
const wrapThenable = require('./wrapThenable')
Expand All @@ -20,42 +21,36 @@ function handleRequest (err, request, reply) {
const headers = request.headers
const context = request[kRouteContext]

if (method === 'GET' || method === 'HEAD') {
if (bodylessMethods.has(method)) {
handler(request, reply)
return
}

const contentType = headers['content-type']
if (bodyMethods.has(method)) {
const contentType = headers['content-type']
const contentLength = headers['content-length']
const transferEncoding = headers['transfer-encoding']

if (method === 'POST' || method === 'PUT' || method === 'PATCH' || method === 'TRACE' || method === 'SEARCH' ||
method === 'PROPFIND' || method === 'PROPPATCH' || method === 'LOCK') {
if (contentType === undefined) {
if (
headers['transfer-encoding'] === undefined &&
(headers['content-length'] === '0' || headers['content-length'] === undefined)
) { // Request has no body to parse
(contentLength === undefined || contentLength === '0') &&
transferEncoding === undefined
) {
// Request has no body to parse
handler(request, reply)
} else {
context.contentTypeParser.run('', handler, request, reply)
}
} else {
context.contentTypeParser.run(contentType, handler, request, reply)
}
return
}
if (contentLength === undefined && transferEncoding === undefined && method === 'OPTIONS') {
// OPTIONS can have a Content-Type header without a body
handler(request, reply)
return
}

if (method === 'OPTIONS' || method === 'DELETE') {
if (
contentType !== undefined &&
(
headers['transfer-encoding'] !== undefined ||
headers['content-length'] !== undefined
)
) {
context.contentTypeParser.run(contentType, handler, request, reply)
} else {
handler(request, reply)
}

return
}

Expand Down
48 changes: 32 additions & 16 deletions lib/httpMethods.js
@@ -1,22 +1,38 @@
'use strict'

const bodylessMethods = new Set([
// Standard
'GET',
'HEAD',
'TRACE',

// WebDAV
'UNLOCK'
])

const bodyMethods = new Set([
// Standard
'DELETE',
'OPTIONS',
'PATCH',
'PUT',
'POST',

// WebDAV
'COPY',
'LOCK',
'MOVE',
'MKCOL',
'PROPFIND',
'PROPPATCH',
'SEARCH'
])

module.exports = {
bodylessMethods,
bodyMethods,
supportedMethods: [
'DELETE',
'GET',
'HEAD',
'PATCH',
'POST',
'PUT',
'OPTIONS',
'PROPFIND',
'PROPPATCH',
'MKCOL',
'COPY',
'MOVE',
'LOCK',
'UNLOCK',
'TRACE',
'SEARCH'
...bodylessMethods,
...bodyMethods
]
}
22 changes: 21 additions & 1 deletion test/delete.test.js
Expand Up @@ -300,8 +300,28 @@ fastify.listen({ port: 0 }, err => {
})
})

test('shorthand - delete with application/json Content-Type header and null body', t => {
t.plan(4)
const fastify = require('..')()
fastify.delete('/', {}, (req, reply) => {
t.equal(req.body, null)
reply.send(req.body)
})
fastify.inject({
method: 'DELETE',
url: '/',
headers: { 'Content-Type': 'application/json' },
body: 'null'
}, (err, response) => {
t.error(err)
t.equal(response.statusCode, 200)
t.same(response.payload.toString(), 'null')
})
})

// https://github.com/fastify/fastify/issues/936
test('shorthand - delete with application/json Content-Type header and without body', t => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand the RFC etc.. but this stops the fastify adoption to big (legacy) companies and tools.

I would create a new issue/feature request as follow-up, to let the user to customise the new bodylessMethods prop

Copy link
Member Author

@gurgunday gurgunday Apr 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Problem is DELETE is also in the class of methods that is permitted to have a body, so in that case DELETE would be broken for requests with with a body — not the best tradeoff even if we let customization imo

The issue in that test is sending the Content-Type header application/json with an empty body, this is just an incorrect request — they should've sent something like '{}' or not set the Content-Type header...

But I can add a 'DELETE' string literal here if you think this would really cause companies a headache:

if (contentLength === undefined && transferEncoding === undefined && method === 'OPTIONS') {
// OPTIONS can have a Content-Type header without a body
handler(request, reply)
return
}

 if (contentLength === undefined && transferEncoding === undefined && (method === 'OPTIONS' || method === 'DELETE')) { 
   // OPTIONS (and DELETE) can have a Content-Type header without a body 
   handler(request, reply) 
   return 
 } 

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue in that test is sending (an invalid) Content-Type header like application/json with an empty body, this is just an incorrect request

But null is a valid JSON: is the body empty/missing from the request?
I thought inject submits null as body payload.

In this case let's skip it

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean this passes:

// https://github.com/fastify/fastify/issues/936
test('shorthand - delete with application/json Content-Type header and without body', t => {
  t.plan(4)
  const fastify = require('..')()
  fastify.delete('/', {}, (req, reply) => {
    t.equal(req.body, null)
    reply.send(req.body)
  })
  fastify.inject({
    method: 'DELETE',
    url: '/',
    headers: { 'Content-Type': 'application/json' },
    body: 'null'
  }, (err, response) => {
    t.error(err)
    t.equal(response.statusCode, 200)
    t.same(response.payload.toString(), 'null')
  })
})

// Skip this test because this is an invalid request
test('shorthand - delete with application/json Content-Type header and without body', { skip: 'https://github.com/fastify/fastify/pull/5419' }, t => {
t.plan(4)
const fastify = require('..')()
fastify.delete('/', {}, (req, reply) => {
Expand Down
6 changes: 1 addition & 5 deletions test/helper.js
Expand Up @@ -216,11 +216,7 @@ module.exports.payloadMethod = function (method, t, isSetErrorHandler = false) {

}, (err, response, body) => {
t.error(err)
if (upMethod === 'OPTIONS') {
t.equal(response.statusCode, 200)
} else {
t.equal(response.statusCode, 415)
}
t.equal(response.statusCode, 415)
})
})

Expand Down
24 changes: 24 additions & 0 deletions test/method-missing.test.js
@@ -0,0 +1,24 @@
const http = require('http')
const { test } = require('tap')
const Fastify = require('../fastify')

test('missing method from http client', t => {
t.plan(2)
const fastify = Fastify()

fastify.listen({ port: 3000 }, (err) => {
t.error(err)

const port = fastify.server.address().port
const req = http.request({
port,
method: 'REBIND',
path: '/'
}, (res) => {
t.equal(res.statusCode, 404)
fastify.close()
})

req.end()
})
})