UNPKG

3.28 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 try {
61 if (reply.context.preValidation !== null) {
62 hookRunner(
63 reply.context.preValidation,
64 hookIterator,
65 request,
66 reply,
67 preValidationCallback
68 )
69 } else {
70 preValidationCallback(null, request, reply)
71 }
72 } catch (err) {
73 preValidationCallback(err, request, reply)
74 }
75}
76
77function preValidationCallback (err, request, reply) {
78 if (reply.sent === true ||
79 reply.raw.writableEnded === true ||
80 reply.raw.writable === false) return
81
82 if (err != null) {
83 reply.send(err)
84 return
85 }
86
87 var result = validateSchema(reply.context, request)
88 if (result) {
89 if (reply.context.attachValidation === false) {
90 reply.code(400).send(result)
91 return
92 }
93
94 reply.request.validationError = result
95 }
96
97 // preHandler hook
98 if (reply.context.preHandler !== null) {
99 hookRunner(
100 reply.context.preHandler,
101 hookIterator,
102 request,
103 reply,
104 preHandlerCallback
105 )
106 } else {
107 preHandlerCallback(null, request, reply)
108 }
109}
110
111function preHandlerCallback (err, request, reply) {
112 if (reply.sent ||
113 reply.raw.writableEnded === true ||
114 reply.raw.writable === false) return
115
116 if (err != null) {
117 reply.send(err)
118 return
119 }
120
121 var result
122
123 try {
124 result = reply.context.handler(request, reply)
125 } catch (err) {
126 reply.send(err)
127 return
128 }
129
130 if (result !== undefined) {
131 if (result !== null && typeof result.then === 'function') {
132 wrapThenable(result, reply)
133 } else {
134 reply.send(result)
135 }
136 }
137}
138
139module.exports = handleRequest
140module.exports[Symbol.for('internals')] = { handler, preHandlerCallback }