-
-
Notifications
You must be signed in to change notification settings - Fork 952
/
Copy pathapp_helpers.py
398 lines (330 loc) · 15 KB
/
app_helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# Copyright 2013 by Rackspace Hosting, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Utilities for the App class."""
from __future__ import annotations
from inspect import iscoroutinefunction
from typing import IO, Iterable, List, Literal, Optional, overload, Tuple, Union
from falcon import util
from falcon._typing import AsgiMiddleware
from falcon._typing import AsgiProcessRequestMethod as APRequest
from falcon._typing import AsgiProcessRequestWsMethod
from falcon._typing import AsgiProcessResourceMethod as APResource
from falcon._typing import AsgiProcessResourceWsMethod
from falcon._typing import AsgiProcessResponseMethod as APResponse
from falcon._typing import Middleware
from falcon._typing import ProcessRequestMethod as PRequest
from falcon._typing import ProcessResourceMethod as PResource
from falcon._typing import ProcessResponseMethod as PResponse
from falcon.constants import MEDIA_JSON
from falcon.constants import MEDIA_XML
from falcon.errors import CompatibilityError
from falcon.errors import HTTPError
from falcon.request import Request
from falcon.response import Response
from falcon.util.sync import _wrap_non_coroutine_unsafe
__all__ = (
'prepare_middleware',
'prepare_middleware_ws',
'default_serialize_error',
'CloseableStreamIterator',
)
PreparedMiddlewareResult = Tuple[
Union[
Tuple[PRequest, ...], Tuple[Tuple[Optional[PRequest], Optional[PResource]], ...]
],
Tuple[PResource, ...],
Tuple[PResponse, ...],
]
AsyncPreparedMiddlewareResult = Tuple[
Union[
Tuple[APRequest, ...],
Tuple[Tuple[Optional[APRequest], Optional[APResource]], ...],
],
Tuple[APResource, ...],
Tuple[APResponse, ...],
]
@overload
def prepare_middleware(
middleware: Iterable[Middleware],
independent_middleware: bool = ...,
asgi: Literal[False] = ...,
) -> PreparedMiddlewareResult: ...
@overload
def prepare_middleware(
middleware: Iterable[AsgiMiddleware],
independent_middleware: bool = ...,
*,
asgi: Literal[True],
) -> AsyncPreparedMiddlewareResult: ...
@overload
def prepare_middleware(
middleware: Union[Iterable[Middleware], Iterable[AsgiMiddleware]],
independent_middleware: bool = ...,
asgi: bool = ...,
) -> Union[PreparedMiddlewareResult, AsyncPreparedMiddlewareResult]: ...
def prepare_middleware(
middleware: Union[Iterable[Middleware], Iterable[AsgiMiddleware]],
independent_middleware: bool = False,
asgi: bool = False,
) -> Union[PreparedMiddlewareResult, AsyncPreparedMiddlewareResult]:
"""Check middleware interfaces and prepare the methods for request handling.
Note:
This method is only applicable to WSGI apps.
Arguments:
middleware (iterable): An iterable of middleware objects.
Keyword Args:
independent_middleware (bool): ``True`` if the request and
response middleware methods should be treated independently
(default ``False``)
asgi (bool): ``True`` if an ASGI app, ``False`` otherwise
(default ``False``)
Returns:
tuple: A tuple of prepared middleware method tuples
"""
# PERF(kgriffs): do getattr calls once, in advance, so we don't
# have to do them every time in the request path.
request_mw: Union[
List[PRequest],
List[Tuple[Optional[PRequest], Optional[PResource]]],
List[APRequest],
List[Tuple[Optional[APRequest], Optional[APResource]]],
] = []
resource_mw: Union[List[APResource], List[PResource]] = []
response_mw: Union[List[APResponse], List[PResponse]] = []
for component in middleware:
# NOTE(kgriffs): Middleware that supports both WSGI and ASGI can
# append an *_async postfix to the ASGI version of the method
# to distinguish the two. Otherwise, the prefix is unnecessary.
if asgi:
process_request: Union[Optional[APRequest], Optional[PRequest]] = (
util.get_bound_method(component, 'process_request_async')
or _wrap_non_coroutine_unsafe(
util.get_bound_method(component, 'process_request')
)
)
process_resource: Union[Optional[APResource], Optional[PResource]] = (
util.get_bound_method(component, 'process_resource_async')
or _wrap_non_coroutine_unsafe(
util.get_bound_method(component, 'process_resource')
)
)
process_response: Union[Optional[APResponse], Optional[PResponse]] = (
util.get_bound_method(component, 'process_response_async')
or _wrap_non_coroutine_unsafe(
util.get_bound_method(component, 'process_response')
)
)
for m in (process_request, process_resource, process_response):
# NOTE(kgriffs): iscoroutinefunction() always returns False
# for cythonized functions.
#
# https://github.com/cython/cython/issues/2273
# https://bugs.python.org/issue38225
#
if m and not iscoroutinefunction(m) and util.is_python_func(m):
msg = (
'{} must be implemented as an awaitable coroutine. If '
'you would like to retain compatibility '
'with WSGI apps, the coroutine versions of the '
'middleware methods may be implemented side-by-side '
'by applying an *_async postfix to the method names. '
)
raise CompatibilityError(msg.format(m))
else:
process_request = util.get_bound_method(component, 'process_request')
process_resource = util.get_bound_method(component, 'process_resource')
process_response = util.get_bound_method(component, 'process_response')
for m in (process_request, process_resource, process_response):
if m and iscoroutinefunction(m):
msg = (
'{} may not implement coroutine methods and '
'remain compatible with WSGI apps without '
'using the *_async postfix to explicitly identify '
'the coroutine version of a given middleware '
'method.'
)
raise CompatibilityError(msg.format(component))
if not (process_request or process_resource or process_response):
if asgi and any(
hasattr(component, m)
for m in [
'process_startup',
'process_shutdown',
'process_request_ws',
'process_resource_ws',
]
):
# NOTE(kgriffs): This middleware only has ASGI lifespan
# event handlers
continue
msg = '{0} must implement at least one middleware method'
raise TypeError(msg.format(component))
# NOTE: depending on whether we want to execute middleware
# independently, we group response and request middleware either
# together or separately.
if independent_middleware:
if process_request:
request_mw.append(process_request) # type: ignore[arg-type]
if process_response:
response_mw.insert(0, process_response) # type: ignore[arg-type]
else:
if process_request or process_response:
request_mw.append((process_request, process_response)) # type: ignore[arg-type]
if process_resource:
resource_mw.append(process_resource) # type: ignore[arg-type]
return tuple(request_mw), tuple(resource_mw), tuple(response_mw) # type: ignore[return-value]
AsyncPreparedMiddlewareWsResult = Tuple[
Tuple[AsgiProcessRequestWsMethod, ...], Tuple[AsgiProcessResourceWsMethod, ...]
]
def prepare_middleware_ws(
middleware: Iterable[AsgiMiddleware],
) -> AsyncPreparedMiddlewareWsResult:
"""Check middleware interfaces and prepare WebSocket methods for request handling.
Note:
This method is only applicable to ASGI apps.
Arguments:
middleware (iterable): An iterable of middleware objects.
Returns:
tuple: A two-item ``(request_mw, resource_mw)`` tuple, where
*request_mw* is an ordered list of ``process_request_ws()`` methods,
and *resource_mw* is an ordered list of ``process_resource_ws()``
methods.
"""
# PERF(kgriffs): do getattr calls once, in advance, so we don't
# have to do them every time in the request path.
request_mw: List[AsgiProcessRequestWsMethod] = []
resource_mw: List[AsgiProcessResourceWsMethod] = []
process_request_ws: Optional[AsgiProcessRequestWsMethod]
process_resource_ws: Optional[AsgiProcessResourceWsMethod]
for component in middleware:
process_request_ws = util.get_bound_method(component, 'process_request_ws')
process_resource_ws = util.get_bound_method(component, 'process_resource_ws')
for m in (process_request_ws, process_resource_ws):
if not m:
continue
# NOTE(kgriffs): iscoroutinefunction() always returns False
# for cythonized functions.
#
# https://github.com/cython/cython/issues/2273
# https://bugs.python.org/issue38225
#
if not iscoroutinefunction(m) and util.is_python_func(m):
msg = '{} must be implemented as an awaitable coroutine.'
raise CompatibilityError(msg.format(m))
if process_request_ws:
request_mw.append(process_request_ws)
if process_resource_ws:
resource_mw.append(process_resource_ws)
return tuple(request_mw), tuple(resource_mw)
def default_serialize_error(req: Request, resp: Response, exception: HTTPError) -> None:
"""Serialize the given instance of HTTPError.
This function determines which of the supported media types, if
any, are acceptable by the client, and serializes the error
to the preferred type.
Currently, JSON and XML are the only supported media types. If the
client accepts both JSON and XML with equal weight, JSON will be
chosen.
Other media types can be supported by using a custom error serializer.
Note:
If a custom media type is used and the type includes a
"+json" or "+xml" suffix, the error will be serialized
to JSON or XML, respectively. If this behavior is not
desirable, a custom error serializer may be used to
override this one.
Args:
req: Instance of ``falcon.Request``
resp: Instance of ``falcon.Response``
exception: Instance of ``falcon.HTTPError``
"""
options = resp.options
predefined = (
[MEDIA_JSON, 'text/xml', MEDIA_XML]
if options.xml_error_serialization
else [MEDIA_JSON]
)
media_handlers = [mt for mt in options.media_handlers if mt not in predefined]
# NOTE(caselit,vytas): Add the registered handlers after the predefined
# ones. This ensures that in the case of an equal match, the first one
# (JSON) is selected and that the q parameter is taken into consideration
# when selecting the media handler.
preferred = req.client_prefers(predefined + media_handlers)
if preferred is None:
# NOTE(kgriffs): See if the client expects a custom media
# type based on something Falcon supports. Returning something
# is probably better than nothing, but if that is not
# desired, this behavior can be customized by adding a
# custom HTTPError serializer for the custom type.
accept = req.accept.lower()
# NOTE(kgriffs): Simple heuristic, but it's fast, and
# should be sufficiently accurate for our purposes. Does
# not take into account weights if both types are
# acceptable (simply chooses JSON). If it turns out we
# need to be more sophisticated, we can always change it
# later (YAGNI).
if '+json' in accept:
preferred = MEDIA_JSON
elif '+xml' in accept:
# NOTE(caselit): Ignore xml_error_serialization when
# checking if the media should be XML. This gives a chance to
# an XML media handler, if any, to be used.
preferred = MEDIA_XML
if preferred is not None:
handler, _, _ = options.media_handlers._resolve(
preferred, MEDIA_JSON, raise_not_found=False
)
if preferred == MEDIA_JSON:
# NOTE(caselit): Special case JSON to ensure that it's always
# possible to serialize an error in JSON even if no JSON handler
# is set in the media_handlers.
resp.data = exception.to_json(handler)
elif handler:
# NOTE(caselit): Let the app serialize the response even if it
# needs to re-get the handler, since async handlers may not have
# a sync version available.
resp.media = exception.to_dict()
elif options.xml_error_serialization:
resp.data = exception._to_xml()
# NOTE(kgriffs): No need to append the charset param, since
# utf-8 is the default for both JSON and XML.
resp.content_type = preferred
resp.append_header('Vary', 'Accept')
class CloseableStreamIterator:
"""Iterator that wraps a file-like stream with support for close().
This iterator can be used to read from an underlying file-like stream
in block_size-chunks until the response from the stream is an empty
byte string.
This class is used to wrap WSGI response streams when a
wsgi_file_wrapper is not provided by the server. The fact that it
also supports closing the underlying stream allows use of (e.g.)
Python tempfile resources that would be deleted upon close.
Args:
stream (object): Readable file-like stream object.
block_size (int): Number of bytes to read per iteration.
"""
def __init__(self, stream: IO[bytes], block_size: int) -> None:
self._stream = stream
self._block_size = block_size
def __iter__(self) -> CloseableStreamIterator:
return self
def __next__(self) -> bytes:
data = self._stream.read(self._block_size)
if data == b'':
raise StopIteration
else:
return data
def close(self) -> None:
try:
self._stream.close()
except (AttributeError, TypeError):
pass