fixed some things. and did some UI changes
This commit is contained in:
65
webserver.py
65
webserver.py
@@ -6,6 +6,7 @@ from http.server import BaseHTTPRequestHandler, HTTPServer
|
||||
import mimetypes
|
||||
from functools import lru_cache
|
||||
from jsmin import jsmin # pip install jsmin
|
||||
import time
|
||||
|
||||
from log.Logger import *
|
||||
logger = Logger()
|
||||
@@ -13,6 +14,7 @@ import PyPost
|
||||
|
||||
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
|
||||
HTML_DIR = os.path.join(PROJECT_ROOT, "html")
|
||||
MARKDOWN_DIR = os.path.join(PROJECT_ROOT, "markdown")
|
||||
BASE_FILE = os.path.join(HTML_DIR, "base", "index.html")
|
||||
|
||||
def get_html_files(directory=HTML_DIR):
|
||||
@@ -23,6 +25,18 @@ def get_html_files(directory=HTML_DIR):
|
||||
html_files.append(entry)
|
||||
return html_files
|
||||
|
||||
def get_markdown_files():
|
||||
"""Get list of .md files from the markdown directory."""
|
||||
if not os.path.exists(MARKDOWN_DIR):
|
||||
return []
|
||||
|
||||
markdown_files = []
|
||||
for entry in os.listdir(MARKDOWN_DIR):
|
||||
full_path = os.path.join(MARKDOWN_DIR, entry)
|
||||
if os.path.isfile(full_path) and entry.endswith(".md"):
|
||||
markdown_files.append(entry)
|
||||
return markdown_files
|
||||
|
||||
def build_index_page():
|
||||
with open(BASE_FILE, "r", encoding="utf-8") as f:
|
||||
base_html = f.read()
|
||||
@@ -63,6 +77,8 @@ def index_footer():
|
||||
class MyHandler(BaseHTTPRequestHandler):
|
||||
def do_GET(self):
|
||||
req_path = self.path.lstrip("/")
|
||||
|
||||
# Handle root/index
|
||||
if req_path == "" or req_path == "index.html":
|
||||
content = build_index_page()
|
||||
self.send_response(200)
|
||||
@@ -71,6 +87,55 @@ class MyHandler(BaseHTTPRequestHandler):
|
||||
self.wfile.write(content.encode("utf-8"))
|
||||
return
|
||||
|
||||
# Handle markdown file downloads
|
||||
if req_path.startswith("markdown/"):
|
||||
markdown_filename = req_path[9:] # Remove "markdown/" prefix
|
||||
|
||||
# Security check: only allow .md files and prevent directory traversal
|
||||
if not markdown_filename.endswith(".md") or ".." in markdown_filename or "/" in markdown_filename:
|
||||
self.send_response(403)
|
||||
self.end_headers()
|
||||
self.wfile.write(b"403 - Forbidden: Only .md files allowed")
|
||||
return
|
||||
|
||||
markdown_file_path = os.path.join(MARKDOWN_DIR, markdown_filename)
|
||||
|
||||
# Check if file exists and is within markdown directory
|
||||
if not os.path.exists(markdown_file_path) or not os.path.isfile(markdown_file_path):
|
||||
self.send_response(404)
|
||||
self.end_headers()
|
||||
self.wfile.write(b"404 - Markdown file not found")
|
||||
return
|
||||
|
||||
# Verify the resolved path is still within the markdown directory (extra security)
|
||||
resolved_path = os.path.realpath(markdown_file_path)
|
||||
resolved_markdown_dir = os.path.realpath(MARKDOWN_DIR)
|
||||
if not resolved_path.startswith(resolved_markdown_dir):
|
||||
self.send_response(403)
|
||||
self.end_headers()
|
||||
self.wfile.write(b"403 - Forbidden")
|
||||
return
|
||||
|
||||
try:
|
||||
with open(markdown_file_path, "rb") as f:
|
||||
content = f.read()
|
||||
|
||||
self.send_response(200)
|
||||
self.send_header("Content-type", "text/markdown")
|
||||
self.send_header("Content-Disposition", f'attachment; filename="{markdown_filename}"')
|
||||
self.end_headers()
|
||||
self.wfile.write(content)
|
||||
logger.log_info(f"Served markdown file: {markdown_filename}")
|
||||
return
|
||||
|
||||
except Exception as e:
|
||||
logger.log_error(f"Error serving markdown file {markdown_filename}: {e}")
|
||||
self.send_response(500)
|
||||
self.end_headers()
|
||||
self.wfile.write(b"500 - Internal Server Error")
|
||||
return
|
||||
|
||||
# Handle other files (existing functionality)
|
||||
file_path = os.path.normpath(os.path.join(PROJECT_ROOT, req_path))
|
||||
if not file_path.startswith(PROJECT_ROOT):
|
||||
self.send_response(403)
|
||||
|
||||
Reference in New Issue
Block a user