Пример #1
0
	def decode(cls, data, charset=None, mimetype=None):
		boundary = mimetype.boundary.encode('ISO8859-1')
		parts = data.split(b'--%s' % (boundary,))
		part = parts.pop(0)
		if part:
			raise DecodeError(_(u'Data before boundary: %r'), part.decode('ISO8859-1'))
		try:
			part = parts.pop()
		except IndexError:
			raise DecodeError(u'No end of boundary')
		if part not in (b'--', b'--\r\n'):
			raise DecodeError(_(u'Invalid multipart end: %r'), part.decode('ISO8859-1'))

		from httoop.messages.body import Body
		multiparts = []
		for part in parts:
			if not part.startswith('\r\n'):
				raise DecodeError(_(u'Invalid boundary end: %r'), part[:2].decode('ISO8859-1'))
			part = part[2:]
			headers, separator, content = part.partition(b'\r\n\r\n')
			if not separator:
				raise DecodeError(_(u'Multipart does not contain CRLF header separator'))
			if not content.endswith('\r\n'):
				raise DecodeError(_(u'Multipart does not end with CRLF: %r'), content[-2].decode('ISO8859-1'))
			content = content[:-2]
			body = Body()
			body.headers.clear()
			body.headers.parse(headers)
			body.headers.setdefault('Content-Type', cls.default_content_type)
			body.parse(content)
			multiparts.append(body)
		return multiparts
Пример #2
0
    def decode(cls, data, charset=None, mimetype=None):
        boundary = mimetype.boundary.encode('ISO8859-1')
        parts = data.split(b'--%s' % (boundary, ))
        part = parts.pop(0)
        if part:
            raise DecodeError(_(u'Data before boundary: %r'),
                              part.decode('ISO8859-1'))
        try:
            part = parts.pop()
        except IndexError:
            raise DecodeError(u'No end of boundary')
        if part not in (b'--', b'--\r\n'):
            raise DecodeError(_(u'Invalid multipart end: %r'),
                              part.decode('ISO8859-1'))

        from httoop.messages.body import Body
        multiparts = []
        for part in parts:
            if not part.startswith('\r\n'):
                raise DecodeError(_(u'Invalid boundary end: %r'),
                                  part[:2].decode('ISO8859-1'))
            part = part[2:]
            headers, separator, content = part.partition(b'\r\n\r\n')
            if not separator:
                raise DecodeError(
                    _(u'Multipart does not contain CRLF header separator'))
            if not content.endswith('\r\n'):
                raise DecodeError(_(u'Multipart does not end with CRLF: %r'),
                                  content[-2].decode('ISO8859-1'))
            content = content[:-2]
            body = Body()
            body.headers.clear()
            body.headers.parse(headers)
            body.headers.setdefault('Content-Type', cls.default_content_type)
            body.parse(content)
            multiparts.append(body)
        return multiparts
Пример #3
0
    def __init__(self, protocol=None, headers=None, body=None):
        u"""Initiates a new Message to hold information about the message.

			:param protocol: the requested protocol
			:type  protocol: str|tuple

			:param headers: the request headers
			:type  headers: dict or :class:`Headers`

			:param body: the request body
			:type  body: any
		"""
        self.__protocol = Protocol(protocol or (1, 1))
        self.__headers = Headers(headers or {})
        self.__body = Body(body or b'')
Пример #4
0
	def multipart_byteranges(self, range_body, range_, content_length, content_type):
		for content, byterange in izip(range_body, range_.ranges):
			body = Body(content)
			body.headers['Content-Type'] = content_type
			body.headers.set_element('Content-Range', b'bytes', byterange, content_length)
			yield body
Пример #5
0
 def body(self):
     if not hasattr(self, '_body'):
         from httoop.messages.body import Body
         self._body = Body(mimetype='application/json')
         self._body.data = self.to_dict()
     return self._body
Пример #6
0
class StatusException(with_metaclass(StatusType, Status, Exception)):
    u"""This class represents a small HTTP Response message
		for error handling purposes"""
    @property
    def headers(self):
        return self._headers

    @property
    def body(self):
        if not hasattr(self, '_body'):
            from httoop.messages.body import Body
            self._body = Body(mimetype='application/json')
            self._body.data = self.to_dict()
        return self._body

    @body.setter
    def body(self, value):
        self.body
        self._body.set(value)

    header_to_remove = ()
    u"""a tuple of header field names which should be
		removed when responding with this error"""

    description = ''

    @property
    def traceback(self):
        return self._traceback

    @traceback.setter
    def traceback(self, tb):
        if self.server_error:
            self._traceback = tb

    code = 0

    def __init__(self,
                 description=None,
                 reason=None,
                 headers=None,
                 traceback=None):
        u"""
			:param description:
				a description of the error which happened
			:type description: str

			:param reason:
				a additional reason phrase
			:type reason: str

			:param headers:
			:type headers: dict

			:param traceback:
				A Traceback for the error
			:type traceback: str
		"""

        Status.__init__(self, self.__class__.code, reason=reason)

        self._headers = dict()
        self._traceback = None

        if isinstance(headers, dict):
            self._headers.update(headers)

        if description is not None:
            self.description = description

        if traceback:
            self.traceback = traceback

    def __repr__(self):
        return '<HTTP Status %d %r>' % (int(self), self.reason)

    __str__ = __repr__

    def to_dict(self):
        u"""the default body arguments"""
        return dict(status=self.status,
                    reason=self.reason,
                    description=self.description,
                    headers=self.headers)
Пример #7
0
	def body(self):
		if not hasattr(self, '_body'):
			from httoop.messages.body import Body
			self._body = Body(mimetype='application/json')
			self._body.data = self.to_dict()
		return self._body
Пример #8
0
class StatusException(Status, Exception):
	u"""This class represents a small HTTP Response message
		for error handling purposes"""

	__metaclass__ = StatusType

	@property
	def headers(self):
		return self._headers

	@property
	def body(self):
		if not hasattr(self, '_body'):
			from httoop.messages.body import Body
			self._body = Body(mimetype='application/json')
			self._body.data = self.to_dict()
		return self._body

	@body.setter
	def body(self, value):
		self.body
		self._body.set(value)

	header_to_remove = ()
	u"""a tuple of header field names which should be
		removed when responding with this error"""

	description = ''

	@property
	def traceback(self):
		return self._traceback

	@traceback.setter
	def traceback(self, tb):
		if self.server_error:
			self._traceback = tb

	code = 0

	def __init__(self, description=None, reason=None, headers=None, traceback=None):
		u"""
			:param description:
				a description of the error which happened
			:type description: str

			:param reason:
				a additional reason phrase
			:type reason: str

			:param headers:
			:type headers: dict

			:param traceback:
				A Traceback for the error
			:type traceback: str
		"""

		Status.__init__(self, self.__class__.code, reason=reason)

		self._headers = dict()
		self._traceback = None

		if isinstance(headers, dict):
			self._headers.update(headers)

		if description is not None:
			self.description = description

		if traceback:
			self.traceback = traceback

	def __repr__(self):
		return '<HTTP Status %d %r>' % (int(self), self.reason)

	__str__ = __repr__

	def to_dict(self):
		u"""the default body arguments"""
		return dict(
			status=self.status,
			reason=self.reason,
			description=self.description,
			headers=self.headers
		)