summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRicardo Wurmus <rekado@elephly.net>2020-04-05 16:58:26 +0200
committerRicardo Wurmus <rekado@elephly.net>2020-04-05 17:46:22 +0200
commitfc8222ebb6b28098020dbd82e588ad46266f7982 (patch)
tree2c7a585c0743d8d38b151b4fc12c799515d6d836
parentf72a8d5bf64f91766a06122ab8e4ba0517b8dbc6 (diff)
web: Add parse-form-submission.
-rw-r--r--mumi/web/render.scm24
1 files changed, 23 insertions, 1 deletions
diff --git a/mumi/web/render.scm b/mumi/web/render.scm
index 146a001..61ccde6 100644
--- a/mumi/web/render.scm
+++ b/mumi/web/render.scm
@@ -20,12 +20,17 @@
(define-module (mumi web render)
#:use-module (srfi srfi-1)
+ #:use-module (srfi srfi-2)
#:use-module (srfi srfi-19)
#:use-module (srfi srfi-26)
#:use-module (ice-9 binary-ports)
+ #:use-module ((ice-9 textual-ports)
+ #:select (get-string-all))
+ #:use-module (ice-9 match)
#:use-module (web request)
#:use-module (web response)
#:use-module (web uri)
+ #:use-module (webutils multipart)
#:use-module (json)
#:use-module (mumi config)
#:use-module (mumi web sxml)
@@ -36,7 +41,8 @@
not-found
unprocessable-entity
created
- redirect))
+ redirect
+ parse-form-submission))
(define file-mime-types
'(("css" . (text/css))
@@ -125,3 +131,19 @@
(location . ,uri))
headers))
(format #f "Redirect to ~a" (uri->string uri)))))
+
+(define (parse-form-submission request body)
+ "Return an alist with keys to values for the submitted form."
+ (let ((parts (parse-request-body request body)))
+ (map (lambda (part)
+ (let ((binary? (match (assoc-ref (part-headers part) 'content-type)
+ (('application/octet-stream . _) #t)
+ (_ #f))))
+ (and-let*
+ ((content (assoc-ref (part-headers part) 'content-disposition))
+ (form-data (and=> (memq 'form-data content) cadr))
+ (name (and=> (memq 'name form-data) cdr)))
+ (cons (string->symbol name)
+ ((if binary? get-bytevector-all get-string-all)
+ (part-body part))))))
+ parts)))