diff options
| author | Ted Zlatanov | 2012-05-15 04:47:38 -0400 |
|---|---|---|
| committer | Ted Zlatanov | 2012-05-15 04:47:38 -0400 |
| commit | 530e968e58a69071207897600ca2a019ea878600 (patch) | |
| tree | 563a48eb196bf01c9edc174b3589565ac82cea0a | |
| parent | 3a427266b688e084e908ae2ede9b6e373d8b0032 (diff) | |
| download | emacs-530e968e58a69071207897600ca2a019ea878600.tar.gz emacs-530e968e58a69071207897600ca2a019ea878600.zip | |
add url-build-query-string and improve url-parse-query-string as per bug#8706
* url/url-util.el (url-build-query-string): New function.
(url-parse-query-string): Allow that '=' is not required and split
URL parameters on ';', not just '&'.
| -rw-r--r-- | lisp/url/ChangeLog | 6 | ||||
| -rw-r--r-- | lisp/url/url-util.el | 67 |
2 files changed, 60 insertions, 13 deletions
diff --git a/lisp/url/ChangeLog b/lisp/url/ChangeLog index 87f2cfe5019..0e5c2787578 100644 --- a/lisp/url/ChangeLog +++ b/lisp/url/ChangeLog | |||
| @@ -1,3 +1,9 @@ | |||
| 1 | 2012-05-15 Ian Eure <ian@simplegeo.com> | ||
| 2 | |||
| 3 | * url-util.el (url-build-query-string): New function. | ||
| 4 | (url-parse-query-string): Allow that '=' is not required and split | ||
| 5 | URL parameters on ';', not just '&'. | ||
| 6 | |||
| 1 | 2012-05-14 Lars Magne Ingebrigtsen <larsi@gnus.org> | 7 | 2012-05-14 Lars Magne Ingebrigtsen <larsi@gnus.org> |
| 2 | 8 | ||
| 3 | * url-expand.el (url-default-expander): Copy over the fullness of | 9 | * url-expand.el (url-default-expander): Copy over the fullness of |
diff --git a/lisp/url/url-util.el b/lisp/url/url-util.el index 71bc84cab09..18fc51f0d10 100644 --- a/lisp/url/url-util.el +++ b/lisp/url/url-util.el | |||
| @@ -263,24 +263,65 @@ Will not do anything if `url-show-status' is nil." | |||
| 263 | ;;;###autoload | 263 | ;;;###autoload |
| 264 | (defun url-parse-query-string (query &optional downcase allow-newlines) | 264 | (defun url-parse-query-string (query &optional downcase allow-newlines) |
| 265 | (let (retval pairs cur key val) | 265 | (let (retval pairs cur key val) |
| 266 | (setq pairs (split-string query "&")) | 266 | (setq pairs (split-string query "[;&]")) |
| 267 | (while pairs | 267 | (while pairs |
| 268 | (setq cur (car pairs) | 268 | (setq cur (car pairs) |
| 269 | pairs (cdr pairs)) | 269 | pairs (cdr pairs)) |
| 270 | (if (not (string-match "=" cur)) | 270 | (unless (string-match "=" cur) |
| 271 | nil ; Grace | 271 | (setq cur (concat cur "="))) |
| 272 | (setq key (url-unhex-string (substring cur 0 (match-beginning 0)) | 272 | |
| 273 | allow-newlines)) | 273 | (when (string-match "=" cur) |
| 274 | (setq val (url-unhex-string (substring cur (match-end 0) nil) | 274 | (setq key (url-unhex-string (substring cur 0 (match-beginning 0)) |
| 275 | allow-newlines)) | 275 | allow-newlines)) |
| 276 | (if downcase | 276 | (setq val (url-unhex-string (substring cur (match-end 0) nil) |
| 277 | (setq key (downcase key))) | 277 | allow-newlines)) |
| 278 | (setq cur (assoc key retval)) | 278 | (if downcase |
| 279 | (if cur | 279 | (setq key (downcase key))) |
| 280 | (setcdr cur (cons val (cdr cur))) | 280 | (setq cur (assoc key retval)) |
| 281 | (setq retval (cons (list key val) retval))))) | 281 | (if cur |
| 282 | (setcdr cur (cons val (cdr cur))) | ||
| 283 | (setq retval (cons (list key val) retval))))) | ||
| 282 | retval)) | 284 | retval)) |
| 283 | 285 | ||
| 286 | ;;;###autoload | ||
| 287 | (defun url-build-query-string (query &optional semicolons keep-empty) | ||
| 288 | "Build a query-string. | ||
| 289 | |||
| 290 | Given a QUERY in the form: | ||
| 291 | '((key1 val1) | ||
| 292 | (key2 val2) | ||
| 293 | (key3 val1 val2) | ||
| 294 | (key4) | ||
| 295 | (key5 "")) | ||
| 296 | |||
| 297 | \(This is the same format as produced by `url-parse-query-string') | ||
| 298 | |||
| 299 | This will return a string | ||
| 300 | \"key1=val1&key2=val2&key3=val1&key3=val2&key4&key5\". Keys may | ||
| 301 | be strings or symbols; if they are symbols, the symbol name will | ||
| 302 | be used. | ||
| 303 | |||
| 304 | When SEMICOLONS is given, the separator will be \";\". | ||
| 305 | |||
| 306 | When KEEP-EMPTY is given, empty values will show as \"key=\" | ||
| 307 | instead of just \"key\" as in the example above." | ||
| 308 | (mapconcat | ||
| 309 | (lambda (key-vals) | ||
| 310 | (let ((escaped | ||
| 311 | (mapcar (lambda (sym) | ||
| 312 | (url-hexify-string (format "%s" sym))) key-vals))) | ||
| 313 | (mapconcat (lambda (val) | ||
| 314 | (let ((vprint (format "%s" val)) | ||
| 315 | (eprint (format "%s" (car escaped)))) | ||
| 316 | (concat eprint | ||
| 317 | (if (or keep-empty | ||
| 318 | (and val (not (zerop (length vprint))))) | ||
| 319 | "=" | ||
| 320 | "") | ||
| 321 | vprint))) | ||
| 322 | (or (cdr escaped) '("")) (if semicolons ";" "&")))) | ||
| 323 | query (if semicolons ";" "&"))) | ||
| 324 | |||
| 284 | (defun url-unhex (x) | 325 | (defun url-unhex (x) |
| 285 | (if (> x ?9) | 326 | (if (> x ?9) |
| 286 | (if (>= x ?a) | 327 | (if (>= x ?a) |