(err, request, reply)
| 18 | const channels = diagnostics.tracingChannel('fastify.request.handler') |
| 19 | |
| 20 | function handleRequest (err, request, reply) { |
| 21 | if (reply.sent === true) return |
| 22 | if (err != null) { |
| 23 | reply[kReplyIsError] = true |
| 24 | reply.send(err) |
| 25 | return |
| 26 | } |
| 27 | |
| 28 | const method = request.method |
| 29 | |
| 30 | if (this[kSupportedHTTPMethods].bodyless.has(method)) { |
| 31 | handler(request, reply) |
| 32 | return |
| 33 | } |
| 34 | |
| 35 | if (this[kSupportedHTTPMethods].bodywith.has(method)) { |
| 36 | const headers = request.headers |
| 37 | const ctHeader = headers['content-type'] |
| 38 | |
| 39 | if (ctHeader === undefined) { |
| 40 | const contentLength = headers['content-length'] |
| 41 | const transferEncoding = headers['transfer-encoding'] |
| 42 | const isEmptyBody = transferEncoding === undefined && |
| 43 | (contentLength === undefined || contentLength === '0') |
| 44 | |
| 45 | if (isEmptyBody) { |
| 46 | // Request has no body to parse |
| 47 | handler(request, reply) |
| 48 | return |
| 49 | } |
| 50 | |
| 51 | request[kRouteContext].contentTypeParser.run('', handler, request, reply) |
| 52 | return |
| 53 | } |
| 54 | |
| 55 | // Conditional assignment to avoid creating a new ContentType instance |
| 56 | // It can be assigned when accessing the .mediaType in hooks |
| 57 | if (!request[kRequestContentType]) { |
| 58 | request[kRequestContentType] = request[kRouteContext].contentTypeParser.getContentType(ctHeader) |
| 59 | } |
| 60 | if (request[kRequestContentType].isValid === false) { |
| 61 | reply[kReplyIsError] = true |
| 62 | reply.status(415).send(new FST_ERR_CTP_INVALID_MEDIA_TYPE()) |
| 63 | return |
| 64 | } |
| 65 | request[kRouteContext].contentTypeParser.run(request[kRequestContentType].toString(), handler, request, reply) |
| 66 | return |
| 67 | } |
| 68 | |
| 69 | // Return 404 instead of 405 see https://github.com/fastify/fastify/pull/862 for discussion |
| 70 | handler(request, reply) |
| 71 | } |
| 72 | |
| 73 | function handler (request, reply) { |
| 74 | try { |
no test coverage detected