Skip to content

Instantly share code, notes, and snippets.

@alterakey
Created September 3, 2011 13:48
Show Gist options
  • Save alterakey/1191213 to your computer and use it in GitHub Desktop.
Save alterakey/1191213 to your computer and use it in GitHub Desktop.
Paste 1.7.5.1 base64 CTE decode patch
From 381a1a0022da60a46542d86cd210cad41a37c204 Mon Sep 17 00:00:00 2001
From: Takahiro Yoshimura <[email protected]>
Date: Sat, 3 Sep 2011 22:43:31 +0900
Subject: [PATCH] Decoding base64 CTE
---
paste/request.py | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 78 insertions(+), 0 deletions(-)
diff --git a/paste/request.py b/paste/request.py
index 9af494d..3f5a544 100644
--- a/paste/request.py
+++ b/paste/request.py
@@ -406,6 +406,84 @@ def _cgi_FieldStorage__repr__patch(self):
cgi.FieldStorage.__repr__ = _cgi_FieldStorage__repr__patch
+# Monkey-patch cgi.FS to recognize and decode base64 CTE.
+# Compatible with Python 2.7.
+import base64
+
+def _cgi_FieldStorage_is_base64_patch(self):
+ return self.headers.get('content-transfer-encoding', None) == 'base64'
+
+def _cgi_FieldStorage_read_binary_patch(self):
+ """Internal: read binary data."""
+ self.file = self.make_file('b')
+ todo = self.length
+ if todo >= 0:
+ while todo > 0:
+ data = self.fp.read(min(todo, self.bufsize))
+ if not data:
+ self.done = -1
+ break
+ if not self.is_base64():
+ self.file.write(data)
+ else:
+ self.file.write(base64.b64decode(data))
+ todo = todo - len(data)
+
+def _cgi_FieldStorage_read_lines_to_eof_patch(self):
+ """Internal: read lines until EOF."""
+ while 1:
+ line = self.fp.readline(1<<16)
+ if not line:
+ self.done = -1
+ break
+ if not self.is_base64():
+ self._FieldStorage__write(line)
+ else:
+ self._FieldStorage__write(base64.b64decode(line))
+
+def _cgi_FieldStorage_read_lines_to_outerboundary_patch(self):
+ """Internal: read lines until outerboundary."""
+ next = "--" + self.outerboundary
+ last = next + "--"
+ delim = ""
+ last_line_lfend = True
+ while 1:
+ line = self.fp.readline(1<<16)
+ if not line:
+ self.done = -1
+ break
+ if line[:2] == "--" and last_line_lfend:
+ strippedline = line.strip()
+ if strippedline == next:
+ break
+ if strippedline == last:
+ self.done = 1
+ break
+ odelim = delim
+ if line[-2:] == "\r\n":
+ delim = "\r\n"
+ line = line[:-2]
+ last_line_lfend = True
+ elif line[-1] == "\n":
+ delim = "\n"
+ line = line[:-1]
+ last_line_lfend = True
+ else:
+ delim = ""
+ last_line_lfend = False
+ if not self.is_base64():
+ self._FieldStorage__write(odelim + line)
+ else:
+ try:
+ self._FieldStorage__write(base64.b64decode(line))
+ except TypeError:
+ self._FieldStorage__write(odelim + line)
+
+setattr(cgi.FieldStorage, 'is_base64', _cgi_FieldStorage_is_base64_patch)
+setattr(cgi.FieldStorage, 'read_binary', _cgi_FieldStorage_read_binary_patch)
+setattr(cgi.FieldStorage, 'read_lines_to_eof', _cgi_FieldStorage_read_lines_to_eof_patch)
+setattr(cgi.FieldStorage, 'read_lines_to_outerboundary', _cgi_FieldStorage_read_lines_to_outerboundary_patch)
+
if __name__ == '__main__':
import doctest
doctest.testmod()
--
1.7.5.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment