diff options
author | cbdev <cb@cbcdn.com> | 2021-07-15 23:41:52 +0200 |
---|---|---|
committer | cbdev <cb@cbcdn.com> | 2021-07-15 23:41:52 +0200 |
commit | e340b9fb90c361cd59997500323a3801030b0638 (patch) | |
tree | 8765501766c9caa4847b9a07e1dd556170f2536b /backend | |
parent | 590870b9ec56f22cd7d7b785b6464ff1b6c46b9d (diff) | |
download | cargohold-e340b9fb90c361cd59997500323a3801030b0638.tar.gz cargohold-e340b9fb90c361cd59997500323a3801030b0638.tar.bz2 cargohold-e340b9fb90c361cd59997500323a3801030b0638.zip |
Basic scripting
Diffstat (limited to 'backend')
-rw-r--r-- | backend/HTTP.py | 16 | ||||
-rw-r--r-- | backend/main.py | 34 |
2 files changed, 50 insertions, 0 deletions
diff --git a/backend/HTTP.py b/backend/HTTP.py new file mode 100644 index 0000000..4ba3d90 --- /dev/null +++ b/backend/HTTP.py @@ -0,0 +1,16 @@ +def cookies(data): + cookies = {} + for cookie in data.split(';'): + data = cookie.strip().split('=', 1) + if(len(data) == 2): + cookies[data[0]] = data[1] + return cookies + +def formdata(data): + fields = {} + for field in data.split('&'): + data = field.strip().split('=', 1) + if len(data) == 2: + fields[data[0]] = data[1] + # FIXME URLdecode the entries + return fields diff --git a/backend/main.py b/backend/main.py new file mode 100644 index 0000000..1678f24 --- /dev/null +++ b/backend/main.py @@ -0,0 +1,34 @@ +import HTTP +import json + +def playout(filename): + return ["", [('Content-Type','text/html'), ('X-Accel-Redirect', filename)], None] + +def route(path, env, session, post): + if path[0] == "8cabc1fce52bcb565ae203267ce7e73f69a9272e": + return playout("interface/listing.htm") + if path[0] == "test": + return playout("interface/listing.htm") + + # Default path + return ["", [("Content-type", "text/html")], "404 None such"] + +def handle_request(env, response): + path = env.get('PATH_INFO', '').lstrip('/').split('/') + post = {} + headers = [] + + # Read POST data + try: + content_length = int(env.get('CONTENT_LENGTH', '0')) + post_raw = env["wsgi.input"].read(content_length).decode('utf-8') + if env.get('CONTENT_TYPE', '') == "multipart/form-data": + post = HTTP.formdata(post_raw) + except ValueError as e: + post = None + + # Route request + data, addtl_headers, code = route(path, env, session, post) + headers.extend(addtl_headers) + response(code if code else "200 OK", headers) + return [bytes(data)] |