aboutsummaryrefslogtreecommitdiff
path: root/weeb/request.scm
diff options
context:
space:
mode:
Diffstat (limited to 'weeb/request.scm')
-rw-r--r--weeb/request.scm28
1 files changed, 27 insertions, 1 deletions
diff --git a/weeb/request.scm b/weeb/request.scm
index 42cd933..3d18b0a 100644
--- a/weeb/request.scm
+++ b/weeb/request.scm
@@ -17,8 +17,34 @@
;;; 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>))
+ #: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