~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:49:22 UTC
  • mto: This revision was merged to the branch mainline in revision 1611.
  • Revision ID: mbp@sourcefrog.net-20060306094922-6a0ccdd7d6637cda
[pycurl] Make Curl instance a local variable not a long-lived object.

There seems no value in keeping it around and in fact it was not retained for
any time.

Show diffs side-by-side

added added

removed removed

Lines of Context:
54
54
        return True
55
55
 
56
56
    def has(self, relpath):
57
 
        self.curl = pycurl.Curl()
 
57
        curl = pycurl.Curl()
58
58
        abspath = self.abspath(relpath)
59
59
        if isinstance(abspath, unicode):
60
60
            abspath = abspath.encode('ascii', 'strict')
61
 
        self.curl.setopt(pycurl.URL, abspath)
62
 
        self.curl.setopt(pycurl.FOLLOWLOCATION, 1) # follow redirect responses
63
 
        self._set_curl_options()
 
61
        curl.setopt(pycurl.URL, abspath)
 
62
        curl.setopt(pycurl.FOLLOWLOCATION, 1) # follow redirect responses
 
63
        self._set_curl_options(curl)
64
64
        # don't want the body - ie just do a HEAD request
65
 
        self.curl.setopt(pycurl.NOBODY, 1)
66
 
        self._curl_perform()
67
 
        try:
68
 
            code = self.curl.getinfo(pycurl.HTTP_CODE)
69
 
            if code == 404: # not found
70
 
                return False
71
 
            elif code in (200, 302): # "ok", "found"
72
 
                return True
73
 
            else:
74
 
                raise TransportError('http error %d probing for %s' %
75
 
                        (code, self.curl.getinfo(pycurl.EFFECTIVE_URL)))
76
 
        finally:
77
 
            del self.curl
 
65
        curl.setopt(pycurl.NOBODY, 1)
 
66
        self._curl_perform(curl)
 
67
        code = curl.getinfo(pycurl.HTTP_CODE)
 
68
        if code == 404: # not found
 
69
            return False
 
70
        elif code in (200, 302): # "ok", "found"
 
71
            return True
 
72
        else:
 
73
            raise TransportError('http error %d probing for %s' %
 
74
                    (code, curl.getinfo(pycurl.EFFECTIVE_URL)))
78
75
        
79
76
    def get(self, relpath):
80
 
        self.curl = pycurl.Curl()
 
77
        curl = pycurl.Curl()
81
78
        abspath = self.abspath(relpath)
82
79
        sio = StringIO()
83
80
        if isinstance(abspath, unicode):
84
81
            abspath = abspath.encode('ascii')
85
 
        self.curl.setopt(pycurl.URL, abspath)
86
 
        self._set_curl_options(self.curl)
87
 
        self.curl.setopt(pycurl.WRITEFUNCTION, sio.write)
88
 
        self.curl.setopt(pycurl.NOBODY, 0)
89
 
        self._curl_perform()
90
 
        code = self.curl.getinfo(pycurl.HTTP_CODE)
 
82
        curl.setopt(pycurl.URL, abspath)
 
83
        self._set_curl_options(curl)
 
84
        curl.setopt(pycurl.WRITEFUNCTION, sio.write)
 
85
        curl.setopt(pycurl.NOBODY, 0)
 
86
        self._curl_perform(curl)
 
87
        code = curl.getinfo(pycurl.HTTP_CODE)
91
88
        if code == 404:
92
89
            raise NoSuchFile(abspath)
93
90
        elif code == 200:
94
91
            sio.seek(0)
95
 
            del self.curl
96
92
            return sio
97
93
        else:
98
94
            raise TransportError('http error %d acccessing %s' % 
99
 
                    (code, self.curl.getinfo(pycurl.EFFECTIVE_URL)))
 
95
                    (code, curl.getinfo(pycurl.EFFECTIVE_URL)))
100
96
 
101
97
    def _set_curl_options(self, curl):
102
98
        """Set options for all requests"""
105
101
        # provided by Curl.
106
102
        headers = ['Cache-control: must-revalidate',
107
103
                   'Pragma:']
108
 
        ## self.curl.setopt(pycurl.VERBOSE, 1)
 
104
        ## curl.setopt(pycurl.VERBOSE, 1)
109
105
        curl.setopt(pycurl.HTTPHEADER, headers)
110
106
        curl.setopt(pycurl.FOLLOWLOCATION, 1) # follow redirect responses
111
107
 
112
 
    def _curl_perform(self):
 
108
    def _curl_perform(self, curl):
113
109
        """Perform curl operation and translate exceptions."""
114
110
        try:
115
 
            self.curl.perform()
 
111
            curl.perform()
116
112
        except pycurl.error, e:
117
113
            # XXX: There seem to be no symbolic constants for these values.
118
114
            if e[0] == 6:
119
115
                # couldn't resolve host
120
 
                raise NoSuchFile(self.curl.getinfo(pycurl.EFFECTIVE_URL), e)
121
 
 
 
116
                raise NoSuchFile(curl.getinfo(pycurl.EFFECTIVE_URL), e)
122
117
 
123
118
 
124
119
def get_test_permutations():