~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Martin Pool
  • Date: 2006-03-06 09:44:05 UTC
  • mto: This revision was merged to the branch mainline in revision 1611.
  • Revision ID: mbp@sourcefrog.net-20060306094405-12f57849f4e01cb4
Curl should follow http redirects, the same as urllib

Show diffs side-by-side

added added

removed removed

Lines of Context:
55
55
 
56
56
    def has(self, relpath):
57
57
        self.curl = pycurl.Curl()
58
 
 
59
58
        abspath = self.abspath(relpath)
60
59
        if isinstance(abspath, unicode):
61
60
            abspath = abspath.encode('ascii', 'strict')
62
 
        #     raise ValueError("paths passed to Transport must be plain strings: "
63
 
        #             + `abspath`)
64
61
        self.curl.setopt(pycurl.URL, abspath)
65
 
        self._set_curl_cache_headers()
 
62
        self.curl.setopt(pycurl.FOLLOWLOCATION, 1) # follow redirect responses
 
63
        self._set_curl_options()
66
64
        # don't want the body - ie just do a HEAD request
67
65
        self.curl.setopt(pycurl.NOBODY, 1)
68
 
 
69
66
        self._curl_perform()
70
 
 
71
67
        try:
72
68
            code = self.curl.getinfo(pycurl.HTTP_CODE)
73
69
            if code == 404: # not found
84
80
        self.curl = pycurl.Curl()
85
81
        abspath = self.abspath(relpath)
86
82
        sio = StringIO()
87
 
        # pycurl needs plain ascii
88
83
        if isinstance(abspath, unicode):
89
 
            # XXX: HttpTransportBase.abspath should probably url-escape
90
 
            # unicode characters if any in the path - domain name must be
91
 
            # IDNA-escaped
92
84
            abspath = abspath.encode('ascii')
93
85
        self.curl.setopt(pycurl.URL, abspath)
94
 
        ## self.curl.setopt(pycurl.VERBOSE, 1)
95
 
        self._set_curl_cache_headers()
 
86
        self._set_curl_options(self.curl)
96
87
        self.curl.setopt(pycurl.WRITEFUNCTION, sio.write)
97
88
        self.curl.setopt(pycurl.NOBODY, 0)
98
 
 
99
89
        self._curl_perform()
100
 
 
101
90
        code = self.curl.getinfo(pycurl.HTTP_CODE)
102
91
        if code == 404:
103
92
            raise NoSuchFile(abspath)
104
 
        elif not 200 <= code <= 399:
 
93
        elif code == 200:
 
94
            sio.seek(0)
 
95
            del self.curl
 
96
            return sio
 
97
        else:
105
98
            raise TransportError('http error %d acccessing %s' % 
106
99
                    (code, self.curl.getinfo(pycurl.EFFECTIVE_URL)))
107
 
        sio.seek(0)
108
 
        del self.curl
109
 
        return sio
110
100
 
111
 
    def _set_curl_cache_headers(self):
 
101
    def _set_curl_options(self, curl):
 
102
        """Set options for all requests"""
112
103
        # There's no way in http/1.0 to say "must revalidate"; we don't want
113
104
        # to force it to always retrieve.  so just turn off the default Pragma
114
105
        # provided by Curl.
115
106
        headers = ['Cache-control: must-revalidate',
116
107
                   'Pragma:']
117
 
        self.curl.setopt(pycurl.HTTPHEADER, headers)
 
108
        ## self.curl.setopt(pycurl.VERBOSE, 1)
 
109
        curl.setopt(pycurl.HTTPHEADER, headers)
 
110
        curl.setopt(pycurl.FOLLOWLOCATION, 1) # follow redirect responses
118
111
 
119
112
    def _curl_perform(self):
120
113
        """Perform curl operation and translate exceptions."""