Commit 87ebda84 authored by Vincent Pelletier's avatar Vincent Pelletier

wsgi: Rework request routing.

Prepares for CORS headers auto-generation.
parent b95aa3ea
...@@ -98,6 +98,24 @@ class Application(object): ...@@ -98,6 +98,24 @@ class Application(object):
""" """
self._cau = cau self._cau = cau
self._cas = cas self._cas = cas
self._context_dict = {
'cau': cau,
'cas': cas,
}
self._routing_dict = {
'crl': {
'GET': self.getCRL,
},
'csr': {
'GET': self.getCSR,
'PUT': self.putCSR,
'DELETE': self.deleteCSR,
},
'crt': {
'GET': self.getCRT,
'PUT': self.putCRT,
},
}
def __call__(self, environ, start_response): def __call__(self, environ, start_response):
""" """
...@@ -107,21 +125,25 @@ class Application(object): ...@@ -107,21 +125,25 @@ class Application(object):
try: # Convert non-wsgi exceptions into WSGI exceptions try: # Convert non-wsgi exceptions into WSGI exceptions
path_item_list = [x for x in environ['PATH_INFO'].split('/') if x] path_item_list = [x for x in environ['PATH_INFO'].split('/') if x]
try: try:
context_id, method_id = path_item_list[:2] context_id, base_path = path_item_list[:2]
except ValueError: except ValueError:
raise NotFound raise NotFound
if context_id == 'cau':
context = self._cau
elif context_id == 'cas':
context = self._cas
else:
raise NotFound
if method_id.startswith('_'):
raise NotFound
try: try:
method = getattr(self, method_id) context = self._context_dict[context_id]
except AttributeError: method_dict = self._routing_dict[base_path]
except KeyError:
raise NotFound raise NotFound
request_method = environ['REQUEST_METHOD']
if request_method == 'OPTIONS':
status = STATUS_NO_CONTENT
header_list = [
]
result = []
else:
try:
method = method_dict[request_method]
except KeyError:
raise BadMethod
status, header_list, result = method(context, environ, path_item_list[2:]) status, header_list, result = method(context, environ, path_item_list[2:])
except ApplicationError: except ApplicationError:
raise raise
...@@ -208,14 +230,12 @@ class Application(object): ...@@ -208,14 +230,12 @@ class Application(object):
raise BadRequest('Invalid json') raise BadRequest('Invalid json')
@staticmethod @staticmethod
def crl(context, environ, subpath): def getCRL(context, environ, subpath):
""" """
Handle /{context}/crl urls. Handle GET /{context}/crl .
""" """
if subpath: if subpath:
raise NotFound raise NotFound
if environ['REQUEST_METHOD'] != 'GET':
raise BadMethod
data = context.getCertificateRevocationList() data = context.getCertificateRevocationList()
return ( return (
STATUS_OK, STATUS_OK,
...@@ -226,12 +246,10 @@ class Application(object): ...@@ -226,12 +246,10 @@ class Application(object):
[data], [data],
) )
def csr(self, context, environ, subpath): def getCSR(self, context, environ, subpath):
""" """
Handle /{context}/csr urls. Handle GET /{context}/csr/{csr_id} and GET /{context}/csr.
""" """
http_method = environ['REQUEST_METHOD']
if http_method == 'GET':
if subpath: if subpath:
try: try:
csr_id, = subpath csr_id, = subpath
...@@ -255,7 +273,11 @@ class Application(object): ...@@ -255,7 +273,11 @@ class Application(object):
], ],
[data], [data],
) )
elif http_method == 'PUT':
def putCSR(self, context, environ, subpath):
"""
Handle PUT /{context}/csr .
"""
if subpath: if subpath:
raise NotFound raise NotFound
csr_id = context.appendCertificateSigningRequest(self._read(environ)) csr_id = context.appendCertificateSigningRequest(self._read(environ))
...@@ -266,7 +288,11 @@ class Application(object): ...@@ -266,7 +288,11 @@ class Application(object):
], ],
[], [],
) )
elif http_method == 'DELETE':
def deleteCSR(self, context, environ, subpath):
"""
Handle DELETE /{context}/csr/{csr_id} .
"""
try: try:
csr_id, = subpath csr_id, = subpath
except ValueError: except ValueError:
...@@ -277,19 +303,15 @@ class Application(object): ...@@ -277,19 +303,15 @@ class Application(object):
except exceptions.NotFound: except exceptions.NotFound:
raise NotFound raise NotFound
return (STATUS_NO_CONTENT, [], []) return (STATUS_NO_CONTENT, [], [])
else:
raise BadMethod
def crt(self, context, environ, subpath): def getCRT(self, context, environ, subpath):
""" """
Handle /{context}/crt urls. Handle GET /{context}/crt/{crt_id} urls.
""" """
http_method = environ['REQUEST_METHOD']
try: try:
crt_id, = subpath crt_id, = subpath
except ValueError: except ValueError:
raise NotFound raise NotFound
if http_method == 'GET':
if crt_id == 'ca.crt.pem': if crt_id == 'ca.crt.pem':
data = context.getCACertificate() data = context.getCACertificate()
content_type = 'application/pkix-cert' content_type = 'application/pkix-cert'
...@@ -311,7 +333,15 @@ class Application(object): ...@@ -311,7 +333,15 @@ class Application(object):
], ],
[data], [data],
) )
elif http_method == 'PUT':
def putCRT(self, context, environ, subpath):
"""
Handle PUT /{context}/crt/{crt_id} urls.
"""
try:
crt_id, = subpath
except ValueError:
raise NotFound
if crt_id == 'renew': if crt_id == 'renew':
payload = utils.unwrap( payload = utils.unwrap(
self._readJSON(environ), self._readJSON(environ),
...@@ -366,5 +396,3 @@ class Application(object): ...@@ -366,5 +396,3 @@ class Application(object):
template_csr=template_csr, template_csr=template_csr,
) )
return (STATUS_NO_CONTENT, [], []) return (STATUS_NO_CONTENT, [], [])
else:
raise BadMethod
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment