Skip to content

Commit

Permalink
Cleanup charset handling
Browse files Browse the repository at this point in the history
- avoid copying the whole dict in _part_response
- avoid decode-encode-decode dance in get_message_html
  • Loading branch information
ThiefMaster committed Jun 21, 2015
1 parent 8f0dc44 commit d1e448d
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions maildump/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,11 @@ def _part_url(part):

def _part_response(part, body=None, charset=None):
charset = charset or part['charset'] or 'utf-8'
if body is None:
body = part['body']
if charset != 'utf-8':
part = dict(part)
part['body'] = part['body'].decode(charset).encode('utf-8')
io = StringIO(part['body'] if body is None else body)
body = body.decode(charset).encode('utf-8')
io = StringIO(body)
io.seek(0)
response = send_file(io, part['type'], part['is_attachment'], part['filename'])
response.charset = charset
Expand Down Expand Up @@ -159,10 +160,7 @@ def get_message_html(message_id):
if not part:
return 404, 'part does not exist'
charset = part['charset'] or 'utf-8'
if charset != 'utf-8':
part = dict(part)
part['body'] = part['body'].decode(charset).encode('utf-8')
soup = bs4.BeautifulSoup(part['body'].decode('utf-8'), 'html5lib')
soup = bs4.BeautifulSoup(part['body'].decode(charset), 'html5lib')
_fix_cid_links(soup, message_id)
return _part_response(part, str(soup), 'utf-8')

Expand Down

0 comments on commit d1e448d

Please sign in to comment.