-
Notifications
You must be signed in to change notification settings - Fork 120
/
wsgi_server.py
71 lines (57 loc) · 2.14 KB
/
wsgi_server.py
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
import importlib
import logging
import sys
import traceback
from saml2.httputil import Unauthorized
from saml2.httputil import NotFound
from saml2.httputil import ServiceError
from satosa.base import SATOSABase
from satosa.context import Context
from satosa.routing import NoBoundEndpointError
from satosa.service import unpack_either
LOGGER = logging.getLogger("")
LOGFILE_NAME = 's2s.log'
hdlr = logging.FileHandler(LOGFILE_NAME)
base_formatter = logging.Formatter("%(asctime)s %(name)s:%(levelname)s %(message)s")
hdlr.setFormatter(base_formatter)
LOGGER.addHandler(hdlr)
LOGGER.setLevel(logging.DEBUG)
class WsgiApplication(object):
def __init__(self, config, debug=False):
self.debug = debug
if config is None:
raise ValueError("Missing configuration")
self.satosa = SATOSABase(config)
def run_server(self, environ, start_response):
"""
The main WSGI application.
If nothing matches return NotFound.
:param environ: The HTTP application environment
:param start_response: The application to run when the handling of the
request is done
:return: The response as a list of lines
"""
path = environ.get('PATH_INFO', '').lstrip('/')
if ".." in path:
resp = Unauthorized()
return resp(environ, start_response)
context = Context()
context.path = path
context.request = unpack_either(environ)
context.cookie = environ.get("HTTP_COOKIE", "")
try:
resp = self.satosa.run(context)
return resp(environ, start_response)
except NoBoundEndpointError:
LOGGER.debug("unknown side: %s" % path)
resp = NotFound("Couldn't find the side you asked for!")
return resp(environ, start_response)
except Exception as err:
if not self.debug:
print("%s" % err, file=sys.stderr)
traceback.print_exc()
LOGGER.exception("%s" % err)
resp = ServiceError("%s" % err)
return resp(environ, start_response)
else:
raise