From 5151fbe4a2b56951009491e9be5897ada7b8955c Mon Sep 17 00:00:00 2001 From: cage Date: Mon, 28 Dec 2020 12:40:47 +0100 Subject: [PATCH] - [gemini] allow percent encoding of query, path and fragment only if not already encoded. --- src/gemini/client.lisp | 10 +++------- src/package.lisp | 5 ++++- src/text-utils.lisp | 18 ++++++++++++++++++ 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/gemini/client.lisp b/src/gemini/client.lisp index 5703167..0a5e3d3 100644 --- a/src/gemini/client.lisp +++ b/src/gemini/client.lisp @@ -287,19 +287,15 @@ (defun percent-encode-path (path) (let ((splitted (split "/" path))) (if splitted - (reduce (lambda (a b) (strcat a "/" (percent-encode b))) + (reduce (lambda (a b) (strcat a "/" (maybe-percent-encode b))) splitted) path))) -(defun percent-encode-allow-null (data) - (when data - (percent-encode data))) - (defun percent-encode-query (query) - (percent-encode-allow-null query)) + (maybe-percent-encode query)) (defun percent-encode-fragment (fragment) - (percent-encode-allow-null fragment)) + (maybe-percent-encode fragment)) (defun request (host path &key (query nil) diff --git a/src/package.lisp b/src/package.lisp index 6c65034..28a3ec1 100644 --- a/src/package.lisp +++ b/src/package.lisp @@ -371,7 +371,10 @@ :annotated-text-value :box-fit-multiple-column-annotated :collect-links - :percent-encode)) + :percent-encode + :percent-decode + :percent-encoded-p + :maybe-percent-encode)) (defpackage :html-utils (:use diff --git a/src/text-utils.lisp b/src/text-utils.lisp index 9ff24df..8bf1ee2 100644 --- a/src/text-utils.lisp +++ b/src/text-utils.lisp @@ -673,3 +673,21 @@ printed in the box column by column; in the example above the results are: (defun percent-encode (string) (percent-encoding:encode string :encoding :utf-8)) + +(defun percent-decode (string) + (percent-encoding:decode string :encoding :utf-8)) + +(defun percent-encoded-p (string) + (conditions:with-default-on-error (t) + (string/= string + (percent-decode string)))) + +(defun percent-encode-allow-null (data) + (when data + (percent-encode data))) + +(defun maybe-percent-encode (data) + "Note that when data is null this function returns nil" + (if (percent-encoded-p data) + data + (percent-encode-allow-null data)))