Patchwork [1,of,6] keepalive: send HTTP request headers in a deterministic order

login
register
mail settings
Submitter Gregory Szorc
Date April 14, 2017, 7:44 a.m.
Message ID <c56b03b176c65a460e28.1492155845@ubuntu-vm-main>
Download mbox | patch
Permalink /patch/20194/
State Accepted
Headers show

Comments

Gregory Szorc - April 14, 2017, 7:44 a.m.
# HG changeset patch
# User Gregory Szorc <gregory.szorc@gmail.com>
# Date 1492131878 25200
#      Thu Apr 13 18:04:38 2017 -0700
# Node ID c56b03b176c65a460e282956827f5f8ab6890283
# Parent  f23d579a5a04e44f5e0370ba13ad20dd626e8ad7
keepalive: send HTTP request headers in a deterministic order

An upcoming patch will add low-level testing of the bytes being sent
over the wire. As part of developing that test, I discovered that the
order of headers in HTTP requests wasn't deterministic. This patch
makes the order deterministic to make things easier to test.

Patch

diff --git a/mercurial/keepalive.py b/mercurial/keepalive.py
--- a/mercurial/keepalive.py
+++ b/mercurial/keepalive.py
@@ -298,11 +298,12 @@  class KeepAliveHandler(object):
 
     def _start_transaction(self, h, req):
         # What follows mostly reimplements HTTPConnection.request()
-        # except it adds self.parent.addheaders in the mix.
-        headers = dict(self.parent.addheaders)
-        headers.update(req.headers)
-        headers.update(req.unredirected_hdrs)
-        headers = dict((n.lower(), v) for n, v in headers.items())
+        # except it adds self.parent.addheaders in the mix and sends headers
+        # in a deterministic order (to make testing easier).
+        headers = util.sortdict(self.parent.addheaders)
+        headers.update(sorted(req.headers.items()))
+        headers.update(sorted(req.unredirected_hdrs.items()))
+        headers = util.sortdict((n.lower(), v) for n, v in headers.items())
         skipheaders = {}
         for n in ('host', 'accept-encoding'):
             if n in headers: