예제 #1
0
    def connectionLost(self, reason):
        """
        Deliver the accumulated response bytes to the waiting L{Deferred}, if
        the response body has been completely received without error.
        """
        if reason.check(ResponseDone):

            body = b''.join(self.dataBuffer)

            # ---8<--- snippet from u1db.remote.http_client
            if self.status in (200, 201):
                self.deferred.callback(body)
            elif self.status in http_errors.ERROR_STATUSES:
                try:
                    respdic = json.loads(body)
                except ValueError:
                    self.deferred.errback(
                        errors.HTTPError(self.status, body, self.headers))
                else:
                    self._error(respdic)
            # special cases
            elif self.status == 503:
                self.deferred.errback(errors.Unavailable(body, self.headers))
            else:
                self.deferred.errback(
                    errors.HTTPError(self.status, body, self.headers))
            # ---8<--- end of snippet from u1db.remote.http_client

        elif reason.check(PotentialDataLoss):
            self.deferred.errback(
                PartialDownloadError(self.status, self.message,
                                     b''.join(self.dataBuffer)))
        else:
            self.deferred.errback(reason)
예제 #2
0
    def test_redfish_request_continues_partial_download_error(self):
        driver = RedfishPowerDriver()
        context = make_context()
        url = driver.get_url(context)
        uri = join(url, b"redfish/v1/Systems")
        headers = driver.make_auth_headers(**context)
        mock_agent = self.patch(redfish_module, "Agent")
        mock_agent.return_value.request = Mock()
        expected_headers = Mock()
        expected_headers.code = HTTPStatus.OK
        expected_headers.headers = "Testing Headers"
        mock_agent.return_value.request.return_value = succeed(
            expected_headers
        )
        mock_readBody = self.patch(redfish_module, "readBody")
        error = PartialDownloadError(
            response=json.dumps(SAMPLE_JSON_SYSTEMS).encode("utf-8"),
            code=HTTPStatus.OK,
        )
        mock_readBody.return_value = fail(error)
        expected_response = SAMPLE_JSON_SYSTEMS

        response, headers = yield driver.redfish_request(b"GET", uri, headers)
        self.assertEqual(expected_response, response)
        self.assertEqual(expected_headers.headers, headers)
예제 #3
0
    def test_webhook_request_continues_partial_download_error(self):
        mock_agent = self.patch(webhook_module, "Agent")
        mock_agent.return_value.request = Mock()
        expected_headers = Mock()
        expected_headers.code = HTTPStatus.OK
        expected_headers.headers = "Testing Headers"
        mock_agent.return_value.request.return_value = succeed(
            expected_headers
        )
        mock_readBody = self.patch(webhook_module, "readBody")
        expected_response = factory.make_name("body")
        error = PartialDownloadError(
            response=expected_response.encode(),
            code=HTTPStatus.OK,
        )
        mock_readBody.return_value = fail(error)
        method = random.choice([b"POST", b"GET"])
        headers = self.webhook._make_auth_headers(
            factory.make_name("system_id"), {}
        )

        response = yield self.webhook._webhook_request(
            method, b"https://10.0.0.42", headers
        )
        self.assertEqual(expected_response, response.decode())
예제 #4
0
 def handleResponse(self, response):
     if self.factory.method.upper() == 'HEAD':
         self.factory.page('')
     elif self.length != None and self.length != 0:
         self.factory.noPage(failure.Failure(
             PartialDownloadError(self.factory.status, None, response)))
     else:
         self.factory.page(response)
     self.transport.loseConnection()
예제 #5
0
 def connectionLost(self, reason):
     if reason.check(ResponseDone):
         yield reactor.callInThread(self._flush)
         self.deferred.callback(None)
     elif reason.check(PotentialDataLoss):
         self.deferred.errback(
             PartialDownloadError(self.status, self.message))
     else:
         self.deferred.errback(reason)
