statichttprepo: handle remote not supporting Range headers
authorPatrick Mezard <pmezard@gmail.com>
Sun, 24 May 2009 18:31:01 +0200
changeset 8612 e10e984bea46
parent 8611 ba42e3c6e602
child 8613 4dea46d4e3f8
statichttprepo: handle remote not supporting Range headers - If remote does not support Range header, 200 is answered instead of 206. The HTTPRangeHandler left these responses unchanged, so the data has to be sliced by the receiver. - httprangereader file pointer was not updated.
mercurial/statichttprepo.py
--- a/mercurial/statichttprepo.py	Sun May 24 18:30:59 2009 +0200
+++ b/mercurial/statichttprepo.py	Sun May 24 18:31:01 2009 +0200
@@ -30,14 +30,31 @@
         try:
             f = self.opener.open(req)
             data = f.read()
+            if hasattr(f, 'getcode'):
+                # python 2.6+
+                code = f.getcode()
+            elif hasattr(f, 'code'):
+                # undocumented attribute, seems to be set in 2.4 and 2.5
+                code = f.code
+            else:
+                # Don't know how to check, hope for the best.
+                code = 206
         except urllib2.HTTPError, inst:
             num = inst.code == 404 and errno.ENOENT or None
             raise IOError(num, inst)
         except urllib2.URLError, inst:
             raise IOError(None, inst.reason[1])
 
-        if bytes:
+        if code == 200:
+            # HTTPRangeHandler does nothing if remote does not support
+            # Range headers and returns the full entity. Let's slice it.
+            if bytes:
+                data = data[self.pos:self.pos + bytes]
+            else:
+                data = data[self.pos:]
+        elif bytes:
             data = data[:bytes]
+        self.pos += len(data)
         return data
 
 def build_opener(ui, authinfo):