From 710b763adf40d55624f7da7ee89b790ea212accc Mon Sep 17 00:00:00 2001 From: "Guilherme C. T" Date: Sun, 31 Jul 2022 02:50:41 -0700 Subject: Added function to map a URL path to a local gemini file. --- gemini.cl | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/gemini.cl b/gemini.cl index 9460439..67399a9 100644 --- a/gemini.cl +++ b/gemini.cl @@ -24,3 +24,31 @@ (defun status->number (key) (cdr (assoc key (reverse-alist +status-codes+)))) + +(defvar +site-root+ (pathname (user-homedir-pathname))) + +(defun find-resource (url-path-string) + ;; If the URL ends with a `.gmi` or `.gemini`, assume it points to a file and check if it exists. + ;; Otherwise, assume it points to a directory, and check if `index.gmi` or `index.gemini` exists in that directory. + ;; Returns the pathname of the file if it exists; nil otherwise. + (let ((path-local (merge-pathnames (pathname url-path-string) +site-root+))) + (cond ( + (search ".." url-path-string) + ; TODO: Replace this with some sort of (is-unsafe-URL) function. + ; URL is unsafe. + nil) + ((or (string= "gmi" (pathname-type path-local)) + (string= "gemini" (pathname-type path-local))) + ; URL points to a file. + (probe-file path-local)) + (t + ; URL points to a directory. + ; Due to how pathnames work, we must ensure that the last character in the URL is a single "/" for it to be treated as a directory. + (let + ((path-local-dir (pathname + (if (pathname-name path-local) + (pathname (concatenate 'string (namestring path-local) "/")) + path-local + )))) + (or (probe-file (merge-pathnames "index.gmi" path-local-dir)) + (probe-file (merge-pathnames "index.gemini" path-local-dir)))))))) \ No newline at end of file -- cgit v1.2.3