2016-11-27 04:56:26 +01:00
|
|
|
|
2017-11-16 00:53:11 +01:00
|
|
|
import logging
|
2016-11-27 04:56:26 +01:00
|
|
|
import sys
|
2017-11-16 00:53:11 +01:00
|
|
|
import urllib
|
|
|
|
from threading import Lock
|
|
|
|
from typing import Any, Callable, Dict, List, Optional
|
|
|
|
|
2016-11-27 04:56:26 +01:00
|
|
|
import tornado.web
|
|
|
|
from django import http
|
|
|
|
from django.conf import settings
|
2018-01-30 06:05:25 +01:00
|
|
|
from django.core import exceptions, signals
|
|
|
|
from django.urls import resolvers
|
2017-11-16 00:53:11 +01:00
|
|
|
from django.core.exceptions import MiddlewareNotUsed
|
2016-11-27 04:56:26 +01:00
|
|
|
from django.core.handlers import base
|
2017-05-18 11:56:03 +02:00
|
|
|
from django.core.handlers.exception import convert_exception_to_response
|
2017-11-16 00:53:11 +01:00
|
|
|
from django.core.handlers.wsgi import WSGIRequest, get_script_name
|
2018-01-30 06:05:25 +01:00
|
|
|
from django.urls import set_script_prefix, set_urlconf
|
2016-11-27 04:56:26 +01:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
2017-05-18 11:56:03 +02:00
|
|
|
from django.utils.module_loading import import_string
|
2016-11-27 04:56:26 +01:00
|
|
|
from tornado.wsgi import WSGIContainer
|
|
|
|
|
|
|
|
from zerver.decorator import RespondAsynchronously
|
|
|
|
from zerver.lib.response import json_response
|
2018-03-14 23:16:27 +01:00
|
|
|
from zerver.lib.types import ViewFuncT
|
2017-11-16 00:53:11 +01:00
|
|
|
from zerver.middleware import async_request_restart, async_request_stop
|
2016-11-27 06:14:48 +01:00
|
|
|
from zerver.tornado.descriptors import get_descriptor_by_handler_id
|
2016-11-27 04:56:26 +01:00
|
|
|
|
2016-11-27 06:36:06 +01:00
|
|
|
current_handler_id = 0
|
2017-10-26 11:38:28 +02:00
|
|
|
handlers = {} # type: Dict[int, 'AsyncDjangoHandler']
|
2016-11-27 04:56:26 +01:00
|
|
|
|
2017-10-26 11:38:28 +02:00
|
|
|
def get_handler_by_id(handler_id: int) -> 'AsyncDjangoHandler':
|
2016-11-27 06:36:06 +01:00
|
|
|
return handlers[handler_id]
|
|
|
|
|
2017-10-26 11:38:28 +02:00
|
|
|
def allocate_handler_id(handler: 'AsyncDjangoHandler') -> int:
|
2016-11-27 06:36:06 +01:00
|
|
|
global current_handler_id
|
|
|
|
handlers[current_handler_id] = handler
|
|
|
|
handler.handler_id = current_handler_id
|
|
|
|
current_handler_id += 1
|
|
|
|
return handler.handler_id
|
|
|
|
|
2017-10-26 11:38:28 +02:00
|
|
|
def clear_handler_by_id(handler_id: int) -> None:
|
2016-11-27 06:36:06 +01:00
|
|
|
del handlers[handler_id]
|
|
|
|
|
2017-10-26 11:38:28 +02:00
|
|
|
def handler_stats_string() -> str:
|
2016-11-27 06:36:06 +01:00
|
|
|
return "%s handlers, latest ID %s" % (len(handlers), current_handler_id)
|
|
|
|
|
2017-10-26 11:38:28 +02:00
|
|
|
def finish_handler(handler_id: int, event_queue_id: str,
|
|
|
|
contents: List[Dict[str, Any]], apply_markdown: bool) -> None:
|
2016-11-27 06:36:06 +01:00
|
|
|
err_msg = "Got error finishing handler for queue %s" % (event_queue_id,)
|
|
|
|
try:
|
|
|
|
# We call async_request_restart here in case we are
|
|
|
|
# being finished without any events (because another
|
|
|
|
# get_events request has supplanted this request)
|
|
|
|
handler = get_handler_by_id(handler_id)
|
|
|
|
request = handler._request
|
|
|
|
async_request_restart(request)
|
|
|
|
if len(contents) != 1:
|
|
|
|
request._log_data['extra'] = "[%s/1]" % (event_queue_id,)
|
|
|
|
else:
|
|
|
|
request._log_data['extra'] = "[%s/1/%s]" % (event_queue_id, contents[0]["type"])
|
|
|
|
|
|
|
|
handler.zulip_finish(dict(result='success', msg='',
|
|
|
|
events=contents,
|
|
|
|
queue_id=event_queue_id),
|
|
|
|
request, apply_markdown=apply_markdown)
|
|
|
|
except IOError as e:
|
|
|
|
if str(e) != 'Stream is closed':
|
|
|
|
logging.exception(err_msg)
|
|
|
|
except AssertionError as e:
|
|
|
|
if str(e) != 'Request closed':
|
|
|
|
logging.exception(err_msg)
|
|
|
|
except Exception:
|
|
|
|
logging.exception(err_msg)
|
|
|
|
|
|
|
|
|
|
|
|
# Modified version of the base Tornado handler for Django
|
2016-11-27 04:56:26 +01:00
|
|
|
class AsyncDjangoHandler(tornado.web.RequestHandler, base.BaseHandler):
|
|
|
|
initLock = Lock()
|
|
|
|
|
2017-10-26 11:38:28 +02:00
|
|
|
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
2017-10-27 08:28:23 +02:00
|
|
|
super().__init__(*args, **kwargs)
|
2016-11-27 04:56:26 +01:00
|
|
|
|
|
|
|
# Set up middleware if needed. We couldn't do this earlier, because
|
|
|
|
# settings weren't available.
|
2017-10-31 02:49:57 +01:00
|
|
|
self._request_middleware = None # type: Optional[List[Callable[[HttpRequest], HttpResponse]]]
|
2016-11-27 04:56:26 +01:00
|
|
|
self.initLock.acquire()
|
|
|
|
# Check that middleware is still uninitialised.
|
|
|
|
if self._request_middleware is None:
|
|
|
|
self.load_middleware()
|
|
|
|
self.initLock.release()
|
|
|
|
self._auto_finish = False
|
|
|
|
# Handler IDs are allocated here, and the handler ID map must
|
|
|
|
# be cleared when the handler finishes its response
|
|
|
|
allocate_handler_id(self)
|
|
|
|
|
2017-10-26 11:38:28 +02:00
|
|
|
def __repr__(self) -> str:
|
2016-11-27 06:02:45 +01:00
|
|
|
descriptor = get_descriptor_by_handler_id(self.handler_id)
|
|
|
|
return "AsyncDjangoHandler<%s, %s>" % (self.handler_id, descriptor)
|
2016-11-27 04:56:26 +01:00
|
|
|
|
2017-10-26 11:38:28 +02:00
|
|
|
def load_middleware(self) -> None:
|
2017-05-18 11:56:03 +02:00
|
|
|
"""
|
|
|
|
Populate middleware lists from settings.MIDDLEWARE. This is copied
|
|
|
|
from Django. This uses settings.MIDDLEWARE setting with the old
|
|
|
|
business logic. The middleware architecture is not compatible
|
|
|
|
with our asynchronous handlers. The problem occurs when we return
|
|
|
|
None from our handler. The Django middlewares throw exception
|
|
|
|
because they can't handler None, so we can either upgrade the Django
|
|
|
|
middlewares or just override this method to use the new setting with
|
|
|
|
the old logic. The added advantage is that due to this our event
|
|
|
|
system code doesn't change.
|
|
|
|
"""
|
2017-10-31 02:49:57 +01:00
|
|
|
self._request_middleware = [] # type: Optional[List[Callable[[HttpRequest], HttpResponse]]]
|
2018-03-14 23:16:27 +01:00
|
|
|
self._view_middleware = [] # type: List[Callable[[HttpRequest, ViewFuncT, List[str], Dict[str, Any]], Optional[HttpResponse]]]
|
2017-10-31 02:49:57 +01:00
|
|
|
self._template_response_middleware = [] # type: List[Callable[[HttpRequest, HttpResponse], HttpResponse]]
|
|
|
|
self._response_middleware = [] # type: List[Callable[[HttpRequest, HttpResponse], HttpResponse]]
|
|
|
|
self._exception_middleware = [] # type: List[Callable[[HttpRequest, Exception], Optional[HttpResponse]]]
|
2017-05-18 11:56:03 +02:00
|
|
|
|
|
|
|
handler = convert_exception_to_response(self._legacy_get_response)
|
|
|
|
for middleware_path in settings.MIDDLEWARE:
|
|
|
|
mw_class = import_string(middleware_path)
|
|
|
|
try:
|
|
|
|
mw_instance = mw_class()
|
|
|
|
except MiddlewareNotUsed as exc:
|
|
|
|
if settings.DEBUG:
|
2017-09-27 10:09:12 +02:00
|
|
|
if str(exc):
|
2017-05-18 11:56:03 +02:00
|
|
|
base.logger.debug('MiddlewareNotUsed(%r): %s', middleware_path, exc)
|
|
|
|
else:
|
|
|
|
base.logger.debug('MiddlewareNotUsed: %r', middleware_path)
|
|
|
|
continue
|
|
|
|
|
|
|
|
if hasattr(mw_instance, 'process_request'):
|
|
|
|
self._request_middleware.append(mw_instance.process_request)
|
|
|
|
if hasattr(mw_instance, 'process_view'):
|
|
|
|
self._view_middleware.append(mw_instance.process_view)
|
|
|
|
if hasattr(mw_instance, 'process_template_response'):
|
|
|
|
self._template_response_middleware.insert(0, mw_instance.process_template_response)
|
|
|
|
if hasattr(mw_instance, 'process_response'):
|
|
|
|
self._response_middleware.insert(0, mw_instance.process_response)
|
|
|
|
if hasattr(mw_instance, 'process_exception'):
|
|
|
|
self._exception_middleware.insert(0, mw_instance.process_exception)
|
|
|
|
|
|
|
|
# We only assign to this when initialization is complete as it is used
|
|
|
|
# as a flag for initialization being complete.
|
|
|
|
self._middleware_chain = handler
|
|
|
|
|
2017-10-26 11:38:28 +02:00
|
|
|
def get(self, *args: Any, **kwargs: Any) -> None:
|
2016-11-27 06:02:45 +01:00
|
|
|
environ = WSGIContainer.environ(self.request)
|
2016-11-27 04:56:26 +01:00
|
|
|
environ['PATH_INFO'] = urllib.parse.unquote(environ['PATH_INFO'])
|
2016-11-27 06:02:45 +01:00
|
|
|
request = WSGIRequest(environ)
|
|
|
|
request._tornado_handler = self
|
2016-11-27 04:56:26 +01:00
|
|
|
|
|
|
|
set_script_prefix(get_script_name(environ))
|
|
|
|
signals.request_started.send(sender=self.__class__)
|
|
|
|
try:
|
|
|
|
response = self.get_response(request)
|
|
|
|
|
|
|
|
if not response:
|
|
|
|
return
|
|
|
|
finally:
|
|
|
|
signals.request_finished.send(sender=self.__class__)
|
|
|
|
|
|
|
|
self.set_status(response.status_code)
|
|
|
|
for h in response.items():
|
|
|
|
self.set_header(h[0], h[1])
|
|
|
|
|
|
|
|
if not hasattr(self, "_new_cookies"):
|
2016-11-27 06:02:45 +01:00
|
|
|
self._new_cookies = [] # type: List[http.cookie.SimpleCookie]
|
2016-11-27 04:56:26 +01:00
|
|
|
self._new_cookies.append(response.cookies)
|
|
|
|
|
|
|
|
self.write(response.content)
|
|
|
|
self.finish()
|
|
|
|
|
2017-10-26 11:38:28 +02:00
|
|
|
def head(self, *args: Any, **kwargs: Any) -> None:
|
2016-11-27 04:56:26 +01:00
|
|
|
self.get(*args, **kwargs)
|
|
|
|
|
2017-10-26 11:38:28 +02:00
|
|
|
def post(self, *args: Any, **kwargs: Any) -> None:
|
2016-11-27 04:56:26 +01:00
|
|
|
self.get(*args, **kwargs)
|
|
|
|
|
2017-10-26 11:38:28 +02:00
|
|
|
def delete(self, *args: Any, **kwargs: Any) -> None:
|
2016-11-27 04:56:26 +01:00
|
|
|
self.get(*args, **kwargs)
|
|
|
|
|
2017-10-26 11:38:28 +02:00
|
|
|
def on_connection_close(self) -> None:
|
2016-11-27 04:56:26 +01:00
|
|
|
client_descriptor = get_descriptor_by_handler_id(self.handler_id)
|
|
|
|
if client_descriptor is not None:
|
|
|
|
client_descriptor.disconnect_handler(client_closed=True)
|
|
|
|
|
|
|
|
# Based on django.core.handlers.base: get_response
|
2017-10-26 11:38:28 +02:00
|
|
|
def get_response(self, request: HttpRequest) -> HttpResponse:
|
2016-11-27 04:56:26 +01:00
|
|
|
"Returns an HttpResponse object for the given HttpRequest"
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
# Setup default url resolver for this thread.
|
|
|
|
urlconf = settings.ROOT_URLCONF
|
2018-01-30 06:05:25 +01:00
|
|
|
set_urlconf(urlconf)
|
|
|
|
resolver = resolvers.RegexURLResolver(r'^/', urlconf)
|
2016-11-27 04:56:26 +01:00
|
|
|
|
|
|
|
response = None
|
|
|
|
|
|
|
|
# Apply request middleware
|
2017-08-07 02:01:59 +02:00
|
|
|
for middleware_method in self._request_middleware:
|
2016-11-27 04:56:26 +01:00
|
|
|
response = middleware_method(request)
|
|
|
|
if response:
|
|
|
|
break
|
|
|
|
|
|
|
|
if hasattr(request, "urlconf"):
|
|
|
|
# Reset url resolver with a custom urlconf.
|
|
|
|
urlconf = request.urlconf
|
2018-01-30 06:05:25 +01:00
|
|
|
set_urlconf(urlconf)
|
|
|
|
resolver = resolvers.RegexURLResolver(r'^/', urlconf)
|
2016-11-27 04:56:26 +01:00
|
|
|
|
|
|
|
### ADDED BY ZULIP
|
|
|
|
request._resolver = resolver
|
|
|
|
### END ADDED BY ZULIP
|
|
|
|
|
|
|
|
callback, callback_args, callback_kwargs = resolver.resolve(
|
2016-11-27 06:02:45 +01:00
|
|
|
request.path_info)
|
2016-11-27 04:56:26 +01:00
|
|
|
|
|
|
|
# Apply view middleware
|
|
|
|
if response is None:
|
2017-10-31 02:49:57 +01:00
|
|
|
for view_middleware_method in self._view_middleware:
|
|
|
|
response = view_middleware_method(request, callback,
|
|
|
|
callback_args, callback_kwargs)
|
2016-11-27 04:56:26 +01:00
|
|
|
if response:
|
|
|
|
break
|
|
|
|
|
|
|
|
### THIS BLOCK MODIFIED BY ZULIP
|
|
|
|
if response is None:
|
|
|
|
try:
|
|
|
|
response = callback(request, *callback_args, **callback_kwargs)
|
|
|
|
if response is RespondAsynchronously:
|
|
|
|
async_request_stop(request)
|
|
|
|
return None
|
|
|
|
clear_handler_by_id(self.handler_id)
|
|
|
|
except Exception as e:
|
|
|
|
clear_handler_by_id(self.handler_id)
|
|
|
|
# If the view raised an exception, run it through exception
|
|
|
|
# middleware, and if the exception middleware returns a
|
|
|
|
# response, use that. Otherwise, reraise the exception.
|
2017-10-31 02:49:57 +01:00
|
|
|
for exception_middleware_method in self._exception_middleware:
|
|
|
|
response = exception_middleware_method(request, e)
|
2016-11-27 04:56:26 +01:00
|
|
|
if response:
|
|
|
|
break
|
|
|
|
if response is None:
|
|
|
|
raise
|
|
|
|
|
|
|
|
if response is None:
|
|
|
|
try:
|
|
|
|
view_name = callback.__name__
|
|
|
|
except AttributeError:
|
|
|
|
view_name = callback.__class__.__name__ + '.__call__'
|
|
|
|
raise ValueError("The view %s.%s returned None." %
|
|
|
|
(callback.__module__, view_name))
|
|
|
|
|
|
|
|
# If the response supports deferred rendering, apply template
|
|
|
|
# response middleware and the render the response
|
|
|
|
if hasattr(response, 'render') and callable(response.render):
|
2017-10-31 02:49:57 +01:00
|
|
|
for template_middleware_method in self._template_response_middleware:
|
|
|
|
response = template_middleware_method(request, response)
|
2016-11-27 04:56:26 +01:00
|
|
|
response = response.render()
|
|
|
|
|
|
|
|
except http.Http404 as e:
|
|
|
|
if settings.DEBUG:
|
|
|
|
from django.views import debug
|
|
|
|
response = debug.technical_404_response(request, e)
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
callback, param_dict = resolver.resolve404()
|
|
|
|
response = callback(request, **param_dict)
|
2017-03-05 10:25:27 +01:00
|
|
|
except Exception:
|
2016-11-27 04:56:26 +01:00
|
|
|
try:
|
2016-11-27 06:02:45 +01:00
|
|
|
response = self.handle_uncaught_exception(request, resolver,
|
|
|
|
sys.exc_info())
|
2016-11-27 04:56:26 +01:00
|
|
|
finally:
|
2016-11-27 06:02:45 +01:00
|
|
|
signals.got_request_exception.send(sender=self.__class__,
|
|
|
|
request=request)
|
2016-11-27 04:56:26 +01:00
|
|
|
except exceptions.PermissionDenied:
|
|
|
|
logging.warning(
|
|
|
|
'Forbidden (Permission denied): %s', request.path,
|
|
|
|
extra={
|
|
|
|
'status_code': 403,
|
|
|
|
'request': request
|
|
|
|
})
|
|
|
|
try:
|
|
|
|
callback, param_dict = resolver.resolve403()
|
|
|
|
response = callback(request, **param_dict)
|
2017-03-05 10:25:27 +01:00
|
|
|
except Exception:
|
2016-11-27 04:56:26 +01:00
|
|
|
try:
|
|
|
|
response = self.handle_uncaught_exception(request,
|
2016-11-27 06:02:45 +01:00
|
|
|
resolver, sys.exc_info())
|
2016-11-27 04:56:26 +01:00
|
|
|
finally:
|
|
|
|
signals.got_request_exception.send(
|
|
|
|
sender=self.__class__, request=request)
|
|
|
|
except SystemExit:
|
|
|
|
# See https://code.djangoproject.com/ticket/4701
|
|
|
|
raise
|
|
|
|
except Exception as e:
|
|
|
|
exc_info = sys.exc_info()
|
|
|
|
signals.got_request_exception.send(sender=self.__class__, request=request)
|
|
|
|
return self.handle_uncaught_exception(request, resolver, exc_info)
|
|
|
|
finally:
|
|
|
|
# Reset urlconf on the way out for isolation
|
2018-01-30 06:05:25 +01:00
|
|
|
set_urlconf(None)
|
2016-11-27 04:56:26 +01:00
|
|
|
|
|
|
|
### ZULIP CHANGE: The remainder of this function was moved
|
|
|
|
### into its own function, just below, so we can call it from
|
|
|
|
### finish().
|
|
|
|
response = self.apply_response_middleware(request, response, resolver)
|
|
|
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
### Copied from get_response (above in this file)
|
2017-10-26 11:38:28 +02:00
|
|
|
def apply_response_middleware(self, request: HttpRequest, response: HttpResponse,
|
2018-01-30 06:05:25 +01:00
|
|
|
resolver: resolvers.RegexURLResolver) -> HttpResponse:
|
2016-11-27 04:56:26 +01:00
|
|
|
try:
|
|
|
|
# Apply response middleware, regardless of the response
|
|
|
|
for middleware_method in self._response_middleware:
|
|
|
|
response = middleware_method(request, response)
|
|
|
|
if hasattr(self, 'apply_response_fixes'):
|
|
|
|
response = self.apply_response_fixes(request, response)
|
2017-03-05 10:25:27 +01:00
|
|
|
except Exception: # Any exception should be gathered and handled
|
2016-11-27 04:56:26 +01:00
|
|
|
signals.got_request_exception.send(sender=self.__class__, request=request)
|
|
|
|
response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
|
|
|
|
|
|
|
|
return response
|
|
|
|
|
2017-10-26 11:38:28 +02:00
|
|
|
def zulip_finish(self, response: Dict[str, Any], request: HttpRequest,
|
|
|
|
apply_markdown: bool) -> None:
|
2016-11-27 04:56:26 +01:00
|
|
|
# Make sure that Markdown rendering really happened, if requested.
|
|
|
|
# This is a security issue because it's where we escape HTML.
|
|
|
|
# c.f. ticket #64
|
|
|
|
#
|
|
|
|
# apply_markdown=True is the fail-safe default.
|
|
|
|
if response['result'] == 'success' and 'messages' in response and apply_markdown:
|
|
|
|
for msg in response['messages']:
|
|
|
|
if msg['content_type'] != 'text/html':
|
|
|
|
self.set_status(500)
|
|
|
|
self.finish('Internal error: bad message format')
|
|
|
|
if response['result'] == 'error':
|
|
|
|
self.set_status(400)
|
|
|
|
|
|
|
|
# Call the Django response middleware on our object so that
|
|
|
|
# e.g. our own logging code can run; but don't actually use
|
|
|
|
# the headers from that since sending those to Tornado seems
|
|
|
|
# tricky; instead just send the (already json-rendered)
|
|
|
|
# content on to Tornado
|
|
|
|
django_response = json_response(res_type=response['result'],
|
|
|
|
data=response, status=self.get_status())
|
|
|
|
django_response = self.apply_response_middleware(request, django_response,
|
|
|
|
request._resolver)
|
|
|
|
# Pass through the content-type from Django, as json content should be
|
|
|
|
# served as application/json
|
|
|
|
self.set_header("Content-Type", django_response['Content-Type'])
|
|
|
|
self.finish(django_response.content)
|