GmCapsule [main]

Report Unicode decode error to client

=> bb9c5735dbb554a92f24b1fb8b941d87e8e788b5

diff --git a/README.md b/README.md
index a1af4b9..80b3f0a 100644
--- a/README.md
+++ b/README.md
@@ -48,6 +48,7 @@ The log can be viewed via journalctl (or syslog):
 
 ### v0.2.3
 * Requests exceeding 1024 bytes should result in an error code and not just be ignored.
+* Respond with an error code to malformed UTF-8 in the request.
 
 ### v0.2.2
 * Reduced required Python version to 3.6 (f-strings).
diff --git a/gmcapsule/gemini.py b/gmcapsule/gemini.py
index b086ec6..34ac7f6 100644
--- a/gmcapsule/gemini.py
+++ b/gmcapsule/gemini.py
@@ -376,22 +376,26 @@ class Worker(threading.Thread):
         req_mime = None
         incoming = safe_recv(stream, MAX_RECV)
 
-        while len(data) < MAX_RECV:
-            data += incoming
-            crlf_pos = data.find(b'\r\n')
-            if crlf_pos >= 0:
-                request = data[:crlf_pos].decode('utf-8')
-                data = data[crlf_pos + 2:]
-                break
-            elif len(data) > MAX_LEN:
-                # At this point we should have received the line terminator.
-                self.log(from_addr, 'sent a malformed request')
-                report_error(stream, 59, "Request exceeds maximum length")
-                return
-
-            incoming = safe_recv(stream, MAX_RECV - len(data))
-            if len(incoming) <= 0:
-                break
+        try:
+            while len(data) < MAX_RECV:
+                data += incoming
+                crlf_pos = data.find(b'\r\n')
+                if crlf_pos >= 0:
+                    request = data[:crlf_pos].decode('utf-8')
+                    data = data[crlf_pos + 2:]
+                    break
+                elif len(data) > MAX_LEN:
+                    # At this point we should have received the line terminator.
+                    self.log(from_addr, 'sent a malformed request')
+                    report_error(stream, 59, "Request exceeds maximum length")
+                    return
+
+                incoming = safe_recv(stream, MAX_RECV - len(data))
+                if len(incoming) <= 0:
+                    break
+        except UnicodeDecodeError:
+            report_error(stream, 59, "Request contains malformed UTF-8")
+            return
                 
         if not request or len(data) > MAX_RECV:
             report_error(stream, 59, "Invalid request")
Proxy Information
Original URL
gemini://git.skyjake.fi/gmcapsule/main/cdiff/bb9c5735dbb554a92f24b1fb8b941d87e8e788b5
Status Code
Success (20)
Meta
text/gemini; charset=utf-8
Capsule Response Time
45.655276 milliseconds
Gemini-to-HTML Time
0.448747 milliseconds

This content has been proxied by September (3851b).