Patchwork D3233: httppeer: extract code for performing an HTTP request

login
register
mail settings
Submitter phabricator
Date April 11, 2018, 5 p.m.
Message ID <5726843d5b2bc86d63927c2c62925035@localhost.localdomain>
Download mbox | patch
Permalink /patch/30717/
State Not Applicable
Headers show

Comments

phabricator - April 11, 2018, 5 p.m.
This revision was automatically updated to reflect the committed changes.
Closed by commit rHG8e7a4435ab6d: httppeer: extract code for performing an HTTP request (authored by indygreg, committed by ).

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D3233?vs=7951&id=7994

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

AFFECTED FILES
  mercurial/httppeer.py

CHANGE DETAILS




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

Patch

diff --git a/mercurial/httppeer.py b/mercurial/httppeer.py
--- a/mercurial/httppeer.py
+++ b/mercurial/httppeer.py
@@ -138,6 +138,46 @@ 
             f.seek(0)
         self._index = 0
 
+def sendrequest(ui, opener, req):
+    """Send a prepared HTTP request.
+
+    Returns the response object.
+    """
+    if (ui.debugflag
+        and ui.configbool('devel', 'debug.peer-request')):
+        dbg = ui.debug
+        line = 'devel-peer-request: %s\n'
+        dbg(line % '%s %s' % (req.get_method(), req.get_full_url()))
+        hgargssize = None
+
+        for header, value in sorted(req.header_items()):
+            if header.startswith('X-hgarg-'):
+                if hgargssize is None:
+                    hgargssize = 0
+                hgargssize += len(value)
+            else:
+                dbg(line % '  %s %s' % (header, value))
+
+        if hgargssize is not None:
+            dbg(line % '  %d bytes of commands arguments in headers'
+                % hgargssize)
+
+        if req.has_data():
+            data = req.get_data()
+            length = getattr(data, 'length', None)
+            if length is None:
+                length = len(data)
+            dbg(line % '  %d bytes of data' % length)
+
+        start = util.timer()
+
+    res = opener.open(req)
+    if ui.configbool('devel', 'debug.peer-request'):
+        dbg(line % '  finished in %.4f seconds (%s)'
+            % (util.timer() - start, res.code))
+
+    return res
+
 class httppeer(wireproto.wirepeer):
     def __init__(self, ui, path, url, opener, requestbuilder):
         self.ui = ui
@@ -152,41 +192,6 @@ 
             h.close()
             getattr(h, "close_all", lambda: None)()
 
-    def _openurl(self, req):
-        if (self.ui.debugflag
-            and self.ui.configbool('devel', 'debug.peer-request')):
-            dbg = self.ui.debug
-            line = 'devel-peer-request: %s\n'
-            dbg(line % '%s %s' % (req.get_method(), req.get_full_url()))
-            hgargssize = None
-
-            for header, value in sorted(req.header_items()):
-                if header.startswith('X-hgarg-'):
-                    if hgargssize is None:
-                        hgargssize = 0
-                    hgargssize += len(value)
-                else:
-                    dbg(line % '  %s %s' % (header, value))
-
-            if hgargssize is not None:
-                dbg(line % '  %d bytes of commands arguments in headers'
-                    % hgargssize)
-
-            if req.has_data():
-                data = req.get_data()
-                length = getattr(data, 'length', None)
-                if length is None:
-                    length = len(data)
-                dbg(line % '  %d bytes of data' % length)
-
-            start = util.timer()
-
-        ret = self._urlopener.open(req)
-        if self.ui.configbool('devel', 'debug.peer-request'):
-            dbg(line % '  finished in %.4f seconds (%s)'
-                % (util.timer() - start, ret.code))
-        return ret
-
     # Begin of ipeerconnection interface.
 
     def url(self):
@@ -322,7 +327,7 @@ 
             self.ui.debug("sending %d bytes\n" % size)
             req.add_unredirected_header(r'Content-Length', r'%d' % size)
         try:
-            resp = self._openurl(req)
+            resp = sendrequest(self.ui, self._urlopener, req)
         except urlerr.httperror as inst:
             if inst.code == 401:
                 raise error.Abort(_('authorization failed'))