예제 #6
0
	def connectionLost(self, reason):
		"""
		Deliver the accumulated response bytes to the waiting L{Deferred}, if
		the response body has been completely received without error.
		"""
		# print self.dataBuffer
		
		if reason.check(ResponseDone):
			self.deferred.callback(b''.join(self.dataBuffer))
		elif reason.check(PotentialDataLoss):
			try:
				self.deferred.errback(
					PartialDownloadError(self.status, self.message,
										 b''.join(self.dataBuffer)))
			except Exception as ex:
				print ex
				self.deferred.errback(PartialDownloadError(0, 'PartialDownloadError', ''))	
		else:
			self.deferred.errback(reason)
예제 #7
0
 def connectionLost(self, reason):
     # Only start the callback chain if the deferred has not alreaby been called,
     # e.g. through a timeout
     if not self.deferred.called:
         if reason.check(ResponseDone):
             self.deferred.callback(b''.join(self.dataBuffer))
         else:
             # On an error return a PartialDownload response with whatever data we have already received
             self.deferred.errback(
                 PartialDownloadError(self.status, self.message,
                                      b''.join(self.dataBuffer)))
예제 #8
0
    def test_redfish_request_raises_failures(self):
        driver = RedfishPowerDriver()
        context = make_context()
        url = driver.get_url(context)
        uri = join(url, b"redfish/v1/Systems")
        headers = driver.make_auth_headers(**context)
        mock_agent = self.patch(redfish_module, "Agent")
        mock_agent.return_value.request = Mock()
        expected_headers = Mock()
        expected_headers.code = HTTPStatus.OK
        expected_headers.headers = "Testing Headers"
        mock_agent.return_value.request.return_value = succeed(
            expected_headers)
        mock_readBody = self.patch(redfish_module, "readBody")
        error = PartialDownloadError(
            response=json.dumps(SAMPLE_JSON_SYSTEMS).encode("utf-8"),
            code=HTTPStatus.NOT_FOUND,
        )
        mock_readBody.return_value = fail(error)

        with ExpectedException(PartialDownloadError):
            yield driver.redfish_request(b"GET", uri, headers)
        self.assertThat(mock_readBody, MockCalledOnceWith(expected_headers))
예제 #9
0
    def test_webhook_request_raises_failures(self):
        mock_agent = self.patch(webhook_module, "Agent")
        mock_agent.return_value.request = Mock()
        expected_headers = Mock()
        expected_headers.code = HTTPStatus.OK
        expected_headers.headers = "Testing Headers"
        mock_agent.return_value.request.return_value = succeed(
            expected_headers
        )
        mock_readBody = self.patch(webhook_module, "readBody")
        expected_response = factory.make_name("body")
        error = PartialDownloadError(
            response=expected_response.encode(),
            code=HTTPStatus.NOT_FOUND,
        )
        mock_readBody.return_value = fail(error)
        method = random.choice([b"POST", b"GET"])

        with ExpectedException(PartialDownloadError):
            yield self.webhook._webhook_request(
                method, b"https://10.0.0.42", {}
            )
        self.assertThat(mock_readBody, MockCalledOnceWith(expected_headers))
예제 #10
0
 def handle_timeout(self, *_):
     self.transport.stopProducing()
     # Raise as we are already in the deferred callback chain here
     raise PartialDownloadError(self.status, self.message,
                                b''.join(self.dataBuffer))
예제 #11
0
    def cancel(deferred):
        """
		Cancel a L{readBody} call, close the connection to the HTTP server
		immediately, if it is still open.
		@param deferred: The cancelled L{defer.Deferred}.
		"""
        abort = getAbort()
        if abort is not None:
            abort()

    d = defer.Deferred(cancel)
    protocol = _ReadBodyProtocol(response.code, response.phrase, d)

    def getAbort():
        return getattr(protocol.transport, 'abortConnection', None)

    response.deliverBody(protocol)

    if protocol.transport is not None and getAbort() is None:
        warnings.warn(
            'Using readBody with a transport that does not have an '
            'abortConnection method',
            category=DeprecationWarning,
            stacklevel=2)

    return d


if __name__ == '__main__':
    e = PartialDownloadError(400, 'data lost', 'some data')
    print e