changeset 7274:95f3694cc5a4

statichttprepo: cleanups, use url.py (proxy, password support) - unify httprangereader.py and statichttprepo.py:rangegreader() - build the opener from url.py, that allows use to puse username and password in the url, to follow the proxy settings from hgrc, etc.
author Benoit Boissinot <benoit.boissinot@ens-lyon.org>
date Tue, 28 Oct 2008 08:26:18 +0100
parents 84f807918864
children c00cdac22d3c
files mercurial/httprangereader.py mercurial/statichttprepo.py
diffstat 2 files changed, 39 insertions(+), 40 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/httprangereader.py	Tue Oct 28 08:18:11 2008 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,28 +0,0 @@
-# httprangereader.py - just what it says
-#
-# Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
-#
-# This software may be used and distributed according to the terms
-# of the GNU General Public License, incorporated herein by reference.
-
-import byterange, urllib2
-
-class httprangereader(object):
-    def __init__(self, url):
-        self.url = url
-        self.pos = 0
-    def seek(self, pos):
-        self.pos = pos
-    def read(self, bytes=None):
-        opener = urllib2.build_opener(byterange.HTTPRangeHandler())
-        urllib2.install_opener(opener)
-        req = urllib2.Request(self.url)
-        end = ''
-        if bytes:
-            end = self.pos + bytes - 1
-        req.add_header('Range', 'bytes=%d-%s' % (self.pos, end))
-        f = urllib2.urlopen(req)
-        data = f.read()
-        if bytes:
-            data = data[:bytes]
-        return data
--- a/mercurial/statichttprepo.py	Tue Oct 28 08:18:11 2008 +0100
+++ b/mercurial/statichttprepo.py	Tue Oct 28 08:26:18 2008 +0100
@@ -8,34 +8,61 @@
 # of the GNU General Public License, incorporated herein by reference.
 
 from i18n import _
-import changelog, httprangereader
+import changelog, byterange, url
 import repo, localrepo, manifest, util, store
 import urllib, urllib2, errno
 
-class rangereader(httprangereader.httprangereader):
-    def read(self, size=None):
+class httprangereader(object):
+    def __init__(self, url, opener):
+        # we assume opener has HTTPRangeHandler
+        self.url = url
+        self.pos = 0
+        self.opener = opener
+    def seek(self, pos):
+        self.pos = pos
+    def read(self, bytes=None):
+        req = urllib2.Request(self.url)
+        end = ''
+        if bytes:
+            end = self.pos + bytes - 1
+        req.add_header('Range', 'bytes=%d-%s' % (self.pos, end))
+
         try:
-            return httprangereader.httprangereader.read(self, size)
+            f = self.opener.open(req)
+            data = f.read()
         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])
 
-def opener(base):
-    """return a function that opens files over http"""
-    p = base
-    def o(path, mode="r"):
-        f = "/".join((p, urllib.quote(path)))
-        return rangereader(f)
-    return o
+        if bytes:
+            data = data[:bytes]
+        return data
+
+def build_opener(ui, authinfo):
+    # urllib cannot handle URLs with embedded user or passwd
+    urlopener = url.opener(ui, authinfo)
+    urlopener.add_handler(byterange.HTTPRangeHandler())
+
+    def opener(base):
+        """return a function that opens files over http"""
+        p = base
+        def o(path, mode="r"):
+            f = "/".join((p, urllib.quote(path)))
+            return httprangereader(f, urlopener)
+        return o
+
+    return opener
 
 class statichttprepository(localrepo.localrepository):
     def __init__(self, ui, path):
         self._url = path
         self.ui = ui
 
-        self.path = path.rstrip('/') + "/.hg"
+        self.path, authinfo = url.getauthinfo(path.rstrip('/') + "/.hg")
+
+        opener = build_opener(ui, authinfo)
         self.opener = opener(self.path)
 
         # find requirements