self._v = MultiDict([(normalize(key), remove_newlines(val))
for (key, val) in items])
self.changed = False
+ self.num_prepends = 0
def __getitem__(self, key):
v = self._v.get(normalize(key), None)
def prepend(self, key, value):
self._v._items.insert(0, (normalize(key), remove_newlines(value)))
- self.changed = True
+ self.num_prepends += 1
def add(self, key, value):
"""Adds header without changing the
v = self._v.getall(normalize(key))
return [encodedword.decode(x) for x in v]
- def have_changed(self):
+ def have_changed(self, ignore_prepends=False):
"""
Tells whether someone has altered the headers after creation.
"""
- return self.changed
+ return self.changed or (self.num_prepends > 0 and not ignore_prepends)
def __str__(self):
return str(self._v)
"""
return cls(parse_stream(stream))
- def to_stream(self, stream):
+ def to_stream(self, stream, prepends_only=False):
"""
Takes a stream and serializes headers in a mime format.
"""
+ i = 0
for h, v in self.iteritems(raw=True):
+ if prepends_only and i == self.num_prepends:
+ break
+ i += 1
try:
h = h.encode('ascii')
except UnicodeDecodeError:
self._body_changed = True
- def headers_changed(self):
- return self._headers is not None and self._headers.have_changed()
+ def headers_changed(self, ignore_prepends=False):
+ return self._headers is not None and self._headers.have_changed(ignore_prepends)
def body_changed(self):
return self._body_changed
def content_type(self):
return self.headers['Content-Type']
- def headers_changed(self):
+ def headers_changed(self, ignore_prepends=False):
return True
def body_changed(self):
def content_type(self):
return self.headers['Content-Type']
- def headers_changed(self):
+ def headers_changed(self, ignore_prepends=False):
return True
def body_changed(self):
Returns a MIME representation of the message.
"""
# this optimisation matters *A LOT*
+ # when there are no prepended headers
# we submit the original string,
# no copying, no alternation, yeah!
- if self.is_root() and not self.was_changed():
- return self._container.string
+ if self.is_root() and not self.was_changed(ignore_prepends=True):
+ with closing(StringIO()) as out:
+ self.headers.to_stream(out, prepends_only=True)
+ return out.getvalue() + self._container.string
else:
with closing(StringIO()) as out:
self.to_stream(out)
out.seek(original_position)
out.write(self._container.read_message())
- def was_changed(self):
- if self._container.headers_changed():
+ def was_changed(self, ignore_prepends=False):
+ if self._container.headers_changed(ignore_prepends):
return True
if self.content_type.is_singlepart():
To: "Bob Sample" <bob@example.com>\r
Subject: Welcome\r
MIME-Version: 1.0\r
-Content-Type: multipart/alternative; boundary="bd1"\r
+Content-Type: multipart/alternative; boundary=bd1\r
\r
--bd1\r
Content-Type: text/plain; charset="UTF-8"\r
eq_('7bit', message.content_encoding.value)
+def preserve_formatting_with_new_headers_test():
+ """Make sure that we don't re-serialize a message and change its formatting when headers were added but nothing else was modified."""
+ # MULTIPART contains this header:
+ # Content-Type: multipart/alternative; boundary=bd1
+ # which will change to this if it is re-serialized:
+ # Content-Type: multipart/alternative; boundary="bd1"
+ message = scan(MULTIPART)
+ message.headers.prepend('X-New-Header', 'Added')
+ new_header, remaining_mime = message.to_string().split('\r\n', 1)
+ eq_('X-New-Header: Added', new_header)
+ eq_(MULTIPART, remaining_mime)
+
+
def ascii_to_unicode_test():
"""Make sure that ascii uprades to unicode whenever needed"""
# contains unicode chars