1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
#!/usr/bin/python
#
# Try to reconstruct a saved chunked response after an editor has mangled all
# the newlines. Not 100% foolproof, but decent enough that the result requires
# very little manual mangling.
import sys
import re
data = sys.stdin.read()
header, body = data.split('\n\n', 1)
header = header.replace('\n', '\r\n')
# All <?xml ... ?> stanzas end with CRLF. Only match on ?> as the stanza may be split over 2 chunks
body = body.replace('?>', '?>\r')
# Chunked response: each chunk ends with CRLF, as does the line with the chunk size
body = re.sub(r'\n([0-9a-f]{1,3})\n', r'\r\n\1\r\n', body)
# First chunk. Don't try to match the last newline of a previous chunk.
body = re.sub('^([0-9a-f]{1,3})\n', r'\1\r\n', body, 1)
print '\r\n\r\n'.join([header, body])
|