MCPcopy
hub / github.com/fastify/fastify / handleRequest

Function handleRequest

lib/handle-request.js:20–71  ·  view source on GitHub ↗
(err, request, reply)

Source from the content-addressed store, hash-verified

18const channels = diagnostics.tracingChannel('fastify.request.handler')
19
20function 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
73function handler (request, reply) {
74 try {

Callers 1

Calls 4

sendMethod · 0.80
statusMethod · 0.80
toStringMethod · 0.80
handlerFunction · 0.70

Tested by

no test coverage detected