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,57 +55,63 @@ def fastcgi_command(subparsers):
def _encode(s):
return s.replace('\n','\r\n').encode('UTF-8')
def despatch_user_page(self, match):
username = match.groups(1)
result = f"""Content-Type: {CONTENTTYPE_ACTIVITY}
Status: 200
Hello world.
"""
def despatch(env):
# XXX Here we check HTTP_ACCEPT
# XXX and DOCUMENT_URI and possibly QUERY_STRING
# XXX and despatch as appropriate
"""
response_headers = ''
response_headers += f'Content-Type: {CONTENTTYPE_ACTIVITY}\r\n'
response_headers += '\r\n'
response_body = response_body.encode('UTF-8')
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, despatch_user_page),
]:
match = re.match(regex, uri)
print(regex, uri, match)
if match is None:
continue
result = handler(
match = match,
)
if result is not None:
return result
return ERROR_404
class KepiHandler(FcgiHandler):
def _handle_user_page(self, match):
username = match.groups(1)
result = f"""Content-Type: {CONTENTTYPE_ACTIVITY}
Status: 200
Hello world.
"""
def handle(self):
# XXX Here we check HTTP_ACCEPT
# XXX and DOCUMENT_URI and possibly QUERY_STRING
# XXX and despatch as appropriate
result = despatch(
env = self.environ,
)
logger.debug('query: %s', self.environ)
uri = self.environ['DOCUMENT_URI']
print(self.__dict__)
"""
response_headers = ''
response_headers += f'Content-Type: {CONTENTTYPE_ACTIVITY}\r\n'
response_headers += '\r\n'
response_body = response_body.encode('UTF-8')
response_headers = response_headers.encode('UTF-8')
"""
for regex, handler in [
(USER_PAGE_RE, self._handle_user_page),
]:
match = re.match(regex, uri)
print(regex, uri, match)
if match is None:
continue
result = handler(
match = match,
)
if result is not None:
self['stdout'].write(_encode(result))
return
self['stdout'].write(_encode(ERROR_404))
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.
"""