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
|
2018-10-17 00:39:10 +02:00
|
|
|
from zerver.middleware import async_request_timer_restart, async_request_timer_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:
|
2018-10-17 00:39:10 +02:00
|
|
|
# We call async_request_timer_restart here in case we are
|
2016-11-27 06:36:06 +01:00
|
|
|
# being finished without any events (because another
|
|
|
|
# get_events request has supplanted this request)
|
|
|
|
handler = get_handler_by_id(handler_id)
|
|
|
|
request = handler._request
|
2018-10-17 00:39:10 +02:00
|
|
|
async_request_timer_restart(request)
|
2016-11-27 06:36:06 +01:00
|
|
|
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
|
2018-05-16 03:39:52 +02:00
|
|
|
# We mark this for nocoverage, since we only change 1 line of actual code.
|
|
|
|
class AsyncDjangoHandlerBase(tornado.web.RequestHandler, base.BaseHandler): # nocoverage
|
2016-11-27 04:56:26 +01:00
|
|
|
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.
|
|
|
|
"""
|
2019-07-22 22:19:48 +02:00
|
|
|
self._request_middleware = []
|
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
|
|
|
|
|
2020-02-08 01:02:23 +01:00
|
|
|
def convert_tornado_request_to_django_request(self) -> HttpRequest:
|
|
|
|
# This takes the WSGI environment that Tornado received (which
|
|
|
|
# fully describes the HTTP request that was sent to Tornado)
|
|
|
|
# and pass it to Django's WSGIRequest to generate a Django
|
|
|
|
# HttpRequest object with the original Tornado request's HTTP
|
|
|
|
# headers, parameters, etc.
|
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'])
|
|
|
|
|
2020-02-08 01:02:23 +01:00
|
|
|
# Django WSGIRequest setup code that should match logic from
|
|
|
|
# Django's WSGIHandler.__call__ before the call to
|
|
|
|
# `get_response()`.
|
2016-11-27 04:56:26 +01:00
|
|
|
set_script_prefix(get_script_name(environ))
|
|
|
|
signals.request_started.send(sender=self.__class__)
|
2020-02-08 01:02:23 +01:00
|
|
|
request = WSGIRequest(environ)
|
|
|
|
|
|
|
|
# Provide a way for application code to access this handler
|
|
|
|
# given the HttpRequest object.
|
|
|
|
request._tornado_handler = self
|
|
|
|
|
|
|
|
return request
|
|
|
|
|
2020-02-08 01:10:35 +01:00
|
|
|
def write_django_response_as_tornado_response(self, response: HttpResponse) -> None:
|
|
|
|
# This takes a Django HttpResponse and copies its HTTP status
|
|
|
|
# code, headers, cookies, and content onto this
|
|
|
|
# tornado.web.RequestHandler (which is how Tornado prepares a
|
|
|
|
# response to write).
|
2016-11-27 04:56:26 +01:00
|
|
|
|
2020-02-08 01:10:35 +01:00
|
|
|
# Copy the HTTP status code.
|
2016-11-27 04:56:26 +01:00
|
|
|
self.set_status(response.status_code)
|
2020-02-08 01:10:35 +01:00
|
|
|
|
|
|
|
# Copy the HTTP headers (iterating through a Django
|
|
|
|
# HttpResponse is the way to access its headers as key/value pairs)
|
2016-11-27 04:56:26 +01:00
|
|
|
for h in response.items():
|
|
|
|
self.set_header(h[0], h[1])
|
|
|
|
|
2020-02-08 01:10:35 +01:00
|
|
|
# Copy any cookies
|
2016-11-27 04:56:26 +01:00
|
|
|
if not hasattr(self, "_new_cookies"):
|
2019-11-13 10:06:02 +01:00
|
|
|
self._new_cookies = [] # type: List[http.cookie.SimpleCookie[str]]
|
2016-11-27 04:56:26 +01:00
|
|
|
self._new_cookies.append(response.cookies)
|
|
|
|
|
2020-02-08 01:10:35 +01:00
|
|
|
# Copy the response content
|
2016-11-27 04:56:26 +01:00
|
|
|
self.write(response.content)
|
2020-02-08 01:10:35 +01:00
|
|
|
|
|
|
|
# Close the connection.
|
2016-11-27 04:56:26 +01:00
|
|
|
self.finish()
|
|
|
|
|
2020-02-08 01:10:35 +01:00
|
|
|
def get(self, *args: Any, **kwargs: Any) -> None:
|
|
|
|
request = self.convert_tornado_request_to_django_request()
|
|
|
|
|
|
|
|
try:
|
|
|
|
response = self.get_response(request)
|
|
|
|
|
|
|
|
if not response:
|
|
|
|
return
|
|
|
|
finally:
|
|
|
|
signals.request_finished.send(sender=self.__class__)
|
|
|
|
|
|
|
|
self.write_django_response_as_tornado_response(response)
|
|
|
|
|
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:
|
2018-10-17 00:39:10 +02:00
|
|
|
async_request_timer_stop(request)
|
2016-11-27 04:56:26 +01:00
|
|
|
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
|
2018-05-24 16:41:34 +02:00
|
|
|
except Exception:
|
2016-11-27 04:56:26 +01:00
|
|
|
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
|
|
|
|
|
2018-05-16 03:39:52 +02:00
|
|
|
class AsyncDjangoHandler(AsyncDjangoHandlerBase):
|
2020-02-06 23:42:07 +01:00
|
|
|
def zulip_finish(self, result_dict: Dict[str, Any], request: HttpRequest,
|
2017-10-26 11:38:28 +02:00
|
|
|
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.
|
2020-02-06 23:42:07 +01:00
|
|
|
if result_dict['result'] == 'success' and 'messages' in result_dict and apply_markdown:
|
|
|
|
for msg in result_dict['messages']:
|
2016-11-27 04:56:26 +01:00
|
|
|
if msg['content_type'] != 'text/html':
|
|
|
|
self.set_status(500)
|
|
|
|
self.finish('Internal error: bad message format')
|
2020-02-06 23:42:07 +01:00
|
|
|
if result_dict['result'] == 'error':
|
2016-11-27 04:56:26 +01:00
|
|
|
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
|
2020-02-06 23:42:07 +01:00
|
|
|
django_response = json_response(res_type=result_dict['result'],
|
|
|
|
data=result_dict, status=self.get_status())
|
2016-11-27 04:56:26 +01:00
|
|
|
django_response = self.apply_response_middleware(request, django_response,
|
|
|
|
request._resolver)
|
2020-02-08 01:14:19 +01:00
|
|
|
|
|
|
|
self.write_django_response_as_tornado_response(django_response)
|