From 6fc5e5fa1990d07de1b44fd4ac50cb388b3da480 Mon Sep 17 00:00:00 2001 From: "W. Kosior" Date: Tue, 23 Jul 2024 23:17:29 +0200 Subject: Add `parse-cookies' procedure. --- src/guile/cantius.scm | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'src/guile') diff --git a/src/guile/cantius.scm b/src/guile/cantius.scm index 76cc5fa..8afb276 100644 --- a/src/guile/cantius.scm +++ b/src/guile/cantius.scm @@ -246,6 +246,38 @@ +;;; +;;; Cookie parser +;;; + +(define %cookie-regex + (make-regexp (string-append + "^\\s*" + "([^[:space:];=]([^;=]*[^[:space:];=])?)" ;; name + "\\s*=\\s*" + "(([^[:space:];]([^;]*[^[:space:];])?)?)" ;; value + "\\s*(;|$)"))) + +(define-public (parse-cookies cookies) + (let loop ((idx 0) + (cookies-alist '())) + (match (regexp-exec %cookie-regex cookies idx) + + (#f + (when (string-index cookies char-set:graphic idx) + (raise (s35:condition + (&bad-request) + (s35:&message + (message "Unparseable cookie"))))) + (reverse cookies-alist)) + + ((and (= (cut match:substring <> 1) name) + (= (cut match:substring <> 3) value) + (= match:end idx*)) + (loop idx* `((,name . ,value) . ,cookies-alist)))))) + + + (define-public %current-path (make-parameter #f)) -- cgit v1.2.3