From 375360214ce32bb545c0fd8beef676e90d36980a Mon Sep 17 00:00:00 2001

From: =?UTF-8?q?Jaakko=20Kera=CC=88nen?= jaakko.keranen@iki.fi

Date: Wed, 31 May 2023 22:19:37 +0300

Subject: [PATCH 1/1] Added "rewrite" module; updated documentation

The Server class was refactored to make it possible to call the

entry point for a given request via extension modules.


README.md | 8 ++-

gmcapsule/init.py | 87 +++++++++++++++++++++--

gmcapsule/gemini.py | 120 ++++++++++++++++++--------------

gmcapsule/modules/10_rewrite.py | 82 ++++++++++++++++++++++

4 files changed, 236 insertions(+), 61 deletions(-)

create mode 100644 gmcapsule/modules/10_rewrite.py

diff --git a/README.md b/README.md

index 823db45..9f8f074 100644

--- a/README.md

+++ b/README.md

@@ -37,7 +37,7 @@ Replace <YOUR-INSTALL-PATH> with the actual path of gmcapsuled. pip will i

Then you can do the usual:

 systemctl --user daemon-reload

 systemctl --user start gmcapsule

The log can be viewed via journalctl (or syslog):

@@ -46,6 +46,12 @@ The log can be viewed via journalctl (or syslog):

Change log

+### v0.4

+* Added built-in module "rewrite" that matches regular expressions against the request path and can rewrite the path or return a custom status for redirection, "Gone" messages, or other exceptional situations.

+* Extension module load order is determined after locating all modules from all directories. Previously, the order was local to each directory.

+* Added a new configuration section [priority] for overriding default module priorities determined from file names. This is useful for changing the priority of the built-in modules.

v0.3

diff --git a/gmcapsule/init.py b/gmcapsule/init.py

index 3afb139..f93f021 100644

--- a/gmcapsule/init.py

+++ b/gmcapsule/init.py

@@ -58,8 +58,9 @@ The GmCapsule configuration file is in `INI format

defined:

-- :ref:static — serving static files

+- :ref:static — serving static files

+- :ref:rewrite.* — URL rewriting rules

@@ -132,6 +133,50 @@ root : path [path...]

 files will be served from `/home/user/gemini/example.com/`.

+rewrite.*

+---------

+Settings for the rewrite module that checks regular expressions against

+the request path and can rewrite the path or return a custom status. You can

+use this for internal remapping of directories and files, redirections,

+"Gone" statuses, or other exceptional situations.

+Each rewriting rule is a section that begins with rewrite..

+.. code-block:: ini

+protocol : string

+host : string

+path : string

+repl : string

+status : string

cgi


@@ -440,7 +485,7 @@ from .gemini import Server, Cache

from .markdown import to_gemtext as markdown_to_gemtext

-version = '0.3.2'

+version = '0.4.0'

all = [

 'Config', 'Capsule', 'Cache',

 'get_mime_type', 'markdown_to_gemtext'

@@ -629,24 +674,40 @@ class Capsule:

     self.sv.add_cache(cache)

 def load_modules(self):

     dirs = []

     for user_dir in self.cfg.mod_dirs():

         if user_dir not in dirs:

             dirs.append(user_dir)

     dirs += [Path(__file__).parent.resolve() / 'modules']

     for mdir in dirs:

         for mod_file in sorted(os.listdir(mdir)):

             m = name_pattern.match(mod_file)

             if m:

                 path = (mdir / mod_file).resolve()

                 loader = importlib.machinery.SourceFileLoader(name, str(path))

                 spec = importlib.util.spec_from_loader(name, loader)

                 mod = importlib.util.module_from_spec(spec)

                 loader.exec_module(mod)

 def shutdown_event(self):

     """

@@ -657,6 +718,20 @@ class Capsule:

     """

     return self.sv.shutdown_event

 def run(self):

     """

     Start worker threads and begin accepting incoming connections. The

diff --git a/gmcapsule/gemini.py b/gmcapsule/gemini.py

index 05794f7..26ded98 100644

--- a/gmcapsule/gemini.py

+++ b/gmcapsule/gemini.py

@@ -16,6 +16,12 @@ import OpenSSL.crypto

from OpenSSL import SSL, crypto

+class GeminiError(Exception):

class AbortedIOError(Exception):

 def __init__(self, msg):

     Exception.__init__(self, msg)

@@ -271,6 +277,9 @@ class Request:

     self.content_mime = content_mime

     self.content = content

def verify_callback(connection, cert, err_num, err_depth, ret_code):

 #print("verify_callback:", connection, cert, ret_code)

@@ -445,72 +454,42 @@ class Worker(threading.Thread):

     url = urlparse(request)

     path = url.path

     if path == '':

         path = '/'

     hostname = url.hostname

     if not stream.get_servername():

         report_error(stream, 59, "Missing TLS server name indication")

         return

         report_error(stream, 53, "Proxy request refused")

         return

         # Determine status code, meta line, and body content.

         if type(response) == tuple:

             if len(response) == 2:

                 response = ''

             else:

         else:

             status = 20

             meta = 'text/gemini; charset=utf-8'

@@ -528,7 +507,7 @@ class Worker(threading.Thread):

         # Save to cache.

         if not from_cache and status == 20 and \

                 (type(response_data) == bytes or type(response_data) == bytearray):

                 if cache.save(hostname + path, meta, response_data):

                     break

@@ -536,8 +515,9 @@ class Worker(threading.Thread):

         if hasattr(response_data, 'close'):

             response_data.close()

class Server:

@@ -693,7 +673,39 @@ class Server:

                 handler = path_pattern(path)

                 if handler:

                     return handler

         return None

     return None

diff --git a/gmcapsule/modules/10_rewrite.py b/gmcapsule/modules/10_rewrite.py

new file mode 100644

index 0000000..584e7a9

--- /dev/null

+++ b/gmcapsule/modules/10_rewrite.py

@@ -0,0 +1,82 @@

+# Copyright (c) 2023 Jaakko Keränen jaakko.keranen@iki.fi

+# License: BSD-2-Clause

+"""Rewriter"""

+import re

+class PathRewriteHandler:

+class Responder:

+class Rewriter:

+def init(capsule):

--

2.25.1

Proxy Information
Original URL
gemini://git.skyjake.fi/gmcapsule/gsorg-style/patch/375360214ce32bb545c0fd8beef676e90d36980a.patch
Status Code
Success (20)
Meta
text/plain
Capsule Response Time
52.051379 milliseconds
Gemini-to-HTML Time
7.34623 milliseconds

This content has been proxied by September (ba2dc).