From 03708faed682c4c116cb750476a383b3ca144aa2 Mon Sep 17 00:00:00 2001 From: Marnanel Thurman Date: Sun, 8 Oct 2023 23:26:34 +0100 Subject: [PATCH] Experiment for the fastcgi handler. --- docs/nginx-config-fragment | 16 ++++++++++++++ fastcgi-test.py | 44 ++++++++++++++++++++++++++++++++++++++ requirements.txt | 2 ++ 3 files changed, 62 insertions(+) create mode 100644 docs/nginx-config-fragment create mode 100644 fastcgi-test.py diff --git a/docs/nginx-config-fragment b/docs/nginx-config-fragment new file mode 100644 index 0000000..1a5af40 --- /dev/null +++ b/docs/nginx-config-fragment @@ -0,0 +1,16 @@ +server { + + root /var/www/html; + + server_name local.example.org; + + location / { + include /etc/nginx/fastcgi_params; + + if ($content_type = "application/activity+json") { + fastcgi_pass 127.0.0.1:17177; + } + + try_files $uri $uri/ =404; + } +} diff --git a/fastcgi-test.py b/fastcgi-test.py new file mode 100644 index 0000000..81a6cba --- /dev/null +++ b/fastcgi-test.py @@ -0,0 +1,44 @@ +from socketserver import TCPServer +from fastcgi.core import FcgiHandler +import accept + +# environ: +# query {'QUERY_STRING': '', 'REQUEST_METHOD': 'GET', 'CONTENT_TYPE': '', 'CONTENT_LENGTH': '', 'SCRIPT_NAME': '/', 'REQUEST_URI': '/', 'DOCUMENT_URI': '/', 'DOCUMENT_ROOT': '/var/www/html', 'SERVER_PROTOCOL': 'HTTP/1.1', 'REQUEST_SCHEME': 'https', 'HTTPS': 'on', 'GATEWAY_INTERFACE': 'CGI/1.1', 'SERVER_SOFTWARE': 'nginx/1.22.1', 'REMOTE_ADDR': '217.155.192.32', 'REMOTE_PORT': '65482', 'REMOTE_USER': '', 'SERVER_ADDR': '192.168.1.83', 'SERVER_PORT': '443', 'SERVER_NAME': 'sandy-heath.thurman.org.uk', 'REDIRECT_STATUS': '200', 'HTTP_HOST': 'sandy-heath.thurman.org.uk', 'HTTP_USER_AGENT': 'Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0', 'HTTP_ACCEPT': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8', 'HTTP_ACCEPT_LANGUAGE': 'en-GB,en;q=0.5', 'HTTP_ACCEPT_ENCODING': 'gzip, deflate, br', 'HTTP_CONNECTION': 'keep-alive', 'HTTP_COOKIE': 'csrftoken=Ga1NZdJTo0F5zaSXCmmYq69xSePMfzIk', 'HTTP_UPGRADE_INSECURE_REQUESTS': '1', 'HTTP_SEC_FETCH_DEST': 'document', 'HTTP_SEC_FETCH_MODE': 'navigate', 'HTTP_SEC_FETCH_SITE': 'cross-site', 'HTTP_SEC_GPC': '1'} + +CONTENTTYPE_ACTIVITY = 'application/activity+json' +CONTENTTYPE_HTML = 'text/html' + +class TestHandler(FcgiHandler): + def handle(self): + # XXX Here we check HTTP_ACCEPT + # XXX and DOCUMENT_URI and possibly QUERY_STRING + # XXX and despatch as appropriate + + print('query', self.environ) + + mime = accept.parse(self.environ['HTTP_ACCEPT']) + print(mime) + print(self.environ['DOCUMENT_URI']) + + response_headers = '' + + response_headers += f'Content-Type: {CONTENTTYPE_ACTIVITY}\r\n' + response_headers += '\r\n' + + response_body = 'foobar' + + response_body = response_body.encode('UTF-8') + response_headers = response_headers.encode('UTF-8') + + self['stdout'].write( + response_headers + + b'\r\n\r\n' + + response_body + ) + +def run(): + with TCPServer(('localhost',17177), TestHandler) as srv: + srv.handle_request() + +if __name__=='__main__': + run() diff --git a/requirements.txt b/requirements.txt index 98c82f6..e3f984e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -62,3 +62,5 @@ tzdata==2023.3 urllib3==2.0.5 vine==5.0.0 wcwidth==0.2.8 +fastcgi==0.0.3 +accept==0.1.0