Ejemplo n.º 1
0
    def request(self, src_file="", dst_file="", method="", maxretries=10):
        retry = 0
        src_url = self.https_path + "/" + src_file
        dst_url = self.https_path + "/" + dst_file
        request = urllib2.Request(src_url)

        if dst_file != "":
            request.add_header('Destination', dst_url)

        request.add_header("Authorization", "Basic %s" % self.auth_str)

        request.get_method = lambda: method

        LOG.debug('Sending WebDAV request:%s %s %s' %
                  (method, src_url, dst_url))

        while retry < maxretries:
            try:
                response = urllib2.urlopen(request, timeout=None)
            except urllib2.HTTPError as err:
                LOG.error(
                    _LE('WebDAV returned with %(code)s error during '
                        '%(method)s call.') % {
                            'code': err.code,
                            'method': method
                        })

                if err.code == httplib.INTERNAL_SERVER_ERROR:
                    exception_msg = (
                        _('WebDAV operation failed with '
                          'error code: %(code)s '
                          'reason: %(reason)s '
                          'Retry attempt %(retry)s in progress.') % {
                              'code': err.code,
                              'reason': err.reason,
                              'retry': retry
                          })
                    LOG.error(exception_msg)
                    if retry < maxretries:
                        retry += 1
                        time.sleep(1)
                        continue

                msg = self._lookup_error(err.code)
                raise exception.WebDAVClientError(msg=msg,
                                                  code=err.code,
                                                  src=src_file,
                                                  dst=dst_file,
                                                  method=method)

            except httplib.BadStatusLine as err:
                msg = self._lookup_error('BadStatusLine')
                raise exception.WebDAVClientError(msg=msg,
                                                  code='httplib.BadStatusLine',
                                                  src=src_file,
                                                  dst=dst_file,
                                                  method=method)

            except urllib2.URLError as err:
                reason = ''
                if getattr(err, 'reason'):
                    reason = err.reason

                msg = self._lookup_error('Bad_Gateway')
                raise exception.WebDAVClientError(msg=msg,
                                                  code=reason,
                                                  src=src_file,
                                                  dst=dst_file,
                                                  method=method)

            break
        return response
Ejemplo n.º 2
0
    def request(self,
                src_file="",
                dst_file="",
                method="",
                maxretries=10,
                data=""):
        retry = 0
        src_url = self.https_path + "/" + src_file
        dst_url = self.https_path + "/" + dst_file
        request = urllib.request.Request(url=src_url, data=data)

        if dst_file != "":
            request.add_header('Destination', dst_url)
        if method == "PROPPATCH":
            request.add_header('Translate', 'F')

        request.add_header("Authorization", "Basic %s" % self.auth_str)

        request.get_method = lambda: method

        LOG.debug('Sending WebDAV request:%(method)s %(src)s %(des)s', {
            'method': method,
            'src': src_url,
            'des': dst_url
        })

        while retry < maxretries:
            try:
                # URLs are prepended with self.https_path which is safe
                # meaning that the URL will either be safe or nonexistant
                response = urllib.request.urlopen(  # nosec
                    request, timeout=None)
            except urllib.error.HTTPError as err:
                LOG.error(
                    'WebDAV returned with %(code)s error during '
                    '%(method)s call.', {
                        'code': err.code,
                        'method': method
                    })

                if err.code == http_client.INTERNAL_SERVER_ERROR:
                    LOG.error(
                        'WebDAV operation failed with error code: '
                        '%(code)s reason: %(reason)s Retry attempt '
                        '%(retry)s in progress.', {
                            'code': err.code,
                            'reason': err.reason,
                            'retry': retry
                        })
                    if retry < maxretries:
                        retry += 1
                        time.sleep(1)
                        continue

                msg = self._lookup_error(err.code)
                raise exception.WebDAVClientError(msg=msg,
                                                  code=err.code,
                                                  src=src_file,
                                                  dst=dst_file,
                                                  method=method)

            except http_client.BadStatusLine as err:
                msg = self._lookup_error('BadStatusLine')
                code = 'http_client.BadStatusLine'
                raise exception.WebDAVClientError(msg=msg,
                                                  code=code,
                                                  src=src_file,
                                                  dst=dst_file,
                                                  method=method)

            except urllib.error.URLError as err:
                reason = ''
                if getattr(err, 'reason'):
                    reason = err.reason

                msg = self._lookup_error('Bad_Gateway')
                raise exception.WebDAVClientError(msg=msg,
                                                  code=reason,
                                                  src=src_file,
                                                  dst=dst_file,
                                                  method=method)

            break
        return response