UNPKG

3.08 kBJavaScriptView Raw
1'use strict'
2
3const { validate: validateSchema } = require('./validation')
4const { hookRunner, hookIterator } = require('./hooks')
5const wrapThenable = require('./wrapThenable')
6
7function handleRequest (err, request, reply) {
8 if (reply.sent === true) return
9 if (err != null) {
10 reply.send(err)
11 return
12 }
13
14 var method = request.raw.method
15 var headers = request.headers
16
17 if (method === 'GET' || method === 'HEAD') {
18 handler(request, reply)
19 return
20 }
21
22 var contentType = headers['content-type']
23
24 if (method === 'POST' || method === 'PUT' || method === 'PATCH') {
25 if (contentType === undefined) {
26 if (
27 headers['transfer-encoding'] === undefined &&
28 (headers['content-length'] === '0' || headers['content-length'] === undefined)
29 ) { // Request has no body to parse
30 handler(request, reply)
31 } else {
32 reply.context.contentTypeParser.run('', handler, request, reply)
33 }
34 } else {
35 reply.context.contentTypeParser.run(contentType, handler, request, reply)
36 }
37 return
38 }
39
40 if (method === 'OPTIONS' || method === 'DELETE') {
41 if (
42 contentType !== undefined &&
43 (
44 headers['transfer-encoding'] !== undefined ||
45 headers['content-length'] !== undefined
46 )
47 ) {
48 reply.context.contentTypeParser.run(contentType, handler, request, reply)
49 } else {
50 handler(request, reply)
51 }
52 return
53 }
54
55 // Return 404 instead of 405 see https://github.com/fastify/fastify/pull/862 for discussion
56 reply.code(404).send(new Error('Not Found'))
57}
58
59function handler (request, reply) {
60 if (reply.context.preValidation !== null) {
61 hookRunner(
62 reply.context.preValidation,
63 hookIterator,
64 request,
65 reply,
66 preValidationCallback
67 )
68 } else {
69 preValidationCallback(null, request, reply)
70 }
71}
72
73function preValidationCallback (err, request, reply) {
74 if (reply.sent === true ||
75 reply.res.writableEnded === true ||
76 (reply.res.writable === false && reply.res.finished === true)) return
77
78 if (err != null) {
79 reply.send(err)
80 return
81 }
82
83 var result = validateSchema(reply.context, request)
84 if (result) {
85 if (reply.context.attachValidation === false) {
86 reply.code(400).send(result)
87 return
88 }
89
90 reply.request.validationError = result
91 }
92
93 // preHandler hook
94 if (reply.context.preHandler !== null) {
95 hookRunner(
96 reply.context.preHandler,
97 hookIterator,
98 request,
99 reply,
100 preHandlerCallback
101 )
102 } else {
103 preHandlerCallback(null, request, reply)
104 }
105}
106
107function preHandlerCallback (err, request, reply) {
108 if (reply.sent ||
109 reply.res.writableEnded === true ||
110 (reply.res.writable === false && reply.res.finished === true)) return
111
112 if (err != null) {
113 reply.send(err)
114 return
115 }
116
117 var result = reply.context.handler(request, reply)
118 if (result && typeof result.then === 'function') {
119 wrapThenable(result, reply)
120 }
121}
122
123module.exports = handleRequest
124module.exports[Symbol.for('internals')] = { handler, preHandlerCallback }