GmCapsule [gsorg-style]

Fixed file path processing issues

=> 0d96ccf7639343b579d17ae597114c3a61cadac4

diff --git a/gmcapsule/__init__.py b/gmcapsule/__init__.py
index 81a2452..e3e3763 100644
--- a/gmcapsule/__init__.py
+++ b/gmcapsule/__init__.py
@@ -36,7 +36,7 @@ class Config:
         return Path(self.ini.get('server', 'certs', fallback='.certs'))
 
     def root_dir(self):
-        return Path(self.ini.get('server', 'root', fallback='.'))
+        return Path(self.ini.get('server', 'root', fallback='.')).resolve()
 
     def mod_dirs(self):
         return [Path(p).resolve() for p in shlex.split(self.ini.get('server', 'modules', fallback='.'))]
@@ -124,10 +124,10 @@ def get_mime_type(path):
     if lp.endswith('.md') or lp.endswith('.markdown') or lp.endswith('.mdown'):
         return 'text/markdown'
 
-    if not path.exists():
+    if not Path(p).exists():
         return None
 
-    mt = mimetypes.guess_type(path)[0]
+    mt = mimetypes.guess_type(p)[0]
     if mt is not None:
        return mt
 
diff --git a/gmcapsule/gemini.py b/gmcapsule/gemini.py
index 1a48fb4..697a81e 100644
--- a/gmcapsule/gemini.py
+++ b/gmcapsule/gemini.py
@@ -81,7 +81,6 @@ class Worker(threading.Thread):
         self.jobs = server.work_queue
 
     def run(self):
-        #print(f'Worker {self.id} started')
         while True:
             job = self.jobs.get()
             if job is None:
@@ -90,10 +89,17 @@ class Worker(threading.Thread):
             try:
                 self.process_request(stream, from_addr)
             except Exception as error:
-                report_error(stream, 42, str(error))
+                import traceback
+                traceback.print_exc()
+                try:
+                    report_error(stream, 42, str(error))
+                except:
+                    pass
             finally:
-                stream.shutdown()
-        #print(f'Worker {self.id} stopped')
+                try:
+                    stream.shutdown()
+                except:
+                    pass
 
     def log(self, *args):
         print(time.strftime('%Y-%m-%d %H:%M:%S'), f'[{self.id}]', '--', *args)
diff --git a/gmcapsule/modules/99_static.py b/gmcapsule/modules/99_static.py
index d0d4827..5fbd9b4 100644
--- a/gmcapsule/modules/99_static.py
+++ b/gmcapsule/modules/99_static.py
@@ -12,10 +12,10 @@ META = '.meta'
 
 def check_meta_rules(path, hostname):
     cfg = Capsule.config()
-    root = (cfg.root_dir() / hostname).resolve()
-    dir = path.parent
+    root = cfg.root_dir() / hostname
+    dir = Path(path).parent
     while True:
-        if not str(dir.resolve()).startswith(str(root)):
+        if not str(dir).startswith(str(root)):
             break
         if (dir / META).exists():
             for rule in open(dir / META, 'rt').readlines():
@@ -46,21 +46,21 @@ def serve_file(req):
         if seg != '.' and seg != '..' and seg.startswith('.'):
             return 51, "Not found"
 
-    host_root = (cfg.root_dir() / req.hostname).resolve()
-    path = (host_root / req.path[1:]).resolve()
-    if not str(path).startswith(str(host_root)):
+    host_root = cfg.root_dir() / req.hostname
+    path = os.path.normpath(host_root / req.path[1:])
+    if not path.startswith(str(host_root)):
         return 51, "Not found"
 
-    if os.path.isdir(str(path)):
+    if os.path.isdir(path):
         if not req.path.endswith('/'):
             return 31, req.path + '/'
-        path = path / 'index.gmi'
+        path = str(Path(path) / 'index.gmi')
 
     status, meta = check_meta_rules(path, req.hostname)
     if status and status != 20:
         return status, meta
 
-    if not os.path.exists(str(path)):
+    if not os.path.exists(path):
         return 51, "Not found"
 
     return status, meta, (open(path, 'rb').read() if status == 20 else None)
Proxy Information
Original URL
gemini://git.skyjake.fi/gmcapsule/gsorg-style/cdiff/0d96ccf7639343b579d17ae597114c3a61cadac4
Status Code
Success (20)
Meta
text/gemini; charset=utf-8
Capsule Response Time
30.40858 milliseconds
Gemini-to-HTML Time
0.417296 milliseconds

This content has been proxied by September (ba2dc).