diff --git a/kepi/fastcgi.py b/kepi/fastcgi.py index 9cd1313..5ffa377 100644 --- a/kepi/fastcgi.py +++ b/kepi/fastcgi.py @@ -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(): diff --git a/test/test_fastcgi.py b/test/test_fastcgi.py new file mode 100644 index 0000000..b0cca4b --- /dev/null +++ b/test/test_fastcgi.py @@ -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. +"""