clean code, update request.headers to utf-8

Commit 1969e61 · patx · 2025-02-21T15:29:41-05:00

Changeset
1969e610ef65bedec070ad310753c5a64875f5e4
Parents
b76c49b9c86a0f52dd4edc5ef91baba6978a9887

View source at this commit

Comments

No comments yet.

Log in to comment

Diff

diff --git a/MicroPie.py b/MicroPie.py
index c8b2547..9460211 100644
--- a/MicroPie.py
+++ b/MicroPie.py
@@ -102,6 +102,34 @@ class InMemorySessionBackend(SessionBackend):
         self.last_access[session_id] = time.time()
 
 
+# -----------------------------
+# Request Object
+# -----------------------------
+current_request: contextvars.ContextVar[Any] = contextvars.ContextVar("current_request")
+
+class Request:
+    """Represents an HTTP request in the MicroPie framework."""
+    def __init__(self, scope: Dict[str, Any]) -> None:
+        """
+        Initialize a new Request instance.
+
+        Args:
+            scope: The ASGI scope dictionary for the request.
+        """
+        self.scope: Dict[str, Any] = scope
+        self.method: str = scope["method"]
+        self.path_params: List[str] = []
+        self.query_params: Dict[str, List[str]] = {}
+        self.body_params: Dict[str, List[str]] = {}
+        self.get_json: Any = {}
+        self.session: Dict[str, Any] = {}
+        self.files: Dict[str, Any] = {}
+        self.headers: Dict[str, str] = {
+            k.decode("utf-8", errors="replace").lower(): v.decode("utf-8", errors="replace")
+            for k, v in scope.get("headers", [])
+        }
+
+
 # -----------------------------
 # Middleware Abstraction
 # -----------------------------
@@ -111,7 +139,7 @@ class HttpMiddleware(ABC):
     """
 
     @abstractmethod
-    async def before_request(self, request: "Request") -> None:
+    async def before_request(self, request: Request) -> None:
         """
         Called before the request is processed.
         """
@@ -120,7 +148,7 @@ class HttpMiddleware(ABC):
     @abstractmethod
     async def after_request(
         self,
-        request: "Request",
+        request: Request,
         status_code: int,
         response_body: Any,
         extra_headers: List[Tuple[str, str]]
@@ -133,34 +161,6 @@ class HttpMiddleware(ABC):
         pass
 
 
-# -----------------------------
-# Request Object
-# -----------------------------
-current_request: contextvars.ContextVar[Any] = contextvars.ContextVar("current_request")
-
-class Request:
-    """Represents an HTTP request in the MicroPie framework."""
-    def __init__(self, scope: Dict[str, Any]) -> None:
-        """
-        Initialize a new Request instance.
-
-        Args:
-            scope: The ASGI scope dictionary for the request.
-        """
-        self.scope: Dict[str, Any] = scope
-        self.method: str = scope["method"]
-        self.path_params: List[str] = []
-        self.query_params: Dict[str, List[str]] = {}
-        self.body_params: Dict[str, List[str]] = {}
-        self.get_json: Any = {}
-        self.session: Dict[str, Any] = {}
-        self.files: Dict[str, Any] = {}
-        self.headers: Dict[str, str] = {
-            k.decode("latin-1").lower(): v.decode("latin-1")
-            for k, v in scope.get("headers", [])
-        }
-
-
 # -----------------------------
 # Application Base
 # -----------------------------
diff --git a/README.md b/README.md
index b57e2fa..9b2b1ba 100644
--- a/README.md
+++ b/README.md
@@ -285,10 +285,10 @@ MicroPie allows you to create pluggable middleware to hook into the request life
 
 #### Methods
 
-- `before_request(request: "Request") -> None`
+- `before_request(request: Request) -> None`
   - Abstract method called before the request is processed.
 
-- `after_request(request: "Request", status_code: int, response_body: Any, extra_headers: List[Tuple[str, str]]) -> None`
+- `after_request(request: Request, status_code: int, response_body: Any, extra_headers: List[Tuple[str, str]]) -> None`
   - Abstract method called after the request is processed but before the final response is sent to the client.
 
 ## Request Object