================================================================================
  Question 28: CGI Login Form and Response Processing
================================================================================

FULL QUESTION:
Task:
1. Create a Login form using CGI and display the submitted input on a different page.
2. Provide the HTML form interface.
3. Provide the Python CGI script to process form submissions:
   - Output mandatory CGI Content-Type header.
   - Read and parse raw POST data.
   - Sanitize user inputs using html.escape.
   - Render a secure HTML confirmation page displaying the username and password.
4. Provide standalone testing capability for terminal execution.

SHORT QUESTION:
CGI web application processing login form submissions via HTTP POST and rendering sanitized responses.

================================================================================
README & SETUP INSTRUCTIONS:
================================================================================
# Question 28: CGI Login Form and Response Processing

- Prerequisites: Python 3.x (Standard library urllib.parse, html).
- Running in CGI Environment: Place under /cgi-bin/ on your Apache/WAMP server.
- Running Standalone: Run `python question_28.py` in your terminal to see simulated CGI response.
- How to Run: python question_28.py
- Expected Behavior: Processes username and password and renders HTML response.

================================================================================
SOURCE CODE FILES:
================================================================================

1. question_28.py
--------------------------------------------------------------------------------
"""
HTML Form (Cell 95):
<!DOCTYPE html>
<html>
<head>
    <title>CGI Login Form</title>
</head>
<body>
    <h2>Login Form</h2>
    <form action="/cgi-bin/login.py" method="post">
        <label for="username">Username:</label><br>
        <input type="text" id="username" name="username" required><br><br>

        <label for="password">Password:</label><br>
        <input type="password" id="password" name="password" required><br><br>

        <input type="submit" value="Login">
    </form>
</body>
</html>
"""

import sys
import os
import urllib.parse
import html

print("Content-Type: text/html\n\n")

content_length = int(os.environ.get('CONTENT_LENGTH', 0))
post_data = sys.stdin.read(content_length)

parsed_data = urllib.parse.parse_qs(post_data)

username = parsed_data.get("username", ["Not Provided"])[0]
password = parsed_data.get("password", ["Not Provided"])[0]

safe_username = html.escape(username)
safe_password = html.escape(password)

print(f"""<!DOCTYPE html>
<html>
<head>
    <title>Login Result</title>
</head>
<body>
    <h2>Login Submission Received</h2>
    <p><b>Username:</b> {safe_username}</p>
    <p><b>Password:</b> {safe_password}</p>
    <br>
    <a href="/index.html">Back to Login</a>
</body>
</html>
""")
--------------------------------------------------------------------------------
