From 3096c1435a817163bdb196b0169b19453d4548ae Mon Sep 17 00:00:00 2001 From: dece Date: Tue, 31 Aug 2021 17:52:02 +0200 Subject: [PATCH] init with smolcgi and demo script --- helloworld | 4 ++++ smolcgi.py | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100755 helloworld create mode 100644 smolcgi.py diff --git a/helloworld b/helloworld new file mode 100755 index 0000000..ed732d8 --- /dev/null +++ b/helloworld @@ -0,0 +1,4 @@ +#!/usr/bin/env python3 +import smolcgi +smolcgi.header(20, "text/plain") +print("Hello world!") diff --git a/smolcgi.py b/smolcgi.py new file mode 100644 index 0000000..a9afe53 --- /dev/null +++ b/smolcgi.py @@ -0,0 +1,60 @@ +#!/bin/false +from os import environ + +def getenv(name): + return environ.get(name, "") + +version = getenv("GATEWAY_INTERFACE") +protocol = getenv("SERVER_PROTOCOL") +software = getenv("SERVER_SOFTWARE") +url = getenv("GEMINI_URL") +script = getenv("SCRIPT_NAME") +path = getenv("PATH_INFO") +query = getenv("QUERY_STRING") +host = getenv("SERVER_NAME") +port = getenv("SERVER_PORT") +remote = getenv("REMOTE_HOST") +tls_cipher = getenv("TLS_CIPHER") +tls_version = getenv("TLS_VERSION") +auth_type = getenv("AUTH_TYPE") +cert_hash = getenv("TLS_CLIENT_HASH") +cert_sn = getenv("TLS_CLIENT_SERIAL_NUMBER") +cert_name = getenv("REMOTE_USER") + +cgi_vars = [ + "version", "protocol", "software", "url", "script", "path", "query", "host", + "port", "remote", "tls_cipher", "tls_version", "auth_type", "cert_hash", + "cert_sn", "cert_name" +] + +def header(code, meta): + print(f"{code} {meta}", end="\r\n") + +def exit_with_header(code, meta): + header(code, meta) + exit() + +def require_input(meta): + exit_with_header(10, meta) + +def redirect_temp(url): + exit_with_header(30, url) + +def redirect_perm(url): + exit_with_header(31, url) + +def temp_error(meta): + exit_with_header(42, meta) + +def not_found(): + exit_with_header(51, "File not found.") + +def debug(): + header(20, "text/plain") + print_env() + exit() + +def print_env(): + globz = globals() + for key in cgi_vars: + print(f"{key} = {repr(globz[key])}")