Ajoutez des fichiers projet.
This commit is contained in:
295
venv/Lib/site-packages/django/core/handlers/asgi.py
Normal file
295
venv/Lib/site-packages/django/core/handlers/asgi.py
Normal file
@@ -0,0 +1,295 @@
|
||||
import logging
|
||||
import sys
|
||||
import tempfile
|
||||
import traceback
|
||||
|
||||
from asgiref.sync import ThreadSensitiveContext, sync_to_async
|
||||
|
||||
from django.conf import settings
|
||||
from django.core import signals
|
||||
from django.core.exceptions import RequestAborted, RequestDataTooBig
|
||||
from django.core.handlers import base
|
||||
from django.http import (
|
||||
FileResponse, HttpRequest, HttpResponse, HttpResponseBadRequest,
|
||||
HttpResponseServerError, QueryDict, parse_cookie,
|
||||
)
|
||||
from django.urls import set_script_prefix
|
||||
from django.utils.functional import cached_property
|
||||
|
||||
logger = logging.getLogger('django.request')
|
||||
|
||||
|
||||
class ASGIRequest(HttpRequest):
|
||||
"""
|
||||
Custom request subclass that decodes from an ASGI-standard request dict
|
||||
and wraps request body handling.
|
||||
"""
|
||||
# Number of seconds until a Request gives up on trying to read a request
|
||||
# body and aborts.
|
||||
body_receive_timeout = 60
|
||||
|
||||
def __init__(self, scope, body_file):
|
||||
self.scope = scope
|
||||
self._post_parse_error = False
|
||||
self._read_started = False
|
||||
self.resolver_match = None
|
||||
self.script_name = self.scope.get('root_path', '')
|
||||
if self.script_name and scope['path'].startswith(self.script_name):
|
||||
# TODO: Better is-prefix checking, slash handling?
|
||||
self.path_info = scope['path'][len(self.script_name):]
|
||||
else:
|
||||
self.path_info = scope['path']
|
||||
# The Django path is different from ASGI scope path args, it should
|
||||
# combine with script name.
|
||||
if self.script_name:
|
||||
self.path = '%s/%s' % (
|
||||
self.script_name.rstrip('/'),
|
||||
self.path_info.replace('/', '', 1),
|
||||
)
|
||||
else:
|
||||
self.path = scope['path']
|
||||
# HTTP basics.
|
||||
self.method = self.scope['method'].upper()
|
||||
# Ensure query string is encoded correctly.
|
||||
query_string = self.scope.get('query_string', '')
|
||||
if isinstance(query_string, bytes):
|
||||
query_string = query_string.decode()
|
||||
self.META = {
|
||||
'REQUEST_METHOD': self.method,
|
||||
'QUERY_STRING': query_string,
|
||||
'SCRIPT_NAME': self.script_name,
|
||||
'PATH_INFO': self.path_info,
|
||||
# WSGI-expecting code will need these for a while
|
||||
'wsgi.multithread': True,
|
||||
'wsgi.multiprocess': True,
|
||||
}
|
||||
if self.scope.get('client'):
|
||||
self.META['REMOTE_ADDR'] = self.scope['client'][0]
|
||||
self.META['REMOTE_HOST'] = self.META['REMOTE_ADDR']
|
||||
self.META['REMOTE_PORT'] = self.scope['client'][1]
|
||||
if self.scope.get('server'):
|
||||
self.META['SERVER_NAME'] = self.scope['server'][0]
|
||||
self.META['SERVER_PORT'] = str(self.scope['server'][1])
|
||||
else:
|
||||
self.META['SERVER_NAME'] = 'unknown'
|
||||
self.META['SERVER_PORT'] = '0'
|
||||
# Headers go into META.
|
||||
for name, value in self.scope.get('headers', []):
|
||||
name = name.decode('latin1')
|
||||
if name == 'content-length':
|
||||
corrected_name = 'CONTENT_LENGTH'
|
||||
elif name == 'content-type':
|
||||
corrected_name = 'CONTENT_TYPE'
|
||||
else:
|
||||
corrected_name = 'HTTP_%s' % name.upper().replace('-', '_')
|
||||
# HTTP/2 say only ASCII chars are allowed in headers, but decode
|
||||
# latin1 just in case.
|
||||
value = value.decode('latin1')
|
||||
if corrected_name in self.META:
|
||||
value = self.META[corrected_name] + ',' + value
|
||||
self.META[corrected_name] = value
|
||||
# Pull out request encoding, if provided.
|
||||
self._set_content_type_params(self.META)
|
||||
# Directly assign the body file to be our stream.
|
||||
self._stream = body_file
|
||||
# Other bits.
|
||||
self.resolver_match = None
|
||||
|
||||
@cached_property
|
||||
def GET(self):
|
||||
return QueryDict(self.META['QUERY_STRING'])
|
||||
|
||||
def _get_scheme(self):
|
||||
return self.scope.get('scheme') or super()._get_scheme()
|
||||
|
||||
def _get_post(self):
|
||||
if not hasattr(self, '_post'):
|
||||
self._load_post_and_files()
|
||||
return self._post
|
||||
|
||||
def _set_post(self, post):
|
||||
self._post = post
|
||||
|
||||
def _get_files(self):
|
||||
if not hasattr(self, '_files'):
|
||||
self._load_post_and_files()
|
||||
return self._files
|
||||
|
||||
POST = property(_get_post, _set_post)
|
||||
FILES = property(_get_files)
|
||||
|
||||
@cached_property
|
||||
def COOKIES(self):
|
||||
return parse_cookie(self.META.get('HTTP_COOKIE', ''))
|
||||
|
||||
|
||||
class ASGIHandler(base.BaseHandler):
|
||||
"""Handler for ASGI requests."""
|
||||
request_class = ASGIRequest
|
||||
# Size to chunk response bodies into for multiple response messages.
|
||||
chunk_size = 2 ** 16
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.load_middleware(is_async=True)
|
||||
|
||||
async def __call__(self, scope, receive, send):
|
||||
"""
|
||||
Async entrypoint - parses the request and hands off to get_response.
|
||||
"""
|
||||
# Serve only HTTP connections.
|
||||
# FIXME: Allow to override this.
|
||||
if scope['type'] != 'http':
|
||||
raise ValueError(
|
||||
'Django can only handle ASGI/HTTP connections, not %s.'
|
||||
% scope['type']
|
||||
)
|
||||
|
||||
async with ThreadSensitiveContext():
|
||||
await self.handle(scope, receive, send)
|
||||
|
||||
async def handle(self, scope, receive, send):
|
||||
"""
|
||||
Handles the ASGI request. Called via the __call__ method.
|
||||
"""
|
||||
# Receive the HTTP request body as a stream object.
|
||||
try:
|
||||
body_file = await self.read_body(receive)
|
||||
except RequestAborted:
|
||||
return
|
||||
# Request is complete and can be served.
|
||||
set_script_prefix(self.get_script_prefix(scope))
|
||||
await sync_to_async(signals.request_started.send, thread_sensitive=True)(sender=self.__class__, scope=scope)
|
||||
# Get the request and check for basic issues.
|
||||
request, error_response = self.create_request(scope, body_file)
|
||||
if request is None:
|
||||
await self.send_response(error_response, send)
|
||||
return
|
||||
# Get the response, using the async mode of BaseHandler.
|
||||
response = await self.get_response_async(request)
|
||||
response._handler_class = self.__class__
|
||||
# Increase chunk size on file responses (ASGI servers handles low-level
|
||||
# chunking).
|
||||
if isinstance(response, FileResponse):
|
||||
response.block_size = self.chunk_size
|
||||
# Send the response.
|
||||
await self.send_response(response, send)
|
||||
|
||||
async def read_body(self, receive):
|
||||
"""Reads an HTTP body from an ASGI connection."""
|
||||
# Use the tempfile that auto rolls-over to a disk file as it fills up.
|
||||
body_file = tempfile.SpooledTemporaryFile(max_size=settings.FILE_UPLOAD_MAX_MEMORY_SIZE, mode='w+b')
|
||||
while True:
|
||||
message = await receive()
|
||||
if message['type'] == 'http.disconnect':
|
||||
# Early client disconnect.
|
||||
raise RequestAborted()
|
||||
# Add a body chunk from the message, if provided.
|
||||
if 'body' in message:
|
||||
body_file.write(message['body'])
|
||||
# Quit out if that's the end.
|
||||
if not message.get('more_body', False):
|
||||
break
|
||||
body_file.seek(0)
|
||||
return body_file
|
||||
|
||||
def create_request(self, scope, body_file):
|
||||
"""
|
||||
Create the Request object and returns either (request, None) or
|
||||
(None, response) if there is an error response.
|
||||
"""
|
||||
try:
|
||||
return self.request_class(scope, body_file), None
|
||||
except UnicodeDecodeError:
|
||||
logger.warning(
|
||||
'Bad Request (UnicodeDecodeError)',
|
||||
exc_info=sys.exc_info(),
|
||||
extra={'status_code': 400},
|
||||
)
|
||||
return None, HttpResponseBadRequest()
|
||||
except RequestDataTooBig:
|
||||
return None, HttpResponse('413 Payload too large', status=413)
|
||||
|
||||
def handle_uncaught_exception(self, request, resolver, exc_info):
|
||||
"""Last-chance handler for exceptions."""
|
||||
# There's no WSGI server to catch the exception further up
|
||||
# if this fails, so translate it into a plain text response.
|
||||
try:
|
||||
return super().handle_uncaught_exception(request, resolver, exc_info)
|
||||
except Exception:
|
||||
return HttpResponseServerError(
|
||||
traceback.format_exc() if settings.DEBUG else 'Internal Server Error',
|
||||
content_type='text/plain',
|
||||
)
|
||||
|
||||
async def send_response(self, response, send):
|
||||
"""Encode and send a response out over ASGI."""
|
||||
# Collect cookies into headers. Have to preserve header case as there
|
||||
# are some non-RFC compliant clients that require e.g. Content-Type.
|
||||
response_headers = []
|
||||
for header, value in response.items():
|
||||
if isinstance(header, str):
|
||||
header = header.encode('ascii')
|
||||
if isinstance(value, str):
|
||||
value = value.encode('latin1')
|
||||
response_headers.append((bytes(header), bytes(value)))
|
||||
for c in response.cookies.values():
|
||||
response_headers.append(
|
||||
(b'Set-Cookie', c.output(header='').encode('ascii').strip())
|
||||
)
|
||||
# Initial response message.
|
||||
await send({
|
||||
'type': 'http.response.start',
|
||||
'status': response.status_code,
|
||||
'headers': response_headers,
|
||||
})
|
||||
# Streaming responses need to be pinned to their iterator.
|
||||
if response.streaming:
|
||||
# Access `__iter__` and not `streaming_content` directly in case
|
||||
# it has been overridden in a subclass.
|
||||
for part in response:
|
||||
for chunk, _ in self.chunk_bytes(part):
|
||||
await send({
|
||||
'type': 'http.response.body',
|
||||
'body': chunk,
|
||||
# Ignore "more" as there may be more parts; instead,
|
||||
# use an empty final closing message with False.
|
||||
'more_body': True,
|
||||
})
|
||||
# Final closing message.
|
||||
await send({'type': 'http.response.body'})
|
||||
# Other responses just need chunking.
|
||||
else:
|
||||
# Yield chunks of response.
|
||||
for chunk, last in self.chunk_bytes(response.content):
|
||||
await send({
|
||||
'type': 'http.response.body',
|
||||
'body': chunk,
|
||||
'more_body': not last,
|
||||
})
|
||||
await sync_to_async(response.close, thread_sensitive=True)()
|
||||
|
||||
@classmethod
|
||||
def chunk_bytes(cls, data):
|
||||
"""
|
||||
Chunks some data up so it can be sent in reasonable size messages.
|
||||
Yields (chunk, last_chunk) tuples.
|
||||
"""
|
||||
position = 0
|
||||
if not data:
|
||||
yield data, True
|
||||
return
|
||||
while position < len(data):
|
||||
yield (
|
||||
data[position:position + cls.chunk_size],
|
||||
(position + cls.chunk_size) >= len(data),
|
||||
)
|
||||
position += cls.chunk_size
|
||||
|
||||
def get_script_prefix(self, scope):
|
||||
"""
|
||||
Return the script prefix to use from either the scope or a setting.
|
||||
"""
|
||||
if settings.FORCE_SCRIPT_NAME:
|
||||
return settings.FORCE_SCRIPT_NAME
|
||||
return scope.get('root_path', '') or ''
|
350
venv/Lib/site-packages/django/core/handlers/base.py
Normal file
350
venv/Lib/site-packages/django/core/handlers/base.py
Normal file
@@ -0,0 +1,350 @@
|
||||
import asyncio
|
||||
import logging
|
||||
import types
|
||||
|
||||
from asgiref.sync import async_to_sync, sync_to_async
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.exceptions import ImproperlyConfigured, MiddlewareNotUsed
|
||||
from django.core.signals import request_finished
|
||||
from django.db import connections, transaction
|
||||
from django.urls import get_resolver, set_urlconf
|
||||
from django.utils.log import log_response
|
||||
from django.utils.module_loading import import_string
|
||||
|
||||
from .exception import convert_exception_to_response
|
||||
|
||||
logger = logging.getLogger('django.request')
|
||||
|
||||
|
||||
class BaseHandler:
|
||||
_view_middleware = None
|
||||
_template_response_middleware = None
|
||||
_exception_middleware = None
|
||||
_middleware_chain = None
|
||||
|
||||
def load_middleware(self, is_async=False):
|
||||
"""
|
||||
Populate middleware lists from settings.MIDDLEWARE.
|
||||
|
||||
Must be called after the environment is fixed (see __call__ in subclasses).
|
||||
"""
|
||||
self._view_middleware = []
|
||||
self._template_response_middleware = []
|
||||
self._exception_middleware = []
|
||||
|
||||
get_response = self._get_response_async if is_async else self._get_response
|
||||
handler = convert_exception_to_response(get_response)
|
||||
handler_is_async = is_async
|
||||
for middleware_path in reversed(settings.MIDDLEWARE):
|
||||
middleware = import_string(middleware_path)
|
||||
middleware_can_sync = getattr(middleware, 'sync_capable', True)
|
||||
middleware_can_async = getattr(middleware, 'async_capable', False)
|
||||
if not middleware_can_sync and not middleware_can_async:
|
||||
raise RuntimeError(
|
||||
'Middleware %s must have at least one of '
|
||||
'sync_capable/async_capable set to True.' % middleware_path
|
||||
)
|
||||
elif not handler_is_async and middleware_can_sync:
|
||||
middleware_is_async = False
|
||||
else:
|
||||
middleware_is_async = middleware_can_async
|
||||
try:
|
||||
# Adapt handler, if needed.
|
||||
adapted_handler = self.adapt_method_mode(
|
||||
middleware_is_async, handler, handler_is_async,
|
||||
debug=settings.DEBUG, name='middleware %s' % middleware_path,
|
||||
)
|
||||
mw_instance = middleware(adapted_handler)
|
||||
except MiddlewareNotUsed as exc:
|
||||
if settings.DEBUG:
|
||||
if str(exc):
|
||||
logger.debug('MiddlewareNotUsed(%r): %s', middleware_path, exc)
|
||||
else:
|
||||
logger.debug('MiddlewareNotUsed: %r', middleware_path)
|
||||
continue
|
||||
else:
|
||||
handler = adapted_handler
|
||||
|
||||
if mw_instance is None:
|
||||
raise ImproperlyConfigured(
|
||||
'Middleware factory %s returned None.' % middleware_path
|
||||
)
|
||||
|
||||
if hasattr(mw_instance, 'process_view'):
|
||||
self._view_middleware.insert(
|
||||
0,
|
||||
self.adapt_method_mode(is_async, mw_instance.process_view),
|
||||
)
|
||||
if hasattr(mw_instance, 'process_template_response'):
|
||||
self._template_response_middleware.append(
|
||||
self.adapt_method_mode(is_async, mw_instance.process_template_response),
|
||||
)
|
||||
if hasattr(mw_instance, 'process_exception'):
|
||||
# The exception-handling stack is still always synchronous for
|
||||
# now, so adapt that way.
|
||||
self._exception_middleware.append(
|
||||
self.adapt_method_mode(False, mw_instance.process_exception),
|
||||
)
|
||||
|
||||
handler = convert_exception_to_response(mw_instance)
|
||||
handler_is_async = middleware_is_async
|
||||
|
||||
# Adapt the top of the stack, if needed.
|
||||
handler = self.adapt_method_mode(is_async, handler, handler_is_async)
|
||||
# We only assign to this when initialization is complete as it is used
|
||||
# as a flag for initialization being complete.
|
||||
self._middleware_chain = handler
|
||||
|
||||
def adapt_method_mode(
|
||||
self, is_async, method, method_is_async=None, debug=False, name=None,
|
||||
):
|
||||
"""
|
||||
Adapt a method to be in the correct "mode":
|
||||
- If is_async is False:
|
||||
- Synchronous methods are left alone
|
||||
- Asynchronous methods are wrapped with async_to_sync
|
||||
- If is_async is True:
|
||||
- Synchronous methods are wrapped with sync_to_async()
|
||||
- Asynchronous methods are left alone
|
||||
"""
|
||||
if method_is_async is None:
|
||||
method_is_async = asyncio.iscoroutinefunction(method)
|
||||
if debug and not name:
|
||||
name = name or 'method %s()' % method.__qualname__
|
||||
if is_async:
|
||||
if not method_is_async:
|
||||
if debug:
|
||||
logger.debug('Synchronous %s adapted.', name)
|
||||
return sync_to_async(method, thread_sensitive=True)
|
||||
elif method_is_async:
|
||||
if debug:
|
||||
logger.debug('Asynchronous %s adapted.', name)
|
||||
return async_to_sync(method)
|
||||
return method
|
||||
|
||||
def get_response(self, request):
|
||||
"""Return an HttpResponse object for the given HttpRequest."""
|
||||
# Setup default url resolver for this thread
|
||||
set_urlconf(settings.ROOT_URLCONF)
|
||||
response = self._middleware_chain(request)
|
||||
response._resource_closers.append(request.close)
|
||||
if response.status_code >= 400:
|
||||
log_response(
|
||||
'%s: %s', response.reason_phrase, request.path,
|
||||
response=response,
|
||||
request=request,
|
||||
)
|
||||
return response
|
||||
|
||||
async def get_response_async(self, request):
|
||||
"""
|
||||
Asynchronous version of get_response.
|
||||
|
||||
Funneling everything, including WSGI, into a single async
|
||||
get_response() is too slow. Avoid the context switch by using
|
||||
a separate async response path.
|
||||
"""
|
||||
# Setup default url resolver for this thread.
|
||||
set_urlconf(settings.ROOT_URLCONF)
|
||||
response = await self._middleware_chain(request)
|
||||
response._resource_closers.append(request.close)
|
||||
if response.status_code >= 400:
|
||||
await sync_to_async(log_response, thread_sensitive=False)(
|
||||
'%s: %s', response.reason_phrase, request.path,
|
||||
response=response,
|
||||
request=request,
|
||||
)
|
||||
return response
|
||||
|
||||
def _get_response(self, request):
|
||||
"""
|
||||
Resolve and call the view, then apply view, exception, and
|
||||
template_response middleware. This method is everything that happens
|
||||
inside the request/response middleware.
|
||||
"""
|
||||
response = None
|
||||
callback, callback_args, callback_kwargs = self.resolve_request(request)
|
||||
|
||||
# Apply view middleware
|
||||
for middleware_method in self._view_middleware:
|
||||
response = middleware_method(request, callback, callback_args, callback_kwargs)
|
||||
if response:
|
||||
break
|
||||
|
||||
if response is None:
|
||||
wrapped_callback = self.make_view_atomic(callback)
|
||||
# If it is an asynchronous view, run it in a subthread.
|
||||
if asyncio.iscoroutinefunction(wrapped_callback):
|
||||
wrapped_callback = async_to_sync(wrapped_callback)
|
||||
try:
|
||||
response = wrapped_callback(request, *callback_args, **callback_kwargs)
|
||||
except Exception as e:
|
||||
response = self.process_exception_by_middleware(e, request)
|
||||
if response is None:
|
||||
raise
|
||||
|
||||
# Complain if the view returned None (a common error).
|
||||
self.check_response(response, callback)
|
||||
|
||||
# If the response supports deferred rendering, apply template
|
||||
# response middleware and then render the response
|
||||
if hasattr(response, 'render') and callable(response.render):
|
||||
for middleware_method in self._template_response_middleware:
|
||||
response = middleware_method(request, response)
|
||||
# Complain if the template response middleware returned None (a common error).
|
||||
self.check_response(
|
||||
response,
|
||||
middleware_method,
|
||||
name='%s.process_template_response' % (
|
||||
middleware_method.__self__.__class__.__name__,
|
||||
)
|
||||
)
|
||||
try:
|
||||
response = response.render()
|
||||
except Exception as e:
|
||||
response = self.process_exception_by_middleware(e, request)
|
||||
if response is None:
|
||||
raise
|
||||
|
||||
return response
|
||||
|
||||
async def _get_response_async(self, request):
|
||||
"""
|
||||
Resolve and call the view, then apply view, exception, and
|
||||
template_response middleware. This method is everything that happens
|
||||
inside the request/response middleware.
|
||||
"""
|
||||
response = None
|
||||
callback, callback_args, callback_kwargs = self.resolve_request(request)
|
||||
|
||||
# Apply view middleware.
|
||||
for middleware_method in self._view_middleware:
|
||||
response = await middleware_method(request, callback, callback_args, callback_kwargs)
|
||||
if response:
|
||||
break
|
||||
|
||||
if response is None:
|
||||
wrapped_callback = self.make_view_atomic(callback)
|
||||
# If it is a synchronous view, run it in a subthread
|
||||
if not asyncio.iscoroutinefunction(wrapped_callback):
|
||||
wrapped_callback = sync_to_async(wrapped_callback, thread_sensitive=True)
|
||||
try:
|
||||
response = await wrapped_callback(request, *callback_args, **callback_kwargs)
|
||||
except Exception as e:
|
||||
response = await sync_to_async(
|
||||
self.process_exception_by_middleware,
|
||||
thread_sensitive=True,
|
||||
)(e, request)
|
||||
if response is None:
|
||||
raise
|
||||
|
||||
# Complain if the view returned None or an uncalled coroutine.
|
||||
self.check_response(response, callback)
|
||||
|
||||
# If the response supports deferred rendering, apply template
|
||||
# response middleware and then render the response
|
||||
if hasattr(response, 'render') and callable(response.render):
|
||||
for middleware_method in self._template_response_middleware:
|
||||
response = await middleware_method(request, response)
|
||||
# Complain if the template response middleware returned None or
|
||||
# an uncalled coroutine.
|
||||
self.check_response(
|
||||
response,
|
||||
middleware_method,
|
||||
name='%s.process_template_response' % (
|
||||
middleware_method.__self__.__class__.__name__,
|
||||
)
|
||||
)
|
||||
try:
|
||||
if asyncio.iscoroutinefunction(response.render):
|
||||
response = await response.render()
|
||||
else:
|
||||
response = await sync_to_async(response.render, thread_sensitive=True)()
|
||||
except Exception as e:
|
||||
response = await sync_to_async(
|
||||
self.process_exception_by_middleware,
|
||||
thread_sensitive=True,
|
||||
)(e, request)
|
||||
if response is None:
|
||||
raise
|
||||
|
||||
# Make sure the response is not a coroutine
|
||||
if asyncio.iscoroutine(response):
|
||||
raise RuntimeError('Response is still a coroutine.')
|
||||
return response
|
||||
|
||||
def resolve_request(self, request):
|
||||
"""
|
||||
Retrieve/set the urlconf for the request. Return the view resolved,
|
||||
with its args and kwargs.
|
||||
"""
|
||||
# Work out the resolver.
|
||||
if hasattr(request, 'urlconf'):
|
||||
urlconf = request.urlconf
|
||||
set_urlconf(urlconf)
|
||||
resolver = get_resolver(urlconf)
|
||||
else:
|
||||
resolver = get_resolver()
|
||||
# Resolve the view, and assign the match object back to the request.
|
||||
resolver_match = resolver.resolve(request.path_info)
|
||||
request.resolver_match = resolver_match
|
||||
return resolver_match
|
||||
|
||||
def check_response(self, response, callback, name=None):
|
||||
"""
|
||||
Raise an error if the view returned None or an uncalled coroutine.
|
||||
"""
|
||||
if not(response is None or asyncio.iscoroutine(response)):
|
||||
return
|
||||
if not name:
|
||||
if isinstance(callback, types.FunctionType): # FBV
|
||||
name = 'The view %s.%s' % (callback.__module__, callback.__name__)
|
||||
else: # CBV
|
||||
name = 'The view %s.%s.__call__' % (
|
||||
callback.__module__,
|
||||
callback.__class__.__name__,
|
||||
)
|
||||
if response is None:
|
||||
raise ValueError(
|
||||
"%s didn't return an HttpResponse object. It returned None "
|
||||
"instead." % name
|
||||
)
|
||||
elif asyncio.iscoroutine(response):
|
||||
raise ValueError(
|
||||
"%s didn't return an HttpResponse object. It returned an "
|
||||
"unawaited coroutine instead. You may need to add an 'await' "
|
||||
"into your view." % name
|
||||
)
|
||||
|
||||
# Other utility methods.
|
||||
|
||||
def make_view_atomic(self, view):
|
||||
non_atomic_requests = getattr(view, '_non_atomic_requests', set())
|
||||
for db in connections.all():
|
||||
if db.settings_dict['ATOMIC_REQUESTS'] and db.alias not in non_atomic_requests:
|
||||
if asyncio.iscoroutinefunction(view):
|
||||
raise RuntimeError(
|
||||
'You cannot use ATOMIC_REQUESTS with async views.'
|
||||
)
|
||||
view = transaction.atomic(using=db.alias)(view)
|
||||
return view
|
||||
|
||||
def process_exception_by_middleware(self, exception, request):
|
||||
"""
|
||||
Pass the exception to the exception middleware. If no middleware
|
||||
return a response for this exception, return None.
|
||||
"""
|
||||
for middleware_method in self._exception_middleware:
|
||||
response = middleware_method(request, exception)
|
||||
if response:
|
||||
return response
|
||||
return None
|
||||
|
||||
|
||||
def reset_urlconf(sender, **kwargs):
|
||||
"""Reset the URLconf after each request is finished."""
|
||||
set_urlconf(None)
|
||||
|
||||
|
||||
request_finished.connect(reset_urlconf)
|
149
venv/Lib/site-packages/django/core/handlers/exception.py
Normal file
149
venv/Lib/site-packages/django/core/handlers/exception.py
Normal file
@@ -0,0 +1,149 @@
|
||||
import asyncio
|
||||
import logging
|
||||
import sys
|
||||
from functools import wraps
|
||||
|
||||
from asgiref.sync import sync_to_async
|
||||
|
||||
from django.conf import settings
|
||||
from django.core import signals
|
||||
from django.core.exceptions import (
|
||||
BadRequest, PermissionDenied, RequestDataTooBig, SuspiciousOperation,
|
||||
TooManyFieldsSent,
|
||||
)
|
||||
from django.http import Http404
|
||||
from django.http.multipartparser import MultiPartParserError
|
||||
from django.urls import get_resolver, get_urlconf
|
||||
from django.utils.log import log_response
|
||||
from django.views import debug
|
||||
|
||||
|
||||
def convert_exception_to_response(get_response):
|
||||
"""
|
||||
Wrap the given get_response callable in exception-to-response conversion.
|
||||
|
||||
All exceptions will be converted. All known 4xx exceptions (Http404,
|
||||
PermissionDenied, MultiPartParserError, SuspiciousOperation) will be
|
||||
converted to the appropriate response, and all other exceptions will be
|
||||
converted to 500 responses.
|
||||
|
||||
This decorator is automatically applied to all middleware to ensure that
|
||||
no middleware leaks an exception and that the next middleware in the stack
|
||||
can rely on getting a response instead of an exception.
|
||||
"""
|
||||
if asyncio.iscoroutinefunction(get_response):
|
||||
@wraps(get_response)
|
||||
async def inner(request):
|
||||
try:
|
||||
response = await get_response(request)
|
||||
except Exception as exc:
|
||||
response = await sync_to_async(response_for_exception, thread_sensitive=False)(request, exc)
|
||||
return response
|
||||
return inner
|
||||
else:
|
||||
@wraps(get_response)
|
||||
def inner(request):
|
||||
try:
|
||||
response = get_response(request)
|
||||
except Exception as exc:
|
||||
response = response_for_exception(request, exc)
|
||||
return response
|
||||
return inner
|
||||
|
||||
|
||||
def response_for_exception(request, exc):
|
||||
if isinstance(exc, Http404):
|
||||
if settings.DEBUG:
|
||||
response = debug.technical_404_response(request, exc)
|
||||
else:
|
||||
response = get_exception_response(request, get_resolver(get_urlconf()), 404, exc)
|
||||
|
||||
elif isinstance(exc, PermissionDenied):
|
||||
response = get_exception_response(request, get_resolver(get_urlconf()), 403, exc)
|
||||
log_response(
|
||||
'Forbidden (Permission denied): %s', request.path,
|
||||
response=response,
|
||||
request=request,
|
||||
exc_info=sys.exc_info(),
|
||||
)
|
||||
|
||||
elif isinstance(exc, MultiPartParserError):
|
||||
response = get_exception_response(request, get_resolver(get_urlconf()), 400, exc)
|
||||
log_response(
|
||||
'Bad request (Unable to parse request body): %s', request.path,
|
||||
response=response,
|
||||
request=request,
|
||||
exc_info=sys.exc_info(),
|
||||
)
|
||||
|
||||
elif isinstance(exc, BadRequest):
|
||||
if settings.DEBUG:
|
||||
response = debug.technical_500_response(request, *sys.exc_info(), status_code=400)
|
||||
else:
|
||||
response = get_exception_response(request, get_resolver(get_urlconf()), 400, exc)
|
||||
log_response(
|
||||
'%s: %s', str(exc), request.path,
|
||||
response=response,
|
||||
request=request,
|
||||
exc_info=sys.exc_info(),
|
||||
)
|
||||
elif isinstance(exc, SuspiciousOperation):
|
||||
if isinstance(exc, (RequestDataTooBig, TooManyFieldsSent)):
|
||||
# POST data can't be accessed again, otherwise the original
|
||||
# exception would be raised.
|
||||
request._mark_post_parse_error()
|
||||
|
||||
# The request logger receives events for any problematic request
|
||||
# The security logger receives events for all SuspiciousOperations
|
||||
security_logger = logging.getLogger('django.security.%s' % exc.__class__.__name__)
|
||||
security_logger.error(
|
||||
str(exc),
|
||||
extra={'status_code': 400, 'request': request},
|
||||
)
|
||||
if settings.DEBUG:
|
||||
response = debug.technical_500_response(request, *sys.exc_info(), status_code=400)
|
||||
else:
|
||||
response = get_exception_response(request, get_resolver(get_urlconf()), 400, exc)
|
||||
|
||||
else:
|
||||
signals.got_request_exception.send(sender=None, request=request)
|
||||
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
|
||||
log_response(
|
||||
'%s: %s', response.reason_phrase, request.path,
|
||||
response=response,
|
||||
request=request,
|
||||
exc_info=sys.exc_info(),
|
||||
)
|
||||
|
||||
# Force a TemplateResponse to be rendered.
|
||||
if not getattr(response, 'is_rendered', True) and callable(getattr(response, 'render', None)):
|
||||
response = response.render()
|
||||
|
||||
return response
|
||||
|
||||
|
||||
def get_exception_response(request, resolver, status_code, exception):
|
||||
try:
|
||||
callback = resolver.resolve_error_handler(status_code)
|
||||
response = callback(request, exception=exception)
|
||||
except Exception:
|
||||
signals.got_request_exception.send(sender=None, request=request)
|
||||
response = handle_uncaught_exception(request, resolver, sys.exc_info())
|
||||
|
||||
return response
|
||||
|
||||
|
||||
def handle_uncaught_exception(request, resolver, exc_info):
|
||||
"""
|
||||
Processing for any otherwise uncaught exceptions (those that will
|
||||
generate HTTP 500 responses).
|
||||
"""
|
||||
if settings.DEBUG_PROPAGATE_EXCEPTIONS:
|
||||
raise
|
||||
|
||||
if settings.DEBUG:
|
||||
return debug.technical_500_response(request, *exc_info)
|
||||
|
||||
# Return an HttpResponse that displays a friendly error message.
|
||||
callback = resolver.resolve_error_handler(500)
|
||||
return callback(request)
|
210
venv/Lib/site-packages/django/core/handlers/wsgi.py
Normal file
210
venv/Lib/site-packages/django/core/handlers/wsgi.py
Normal file
@@ -0,0 +1,210 @@
|
||||
from io import BytesIO
|
||||
|
||||
from django.conf import settings
|
||||
from django.core import signals
|
||||
from django.core.handlers import base
|
||||
from django.http import HttpRequest, QueryDict, parse_cookie
|
||||
from django.urls import set_script_prefix
|
||||
from django.utils.encoding import repercent_broken_unicode
|
||||
from django.utils.functional import cached_property
|
||||
from django.utils.regex_helper import _lazy_re_compile
|
||||
|
||||
_slashes_re = _lazy_re_compile(br'/+')
|
||||
|
||||
|
||||
class LimitedStream:
|
||||
"""Wrap another stream to disallow reading it past a number of bytes."""
|
||||
def __init__(self, stream, limit, buf_size=64 * 1024 * 1024):
|
||||
self.stream = stream
|
||||
self.remaining = limit
|
||||
self.buffer = b''
|
||||
self.buf_size = buf_size
|
||||
|
||||
def _read_limited(self, size=None):
|
||||
if size is None or size > self.remaining:
|
||||
size = self.remaining
|
||||
if size == 0:
|
||||
return b''
|
||||
result = self.stream.read(size)
|
||||
self.remaining -= len(result)
|
||||
return result
|
||||
|
||||
def read(self, size=None):
|
||||
if size is None:
|
||||
result = self.buffer + self._read_limited()
|
||||
self.buffer = b''
|
||||
elif size < len(self.buffer):
|
||||
result = self.buffer[:size]
|
||||
self.buffer = self.buffer[size:]
|
||||
else: # size >= len(self.buffer)
|
||||
result = self.buffer + self._read_limited(size - len(self.buffer))
|
||||
self.buffer = b''
|
||||
return result
|
||||
|
||||
def readline(self, size=None):
|
||||
while b'\n' not in self.buffer and \
|
||||
(size is None or len(self.buffer) < size):
|
||||
if size:
|
||||
# since size is not None here, len(self.buffer) < size
|
||||
chunk = self._read_limited(size - len(self.buffer))
|
||||
else:
|
||||
chunk = self._read_limited()
|
||||
if not chunk:
|
||||
break
|
||||
self.buffer += chunk
|
||||
sio = BytesIO(self.buffer)
|
||||
if size:
|
||||
line = sio.readline(size)
|
||||
else:
|
||||
line = sio.readline()
|
||||
self.buffer = sio.read()
|
||||
return line
|
||||
|
||||
|
||||
class WSGIRequest(HttpRequest):
|
||||
def __init__(self, environ):
|
||||
script_name = get_script_name(environ)
|
||||
# If PATH_INFO is empty (e.g. accessing the SCRIPT_NAME URL without a
|
||||
# trailing slash), operate as if '/' was requested.
|
||||
path_info = get_path_info(environ) or '/'
|
||||
self.environ = environ
|
||||
self.path_info = path_info
|
||||
# be careful to only replace the first slash in the path because of
|
||||
# http://test/something and http://test//something being different as
|
||||
# stated in https://www.ietf.org/rfc/rfc2396.txt
|
||||
self.path = '%s/%s' % (script_name.rstrip('/'),
|
||||
path_info.replace('/', '', 1))
|
||||
self.META = environ
|
||||
self.META['PATH_INFO'] = path_info
|
||||
self.META['SCRIPT_NAME'] = script_name
|
||||
self.method = environ['REQUEST_METHOD'].upper()
|
||||
# Set content_type, content_params, and encoding.
|
||||
self._set_content_type_params(environ)
|
||||
try:
|
||||
content_length = int(environ.get('CONTENT_LENGTH'))
|
||||
except (ValueError, TypeError):
|
||||
content_length = 0
|
||||
self._stream = LimitedStream(self.environ['wsgi.input'], content_length)
|
||||
self._read_started = False
|
||||
self.resolver_match = None
|
||||
|
||||
def _get_scheme(self):
|
||||
return self.environ.get('wsgi.url_scheme')
|
||||
|
||||
@cached_property
|
||||
def GET(self):
|
||||
# The WSGI spec says 'QUERY_STRING' may be absent.
|
||||
raw_query_string = get_bytes_from_wsgi(self.environ, 'QUERY_STRING', '')
|
||||
return QueryDict(raw_query_string, encoding=self._encoding)
|
||||
|
||||
def _get_post(self):
|
||||
if not hasattr(self, '_post'):
|
||||
self._load_post_and_files()
|
||||
return self._post
|
||||
|
||||
def _set_post(self, post):
|
||||
self._post = post
|
||||
|
||||
@cached_property
|
||||
def COOKIES(self):
|
||||
raw_cookie = get_str_from_wsgi(self.environ, 'HTTP_COOKIE', '')
|
||||
return parse_cookie(raw_cookie)
|
||||
|
||||
@property
|
||||
def FILES(self):
|
||||
if not hasattr(self, '_files'):
|
||||
self._load_post_and_files()
|
||||
return self._files
|
||||
|
||||
POST = property(_get_post, _set_post)
|
||||
|
||||
|
||||
class WSGIHandler(base.BaseHandler):
|
||||
request_class = WSGIRequest
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.load_middleware()
|
||||
|
||||
def __call__(self, environ, start_response):
|
||||
set_script_prefix(get_script_name(environ))
|
||||
signals.request_started.send(sender=self.__class__, environ=environ)
|
||||
request = self.request_class(environ)
|
||||
response = self.get_response(request)
|
||||
|
||||
response._handler_class = self.__class__
|
||||
|
||||
status = '%d %s' % (response.status_code, response.reason_phrase)
|
||||
response_headers = [
|
||||
*response.items(),
|
||||
*(('Set-Cookie', c.output(header='')) for c in response.cookies.values()),
|
||||
]
|
||||
start_response(status, response_headers)
|
||||
if getattr(response, 'file_to_stream', None) is not None and environ.get('wsgi.file_wrapper'):
|
||||
# If `wsgi.file_wrapper` is used the WSGI server does not call
|
||||
# .close on the response, but on the file wrapper. Patch it to use
|
||||
# response.close instead which takes care of closing all files.
|
||||
response.file_to_stream.close = response.close
|
||||
response = environ['wsgi.file_wrapper'](response.file_to_stream, response.block_size)
|
||||
return response
|
||||
|
||||
|
||||
def get_path_info(environ):
|
||||
"""Return the HTTP request's PATH_INFO as a string."""
|
||||
path_info = get_bytes_from_wsgi(environ, 'PATH_INFO', '/')
|
||||
|
||||
return repercent_broken_unicode(path_info).decode()
|
||||
|
||||
|
||||
def get_script_name(environ):
|
||||
"""
|
||||
Return the equivalent of the HTTP request's SCRIPT_NAME environment
|
||||
variable. If Apache mod_rewrite is used, return what would have been
|
||||
the script name prior to any rewriting (so it's the script name as seen
|
||||
from the client's perspective), unless the FORCE_SCRIPT_NAME setting is
|
||||
set (to anything).
|
||||
"""
|
||||
if settings.FORCE_SCRIPT_NAME is not None:
|
||||
return settings.FORCE_SCRIPT_NAME
|
||||
|
||||
# If Apache's mod_rewrite had a whack at the URL, Apache set either
|
||||
# SCRIPT_URL or REDIRECT_URL to the full resource URL before applying any
|
||||
# rewrites. Unfortunately not every web server (lighttpd!) passes this
|
||||
# information through all the time, so FORCE_SCRIPT_NAME, above, is still
|
||||
# needed.
|
||||
script_url = get_bytes_from_wsgi(environ, 'SCRIPT_URL', '') or get_bytes_from_wsgi(environ, 'REDIRECT_URL', '')
|
||||
|
||||
if script_url:
|
||||
if b'//' in script_url:
|
||||
# mod_wsgi squashes multiple successive slashes in PATH_INFO,
|
||||
# do the same with script_url before manipulating paths (#17133).
|
||||
script_url = _slashes_re.sub(b'/', script_url)
|
||||
path_info = get_bytes_from_wsgi(environ, 'PATH_INFO', '')
|
||||
script_name = script_url[:-len(path_info)] if path_info else script_url
|
||||
else:
|
||||
script_name = get_bytes_from_wsgi(environ, 'SCRIPT_NAME', '')
|
||||
|
||||
return script_name.decode()
|
||||
|
||||
|
||||
def get_bytes_from_wsgi(environ, key, default):
|
||||
"""
|
||||
Get a value from the WSGI environ dictionary as bytes.
|
||||
|
||||
key and default should be strings.
|
||||
"""
|
||||
value = environ.get(key, default)
|
||||
# Non-ASCII values in the WSGI environ are arbitrarily decoded with
|
||||
# ISO-8859-1. This is wrong for Django websites where UTF-8 is the default.
|
||||
# Re-encode to recover the original bytestring.
|
||||
return value.encode('iso-8859-1')
|
||||
|
||||
|
||||
def get_str_from_wsgi(environ, key, default):
|
||||
"""
|
||||
Get a value from the WSGI environ dictionary as str.
|
||||
|
||||
key and default should be str objects.
|
||||
"""
|
||||
value = get_bytes_from_wsgi(environ, key, default)
|
||||
return value.decode(errors='replace')
|
Reference in New Issue
Block a user