Patchwork D3232: httppeer: move requestbuilder defaults into makepeer() argument

login
register
mail settings
Submitter phabricator
Date April 11, 2018, 1:29 a.m.
Message ID <differential-rev-PHID-DREV-3i5c4gukdhg2pwtyb2xy-req@phab.mercurial-scm.org>
Download mbox | patch
Permalink /patch/30665/
State Superseded
Headers show

Comments

phabricator - April 11, 2018, 1:29 a.m.
indygreg created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  Upcoming commits will move the initial ?cmd=capabilities handshake
  request out of httppeer so the handshake can be performed before a
  peer instance is constructed. In order to do this, we'll need to
  refactor code for making HTTP requests.
  
  The type used to construct HTTP requests is configurable. If we'll
  be making HTTP requests outside of httppeer, we should be able to
  use a custom request builder. So move the definition of that type
  into makepeer().
  
  Extensions can monkeypatch the function and override the argument
  value.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D3232

AFFECTED FILES
  mercurial/debugcommands.py
  mercurial/httppeer.py
  tests/test-check-interfaces.py

CHANGE DETAILS




To: indygreg, #hg-reviewers
Cc: mercurial-devel

Patch

diff --git a/tests/test-check-interfaces.py b/tests/test-check-interfaces.py
--- a/tests/test-check-interfaces.py
+++ b/tests/test-check-interfaces.py
@@ -70,7 +70,7 @@ 
 # Facilitates testing sshpeer without requiring an SSH server.
 class badpeer(httppeer.httppeer):
     def __init__(self):
-        super(badpeer, self).__init__(None, None, None, dummyopener())
+        super(badpeer, self).__init__(None, None, None, dummyopener(), None)
         self.badattribute = True
 
     def badmethod(self):
@@ -89,7 +89,7 @@ 
 
     ziverify.verifyClass(repository.ipeerbaselegacycommands,
                          httppeer.httppeer)
-    checkzobject(httppeer.httppeer(None, None, None, dummyopener()))
+    checkzobject(httppeer.httppeer(None, None, None, dummyopener(), None))
 
     ziverify.verifyClass(repository.ipeerbase,
                          localrepo.localpeer)
diff --git a/mercurial/httppeer.py b/mercurial/httppeer.py
--- a/mercurial/httppeer.py
+++ b/mercurial/httppeer.py
@@ -139,15 +139,13 @@ 
         self._index = 0
 
 class httppeer(wireproto.wirepeer):
-    def __init__(self, ui, path, url, opener):
+    def __init__(self, ui, path, url, opener, requestbuilder):
         self.ui = ui
         self._path = path
         self._url = url
         self._caps = None
         self._urlopener = opener
-        # This is an its own attribute to facilitate extensions overriding
-        # the default type.
-        self._requestbuilder = urlreq.request
+        self._requestbuilder = requestbuilder
 
     def __del__(self):
         for h in self._urlopener.handlers:
@@ -570,7 +568,12 @@ 
 
         return results
 
-def makepeer(ui, path):
+def makepeer(ui, path, requestbuilder=urlreq.request):
+    """Construct an appropriate HTTP peer instance.
+
+    ``requestbuilder`` is the type used for constructing HTTP requests.
+    It exists as an argument so extensions can override the default.
+    """
     u = util.url(path)
     if u.query or u.fragment:
         raise error.Abort(_('unsupported URL component: "%s"') %
@@ -582,7 +585,7 @@ 
 
     opener = urlmod.opener(ui, authinfo)
 
-    return httppeer(ui, path, url, opener)
+    return httppeer(ui, path, url, opener, requestbuilder)
 
 def instance(ui, path, create):
     if create:
diff --git a/mercurial/debugcommands.py b/mercurial/debugcommands.py
--- a/mercurial/debugcommands.py
+++ b/mercurial/debugcommands.py
@@ -2915,7 +2915,8 @@ 
             raise error.Abort(_('--peer %s not supported with HTTP peers') %
                               opts['peer'])
         else:
-            peer = httppeer.httppeer(ui, path, url, opener)
+            peer = httppeer.httppeer(ui, path, url, opener,
+                                     requestbuilder=httppeer.urlreq.request)
             peer._fetchcaps()
 
         # We /could/ populate stdin/stdout with sock.makefile()...