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):
"""
self._cau = cau
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):
"""
......@@ -107,21 +125,25 @@ class Application(object):
try: # Convert non-wsgi exceptions into WSGI exceptions
path_item_list = [x for x in environ['PATH_INFO'].split('/') if x]
try:
context_id, method_id = path_item_list[:2]
context_id, base_path = path_item_list[:2]
except ValueError:
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:
method = getattr(self, method_id)
except AttributeError:
context = self._context_dict[context_id]
method_dict = self._routing_dict[base_path]
except KeyError:
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:])
except ApplicationError:
raise
......@@ -208,14 +230,12 @@ class Application(object):
raise BadRequest('Invalid json')
@staticmethod
def crl(context, environ, subpath):
def getCRL(context, environ, subpath):
"""
Handle /{context}/crl urls.
Handle GET /{context}/crl .
"""
if subpath:
raise NotFound
if environ['REQUEST_METHOD'] != 'GET':
raise BadMethod
data = context.getCertificateRevocationList()
return (
STATUS_OK,
......@@ -226,12 +246,10 @@ class Application(object):
[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:
try:
csr_id, = subpath
......@@ -255,7 +273,11 @@ class Application(object):
],
[data],
)
elif http_method == 'PUT':
def putCSR(self, context, environ, subpath):
"""
Handle PUT /{context}/csr .
"""
if subpath:
raise NotFound
csr_id = context.appendCertificateSigningRequest(self._read(environ))
......@@ -266,7 +288,11 @@ class Application(object):
],
[],
)
elif http_method == 'DELETE':
def deleteCSR(self, context, environ, subpath):
"""
Handle DELETE /{context}/csr/{csr_id} .
"""
try:
csr_id, = subpath
except ValueError:
......@@ -277,19 +303,15 @@ class Application(object):
except exceptions.NotFound:
raise NotFound
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:
crt_id, = subpath
except ValueError:
raise NotFound
if http_method == 'GET':
if crt_id == 'ca.crt.pem':
data = context.getCACertificate()
content_type = 'application/pkix-cert'
......@@ -311,7 +333,15 @@ class Application(object):
],
[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':
payload = utils.unwrap(
self._readJSON(environ),
......@@ -366,5 +396,3 @@ class Application(object):
template_csr=template_csr,
)
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