~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transport/http.py

  • Committer: James Henstridge
  • Date: 2006-02-10 06:40:25 UTC
  • mto: (1185.50.83 bzr-jam-integration)
  • mto: This revision was merged to the branch mainline in revision 1562.
  • Revision ID: james.henstridge@canonical.com-20060210064025-eb9506a367a93ff2
Make HTTP transport has() method do HEAD requests, and update test to
verify that this is the case.

Show diffs side-by-side

added added

removed removed

Lines of Context:
61
61
            password_manager.add_password(None, host, username, password)
62
62
    url = scheme + '//' + host + port + path
63
63
    return url
64
 
    
65
 
def get_url(url):
 
64
 
 
65
class Request(urllib2.Request):
 
66
    """Request object for urllib2 that allows the method to be overridden."""
 
67
 
 
68
    method = None
 
69
 
 
70
    def get_method(self):
 
71
        if self.method is not None:
 
72
            return self.method
 
73
        else:
 
74
            return urllib2.Request.get_method(self)
 
75
 
 
76
def get_url(url, method=None):
66
77
    import urllib2
67
78
    mutter("get_url %s" % url)
68
79
    manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
70
81
    auth_handler = urllib2.HTTPBasicAuthHandler(manager)
71
82
    opener = urllib2.build_opener(auth_handler)
72
83
 
73
 
    request = urllib2.Request(url)
 
84
    request = Request(url)
 
85
    request.method = method
74
86
    request.add_header('User-Agent', 'bzr/%s' % bzrlib.__version__)
75
87
    response = opener.open(request)
76
88
    return response
150
162
    def has(self, relpath):
151
163
        """Does the target location exist?
152
164
 
153
 
        TODO: HttpTransport.has() should use a HEAD request,
154
 
        not a full GET request.
155
 
 
156
165
        TODO: This should be changed so that we don't use
157
166
        urllib2 and get an exception, the code path would be
158
167
        cleaner if we just do an http HEAD request, and parse
161
170
        path = relpath
162
171
        try:
163
172
            path = self.abspath(relpath)
164
 
            f = get_url(path)
 
173
            f = get_url(path, method='HEAD')
165
174
            # Without the read and then close()
166
175
            # we tend to have busy sockets.
167
176
            f.read()