/usr/lib/python3/dist-packages/trytond/wsgi.py is in tryton-server 4.6.3-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
import http.client
import logging
import sys
import traceback
from werkzeug.wrappers import Response
from werkzeug.routing import Map, Rule
from werkzeug.exceptions import abort, HTTPException, InternalServerError
import wrapt
from trytond.config import config
from trytond.protocols.wrappers import Request
from trytond.protocols.jsonrpc import JSONProtocol
from trytond.protocols.xmlrpc import XMLProtocol
__all__ = ['TrytondWSGI', 'app']
logger = logging.getLogger(__name__)
class TrytondWSGI(object):
def __init__(self):
self.url_map = Map([])
self.protocols = [JSONProtocol, XMLProtocol]
self.error_handlers = []
def route(self, string, methods=None):
def decorator(func):
self.url_map.add(Rule(string, endpoint=func, methods=methods))
return func
return decorator
@wrapt.decorator
def auth_required(self, wrapped, instance, args, kwargs):
request = args[0]
if request.user_id:
return wrapped(*args, **kwargs)
else:
abort(http.client.UNAUTHORIZED)
def check_request_size(self, request, size=None):
if request.method not in {'POST', 'PUT', 'PATCH'}:
return
if size is None:
if request.user_id:
max_size = config.getint(
'request', 'max_size_authenticated')
else:
max_size = config.getint(
'request', 'max_size')
else:
max_size = size
if max_size:
content_length = request.content_length
if content_length is None:
abort(http.client.LENGTH_REQUIRED)
elif content_length > max_size:
abort(http.client.REQUEST_ENTITY_TOO_LARGE)
def dispatch_request(self, request):
adapter = self.url_map.bind_to_environ(request.environ)
try:
endpoint, request.view_args = adapter.match()
max_request_size = getattr(endpoint, 'max_request_size', None)
self.check_request_size(request, max_request_size)
return endpoint(request, **request.view_args)
except HTTPException as e:
return e
except Exception as e:
tb_s = ''.join(traceback.format_exception(*sys.exc_info()))
for path in sys.path:
tb_s = tb_s.replace(path, '')
e.__format_traceback__ = tb_s
response = e
for error_handler in self.error_handlers:
rv = error_handler(e)
if isinstance(rv, Response):
response = rv
return response
def wsgi_app(self, environ, start_response):
for cls in self.protocols:
if cls.content_type in environ.get('CONTENT_TYPE', ''):
request = cls.request(environ)
break
else:
request = Request(environ)
data = self.dispatch_request(request)
if not isinstance(data, (Response, HTTPException)):
for cls in self.protocols:
for mimetype in request.accept_mimetypes:
if cls.content_type in mimetype:
response = cls.response(data, request)
break
else:
continue
break
else:
for cls in self.protocols:
if cls.content_type in environ.get('CONTENT_TYPE', ''):
response = cls.response(data, request)
break
else:
if isinstance(data, Exception):
response = InternalServerError(data)
else:
response = Response(data)
else:
response = data
# TODO custom process response
return response(environ, start_response)
def __call__(self, environ, start_response):
return self.wsgi_app(environ, start_response)
app = TrytondWSGI()
import trytond.protocols.dispatcher
|