diff mercurial/statichttprepo.py @ 36455:24c2c760c1cb

statichttprepo: move HTTPRangeHandler from byterange and delete the latter It turns out we've been toting around 472 lines of Python just for this 20-ish line class that teaches urllib how to handle '206 Partial Content'. byterange.py showed up in my \sstr\( Python 3 dragnet, and found itself having overstayed its welcome in our codebase. # no-check-commit because we're moving code that has to have foo_bar naming. Differential Revision: https://phab.mercurial-scm.org/D2443
author Augie Fackler <augie@google.com>
date Sun, 25 Feb 2018 23:34:58 -0500
parents c752fbe228fb
children c77c925987d7
line wrap: on
line diff
--- a/mercurial/statichttprepo.py	Sun Feb 25 23:09:58 2018 -0500
+++ b/mercurial/statichttprepo.py	Sun Feb 25 23:34:58 2018 -0500
@@ -13,7 +13,6 @@
 
 from .i18n import _
 from . import (
-    byterange,
     changelog,
     error,
     localrepo,
@@ -82,10 +81,36 @@
     def close(self):
         pass
 
+# _RangeError and _HTTPRangeHandler were originally in byterange.py,
+# which was itself extracted from urlgrabber. See the last version of
+# byterange.py from history if you need more information.
+class _RangeError(IOError):
+    """Error raised when an unsatisfiable range is requested."""
+
+class _HTTPRangeHandler(urlreq.basehandler):
+    """Handler that enables HTTP Range headers.
+
+    This was extremely simple. The Range header is a HTTP feature to
+    begin with so all this class does is tell urllib2 that the
+    "206 Partial Content" response from the HTTP server is what we
+    expected.
+    """
+
+    def http_error_206(self, req, fp, code, msg, hdrs):
+        # 206 Partial Content Response
+        r = urlreq.addinfourl(fp, hdrs, req.get_full_url())
+        r.code = code
+        r.msg = msg
+        return r
+
+    def http_error_416(self, req, fp, code, msg, hdrs):
+        # HTTP's Range Not Satisfiable error
+        raise _RangeError('Requested Range Not Satisfiable')
+
 def build_opener(ui, authinfo):
     # urllib cannot handle URLs with embedded user or passwd
     urlopener = url.opener(ui, authinfo)
-    urlopener.add_handler(byterange.HTTPRangeHandler())
+    urlopener.add_handler(_HTTPRangeHandler())
 
     class statichttpvfs(vfsmod.abstractvfs):
         def __init__(self, base):