_write_headers(), _split_header(): All of the smarts for splitting
long header lines is now (properly) in the Header class. So we no longer need _split_header() and we'll just defer to Header.encode() when we have a plain string.
This commit is contained in:
parent
9f3fcd9c23
commit
ce6bf59b2d
|
@ -161,44 +161,29 @@ class Generator:
|
||||||
|
|
||||||
def _write_headers(self, msg):
|
def _write_headers(self, msg):
|
||||||
for h, v in msg.items():
|
for h, v in msg.items():
|
||||||
# RFC 2822 says that lines SHOULD be no more than maxheaderlen
|
print >> self._fp, '%s:' % h,
|
||||||
# characters wide, so we're well within our rights to split long
|
if self.__maxheaderlen == 0:
|
||||||
# headers.
|
# Explicit no-wrapping
|
||||||
text = '%s: %s' % (h, v)
|
print >> self._fp, v
|
||||||
if self.__maxheaderlen > 0 and len(text) > self.__maxheaderlen:
|
elif isinstance(v, Header):
|
||||||
text = self._split_header(text)
|
# Header instances know what to do
|
||||||
print >> self._fp, text
|
print >> self._fp, v.encode()
|
||||||
|
elif _is8bitstring(v):
|
||||||
|
# If we have raw 8bit data in a byte string, we have no idea
|
||||||
|
# what the encoding is. There is no safe way to split this
|
||||||
|
# string. If it's ascii-subset, then we could do a normal
|
||||||
|
# ascii split, but if it's multibyte then we could break the
|
||||||
|
# string. There's no way to know so the least harm seems to
|
||||||
|
# be to not split the string and risk it being too long.
|
||||||
|
print >> self._fp, v
|
||||||
|
else:
|
||||||
|
# Header's got lots of smarts, so use it.
|
||||||
|
print >> self._fp, Header(
|
||||||
|
v, maxlinelen=self.__maxheaderlen,
|
||||||
|
header_name=h, continuation_ws='\t').encode()
|
||||||
# A blank line always separates headers from body
|
# A blank line always separates headers from body
|
||||||
print >> self._fp
|
print >> self._fp
|
||||||
|
|
||||||
def _split_header(self, text):
|
|
||||||
maxheaderlen = self.__maxheaderlen
|
|
||||||
# Find out whether any lines in the header are really longer than
|
|
||||||
# maxheaderlen characters wide. There could be continuation lines
|
|
||||||
# that actually shorten it. Also, replace hard tabs with 8 spaces.
|
|
||||||
lines = [s.replace('\t', SPACE8) for s in text.splitlines()]
|
|
||||||
for line in lines:
|
|
||||||
if len(line) > maxheaderlen:
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
# No line was actually longer than maxheaderlen characters, so
|
|
||||||
# just return the original unchanged.
|
|
||||||
return text
|
|
||||||
# If we have raw 8bit data in a byte string, we have no idea what the
|
|
||||||
# encoding is. I think there is no safe way to split this string. If
|
|
||||||
# it's ascii-subset, then we could do a normal ascii split, but if
|
|
||||||
# it's multibyte then we could break the string. There's no way to
|
|
||||||
# know so the least harm seems to be to not split the string and risk
|
|
||||||
# it being too long.
|
|
||||||
if _is8bitstring(text):
|
|
||||||
return text
|
|
||||||
# The `text' argument already has the field name prepended, so don't
|
|
||||||
# provide it here or the first line will get folded too short.
|
|
||||||
h = Header(text, maxlinelen=maxheaderlen,
|
|
||||||
# For backwards compatibility, we use a hard tab here
|
|
||||||
continuation_ws='\t')
|
|
||||||
return h.encode()
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# Handlers for writing types and subtypes
|
# Handlers for writing types and subtypes
|
||||||
#
|
#
|
||||||
|
|
Loading…
Reference in New Issue