changeset 014c206382b8 in trytond:default
details: https://hg.tryton.org/trytond?cmd=changeset;node=014c206382b8
description:
        Allow to redirect bus request

        issue7943
        review48691003
diffstat:

 CHANGELOG                       |   3 +++
 doc/topics/configuration.rst    |  18 ++++++++++++++++++
 trytond/bus.py                  |  15 +++++++++++++++
 trytond/config.py               |   1 +
 trytond/protocols/dispatcher.py |   6 ++++++
 trytond/wsgi.py                 |  29 ++++++++++++++++++++++++++++-
 6 files changed, 71 insertions(+), 1 deletions(-)

diffs (179 lines):

diff -r d387b2d058df -r 014c206382b8 CHANGELOG
--- a/CHANGELOG Fri Jan 18 00:24:28 2019 +0100
+++ b/CHANGELOG Fri Jan 18 11:01:22 2019 +0100
@@ -1,3 +1,6 @@
+* Add cache timeout for web
+* Allow the bus requests to be redirected to another host
+* Add support for CORS
 * Add extras modules to ModuleTestCase
 * Add timeout to Report.convert
 * Allow records from XML with noupdate to be deleted
diff -r d387b2d058df -r 014c206382b8 doc/topics/configuration.rst
--- a/doc/topics/configuration.rst      Fri Jan 18 00:24:28 2019 +0100
+++ b/doc/topics/configuration.rst      Fri Jan 18 11:01:22 2019 +0100
@@ -64,6 +64,19 @@
 
 Default: 0
 
+cache_timeout
+~~~~~~~~~~~~~
+
+The cache timeout in seconds.
+
+Default: 12h
+
+cors
+~~~~
+
+The list (one per line) of origins allowed for `Cross-Origin Resource sharing
+<https://en.wikipedia.org/wiki/Cross-origin_resource_sharing>`_.
+
 database
 --------
 
@@ -382,6 +395,11 @@
 
 Default: `False`
 
+url_host
+~~~~~~~~
+
+If set redirects bus requests to the host URL.
+
 long_polling_timeout
 ~~~~~~~~~~~~~~~~~~~~
 
diff -r d387b2d058df -r 014c206382b8 trytond/bus.py
--- a/trytond/bus.py    Fri Jan 18 00:24:28 2019 +0100
+++ b/trytond/bus.py    Fri Jan 18 11:01:22 2019 +0100
@@ -8,7 +8,13 @@
 import threading
 import time
 import uuid
+try:
+    from http import HTTPStatus
+except ImportError:
+    from http import client as HTTPStatus
+from urllib.parse import urljoin
 
+from werkzeug.utils import redirect
 from werkzeug.wrappers import Response
 from werkzeug.exceptions import NotImplemented, BadRequest
 
@@ -27,6 +33,8 @@
 _select_timeout = config.getint('bus', 'select_timeout')
 _long_polling_timeout = config.getint('bus', 'long_polling_timeout')
 _allow_subscribe = config.getboolean('bus', 'allow_subscribe')
+_url_host = config.get('bus', 'url_host')
+_web_cache_timeout = config.getint('web', 'cache_timeout')
 
 
 class _MessageQueue:
@@ -210,6 +218,13 @@
 def subscribe(request, database_name):
     if not _allow_subscribe:
         raise NotImplemented
+    if _url_host and _url_host != request.host_url:
+        response = redirect(
+            urljoin(_url_host, request.path), HTTPStatus.PERMANENT_REDIRECT)
+        # Allow to change the redirection after some time
+        response.headers['Cache-Control'] = (
+            'private, max-age=%s' % _web_cache_timeout)
+        return response
     user = request.authorization.get('userid')
     channels = request.parsed_data.get('channels', [])
     if user is None:
diff -r d387b2d058df -r 014c206382b8 trytond/config.py
--- a/trytond/config.py Fri Jan 18 00:24:28 2019 +0100
+++ b/trytond/config.py Fri Jan 18 11:01:22 2019 +0100
@@ -45,6 +45,7 @@
         self.set('web', 'listen', 'localhost:8000')
         self.set('web', 'root', os.path.join(os.path.expanduser('~'), 'www'))
         self.set('web', 'num_proxies', 0)
+        self.set('web', 'cache_timeout', 60 * 60 * 12)
         self.add_section('database')
         self.set('database', 'uri',
             os.environ.get('TRYTOND_DATABASE_URI', 'sqlite://'))
diff -r d387b2d058df -r 014c206382b8 trytond/protocols/dispatcher.py
--- a/trytond/protocols/dispatcher.py   Fri Jan 18 00:24:28 2019 +0100
+++ b/trytond/protocols/dispatcher.py   Fri Jan 18 11:01:22 2019 +0100
@@ -10,6 +10,7 @@
     from http import client as HTTPStatus
 
 from werkzeug.exceptions import abort
+from werkzeug.wrappers import Response
 from sql import Table
 
 from trytond import security
@@ -87,6 +88,11 @@
     return methods[request.rpc_method](request, *request.rpc_params)
 
 
[email protected]('/<path:path>', methods=['OPTIONS'])
+def options(request, path):
+    return Response(status=HTTPStatus.NO_CONTENT)
+
+
 def db_exist(request, database_name):
     Database = backend.get('Database')
     try:
diff -r d387b2d058df -r 014c206382b8 trytond/wsgi.py
--- a/trytond/wsgi.py   Fri Jan 18 00:24:28 2019 +0100
+++ b/trytond/wsgi.py   Fri Jan 18 11:01:22 2019 +0100
@@ -5,6 +5,10 @@
 import os
 import sys
 import traceback
+try:
+    from http import HTTPStatus
+except ImportError:
+    from http import client as HTTPStatus
 
 from werkzeug.wrappers import Response
 from werkzeug.routing import Map, Rule
@@ -92,6 +96,14 @@
                 break
         else:
             request = Request(environ)
+
+        origin = request.headers.get('Origin')
+        if origin:
+            cors = filter(
+                None, config.get('web', 'cors', default='').splitlines())
+            if origin not in cors:
+                abort(HTTPStatus.FORBIDDEN)
+
         data = self.dispatch_request(request)
         if not isinstance(data, (Response, HTTPException)):
             for cls in self.protocols:
@@ -114,6 +126,19 @@
                         response = Response(data)
         else:
             response = data
+
+        if origin:
+            response.headers['Access-Control-Allow-Origin'] = origin
+            response.headers['Vary'] = 'Origin'
+            method = request.headers.get('Access-Control-Request-Method')
+            if method:
+                response.headers['Access-Control-Allow-Methods'] = method
+            headers = request.headers.get('Access-Control-Request-Headers')
+            if headers:
+                response.headers['Access-Control-Allow-Headers'] = headers
+            response.headers['Access-Control-Max-Age'] = config.getint(
+                'web', 'cache_timeout')
+
         # TODO custom process response
         return response(environ, start_response)
 
@@ -147,7 +172,9 @@
     static_files = {
         '/': config.get('web', 'root'),
         }
-    app.wsgi_app = SharedDataMiddlewareIndex(app.wsgi_app, static_files)
+    app.wsgi_app = SharedDataMiddlewareIndex(
+        app.wsgi_app, static_files,
+        cache_timeout=config.getint('web', 'cache_timeout'))
 num_proxies = config.getint('web', 'num_proxies')
 if num_proxies:
     app.wsgi_app = ProxyFix(app.wsgi_app, num_proxies=num_proxies)

Reply via email to