fastcgi goes through a despatch() handler for separation of concerns

test added; more to come
marnanel-wip
Marnanel Thurman 2023-10-17 13:11:02 +01:00
rodzic a1c6007326
commit 2858cddec3
2 zmienionych plików z 67 dodań i 47 usunięć

Wyświetl plik

@ -55,9 +55,7 @@ def fastcgi_command(subparsers):
def _encode(s):
return s.replace('\n','\r\n').encode('UTF-8')
class KepiHandler(FcgiHandler):
def _handle_user_page(self, match):
def despatch_user_page(self, match):
username = match.groups(1)
@ -68,16 +66,11 @@ class KepiHandler(FcgiHandler):
Hello world.
"""
def handle(self):
def despatch(env):
# XXX Here we check HTTP_ACCEPT
# XXX and DOCUMENT_URI and possibly QUERY_STRING
# XXX and despatch as appropriate
logger.debug('query: %s', self.environ)
uri = self.environ['DOCUMENT_URI']
print(self.__dict__)
"""
response_headers = ''
@ -88,8 +81,13 @@ class KepiHandler(FcgiHandler):
response_headers = response_headers.encode('UTF-8')
"""
logger.debug('query: %s', env)
uri = env['DOCUMENT_URI']
print(env)
for regex, handler in [
(USER_PAGE_RE, self._handle_user_page),
(USER_PAGE_RE, despatch_user_page),
]:
match = re.match(regex, uri)
@ -102,10 +100,18 @@ class KepiHandler(FcgiHandler):
)
if result is not None:
self['stdout'].write(_encode(result))
return
return result
self['stdout'].write(_encode(ERROR_404))
return ERROR_404
class KepiHandler(FcgiHandler):
def handle(self):
result = despatch(
env = self.environ,
)
self['stdout'].write(_encode(result))
def run():

Wyświetl plik

@ -0,0 +1,14 @@
from kepi.fastcgi import despatch
from test import *
def test_fastcgi_simple():
found = despatch(
env = {
'DOCUMENT_URI': '',
},
)
assert found=="""Content-Type: text/html
Status: 404 Not found
That resource does not exist here.
"""