diff MoinMoin/support/werkzeug/serving.py @ 6129:7f12cf241d5e

update werkzeug to 0.12.1, update CHANGES
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Tue, 11 Apr 2017 22:42:23 +0200
parents 9f12f41504fc
children df65dcf7daea
line wrap: on
line diff
--- a/MoinMoin/support/werkzeug/serving.py	Tue Nov 01 17:56:32 2016 +0100
+++ b/MoinMoin/support/werkzeug/serving.py	Tue Apr 11 22:42:23 2017 +0200
@@ -42,6 +42,15 @@
 import sys
 import signal
 
+
+can_fork = hasattr(os, "fork")
+
+
+try:
+    import termcolor
+except ImportError:
+    termcolor = None
+
 try:
     import ssl
 except ImportError:
@@ -62,22 +71,30 @@
 
 
 try:
-    from SocketServer import ThreadingMixIn, ForkingMixIn
+    import SocketServer as socketserver
     from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
 except ImportError:
-    from socketserver import ThreadingMixIn, ForkingMixIn
+    import socketserver
     from http.server import HTTPServer, BaseHTTPRequestHandler
 
+ThreadingMixIn = socketserver.ThreadingMixIn
+
+if can_fork:
+    ForkingMixIn = socketserver.ForkingMixIn
+else:
+    class ForkingMixIn(object):
+        pass
+
 # important: do not use relative imports here or python -m will break
 import werkzeug
 from werkzeug._internal import _log
-from werkzeug._compat import PY2, reraise, wsgi_encoding_dance
+from werkzeug._compat import PY2, WIN, reraise, wsgi_encoding_dance
 from werkzeug.urls import url_parse, url_unquote
 from werkzeug.exceptions import InternalServerError
 
 
 LISTEN_QUEUE = 128
-can_open_by_fd = hasattr(socket, 'fromfd')
+can_open_by_fd = not WIN and hasattr(socket, 'fromfd')
 
 
 class WSGIRequestHandler(BaseHTTPRequestHandler, object):
@@ -111,8 +128,6 @@
             'SCRIPT_NAME':          '',
             'PATH_INFO':            wsgi_encoding_dance(path_info),
             'QUERY_STRING':         wsgi_encoding_dance(request_url.query),
-            'CONTENT_TYPE':         self.headers.get('Content-Type', ''),
-            'CONTENT_LENGTH':       self.headers.get('Content-Length', ''),
             'REMOTE_ADDR':          self.address_string(),
             'REMOTE_PORT':          self.port_integer(),
             'SERVER_NAME':          self.server.server_address[0],
@@ -121,9 +136,10 @@
         }
 
         for key, value in self.headers.items():
-            key = 'HTTP_' + key.upper().replace('-', '_')
-            if key not in ('HTTP_CONTENT_TYPE', 'HTTP_CONTENT_LENGTH'):
-                environ[key] = value
+            key = key.upper().replace('-', '_')
+            if key not in ('CONTENT_TYPE', 'CONTENT_LENGTH'):
+                key = 'HTTP_' + key
+            environ[key] = value
 
         if request_url.scheme and request_url.netloc:
             environ['HTTP_HOST'] = request_url.netloc
@@ -269,7 +285,28 @@
         return self.client_address[1]
 
     def log_request(self, code='-', size='-'):
-        self.log('info', '"%s" %s %s', self.requestline, code, size)
+        msg = self.requestline
+        code = str(code)
+
+        if termcolor:
+            color = termcolor.colored
+
+            if code[0] == '1':    # 1xx - Informational
+                msg = color(msg, attrs=['bold'])
+            if code[0] == '2':    # 2xx - Success
+                msg = color(msg, color='white')
+            elif code == '304':   # 304 - Resource Not Modified
+                msg = color(msg, color='cyan')
+            elif code[0] == '3':  # 3xx - Redirection
+                msg = color(msg, color='green')
+            elif code == '404':   # 404 - Resource Not Found
+                msg = color(msg, color='yellow')
+            elif code[0] == '4':  # 4xx - Client Error
+                msg = color(msg, color='red', attrs=['bold'])
+            else:                 # 5xx, or any other response
+                msg = color(msg, color='magenta', attrs=['bold'])
+
+        self.log('info', '"%s" %s %s', msg, code, size)
 
     def log_error(self, *args):
         self.log('error', *args)
@@ -309,9 +346,9 @@
     issuer.O = 'Self-Signed'
 
     pkey = crypto.PKey()
-    pkey.generate_key(crypto.TYPE_RSA, 1024)
+    pkey.generate_key(crypto.TYPE_RSA, 2048)
     cert.set_pubkey(pkey)
-    cert.sign(pkey, 'md5')
+    cert.sign(pkey, 'sha256')
 
     return cert, pkey
 
@@ -466,7 +503,7 @@
         self.passthrough_errors = passthrough_errors
         self.shutdown_signal = False
         self.host = host
-        self.port = port
+        self.port = self.socket.getsockname()[1]
 
         # Patch in the original socket.
         if fd is not None:
@@ -516,6 +553,7 @@
 
     """A WSGI server that does threading."""
     multithread = True
+    daemon_threads = True
 
 
 class ForkingWSGIServer(ForkingMixIn, BaseWSGIServer):
@@ -525,6 +563,8 @@
 
     def __init__(self, host, port, app, processes=40, handler=None,
                  passthrough_errors=False, ssl_context=None, fd=None):
+        if not can_fork:
+            raise ValueError('Your platform does not support forking.')
         BaseWSGIServer.__init__(self, host, port, app, handler,
                                 passthrough_errors, ssl_context, fd)
         self.max_children = processes
@@ -687,7 +727,9 @@
             else:
                 s.close()
 
-        from ._reloader import run_with_reloader
+        # Do not use relative imports, otherwise "python -m werkzeug.serving"
+        # breaks.
+        from werkzeug._reloader import run_with_reloader
         run_with_reloader(inner, extra_files, reloader_interval,
                           reloader_type)
     else:
@@ -697,7 +739,7 @@
 def run_with_reloader(*args, **kwargs):
     # People keep using undocumented APIs.  Do not use this function
     # please, we do not guarantee that it continues working.
-    from ._reloader import run_with_reloader
+    from werkzeug._reloader import run_with_reloader
     return run_with_reloader(*args, **kwargs)