-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better CGI Status handling, improved cgi testing website (now it has …
…an actual purpose, you can save and view data)
- Loading branch information
1 parent
8bdc27e
commit 92fda29
Showing
8 changed files
with
178 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,8 @@ __pycache__ | |
sessions.txt | ||
users.txt | ||
|
||
www/cgi/data/ | ||
|
||
# Prerequisites | ||
*.d | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#!/usr/bin/env python3 | ||
import os | ||
import cgi | ||
import cgitb | ||
from http import cookies | ||
from common import load_sessions, find_user | ||
|
||
cgitb.enable() | ||
|
||
form = cgi.FieldStorage() | ||
data = form.getvalue("data", "").strip() | ||
|
||
print("Content-Type: text/html; charset=utf-8") | ||
|
||
# Parse cookies | ||
cookie = cookies.SimpleCookie(os.environ.get("HTTP_COOKIE", "")) | ||
session_id = cookie.get("session_id") | ||
|
||
print("\n") # End headers | ||
|
||
if not session_id: | ||
# No session id found | ||
print("<html><body style='font-family:serif;'>") | ||
print("<h1>No Session Found! (っ °Д °;)っ </h1>") | ||
print("<p><a href='../index.html'>Back Home</a></p>") | ||
print("</body></html>") | ||
else: | ||
session_id = session_id.value | ||
sessions = load_sessions() | ||
if session_id in sessions: | ||
email = sessions[session_id] | ||
user = find_user(email) | ||
if user: | ||
name = user[0] | ||
# Save data to a file named after the user's email | ||
data_dir = os.path.join(os.path.dirname(__file__), "..", "data") | ||
os.makedirs(data_dir, exist_ok=True) | ||
user_data_file = os.path.join(data_dir, f"{email}.txt") | ||
try: | ||
with open(user_data_file, "a") as f: | ||
f.write(f"{data}\n") | ||
print("<html><body style='font-family:serif;'>") | ||
print(f"<h1>Data Saved Successfully, {name}! 🎉 </h1>") | ||
print("<p><a href='welcome.py'>Back to Welcome Page</a></p>") | ||
print("</body></html>") | ||
except Exception as e: | ||
print("<html><body style='font-family:serif;'>") | ||
print(f"<h1>Error Saving Data: {e} 🛑 </h1>") | ||
print("<p><a href='save_data.html'>Try Again</a></p>") | ||
print("</body></html>") | ||
else: | ||
print("<html><body style='font-family:serif;'>") | ||
print("<h1>Oops, could not find your user info! (⊙_⊙;) </h1>") | ||
print("<p><a href='../index.html'>Back Home</a></p>") | ||
print("</body></html>") | ||
else: | ||
print("<html><body style='font-family:serif;'>") | ||
print("<h1>Invalid Session, old chap! (ಥ_ಥ) </h1>") | ||
print("<p><a href='../index.html'>Back Home</a></p>") | ||
print("</body></html>") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#!/usr/bin/env python3 | ||
import os | ||
import cgi | ||
import cgitb | ||
import html # Import the html module for escaping | ||
from http import cookies | ||
from common import load_sessions, find_user | ||
|
||
cgitb.enable() | ||
|
||
print("Content-Type: text/html; charset=utf-8") | ||
|
||
# Parse cookies | ||
cookie = cookies.SimpleCookie(os.environ.get("HTTP_COOKIE", "")) | ||
session_id = cookie.get("session_id") | ||
|
||
print("\n") # End headers | ||
|
||
if not session_id: | ||
# No session id found | ||
print("<html><body style='font-family:serif;'>") | ||
print("<h1>No Session Found! (っ °Д °;)っ </h1>") | ||
print("<p><a href='../index.html'>Back Home</a></p>") | ||
print("</body></html>") | ||
else: | ||
session_id = session_id.value | ||
sessions = load_sessions() | ||
if session_id in sessions: | ||
email = sessions[session_id] | ||
user = find_user(email) | ||
if user: | ||
name = user[0] | ||
# Read data from user's file | ||
data_dir = os.path.join(os.path.dirname(__file__), "..", "data") | ||
user_data_file = os.path.join(data_dir, f"{email}.txt") | ||
print("<html><body style='font-family:serif;'>") | ||
print(f"<h1>Your Saved Data, {html.escape(name)}! 📂 </h1>") | ||
if os.path.exists(user_data_file): | ||
try: | ||
with open(user_data_file, "r") as f: | ||
data_lines = f.readlines() | ||
if data_lines: | ||
print("<ul>") | ||
for line in data_lines: | ||
safe_line = html.escape(line.strip()) | ||
print(f"<li>{safe_line}</li>") | ||
print("</ul>") | ||
else: | ||
print("<p>You haven’t saved any data yet! Start now! 🚀</p>") | ||
except Exception as e: | ||
# Escape the error message to prevent potential XSS | ||
safe_error = html.escape(str(e)) | ||
print(f"<p>Error reading data: {safe_error} 🛑 </p>") | ||
else: | ||
print("<p>No data found. Maybe it’s hiding? 🕵️♂️🔍</p>") | ||
print("<p><a href='welcome.py'>Back to Welcome Page</a></p>") | ||
print("</body></html>") | ||
else: | ||
print("<html><body style='font-family:serif;'>") | ||
print("<h1>Oops, could not find your user info! (⊙_⊙;) </h1>") | ||
print("<p><a href='../index.html'>Back Home</a></p>") | ||
print("</body></html>") | ||
else: | ||
print("<html><body style='font-family:serif;'>") | ||
print("<h1>Invalid Session, old chap! (ಥ_ಥ) </h1>") | ||
print("<p><a href='../index.html'>Back Home</a></p>") | ||
print("</body></html>") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Save Your Data 📝</title> | ||
</head> | ||
<body style="font-family:serif;"> | ||
<h1>Save Your Data 🛡️✨</h1> | ||
<p>Got something to save? Do it here! (•̀ᴗ•́)و ̑̑</p> | ||
<form action="/cgi-bin/save_data.py" method="post"> | ||
<label for="data">Your Data:</label><br> | ||
<textarea id="data" name="data" rows="4" cols="50" required></textarea><br><br> | ||
<input type="submit" value="Save Data"> | ||
</form> | ||
<p><a href="welcome.py">Back to Welcome Page</a></p> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>View Your Data 👀</title> | ||
</head> | ||
<body style="font-family:serif;"> | ||
<h1>Your Saved Data 📂</h1> | ||
<p>Here’s what you’ve saved! Isn’t technology marvelous? 🤖✨</p> | ||
<div id="data"> | ||
<!-- Data will be populated by view_data.py --> | ||
</div> | ||
<p><a href="welcome.py">Back to Welcome Page</a></p> | ||
</body> | ||
</html> |