decode.scm 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. (define-module (decode))
  2. (use-modules (ice-9 match))
  3. (use-modules (rnrs bytevectors))
  4. (use-modules (srfi srfi-1))
  5. (use-modules (srfi srfi-26))
  6. (use-modules (web uri))
  7. ;;;
  8. ;;; decode
  9. ;;;
  10. (define (acons-list k v alist)
  11. "Add V to K to alist as list"
  12. (let ((value (assoc-ref alist k)))
  13. (if value
  14. (let ((alist (alist-delete k alist)))
  15. (acons k (cons v value) alist))
  16. (acons k (list v) alist))))
  17. (define (list->alist lst)
  18. "Build a alist of list based on a list of key and values.
  19. Multiple values can be associated with the same key"
  20. (let next ((lst lst)
  21. (out '()))
  22. (if (null? lst)
  23. out
  24. (next (cdr lst) (acons-list (caar lst) (cdar lst) out)))))
  25. (define-public (decode bv)
  26. "Convert BV querystring or form data to an alist"
  27. (define string (utf8->string bv))
  28. (define pairs (map (cut string-split <> #\=)
  29. ;; semi-colon and amp can be used as pair separator
  30. (append-map (cut string-split <> #\;)
  31. (string-split string #\&))))
  32. (list->alist (map (match-lambda
  33. ((key value)
  34. (cons (uri-decode key) (uri-decode value)))) pairs)))