Skip to content

Commit

Permalink
clean up url-encoding file
Browse files Browse the repository at this point in the history
  • Loading branch information
marijnh committed Nov 25, 2010
1 parent 2fe7a1e commit 1451b1d
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 288 deletions.
4 changes: 2 additions & 2 deletions defservice.asd
@@ -1,3 +1,3 @@
(defsystem :defservice
:components ((:file "trivial-utf-8")
(:file "defservice" :depends-on ("trivial-utf-8"))))
:components ((:file "url-encode")
(:file "defservice" :depends-on ("url-encode"))))
36 changes: 1 addition & 35 deletions defservice.lisp
@@ -1,5 +1,5 @@
(defpackage :defservice
(:use :cl)
(:use :cl :url-encode)
(:export #:make-start-context #:defservice #:defcontext #:allow-empty-resource-name
#:dispatch-request #:go-on
#:url-encode #:url-decode
Expand All @@ -24,40 +24,6 @@

(defun ensure-list (x) (if (listp x) x (list x)))

(defun url-encode (string &optional (to-escape "\"#$%&+,/:;<=>?@"))
(declare (optimize speed (safety 0)))
(let ((size (loop :for ch :across string :for code := (char-code ch) :sum
(cond ((> code 127) (* (trivial-utf-8:char-utf-8-byte-length ch) 3))
((or (< code 33) (find ch to-escape)) 3)
(t 1)))))
(if (= size (length string))
string
(let ((out (make-string size)) (pos 0))
(macrolet ((wr (ch) `(progn (setf (schar out pos) ,ch) (incf pos))))
(flet ((code-out (ch)
(multiple-value-bind (hi lo) (floor (char-code ch) 16)
(wr #\%) (wr (digit-char hi)) (wr (digit-char lo)))))
(loop :for ch :across string :for code := (char-code ch) :do
(cond ((> code 127) (trivial-utf-8::as-utf-8-bytes ch code-out))
((or (< code 33) (find ch to-escape)) (code-out ch))
(t (wr ch))))))
out))))

(defun url-decode (string)
(declare (optimize speed (safety 0)))
(let ((bytes (make-array (length string) :element-type '(unsigned-byte 8))))
(with-input-from-string (in string)
(loop :for pos :from 0 :for ch := (read-char in nil nil) :while ch :do
(setf (aref bytes pos)
(case ch
(#\+ #.(char-code #\space))
(#\% (let ((big (digit-char-p (read-char in) 16))
(small (digit-char-p (read-char in) 16)))
(unless (and big small) (error "Junk in URL."))
(+ small (* 16 big))))
(t (char-code ch))))
:finally (return (trivial-utf-8:utf-8-bytes-to-string bytes :end pos))))))

(defun method-pos (method)
(case method (:get 0) (:head 0) (:post 1) (:put 2) (:delete 3)))

Expand Down
251 changes: 0 additions & 251 deletions trivial-utf-8.lisp

This file was deleted.

0 comments on commit 1451b1d

Please sign in to comment.