Example #1
0
    def _urlopen(url,
                 data=None,
                 timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
                 cafile=None,
                 capath=None,
                 cadefault=False):
        def http_response(request, response):
            return response

        http_error_processor = HTTPErrorProcessor()
        http_error_processor.https_response = http_response

        if _have_sslcontext and (cafile or capath or cadefault):
            context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
            context.options |= ssl.OP_NO_SSLv2
            context.verify_mode = ssl.CERT_REQUIRED
            if cafile or capath:
                context.load_verify_locations(cafile, capath)
            else:
                context.set_default_verify_paths()
            https_handler = HTTPSHandler(context=context, check_hostname=True)
            opener = build_opener(https_handler, http_error_processor)
        else:
            opener = build_opener(http_error_processor)
        return opener.open(url, data, timeout)
Example #2
0
    def _urlopen(
        url, data=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, cafile=None, capath=None, cadefault=False, context=None
    ):
        def http_response(request, response):
            return response

        http_error_processor = HTTPErrorProcessor()
        http_error_processor.https_response = http_response

        if context:
            https_handler = HTTPSHandler(context=context)
            opener = build_opener(https_handler, http_error_processor)
        else:
            opener = build_opener(http_error_processor)

        return opener.open(url, data, timeout)
Example #3
0
    def _urlopen(url, data=None,
                 timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
                 cafile=None, capath=None, cadefault=False,
                 context=None):

        def http_response(request, response):
            return response

        http_error_processor = HTTPErrorProcessor()
        http_error_processor.https_response = http_response

        if context:
            https_handler = HTTPSHandler(context=context)
            opener = build_opener(https_handler, http_error_processor)
        else:
            opener = build_opener(http_error_processor)

        return opener.open(url, data, timeout)
    def _urlopen(url, data=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
                 cafile=None, capath=None, cadefault=False):

        def http_response(request, response):
            return response

        http_error_processor = HTTPErrorProcessor()
        http_error_processor.https_response = http_response

        if _have_sslcontext and (cafile or capath or cadefault):
            context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
            context.options |= ssl.OP_NO_SSLv2
            context.verify_mode = ssl.CERT_REQUIRED
            if cafile or capath:
                context.load_verify_locations(cafile, capath)
            else:
                context.set_default_verify_paths()
            https_handler = HTTPSHandler(context=context, check_hostname=True)
            opener = build_opener(https_handler, http_error_processor)
        else:
            opener = build_opener(http_error_processor)
        return opener.open(url, data, timeout)
Example #5
0
    def http_response(self, request, response):
        try:
            return HTTPErrorProcessor.http_response(self, request, response)
        except HTTPError as e:
            data = e.read()

            try:
                error_json = parse_json(data)
            except ValueError:
                logging.debug(
                    'Value error when trying to parse JSON from response data:\n%s'
                    % response)
                raise

            if e.code == 401:
                raise UnauthorizedError(error_json)
            elif e.code in (400, 500):
                raise BadRequestError(error_json)
            else:
                raise ClientException(error_json)
 def http_response(self, request, response):
     if response.code in [200, 201, 204]:
         return response
     return HTTPErrorProcessor.http_response(self, request, response)
 def http_response(self, request, response):
     return HTTPErrorProcessor.http_response(self, request, response)
Example #8
0
    def __api_request(self, request_uri, body, headers={}):
        url = self.uri
        url += request_uri

        if self.debug1:
            print('URL:', url, file=sys.stderr)
            print('headers:', headers, file=sys.stderr)

        # body must by type 'bytes' for 3.x
        if _isunicode(body):
            body = body.encode()

        if self.debug3:
            print('body:', repr(body), file=sys.stderr)

        request = Request(url, body, headers)

        if self.debug1:
            print('method:', request.get_method(), file=sys.stderr)
            print('headers:', request.header_items(), file=sys.stderr)

        kwargs = {
            'url': request,
        }
        # Changed in version 3.2: cafile and capath were added.
        if sys.hexversion >= 0x03020000:
            kwargs['cafile'] = self.cafile
            kwargs['capath'] = self.capath
        # Changed in version 3.3: cadefault added
        if sys.hexversion >= 0x03030000:
            pass
