;;; Weeb --- Web microframework for GNU Guile ;;; Copyright © 2026 Jakob Kreuze ;;; ;;; 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 . (define-module (weeb request) #:use-module (ice-9 match) #:use-module (weeb struct) #:export ( urldecode parse-form-urlencoded)) (define-struct 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 #\&))))