kopia lustrzana https://gitlab.com/marnanel/chapeau
fastcgi goes through a despatch() handler for separation of concerns
test added; more to comemarnanel-wip
rodzic
a1c6007326
commit
2858cddec3
100
kepi/fastcgi.py
100
kepi/fastcgi.py
|
@ -55,57 +55,63 @@ def fastcgi_command(subparsers):
|
||||||
def _encode(s):
|
def _encode(s):
|
||||||
return s.replace('\n','\r\n').encode('UTF-8')
|
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):
|
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):
|
def handle(self):
|
||||||
# XXX Here we check HTTP_ACCEPT
|
result = despatch(
|
||||||
# XXX and DOCUMENT_URI and possibly QUERY_STRING
|
env = self.environ,
|
||||||
# XXX and despatch as appropriate
|
)
|
||||||
|
|
||||||
logger.debug('query: %s', self.environ)
|
self['stdout'].write(_encode(result))
|
||||||
|
|
||||||
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))
|
|
||||||
|
|
||||||
def run():
|
def run():
|
||||||
|
|
||||||
|
|
|
@ -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.
|
||||||
|
"""
|
Ładowanie…
Reference in New Issue