]> git.eshelyaron.com Git - emacs.git/commitdiff
Avoid unnecessary escaping in 'url-build-query-string'
authorDagfinn Ilmari Mannsåker <ilmari@ilmari.org>
Tue, 9 Apr 2024 14:02:45 +0000 (15:02 +0100)
committerEshel Yaron <me@eshelyaron.com>
Sat, 20 Apr 2024 11:05:55 +0000 (14:05 +0300)
* lisp/url/url-util.el (url-query-key-value-allowed-chars):
New defconst.
(url-build-query-string): Use it to escape only those
characters that need it in keys and values.
* test/lisp/url/url-util-tests.el (url-util-tests): Add
new test cases.  (Bug#70312)

(cherry picked from commit 40629706b1ff1a2a596420dd2ac388b7e5fc656c)

lisp/url/url-util.el
test/lisp/url/url-util-tests.el

index 5f45b98c7a5c0a59176b7093e7c84497cfdaf66c..f063efe18a655c0dbc2e2742ca8dd51e281462f2 100644 (file)
@@ -268,7 +268,8 @@ instead of just \"key\" as in the example above."
    (lambda (key-vals)
      (let ((escaped
             (mapcar (lambda (sym)
-                      (url-hexify-string (format "%s" sym))) key-vals)))
+                      (url-hexify-string (format "%s" sym) url-query-key-value-allowed-chars))
+                    key-vals)))
        (mapconcat (lambda (val)
                     (let ((vprint (format "%s" val))
                           (eprint (format "%s" (car escaped))))
@@ -410,6 +411,15 @@ These characters are specified in RFC 3986, Appendix A.")
   "Allowed-character byte mask for the query segment of a URI.
 These characters are specified in RFC 3986, Appendix A.")
 
+(defconst url-query-key-value-allowed-chars
+  (let ((vec (copy-sequence url-query-allowed-chars)))
+    (aset vec ?= nil)
+    (aset vec ?& nil)
+    (aset vec ?\; nil)
+    vec)
+  "Allowed-charcter byte mask for keys and values in the query segment of a URI.
+url-query-allowed-chars minus '=', '&', and ';'.")
+
 ;;;###autoload
 (defun url-encode-url (url)
   "Return a properly URI-encoded version of URL.
index 133aa0ffd8829b77bab41b77b34a5118225d0aa4..c6246d69a2aef55cb26c71cd053f181e98884da7 100644 (file)
            ("key1=val1;key2=val2;key3=val1;key3=val2;key4;key5"
             ((key1 "val1") (key2 val2) (key3 val1 val2) ("key4") (key5 "")) t)
            ("key1=val1;key2=val2;key3=val1;key3=val2;key4=;key5="
-            ((key1 val1) (key2 val2) ("key3" val1 val2) (key4) (key5 "")) t t)))
+            ((key1 val1) (key2 val2) ("key3" val1 val2) (key4) (key5 "")) t t)
+           ("key1=val/slash;key2=val%3Bsemi;key3=val%26amp;key4=val%3Deq"
+            ((key1 "val/slash") (key2 "val;semi") (key3 "val&amp") (key4 "val=eq")) t)
+           ("key%3Deq=val1;key%3Bsemi=val2;key%26amp=val3"
+            (("key=eq" val1) ("key;semi" val2) ("key&amp" val3)) t)))
         test)
     (while tests
       (setq test (car tests)