#            kwargs['cadefault'] = True

        if self.timeout is not None:
            kwargs['timeout'] = self.timeout

        # override HTTPError for (not 200 <= code 300) and handle below
        def http_response(request, response):
            return response

        http_error_processor = HTTPErrorProcessor()
        http_error_processor.https_response = http_response
        opener = build_opener(http_error_processor)
        # install so we can use **kwargs
        install_opener(opener)

        try:
            response = urlopen(**kwargs)

        # XXX handle httplib.BadStatusLine when http to port 443
        except URLError as e:
            self._msg = str(e)
            return False
        # invalid cafile, capath
        except IOError as e:
            self._msg = str(e)
            return False

        self.http_code = response.getcode()
        if hasattr(response, 'reason'):
            # 3.2
            self.http_reason = response.reason
        elif hasattr(response, 'msg'):
            # 2.7
            self.http_reason = response.msg
        elif self.http_code in responses:
            self.http_reason = responses[self.http_code]

        if self.debug2:
            print('HTTP response code:', self.http_code, file=sys.stderr)
            print('HTTP response reason:', self.http_reason, file=sys.stderr)
            print('HTTP response headers:', file=sys.stderr)
            print(response.info(), file=sys.stderr)

        if not (200 <= self.http_code < 300):
            self._msg = 'HTTP Error %s: %s' % (self.http_code,
                                               self.http_reason)
            self.__set_response(response)
            return False

        return response
    def __api_request(self, request_uri, body, headers={}):
        url = self.uri
        url += request_uri

        if self.debug1:
            print("URL:", url, file=sys.stderr)
            print("headers:", headers, file=sys.stderr)

        # body must by type 'bytes' for 3.x
        if _isunicode(body):
            body = body.encode()

        if self.debug3:
            print("body:", repr(body), file=sys.stderr)

        request = Request(url, body, headers)

        if self.debug1:
            print("method:", request.get_method(), file=sys.stderr)
            print("headers:", request.header_items(), file=sys.stderr)

        kwargs = {"url": request}
        # Changed in version 3.2: cafile and capath were added.
        if sys.hexversion >= 0x03020000:
            kwargs["cafile"] = self.cafile
            kwargs["capath"] = self.capath
        # Changed in version 3.3: cadefault added
        if sys.hexversion >= 0x03030000:
            pass
        #            kwargs['cadefault'] = True

        if self.timeout is not None:
            kwargs["timeout"] = self.timeout

        # override HTTPError for (not 200 <= code 300) and handle below
        def http_response(request, response):
            return response

        http_error_processor = HTTPErrorProcessor()
        http_error_processor.https_response = http_response
        opener = build_opener(http_error_processor)
        # install so we can use **kwargs
        install_opener(opener)

        try:
            response = urlopen(**kwargs)

        # XXX handle httplib.BadStatusLine when http to port 443
        except URLError as e:
            self._msg = str(e)
            return False
        # invalid cafile, capath
        except IOError as e:
            self._msg = str(e)
            return False

        self.http_code = response.getcode()
        if hasattr(response, "reason"):
            # 3.2
            self.http_reason = response.reason
        elif hasattr(response, "msg"):
            # 2.7
            self.http_reason = response.msg
        elif self.http_code in responses:
            self.http_reason = responses[self.http_code]

        if self.debug2:
            print("HTTP response code:", self.http_code, file=sys.stderr)
            print("HTTP response reason:", self.http_reason, file=sys.stderr)
            print("HTTP response headers:", file=sys.stderr)
            print(response.info(), file=sys.stderr)

        if not (200 <= self.http_code < 300):
            self._msg = "HTTP Error %s: %s" % (self.http_code, self.http_reason)
            self.__set_response(response)
            return False

        return response
Example #10
0
 def http_response(self, request, response):
     if response.code in [200, 201, 204]:
         return response
     return HTTPErrorProcessor.http_response(self, request, response)