Skip to content

Commit

Permalink
chore: restore #5419
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumners committed May 4, 2024
1 parent 276f444 commit 5210253
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 40 deletions.
6 changes: 6 additions & 0 deletions fastify.js
Expand Up @@ -280,6 +280,9 @@ function fastify (options) {
proppatch: function _proppatch (url, options, handler) {
return router.prepareRoute.call(this, { method: 'PROPPATCH', url, options, handler })
},
mkcalendar: function _mkcalendar (url, options, handler) {
return router.prepareRoute.call(this, { method: 'MKCALENDAR', url, options, handler })
},
mkcol: function _mkcol (url, options, handler) {
return router.prepareRoute.call(this, { method: 'MKCOL', url, options, handler })
},
Expand All @@ -298,6 +301,9 @@ function fastify (options) {
trace: function _trace (url, options, handler) {
return router.prepareRoute.call(this, { method: 'TRACE', url, options, handler })
},
report: function _mkcalendar (url, options, handler) {
return router.prepareRoute.call(this, { method: 'REPORT', url, options, handler })
},
search: function _search (url, options, handler) {
return router.prepareRoute.call(this, { method: 'SEARCH', url, options, handler })
},
Expand Down
37 changes: 15 additions & 22 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,45 +21,37 @@ 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' || method === 'REPORT' || method === 'MKCALENDAR') {
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 {
if (contentLength === undefined && transferEncoding === undefined && method === 'OPTIONS') {
// OPTIONS can have a Content-Type header without a body
handler(request, reply)
return
}
context.contentTypeParser.run(contentType, 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
}

// Return 404 instead of 405 see https://github.com/fastify/fastify/pull/862 for discussion
handler(request, reply)
}
Expand Down
52 changes: 34 additions & 18 deletions lib/httpMethods.js
@@ -1,24 +1,40 @@
'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',
'REPORT',
'SEARCH',
'MKCALENDAR'
])

module.exports = {
bodylessMethods,
bodyMethods,
supportedMethods: [
'DELETE',
'GET',
'HEAD',
'PATCH',
'POST',
'PUT',
'OPTIONS',
'PROPFIND',
'PROPPATCH',
'MKCOL',
'COPY',
'MOVE',
'LOCK',
'UNLOCK',
'TRACE',
'SEARCH',
'REPORT',
'MKCALENDAR'
...bodylessMethods,
...bodyMethods
]
}

0 comments on commit 5210253

Please sign in to comment.