I’ve start playing with cgi in guile scheme and I came up with the functions to read GET, POST and Cookie values that I want to share. here is the code:
(use-modules (web uri)) ;; includes uri-decode procedure (define (read-port port) (let iter ((result '()) (chr (read-char port))) (if (eof-object? chr) (list->string result) (iter (append result (list chr)) (read-char port))))) (define (params->alist string fn sep) (map (lambda (pair) (let* ((list (string-split pair #\=)) (key (fn (string-trim (car list)))) (value (fn (string-trim (string-join (cdr list) "="))))) (cons key value))) (string-split string sep))) (define (create-param-proc string fn sep) (if (or (equal? string #f) (equal? string "")) (lambda (var) "") (let ((query (params->alist string fn sep))) (lambda (var) (let ((pair (assoc var query))) (if (null? pair) "" (cdr pair))))))) (define GET (create-param-proc (getenv "QUERY_STRING") uri-decode #\&)) (define POST (create-param-proc (read-port (current-input-port)) uri-decode #\&)) (define COOKIE (create-param-proc (getenv "HTTP_COOKIE") identity #\;))
And to get the values you just execute one of GET, POST, COOKIE procedures:
(display (string-append "foo: " (GET "foo"))) (newline) (display (string-append "foo: " (POST "foo"))) (newline) (display (string-append "foo: " (COOKIE "foo"))) (newline)