Removed uri-encode, uri-decode functions

Replaced internal implementation with Manabu Takayama's
percent-encoding
parent 5dec5935
2019-08-15 Vladimir Sedach <vas@oneofus.la>
* Removed uri-encode, uri-decode functions.
Replaced internal implementation with Manabu Takayama's
percent-encoding
2019-08-01 Vladimir Sedach <vas@oneofus.la>
* Forked uri-template2:
......
......@@ -69,12 +69,6 @@ uri-encode? (special variable)
Controls whether URI encoding/escaping is done on the templated value.
True by default.
uri-decode (function)
Decodes URI encoded/escaped characters in the given string.
uri-encode (function)
URI encodes/escapes the given string.
uri-template (symbol)
The car of the list that the URI template reader produces. A
function or macro.
......
......@@ -24,30 +24,13 @@
"Controls whether URI decoding/unescaping is done on the templated value when destructuring.
True by default.")
(defun uri-decode (str)
"Decodes URI encoded/escaped characters in the given string."
(let ((octets (make-array (length str)
:element-type 'flexi-streams:octet
:adjustable t :fill-pointer 0)))
(do ((i 0 (incf i)))
((>= i (length str)))
(let ((c (aref str i)))
(vector-push
(if (eql c #\%)
(prog1 (parse-integer str :start (1+ i) :end (+ i 3)
:radix 16)
(incf i 2))
(char-code c))
octets)))
(flexi-streams:octets-to-string octets :external-format :utf8)))
(defmacro weak-register-groups-bind (vars regex str &body body)
`(destructuring-bind (&optional ,@vars)
(coerce (nth-value 1 (scan-to-strings ,regex ,str)) 'list)
(declare (ignorable ,@vars))
,(when uri-decode?
`(setf ,@(loop for var in vars append
`(,var (when ,var (uri-decode ,var))))))
`(,var (when ,var (percent-encoding:decode ,var))))))
,@body))
(defmacro uri-template-bind ((template) uri &body body)
......@@ -71,7 +54,8 @@ The standard URI components look like:
%uri-fragment hash
%uri-head http://user@www.foo.com:8080
%uri-tail /dir/abc?bar=baz&xyz=1#hash"
(let* ((template (cdr template)) ;; template is expected to look like output of #U: '(uri-template &rest args)
;; template is expected to look like output of #U: '(uri-template &rest args)
(let* ((template (cdr template))
(template-vars (mapcar (lambda (x)
(car (last (second x))))
(remove-if #'stringp template)))
......
......@@ -30,12 +30,10 @@
#:enable-uri-template-syntax
#:read-uri-template
#:uri-encode?
#:uri-encode
;; destructuring
#:uri-template-bind
#:uri-decode?
#:uri-decode
;; RFC 2396 standard URI components
#:%uri-scheme
......
......@@ -21,7 +21,9 @@
(in-package #:uri-template2)
(parenscript:defpsmacro maybe-uri-encode (x)
(if uri-encode? `(encode-u-r-i-component ,x) x))
(if uri-encode?
`(encode-u-r-i-component ,x)
x))
(parenscript:defpsmacro uri-template (&rest template-args)
`(+ ,@template-args))
......@@ -106,19 +106,20 @@
(test encoding1
(is (string= "abc123" (uri-encode "abc123"))))
(is (string= "abc123" #U{"abc123"})))
(test encoding2
(is (string= "abc%20123" (uri-encode "abc 123"))))
(is (string= "abc%20123" #U{"abc 123"})))
(test encoding3
(is (string= "abc123" (uri-decode "abc123"))))
(test decoding3
(is (string= "abc123" (uri-template-bind (#U{x}) "abc123" x))))
(test encoding4
(is (string= "abc 123" (uri-decode "abc%20123"))))
(test decoding4
(is (string= "abc 123" (uri-template-bind (#U{x}) "abc%20123" x))))
(test unicode-encoding1
(is (string= "%D1%84%D0%BE%D0%BE" (uri-encode "фоо"))))
(is (string= "%D1%84%D0%BE%D0%BE" #U{"фоо"})))
(test unicode-encoding2
(is (string= "бар" (uri-decode "%D0%B1%D0%B0%D1%80"))))
(test unicode-decoding2
(is (string= "бар"
(uri-template-bind (#U{x}) "%D0%B1%D0%B0%D1%80" x))))
......@@ -24,18 +24,6 @@
"Controls whether URI encoding/escaping is done on the templated value.
True by default.")
;;; stolen from hunchentoot
(defun uri-encode (str)
"URI encodes/escapes the given string."
(with-output-to-string (s)
(loop for c across (flexi-streams:string-to-octets str :external-format :utf-8)
do (if (or (<= 48 c 57)
(<= 65 c 90)
(<= 97 c 122)
(find c '(45 95 46 126)))
(write-char (code-char c) s)
(format s "%~2,'0x" c)))))
(defun read-uri-template (stream &optional recursive-p)
"A function suitable for inserting into the readtable so you can
read URI templates from your own dispatch character."
......@@ -63,7 +51,9 @@ read URI templates from your own dispatch character."
(reverse template-accumulator))))
(defmacro maybe-uri-encode (x)
(if uri-encode? `(uri-encode (princ-to-string ,x)) x))
(if uri-encode?
`(percent-encoding:encode (princ-to-string ,x))
x))
(defun uri-template (&rest template-args)
"The car of the list that the URI template reader produces. A
......
......@@ -5,7 +5,7 @@
:long-description "An implementation of the URI Template proposed standard draft version 01. Lets you easily create and parse URIs by using the URI Template reader macro syntax."
:author "Vladimir Sedach <vas@oneofus.la>"
:license "GPL-3.0-or-later"
:depends-on ("cl-ppcre" "named-readtables" "flexi-streams")
:depends-on ("cl-ppcre" "named-readtables" "percent-encoding")
:serial t
:components ((:file "package")
(:file "uri-template")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment