diff options
-rw-r--r-- | gemini.cl | 28 |
1 files changed, 28 insertions, 0 deletions
@@ -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 |