Newer
Older
python_redirect / cgi_redirect.py
@Jason Poage Jason Poage on 24 Mar 944 bytes First commit
"""
CGI Implementation for proxy redirection.
Executes the redirect logic and outputs standard CGI headers and body.
"""

import os

from proxy_redirect import ProxyRedirectBase


class CGIProxyRedirect(ProxyRedirectBase):
    """
    Subclass of ProxyRedirectBase that handles CGI-specific I/O.
    """

    def __init__(self):
        """
        Initializes the CGI proxy using the system environment variables.
        """
        super().__init__(dict(os.environ))

    def emit(self):
        """Executes the logic and prints to STDOUT in CGI format."""
        response = self.run()

        # Emit headers
        print(f"Status: {response['status']}")
        for key, value in response.get("headers", {}).items():
            print(f"{key}: {value}")

        # Header/Body separator
        print()

        # Emit body
        print(response.get("body", ""))


if __name__ == "__main__":
    app = CGIProxyRedirect()
    app.emit()