diff options
Diffstat (limited to 'prototypes/c3b2/web/decode.scm')
-rw-r--r-- | prototypes/c3b2/web/decode.scm | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/prototypes/c3b2/web/decode.scm b/prototypes/c3b2/web/decode.scm new file mode 100644 index 0000000..abf200a --- /dev/null +++ b/prototypes/c3b2/web/decode.scm | |||
@@ -0,0 +1,54 @@ | |||
1 | ;;; Copyright © 2017 Amirouche Boubekki <amirouche@hypermove.net> | ||
2 | ;; | ||
3 | ;; This program is free software: you can redistribute it and/or modify | ||
4 | ;; it under the terms of the GNU General Public License as published by | ||
5 | ;; the Free Software Foundation, either version 3 of the License, or | ||
6 | ;; (at your option) any later version. | ||
7 | |||
8 | ;; This program is distributed in the hope that it will be useful, | ||
9 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
10 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
11 | ;; GNU General Public License for more details. | ||
12 | |||
13 | ;; You should have received a copy of the GNU General Public License | ||
14 | ;; along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
15 | (define-module (web decode)) | ||
16 | |||
17 | (use-modules (ice-9 match)) | ||
18 | (use-modules (rnrs bytevectors)) | ||
19 | (use-modules (srfi srfi-1)) | ||
20 | (use-modules (srfi srfi-26)) | ||
21 | (use-modules (web uri)) | ||
22 | |||
23 | ;;; | ||
24 | ;;; decode | ||
25 | ;;; | ||
26 | |||
27 | (define (acons-list k v alist) | ||
28 | "Add V to K to alist as list" | ||
29 | (let ((value (assoc-ref alist k))) | ||
30 | (if value | ||
31 | (let ((alist (alist-delete k alist))) | ||
32 | (acons k (cons v value) alist)) | ||
33 | (acons k (list v) alist)))) | ||
34 | |||
35 | (define (list->alist lst) | ||
36 | "Build a alist of list based on a list of key and values. | ||
37 | |||
38 | Multiple values can be associated with the same key" | ||
39 | (let next ((lst lst) | ||
40 | (out '())) | ||
41 | (if (null? lst) | ||
42 | out | ||
43 | (next (cdr lst) (acons-list (caar lst) (cdar lst) out))))) | ||
44 | |||
45 | (define-public (decode bv) | ||
46 | "Convert BV querystring or form data to an alist" | ||
47 | (define string (utf8->string bv)) | ||
48 | (define pairs (map (cut string-split <> #\=) | ||
49 | ;; semi-colon and amp can be used as pair separator | ||
50 | (append-map (cut string-split <> #\;) | ||
51 | (string-split string #\&)))) | ||
52 | (list->alist (map (match-lambda | ||
53 | ((key value) | ||
54 | (cons (uri-decode key) (uri-decode value)))) pairs))) | ||