aboutsummaryrefslogtreecommitdiff
path: root/weeb/request.scm
blob: 3d18b0a6c811eb4b7a52454f58d59f8458f0f7d7 (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
;;; 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 (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)
  (map (lambda (encoded-pair)
         (match (map urldecode (string-split encoded-pair #\=))
           ((key value) `(,key . ,value))))
       (filter (lambda (x) (not (zero? (string-length x))))
               (string-split encoded #\&))))

© 2015 - 2026 Jakob L. Kreuze