diff --git a/isso/__init__.py b/isso/__init__.py index 769afdd..dc4c650 100644 --- a/isso/__init__.py +++ b/isso/__init__.py @@ -40,7 +40,7 @@ except ImportError: import sys import os -import socket +import errno import logging import tempfile @@ -54,7 +54,7 @@ from werkzeug.exceptions import HTTPException, InternalServerError from werkzeug.wsgi import SharedDataMiddleware from werkzeug.local import Local, LocalManager -from werkzeug.serving import run_simple, WSGIRequestHandler +from werkzeug.serving import run_simple from werkzeug.contrib.fixers import ProxyFix local = Local() @@ -211,43 +211,13 @@ def main(): run_simple(host, port, make_app(conf), threaded=True, use_reloader=conf.getboolean('server', 'reload')) else: - try: - from socketserver import ThreadingMixIn - from http.server import HTTPServer - except ImportError: - from SocketServer import ThreadingMixIn - from BaseHTTPServer import HTTPServer - - class SocketWSGIRequestHandler(WSGIRequestHandler): - - def run_wsgi(self): - self.client_address = ("", 0) - super(SocketWSGIRequestHandler, self).run_wsgi() - - class SocketHTTPServer(HTTPServer, ThreadingMixIn): - - multithread = True - multiprocess = False - - allow_reuse_address = 1 - address_family = socket.AF_UNIX - - request_queue_size = 128 - - def __init__(self, sock, app): - HTTPServer.__init__(self, sock, SocketWSGIRequestHandler) - self.app = app - self.ssl_context = None - self.shutdown_signal = False - sock = conf.get("server", "listen").partition("unix://")[2] - try: os.unlink(sock) - except OSError: - pass - - SocketHTTPServer(sock, make_app(conf)).serve_forever() + except OSError as ex: + if ex.errno != errno.ENOENT: + raise + wsgi.SocketHTTPServer(sock, make_app(conf)).serve_forever() try: import uwsgi diff --git a/isso/wsgi.py b/isso/wsgi.py index 1b3e202..35ddd84 100644 --- a/isso/wsgi.py +++ b/isso/wsgi.py @@ -1,10 +1,19 @@ # -*- encoding: utf-8 -*- +import socket + try: - from urllib import quote -except ImportError: from urllib.parse import quote + from socketserver import ThreadingMixIn + from http.server import HTTPServer +except ImportError: + from urllib import quote + + from SocketServer import ThreadingMixIn + from BaseHTTPServer import HTTPServer + +from werkzeug.serving import WSGIRequestHandler from werkzeug.datastructures import Headers @@ -23,10 +32,10 @@ def host(environ): if environ['wsgi.url_scheme'] == 'https': if environ['SERVER_PORT'] != '443': - url += ':' + environ['SERVER_PORT'] + url += ':' + environ['SERVER_PORT'] else: if environ['SERVER_PORT'] != '80': - url += ':' + environ['SERVER_PORT'] + url += ':' + environ['SERVER_PORT'] return url + quote(environ.get('SCRIPT_NAME', '')) @@ -49,6 +58,7 @@ class SubURI(object): class CORSMiddleware(object): + """Add Cross-origin resource sharing headers to every request.""" def __init__(self, app, origin): self.app = app @@ -70,3 +80,30 @@ class CORSMiddleware(object): return ['200 Ok'] return self.app(environ, add_cors_headers) + + +class SocketWSGIRequestHandler(WSGIRequestHandler): + + def run_wsgi(self): + self.client_address = ("", 0) + super(SocketWSGIRequestHandler, self).run_wsgi() + + +class SocketHTTPServer(HTTPServer, ThreadingMixIn): + """ + A simple SocketServer to serve werkzeug's WSGIRequesthandler. + """ + + multithread = True + multiprocess = False + + allow_reuse_address = 1 + address_family = socket.AF_UNIX + + request_queue_size = 128 + + def __init__(self, sock, app): + HTTPServer.__init__(self, sock, SocketWSGIRequestHandler) + self.app = app + self.ssl_context = None + self.shutdown_signal = False