From 8bcb8fb88f7039404e01eeac8814564062ecf350 Mon Sep 17 00:00:00 2001 From: Coin Okay Date: Wed, 22 Apr 2020 21:06:03 -0500 Subject: multipart/form-data decoder --- decoders.lisp | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 decoders.lisp (limited to 'decoders.lisp') diff --git a/decoders.lisp b/decoders.lisp new file mode 100644 index 0000000..e4dcc8c --- /dev/null +++ b/decoders.lisp @@ -0,0 +1,109 @@ + +(in-package #:lazybones.decoders) + +(defun read-body-to-string (stream content-length) + "Reads CONTENT-LENGTH characters from STREAM and returns a string." + (let ((string (make-string content-length))) + (read-sequence string stream) + string)) + + +;;; JSON DECODER + +(defun decode-json-body (stream content-type content-length) + "Reads LEN characters from stream and decodes them as JSON, returning a PLIST" + (declare (ignore content-type)) + (jonathan:parse (read-body-to-string stream content-length))) + +(add-decoder "application/json" #'decode-json-body) + + +;;; MULTIPART/FORM-DATA DECODER + + +(defun butlast-to-string (res) + (map 'string 'identity (butlast res))) + +(defun make-keyword (str) + (read-from-string (format nil ":~a" str))) + +(defun write-image-to-tmp-file (body) + (cl-fad:with-output-to-temporary-file (out-file :element-type '(unsigned-byte 8)) + (loop :for char :across body :do (write-byte (char-int char) out-file)))) + +(defun dump-stream-to-text (stream) + (with-output-to-string (out) + (loop :for char = (read-char stream nil nil) + :while char :do (write-char char out)))) + +(<