Ejemplo n.º 1
0
	def append(self, _name, _value, **params):
		if params:
			Element = HEADER.get(_name, HeaderElement)
			parts = [_value or u'']
			for k, v in iteritems(params):
				if v is None:
					parts.append(k)
				else:
					parts.append(Element.formatparam(k, v))
			_value = u'; '.join(parts)

		if _name not in self or not self[_name]:
			self[_name] = _value
		else:
			Element = HEADER.get(_name, HeaderElement)
			self[_name] = Element.join([self[_name], _value])
Ejemplo n.º 2
0
	def parse(self, data):
		r"""parses HTTP headers

			:param data:
				the header string containing headers separated by "\r\n"
				without trailing "\r\n"
			:type  data: bytes
		"""

		lines = data.split(b'\r\n')

		while lines:
			curr = lines.pop(0)
			name, __, value = curr.partition(b':')
			if __ != b':':
				raise InvalidHeader(_(u"Invalid header line: %r"), curr.decode('ISO8859-1'))

			if self.HEADER_RE.search(name):
				raise InvalidHeader(_(u"Invalid header name: %r"), name.decode('ISO8859-1'))

			name, value = name.strip(), [value.lstrip()]

			# continuation lines
			while lines and lines[0].startswith((' ', '\t')):
				value.append(lines.pop(0)[1:])
			value = b''.join(value).rstrip()
			Element = HEADER.get(name, HeaderElement)
			value = Element.decode(value)

			self.append(name, value)
Ejemplo n.º 3
0
	def parse(self, data):
		r"""parses HTTP headers

			:param data:
				the header string containing headers separated by "\r\n"
				without trailing "\r\n"
			:type  data: bytes
		"""

		lines = data.split(b'\r\n')

		while lines:
			curr = lines.pop(0)
			name, __, value = curr.partition(b':')
			if __ != b':':
				raise InvalidHeader(_(u"Invalid header line: %r"), curr.decode('ISO8859-1'))

			if self.HEADER_RE.search(name):
				raise InvalidHeader(_(u"Invalid header name: %r"), name.decode('ISO8859-1'))

			name, value = name.strip(), [value.lstrip()]

			# continuation lines
			while lines and lines[0].startswith((b' ', b'\t')):
				value.append(lines.pop(0)[1:])
			value = b''.join(value).rstrip()
			Element = HEADER.get(name, HeaderElement)
			value = Element.decode(value)

			self.append(name, value)
Ejemplo n.º 4
0
	def append(self, _name, _value, **params):
		if params:
			Element = HEADER.get(_name, HeaderElement)
			parts = [_value or b'']
			for k, v in iteritems(params):
				k = k.replace('_', '-')  # TODO: find out why this is done
				if v is None:
					parts.append(k)
				else:
					parts.append(Element.formatparam(k, v))
			_value = "; ".join(parts)

		if _name not in self or not self[_name]:
			self[_name] = _value
		else:
			Element = HEADER.get(_name, HeaderElement)
			self[_name] = Element.join([self[_name], _value])
Ejemplo n.º 5
0
	def append(self, _name, _value, **params):
		if params:
			Element = HEADER.get(_name, HeaderElement)
			parts = [_value or b'']
			for k, v in iteritems(params):
				k = k.replace('_', '-')  # TODO: find out why this is done
				if v is None:
					parts.append(k)
				else:
					parts.append(Element.formatparam(k, v))
			_value = "; ".join(parts)

		if _name not in self or not self[_name]:
			self[_name] = _value
		else:
			Element = HEADER.get(_name, HeaderElement)
			self[_name] = Element.join([self[_name], _value])
Ejemplo n.º 6
0
	def elements(self, fieldname):
		u"""Return a sorted list of HeaderElements from
			the given comma-separated header string."""

		fieldvalue = self.get(fieldname)
		if not fieldvalue:
			return []

		Element = HEADER.get(fieldname, HeaderElement)
		return Element.sorted([Element.parse(element) for element in Element.split(fieldvalue)])
Ejemplo n.º 7
0
	def __encoded_items(self):
		for key, values in iteritems(self):
			Element = HEADER.get(key, HeaderElement)
			if Element is not HeaderElement:
				key = Element.__name__
			if Element.list_element:
				for value in Element.split(values):
					yield key, Element.encode(value)
			else:
				yield key, Element.encode(values)
Ejemplo n.º 8
0
	def elements(self, fieldname):
		u"""Return a sorted list of HeaderElements from
			the given comma-separated header string."""

		fieldvalue = self.get(fieldname)
		if not fieldvalue:
			return []

		Element = HEADER.get(fieldname, HeaderElement)
		return Element.sorted([Element.parse(element) for element in Element.split(fieldvalue.encode('ascii'))])
Ejemplo n.º 9
0
	def __encoded_items(self):
		for key, values in iteritems(self):
			Element = HEADER.get(key, HeaderElement)
			if Element is not HeaderElement:
				key = Element.__name__
			if Element.list_element:
				for value in Element.split(values):
					yield key, Element.encode(value)
			else:
				yield key, Element.encode(values)
Ejemplo n.º 10
0
	def elements(self, fieldname):
		u"""Return a sorted list of HeaderElements from
			the given comma-separated header string."""

		fieldvalue = self.get(fieldname)
		if not fieldvalue:
			return []

		Element = HEADER.get(fieldname, HeaderElement)

		result = []
		for element in Element.split(fieldvalue):
			result.append(Element.from_str(element))

		return list(reversed(sorted(result)))
Ejemplo n.º 11
0
	def create_element(self, fieldname, *args, **kwargs):
		Element = HEADER.get(fieldname, HeaderElement)
		return Element(*args, **kwargs)
Ejemplo n.º 12
0
	def element(self, fieldname, default=None):
		u"""Treat the field as single element"""
		if fieldname in self:
			Element = HEADER.get(fieldname, HeaderElement)
			return Element.parse(self[fieldname])
		return default
Ejemplo n.º 13
0
	def __items(self):
		return sorted(self.__encoded_items(), key=lambda x: HEADER.get(x[0], HeaderElement).priority or x[0])
Ejemplo n.º 14
0
	def create_element(self, fieldname, *args, **kwargs):
		Element = HEADER.get(fieldname, HeaderElement)
		return Element(*args, **kwargs)
Ejemplo n.º 15
0
	def element(self, fieldname, default=None):
		u"""Treat the field as single element"""
		if fieldname in self:
			Element = HEADER.get(fieldname, HeaderElement)
			return Element.parse(self[fieldname])
		return default
Ejemplo n.º 16
0
	def __items(self):
		return sorted(self.__encoded_items(), key=lambda x: HEADER.get(x[0], HeaderElement).priority or x[0])
Ejemplo n.º 17
0
	def merge(self, other):
		other = self.__class__(other)
		for key in other:
			Element = HEADER.get(key, HeaderElement)
			self[key] = Element.merge(self.elements(key), other.elements(key))
Ejemplo n.º 18
0
	def merge(self, other):
		other = self.__class__(other)
		for key in other:
			Element = HEADER.get(key, HeaderElement)
			self[key] = Element.merge(self.elements(key), other.elements(key))