~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transport/http/_urllib.py

  • Committer: Martin Pool
  • Date: 2006-03-06 11:20:10 UTC
  • mfrom: (1593 +trunk)
  • mto: This revision was merged to the branch mainline in revision 1611.
  • Revision ID: mbp@sourcefrog.net-20060306112010-17c0170dde5d1eea
[merge] large merge to sync with bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
import urllib, urllib2
18
18
 
 
19
import bzrlib  # for the version
19
20
from bzrlib.errors import BzrError
20
21
from bzrlib.trace import mutter
21
22
from bzrlib.transport.http import HttpTransportBase, extract_auth, HttpServer
23
24
                           TransportError, ConnectionError)
24
25
 
25
26
 
 
27
class Request(urllib2.Request):
 
28
    """Request object for urllib2 that allows the method to be overridden."""
 
29
 
 
30
    method = None
 
31
 
 
32
    def get_method(self):
 
33
        if self.method is not None:
 
34
            return self.method
 
35
        else:
 
36
            return urllib2.Request.get_method(self)
 
37
 
 
38
 
26
39
class HttpTransport(HttpTransportBase):
27
40
    """Python urllib transport for http and https.
28
41
    """
33
46
        """Set the base path where files will be stored."""
34
47
        super(HttpTransport, self).__init__(base)
35
48
 
36
 
    def _get_url(self, url):
 
49
    def _get_url(self, url, method=None):
37
50
        mutter("get_url %s" % url)
38
51
        manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
39
52
        url = extract_auth(url, manager)
40
53
        auth_handler = urllib2.HTTPBasicAuthHandler(manager)
41
54
        opener = urllib2.build_opener(auth_handler)
42
 
        url_f = opener.open(url)
43
 
        return url_f
 
55
        request = Request(url)
 
56
        request.method = method
 
57
        request.add_header('User-Agent', 'bzr/%s' % bzrlib.__version__)
 
58
        response = opener.open(request)
 
59
        return response
44
60
 
45
61
    def should_cache(self):
46
62
        """Return True if the data pulled across should be cached locally.
49
65
 
50
66
    def has(self, relpath):
51
67
        """Does the target location exist?
52
 
 
53
 
        TODO: HttpTransport.has() should use a HEAD request,
54
 
        not a full GET request.
55
 
 
56
 
        TODO: This should be changed so that we don't use
57
 
        urllib2 and get an exception, the code path would be
58
 
        cleaner if we just do an http HEAD request, and parse
59
 
        the return code.
60
68
        """
61
69
        path = relpath
62
70
        try:
63
71
            path = self.abspath(relpath)
64
 
            f = self._get_url(path)
 
72
            f = self._get_url(path, 'HEAD')
65
73
            # Without the read and then close()
66
74
            # we tend to have busy sockets.
67
75
            f.read()