From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.ciao.gmane.io!not-for-mail From: philip@warpmail.net (Philip K.) Newsgroups: gmane.emacs.devel Subject: Re: master 421eeff: Add support for multiple Gravatar services Date: Tue, 31 Mar 2020 13:42:17 +0200 Message-ID: <87h7y4px7q.fsf@bulbul> References: <87lfnpsb0b.fsf@bulbul> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="ciao.gmane.io:159.69.161.202"; logging-data="51740"; mail-complaints-to="usenet@ciao.gmane.io" To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Tue Mar 31 13:43:15 2020 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jJFIY-000DKS-0d for ged-emacs-devel@m.gmane-mx.org; Tue, 31 Mar 2020 13:43:14 +0200 Original-Received: from localhost ([::1]:36443 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jJFIX-0000gl-1X for ged-emacs-devel@m.gmane-mx.org; Tue, 31 Mar 2020 07:43:13 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:58367) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jJFHl-0006xj-NX for emacs-devel@gnu.org; Tue, 31 Mar 2020 07:42:28 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jJFHk-0002Y7-78 for emacs-devel@gnu.org; Tue, 31 Mar 2020 07:42:25 -0400 Original-Received: from wout5-smtp.messagingengine.com ([64.147.123.21]:45959) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jJFHj-0002W6-MH for emacs-devel@gnu.org; Tue, 31 Mar 2020 07:42:24 -0400 Original-Received: from compute2.internal (compute2.nyi.internal [10.202.2.42]) by mailout.west.internal (Postfix) with ESMTP id 596D387F for ; Tue, 31 Mar 2020 07:42:21 -0400 (EDT) Original-Received: from mailfrontend1 ([10.202.2.162]) by compute2.internal (MEProxy); Tue, 31 Mar 2020 07:42:21 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=warpmail.net; h= from:to:subject:in-reply-to:date:message-id:mime-version :content-type; s=fm3; bh=ppj8cDvbg3KKxU4IoyH+sbkyzZH7W35szfXmZUH QM44=; b=Ecv1wz0HofGz6l571m06lSh6tMCaoTvLmYcRJWU35F3CNNpBYBCMJ9Y Bs74kns7fvFSlu4vV9wiSUlV7vNyC+TyAA3bJ0LtRKKiszddoWLRQ7D+OTlLgK06 WEevR/hB2f8A89NcnTB3D5/EFau9SzGz0knyoTJw/dvQ1feHB1qRAYi1MTJ7LsWW qTK/YVAJ7Sosq7kJKSgXJlQio74c0ZhOai1/hh4SoEs90tJbqz3NQBny1VX9LgBB KHisI5Su1Vi8E2y9a6f4cpd+BkVMttjYJ42EUzG2kgu0Pup9QSkS2pJXjIFfE7ko 3zDDUpP8ww3sj0RUarodbqUoj8+O0NQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-type:date:from:in-reply-to :message-id:mime-version:subject:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=ppj8cDvbg3KKxU4Io yH+sbkyzZH7W35szfXmZUHQM44=; b=xU3uCnvSc7XQQ8OxyxJ87wHKoBKCC6gTo d4yQmJHbGPYLqCaZlM1lKWWcR7qe6gwJxgU6S1wmd+6sYXuERzlBDv/hwqb11BPC GKtMTXOk556AklgYOcJ7qOA+UOSodYdlAh5rllffJNgqY5VCF020emYQMlny1BYZ dGDKQFU2Ukt9AVgy2xoQUWtxdQn4xH3y/ozByg5SY1/KE5k2GjHEc6phKghZ0Tyv sWPjiwuuUriZcUQchVCvDcaVV2GF+5WqdnRS32q5iSTsKSc8ZoXnsMkp4sCpJ7cp Zf9iz4jfwGTkujn15T4/BbcNnJ/fCpRnNFedrYLLuOO0EvB9w2y7A== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduhedrtddtgddutdcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecunecujfgurhephffvufgjfffkgggtsehmtderredttd dtnecuhfhrohhmpehphhhilhhiphesfigrrhhpmhgrihhlrdhnvghtucdlrfhhihhlihhp ucfmrddmnecukfhppeejledrvdduledrudelledrudeftdenucevlhhushhtvghrufhiii gvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehphhhilhhiphesfigrrhhpmhgrihhl rdhnvght X-ME-Proxy: Original-Received: from localhost (p4fdbc782.dip0.t-ipconnect.de [79.219.199.130]) by mail.messagingengine.com (Postfix) with ESMTPA id 7E49E3280065 for ; Tue, 31 Mar 2020 07:42:20 -0400 (EDT) In-Reply-To: <87lfnpsb0b.fsf@bulbul> (philip@warpmail.net) Autocrypt: addr=philip@warpmail.net; prefer-encrypt=mutual; keydata= mDMEXmoX2BYJKwYBBAHaRw8BAQdA/YuGV2Dy6UZtMk+KfA7Nnlj38k22KQ/+sXVa2ttGaHm0O1Bo aWxpcCBLLiAoZ2VuZXJhdGVkIGJ5IGF1dG9jcnlwdC5lbCkgPHBoaWxpcEB3YXJwbWFpbC5uZXQ+ iJAEExYIADgWIQSx6XCgW+bhLMR4DmPClWiPoyRe6AUCXmoX2AIbAwULCQgHAgYVCgkICwIEFgID AQIeAQIXgAAKCRDClWiPoyRe6IDNAP0eOlYoYIGVcCinuewkEb762T6lqA2hIHcNeRyX8LyQ8gEA krv4pIlZNLF+nBvaCtKvCPZF9428X50CvNvRHCNqgwe4OAReahfYEgorBgEEAZdVAQUBAQdACM09 XXce+t9YpOLbLdESh5g4RTY/0Zys7U1FapIZLA4DAQgHiHgEGBYIACAWIQSx6XCgW+bhLMR4DmPC lWiPoyRe6AUCXmoX2AIbDAAKCRDClWiPoyRe6LYcAQDn15KGn/E9MqloWN4mBJ7/tepYIk2IxrkZ imC531HsIAD+LKQYjorQosVe1oym0VZjmgKsem1kJZHG2tY601I+QwM= X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 64.147.123.21 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:246087 Archived-At: --=-=-= Content-Type: text/plain I was re-reading the libravatar specification today, and noticed that I had missed a section that described what to do if a DNS query results in multiple records. The current implementation just used a "simple" (dns-query ...) but that is explicit not sufficient. Instead one must choose the highest priority servers and then randomly select one of these, according to their weights. The patch I have attached to this mail implements this, and can be tested by evaluating (gravatar--service-libravatar "test@pusling.com") It's rather hard to find a domain with multiple libravatar entries, but the selection algorithm can be tested using this slightly modified excerpt from the patch: (defun test-selection () (catch 'found ;; res is a hardcoded and simplified (dns-query "..." t) result. ;; "target" would usually point to a domain. (let* ((res '(((target alpha) (priority 10) (weight 10)) ((target beta) (priority 10) (weight 0)) ((target gamma) (priority 10) (weight 30)) ((target delta) (priority 5) (weight 10)))) (prio (mapcar (lambda (r) (dns-get 'priority r)) res)) (max (apply #'max prio)) (sum 0) top) (dolist (rec res) (when (= max (dns-get 'priority rec)) (setq sum (+ sum (dns-get 'weight rec))) (push rec top))) (dolist (rec top) (when (<= (if (= 0 sum) -1 (random sum)) (dns-get 'weight rec)) (throw 'found (dns-get 'target rec))) (setq sum (- sum (dns-get 'weight rec))))))) (/ (cl-loop repeat 1000 count (eq (test-selection) 'gamma)) 1000.0) where the last expression should approximately equal 0.75 (3/4), because only the records alpha, beta and gamma have the highest priority (10), and between them gamma has a 30/(10 + 30) = 3/4'th chance of being chosen. In case all records are weighted 0, the first one is chosen. I once again apologise for not thoroughly enough testing my last patch, and hope this at least somewhat makes up for it. -- Philip K. --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=0001-Fix-Libravatar-federation-handling.patch >From 6191aa6577db1056bb74a4359d87a119e9517aca Mon Sep 17 00:00:00 2001 From: Philip K Date: Tue, 31 Mar 2020 13:23:09 +0200 Subject: [PATCH] Fix Libravatar federation handling Previous implementation didn't correctly handle the result of dns-query, and didn't implement the necessary record selection algorithm as specified on the wiki (https://wiki.libravatar.org/api/, "Federated servers"). * lisp/image/gravatar.el (gravatar--service-libravatar): Fix libravatar image host resolver algorithm. --- lisp/image/gravatar.el | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/lisp/image/gravatar.el b/lisp/image/gravatar.el index ff59a72ac8..d800e5dc55 100644 --- a/lisp/image/gravatar.el +++ b/lisp/image/gravatar.el @@ -149,12 +149,33 @@ gravatar--service-libravatar (dolist (record '(("_avatars-sec" . "https") ("_avatars" . "http"))) (let* ((query (concat (car record) "._tcp." domain)) - (result (dns-query query 'SRV))) + (result (dns-query query 'SRV t))) (when result - (throw 'found (format "%s://%s/avatar" - (cdr record) - result))))) - "https://seccdn.libravatar.org/avatar"))))) + (let* ((res (mapcar (lambda (rec) + (dns-get 'data (cdr rec))) + (dns-get 'answers result))) + (prio + (mapcar (lambda (r) (dns-get 'priority r)) res)) + (max (apply #'max prio)) + (sum 0) top) + ;; Attempt to find all records with the same maximal + ;; priority, and calculate the sum of their weights. + (dolist (rec res) + (when (= max (dns-get 'priority rec)) + (setq sum (+ sum (dns-get 'weight rec))) + (push rec top))) + ;; In case there is more than one maximal priority + ;; record, choose one at random, while taking the + ;; individual record weights into consideration. + (dolist (rec top) + (when (<= (if (= 0 sum) -1 (random sum)) + (dns-get 'weight rec)) + (throw 'found (format "%s://%s:%s/avatar" + (cdr record) + (dns-get 'target rec) + (dns-get 'port rec)))) + (setq sum (- sum (dns-get 'weight rec))))))) + "https://seccdn.libravatar.org/avatar")))))) (defun gravatar-hash (mail-address) "Return the Gravatar hash for MAIL-ADDRESS." -- 2.20.1 --=-=-=--