From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Ian Eure Newsgroups: gmane.emacs.bugs Subject: bug#8706: 24.0.50; [PATCH] Function to build a URL query-string Date: Tue, 7 Jun 2011 10:07:40 -0700 Message-ID: <70084BE3-710F-47B6-8949-B420F5BF47B4@simplegeo.com> References: <87d3j932n7.fsf@lifelogs.com> <87zkmaopfs.fsf@lifelogs.com> <91C69C7A-1980-4AF4-9B4B-4AFDB16D2C0B@simplegeo.com> <87y61uldyj.fsf@lifelogs.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 (Apple Message framework v1084) Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable X-Trace: dough.gmane.org 1307469040 19824 80.91.229.12 (7 Jun 2011 17:50:40 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Tue, 7 Jun 2011 17:50:40 +0000 (UTC) Cc: 8706@debbugs.gnu.org To: Ted Zlatanov Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Tue Jun 07 19:50:33 2011 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([140.186.70.17]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1QU0QC-0005lD-Ss for geb-bug-gnu-emacs@m.gmane.org; Tue, 07 Jun 2011 19:50:33 +0200 Original-Received: from localhost ([::1]:49163 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QU0QB-0003il-Ck for geb-bug-gnu-emacs@m.gmane.org; Tue, 07 Jun 2011 13:50:31 -0400 Original-Received: from eggs.gnu.org ([140.186.70.92]:45452) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QTzl6-0000rR-TA for bug-gnu-emacs@gnu.org; Tue, 07 Jun 2011 13:08:07 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1QTzl5-0007hj-7I for bug-gnu-emacs@gnu.org; Tue, 07 Jun 2011 13:08:04 -0400 Original-Received: from debbugs.gnu.org ([140.186.70.43]:39454) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QTzl4-0007hW-Tq for bug-gnu-emacs@gnu.org; Tue, 07 Jun 2011 13:08:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.69) (envelope-from ) id 1QTzl3-0003FT-MJ; Tue, 07 Jun 2011 13:08:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Ian Eure Original-Sender: debbugs-submit-bounces@debbugs.gnu.org Resent-To: owner@debbugs.gnu.org Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 07 Jun 2011 17:08:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 8706 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 8706-submit@debbugs.gnu.org id=B8706.130746647012468 (code B ref 8706); Tue, 07 Jun 2011 17:08:01 +0000 Original-Received: (at 8706) by debbugs.gnu.org; 7 Jun 2011 17:07:50 +0000 Original-Received: from localhost ([127.0.0.1] helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1QTzkr-0003F3-Uh for submit@debbugs.gnu.org; Tue, 07 Jun 2011 13:07:50 -0400 Original-Received: from mail-px0-f170.google.com ([209.85.212.170]) by debbugs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1QTzkq-0003Er-9W for 8706@debbugs.gnu.org; Tue, 07 Jun 2011 13:07:49 -0400 Original-Received: by pxi19 with SMTP id 19so4129687pxi.29 for <8706@debbugs.gnu.org>; Tue, 07 Jun 2011 10:07:42 -0700 (PDT) Original-Received: by 10.68.65.110 with SMTP id w14mr266191pbs.382.1307466462355; Tue, 07 Jun 2011 10:07:42 -0700 (PDT) Original-Received: from [10.0.1.153] (c-76-104-208-94.hsd1.wa.comcast.net [76.104.208.94]) by mx.google.com with ESMTPS id y2sm262398pbg.72.2011.06.07.10.07.41 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 07 Jun 2011 10:07:41 -0700 (PDT) In-Reply-To: <87y61uldyj.fsf@lifelogs.com> X-Mailer: Apple Mail (2.1084) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.11 Precedence: list Resent-Date: Tue, 07 Jun 2011 13:08:01 -0400 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) X-Received-From: 140.186.70.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.bugs:47025 Archived-At: Apologies for the late response, I got distracted by other things. On May 25, 2011, at 1:31 PM, Ted Zlatanov wrote: > On Wed, 25 May 2011 09:32:15 -0700 Ian Eure wrote:=20= >=20 > IE> 1. Two-element sequences should work as they do now. > IE> 2. One-element sequences should get an empty string appended. > IE> 3. Invalid sequences ignored. >=20 > IE> I'm not sure it makes sense to support #2, since it seems somewhat > IE> opaque and you can do the same thing with an empty string in = scenario > IE> #1.=20 >=20 > Single-element parameters, shown as just "key" instead of "key=3Dval", = are > a well-known URL query string convention. They are not opaque. I = think > they should be explicitly supported. >=20 As I said, they _are_ supported. You just have to explicitly pass an = empty string in the pair: (url-build-query-string '(("a" "b") ("c" "")) -> "a=3Db&c=3D" I believe this is the correct behavior. It's also precisely what = url-parse-query-string returns: (url-parse-query-string "a=3Db&c=3D") -> (("c" "") ("a" "b")) This is also relevant to Stefan's comment. All else being equal, I think = we should support the same conventions it does. The one issue I = discovered is how it handles sending multiple values for the same key. = Given "a=3Done&a=3Dtwo", it returns: (("a" "one" "two")), which my = previous iterations don't know what to do with. Here's an updated patch which should correctly support everything = url-parse-query-string produces. I also updated it to allow empty keys, = as this was fairly easy with the code refactored to support (key val val = val) syntax. Updated patch: --- url-util.el.orig 2011-06-07 09:56:36.000000000 -0700 +++ url-util.el 2011-06-07 10:06:09.000000000 -0700 @@ -281,6 +281,31 @@ (setq retval (cons (list key val) retval))))) retval)) =20 +;;;###autoload +(defun url-build-query-string (query) + "Build a query-string. + +Given a QUERY in the form: +'((key1 val1) + (key2 val2) + (key3 val1 val2)) + +\(This is the same format as produced by `url-parse-query-string') + +This will return a string +`key1=3Dval1&key2=3Dval2&key3=3Dval1&key3=3Dval2'. Keys may be strings = or +symbols; if they are symbols, the string name will be used." + + (mapconcat + (lambda (key-vals) + (let ((escaped + (mapcar (lambda (sym) + (url-hexify-string (format "%s" sym))) = key-vals))) + + (mapconcat (lambda (val) (format "%s=3D%s" (car escaped) val)) + (or (cdr escaped) '("")) "&"))) + query "&")) + (defun url-unhex (x) (if (> x ?9) (if (>=3D x ?a)=