Skip to content

Commit

Permalink
Better CGI Status handling, improved cgi testing website (now it has …
Browse files Browse the repository at this point in the history
…an actual purpose, you can save and view data)
  • Loading branch information
FreddyMSchubert committed Dec 18, 2024
1 parent 8bdc27e commit 92fda29
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ __pycache__
sessions.txt
users.txt

www/cgi/data/

# Prerequisites
*.d

Expand Down
4 changes: 4 additions & 0 deletions src/Packets/CgiResponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,5 +189,9 @@ void Response::handleCgiResponse(Request &req, Config &config)
addHeader("Content-Type", "text/plain");
if (getHeader("Content-Length").empty())
addHeader("Content-Length", std::to_string(bodyPart.size()));
if (getHeader("Status").empty())
setStatus(Status::OK);
else
setStatus((Status)std::stoi(getHeader("Status")));
setBody(bodyPart);
}
60 changes: 60 additions & 0 deletions www/cgi/cgi-bin/save_data.py
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>")
67 changes: 67 additions & 0 deletions www/cgi/cgi-bin/view_data.py
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>")
15 changes: 10 additions & 5 deletions www/cgi/cgi-bin/welcome.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
if not session_id:
# No session id found
print("<html><body style='font-family:serif;'>")
print("<h1>No Session Found (っ °Д °;)っ</h1>")
print("<h1>No Session Found (っ °Д °;)っ </h1>")
print("<p><a href='../index.html'>Back Home</a></p>")
print("</body></html>")
else:
Expand All @@ -30,17 +30,22 @@
if user:
name = user[0]
print("<html><body style='font-family:serif;'>")
print(f"<h1>Greetings, {name}! 🥂</h1>")
print(f"<h1>Greetings, {name}! 🥂 </h1>")
print("<p>You are logged in. (ง'̀-'́)ง</p>")
print("<p><a href='logout.py'>Logout</a></p>")
print("<ul>")
print("<li><a href='../save_data.html'>Save Data</a></li>")
print("<li><a href='view_data.py'>View Data</a></li>")
print("<li><a href='logout.py'>Logout</a></li>")
print("</ul>")
print("<p>Why did the computer show up at work late? It had a hard drive! 💻😂</p>")
print("</body></html>")
else:
print("<html><body style='font-family:serif;'>")
print("<h1>Oops, could not find your user info! (⊙_⊙;)</h1>")
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("<h1>Invalid Session, old chap! (ಥ_ಥ) </h1>")
print("<p><a href='../index.html'>Back Home</a></p>")
print("</body></html>")
5 changes: 3 additions & 2 deletions www/cgi/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Welcome, Mr. Elsher - Choose An Action 🏰✨</title>
<title>Welcome - Choose An Action 🏰✨</title>
</head>
<body style="font-family:serif;">
<h1>Welcome to Our Splendid Service</h1>
<h1>Welcome to Our Splendid Data Storage Service</h1>
<h2>It doesn't even cost anything - that's better than google drive!</h2>
<p>Please choose your action:</p>
<ul>
<li><a href="register.html">Register</a></li>
Expand Down
17 changes: 17 additions & 0 deletions www/cgi/save_data.html
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>
15 changes: 15 additions & 0 deletions www/cgi/view_data.html
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>

0 comments on commit 92fda29

Please sign in to comment.