blob: 3704ba2dec989433030d7e321ba8721216ed23b0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
;;; Weeb --- Web microframework for GNU Guile
;;; Copyright © 2026 Jakob Kreuze <zerodaysfordays@sdf.org>
;;;
;;; This file is part of Weeb.
;;;
;;; Weeb is free software: you can redistribute it and/or modify it
;;; under the terms of the GNU Affero General Public License as
;;; published by the Free Software Foundation, either version 3 of the
;;; License, or (at your option) any later version.
;;;
;;; Weeb is distributed in the hope that it will be useful, but WITHOUT
;;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
;;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General
;;; Public License for more details.
;;;
;;; You should have received a copy of the GNU Affero General Public
;;; License along with Weeb. If not, see <https://www.gnu.org/licenses/>.
(define-module (weeb request)
#:use-module (ice-9 match)
#:use-module (srfi srfi-69)
#:use-module (weeb struct)
#:export (<request>
urldecode
parse-form-urlencoded))
(define-struct <request>
method uri parameters headers body)
(define (urldecode str)
(let loop ((i 0) (acc ""))
(cond
((>= i (string-length str)) acc)
((char=? (string-ref str i) #\+)
(loop (+ i 1) (string-append acc " ")))
((and (char=? (string-ref str i) #\%)
(<= (+ i 2) (string-length str)))
(let* ((hex-part (substring str (+ i 1) (+ i 3)))
(code (string->number hex-part 16)))
(if code
(loop (+ i 3) (string-append acc (string (integer->char code))))
(loop (+ i 1) (string-append acc (string (string-ref str i)))))))
(else
(loop (+ i 1) (string-append acc (string (string-ref str i))))))))
(define (parse-form-urlencoded encoded)
(let ((result (make-hash-table)))
(for-each
(lambda (encoded-pair)
(match (map urldecode (string-split encoded-pair #\=))
((key value)
(let ((possible-duplicate (hash-table-ref/default result key #f)))
(cond ((list? possible-duplicate)
(hash-table-set! result key (append possible-duplicate (list value))))
(possible-duplicate
(hash-table-set! result key (list possible-duplicate value)))
(else
(hash-table-set! result key value)))))))
(filter (lambda (x) (not (zero? (string-length x))))
(string-split encoded #\&)))
(hash-table->alist result)))
|
© 2015 - 2026 Jakob L. Kreuze