From d27eed97e844f22bc929ec99d4bb90c80360e6c9 Mon Sep 17 00:00:00 2001 From: Jaidyn Ann <10477760+JadedCtrl@users.noreply.github.com> Date: Thu, 25 May 2023 15:59:10 -0500 Subject: [PATCH] Clean up path parsing; URI params to alist --- activitypub-server.lisp | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/activitypub-server.lisp b/activitypub-server.lisp index c249eec..870b01b 100644 --- a/activitypub-server.lisp +++ b/activitypub-server.lisp @@ -28,7 +28,7 @@ ;; The default 404 response. (defun http-404 (env path-items params) '(404 (:content-type "text/plain") - ("uh-oh, I did a war crime!"))) + ("404, you goddamn fool!"))) ;; Respond to requests within the /u/* directory. @@ -42,11 +42,10 @@ ;; Returns the response data for Clack, given the request data `env`. (defun server (env) - (let* ((path-split-? (str:split #\? (getf env :request-uri))) - (path (car path-split-?)) - (params (cadr path-split-?)) + (let* ((path (pathname-sans-parameters (getf env :request-uri))) + (params (pathname-parameters (getf env :request-uri))) (response-function - (or (assoc-by-path *directories* (pathname-components path)) + (or (assoc-by-path (directories) (pathname-components path)) '("" . http-404))) ;; So that response functions only deal with relative paths… (path-sans-response-root @@ -98,6 +97,23 @@ (+ depth 1)))))) +;; Removes parameters from a URI pathname, returning the bare path. +;; "/path/a/b?a=1&b=3" → "/path/a/b" +(defun pathname-sans-parameters (path) + (car (str:split #\? path))) + + +;; Convert the parameters of a URI pathname into an associative list. +;; "/path/a/b?a=1&b=2&c=3" → (("a" . "1") ("b" . "2") ("c" . "3")) +(defun pathname-parameters (path) + (mapcar + (lambda (pair) + (let ((pair-items (str:split #\= pair))) + (cons (car pair-items) + (cadr pair-items)))) + (str:split #\& (cadr (str:split #\? path))))) + + ;; Split a pathname into a list of its components. ;; "/u/bear/apple.txt" → '("u" "bear" "apple.txt") (defun pathname-components (pathname)