From 1e669cb4d61fa21cb304bbba326a9480dde38524 Mon Sep 17 00:00:00 2001 From: Jaidyn Ann <10477760+JadedCtrl@users.noreply.github.com> Date: Tue, 8 Aug 2023 14:34:49 -0500 Subject: [PATCH] Actually serve captcha images --- kaptchapelo.lisp | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/kaptchapelo.lisp b/kaptchapelo.lisp index 083d6cd..2a589a9 100644 --- a/kaptchapelo.lisp +++ b/kaptchapelo.lisp @@ -57,28 +57,38 @@ captcha-md5-str))))) -(defun index-response () - '(201 (:content-type "text/plain") #p"kaptchapelo.lisp")) -;; '(201 (:content-type "text/plain") ("You’ve installed Kaptĉapelo! Good work, guy!"))) +(defun image-response (request-uri captcha-dir) + (let ((image-path (str:replace-first "/captcha/" (format nil "~A" captcha-dir) request-uri))) + (list 201 '(:content-type "image/png") (pathname image-path)))) +(defun index-response () + '(201 (:content-type "text/plain") ("You’ve installed Kaptĉapelo! Good work, guy!"))) + (defun 404-response () '(404 (:content-type "text/plain") ("No such page."))) (defun server (env captcha-dir) (let* ((uri (quri:uri (getf env :request-uri))) + (uri-path (quri:uri-path uri)) (params (quri:uri-query-params uri))) - (cond ((string= (quri:uri-path uri) "/new") + (format *error-output* "~A" uri-path) + (cond ((string= uri-path "/new") (new-captcha-response captcha-dir)) - ((or (string= (quri:uri-path uri) "/") - (string= (quri:uri-path uri) "/index.html")) + ((or (string= uri-path "/") + (string= uri-path "/index.html")) (index-response)) + ((str:starts-with? "/captcha/" uri-path) +;; (str:ends-with? ".png" uri-path)) + (image-response uri-path captcha-dir)) ('t (404-response))))) -(defun start-server (&key (captcha-directory #p"captcha/")) +(defun start-server (&key (address "0.0.0.0") (port 5001) (captcha-directory #p"captcha/")) (clack:clackup (lambda (env) - (funcall #'server env captcha-directory)))) + (funcall #'server env captcha-directory)) + :address address + :port port))