Kaydet (Commit) 9f3fcd9c authored tarafından Barry Warsaw's avatar Barry Warsaw

More internal refinements of the ascii splitting algorithm.

_encode_chunks(): Pass maxlinelen in instead of always using
self._maxlinelen, so we can adjust for shorter initial lines.
Pass this value through to _max_append().

encode(): Weave maxlinelen through to the _encode_chunks() call.

_split_ascii(): When recursively splitting a line on spaces
(i.e. lower level syntactic split), don't append the whole returned
string.  Instead, split it on linejoiners and extend the lines up to
the last line (for proper packing).  Calculate the linelen based on
the last element in the this list.
üst c4d6bdd5
...@@ -342,7 +342,7 @@ class Header: ...@@ -342,7 +342,7 @@ class Header:
lines = line.splitlines() lines = line.splitlines()
return zip(lines, [charset]*len(lines)) return zip(lines, [charset]*len(lines))
def _encode_chunks(self, newchunks): def _encode_chunks(self, newchunks, maxlinelen):
# MIME-encode a header with many different charsets and/or encodings. # MIME-encode a header with many different charsets and/or encodings.
# #
# Given a list of pairs (string, charset), return a MIME-encoded # Given a list of pairs (string, charset), return a MIME-encoded
...@@ -367,7 +367,7 @@ class Header: ...@@ -367,7 +367,7 @@ class Header:
s = header s = header
else: else:
s = charset.header_encode(header) s = charset.header_encode(header)
_max_append(chunks, s, self._maxlinelen, ' ') _max_append(chunks, s, maxlinelen, ' ')
joiner = NL + self._continuation_ws joiner = NL + self._continuation_ws
return joiner.join(chunks) return joiner.join(chunks)
...@@ -407,11 +407,12 @@ class Header: ...@@ -407,11 +407,12 @@ class Header:
newchunks += self._split(s, charset, targetlen, splitchars) newchunks += self._split(s, charset, targetlen, splitchars)
lastchunk, lastcharset = newchunks[-1] lastchunk, lastcharset = newchunks[-1]
lastlen = lastcharset.encoded_header_len(lastchunk) lastlen = lastcharset.encoded_header_len(lastchunk)
return self._encode_chunks(newchunks) return self._encode_chunks(newchunks, maxlinelen)
def _split_ascii(s, firstlen, restlen, continuation_ws, splitchars): def _split_ascii(s, firstlen, restlen, continuation_ws, splitchars):
linejoiner = '\n' + continuation_ws
lines = [] lines = []
maxlen = firstlen maxlen = firstlen
for line in s.splitlines(): for line in s.splitlines():
...@@ -460,11 +461,14 @@ def _split_ascii(s, firstlen, restlen, continuation_ws, splitchars): ...@@ -460,11 +461,14 @@ def _split_ascii(s, firstlen, restlen, continuation_ws, splitchars):
# splitting on whitespace, try to recursively split this line # splitting on whitespace, try to recursively split this line
# on whitespace. # on whitespace.
if partlen > maxlen and ch <> ' ': if partlen > maxlen and ch <> ' ':
this = [_split_ascii(part, maxlen, restlen, subs = _split_ascii(part, maxlen, restlen,
continuation_ws, ' ')] continuation_ws, ' ')
subl = re.split(linejoiner, subs)
lines.extend(subl[:-1])
this = [subl[-1]]
else: else:
this = [part] this = [part]
linelen = wslen + partlen linelen = wslen + len(this[-1])
maxlen = restlen maxlen = restlen
else: else:
this.append(part) this.append(part)
...@@ -472,7 +476,6 @@ def _split_ascii(s, firstlen, restlen, continuation_ws, splitchars): ...@@ -472,7 +476,6 @@ def _split_ascii(s, firstlen, restlen, continuation_ws, splitchars):
# Put any left over parts on a line by themselves # Put any left over parts on a line by themselves
if this: if this:
lines.append(joiner.join(this)) lines.append(joiner.join(this))
linejoiner = '\n' + continuation_ws
return linejoiner.join(lines) return linejoiner.join(lines)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment