[Tramline-dev] Tramline and not tramlining it all

Jeroen Vloothuis jeroen.vloothuis at pareto.nl
Thu Apr 13 21:30:42 CEST 2006


Hi,
This time I return to you with a complete patch. The explicit handling
is nog configurable through a mod_python option. Just stick this above
your input filter etc. lines:

PythonOption explicit_enable True

Note that the True is actually passed as a string. So typing anything
there will result in explicitness being enabled. To disable it just
leave it our (default Tramline behaviour, nothing changed).
Regards,
        Jeroen
-------------- next part --------------
Index: core.py
===================================================================
--- core.py	(revision 25809)
+++ core.py	(working copy)
@@ -205,6 +205,8 @@
         # we use a state pattern where the handle method gets
         # replaced by the current handle method for this state.
         self.handle = self.handle_first_boundary
+        self.vars_to_handle = []
+        self._enable_vars=''
 
     def pushInput(self, data, out):
         lines = data.splitlines(True)
@@ -253,7 +255,7 @@
         self._disposition_options = {}
         self._content_type = 'text/plain'
         self._content_type_options = {}
-        
+
     def handle_headers(self, line, out):
         out.write(line)
         if line in ['\n', '\r\n']:
@@ -272,7 +274,13 @@
         filename = self._disposition_options.get('filename')
         # if filename is empty, assume no file is submitted and submit
         # empty file -- don't tramline this special case
-        if filename is None or not filename:
+        if out.req.get_options().get('explicit_enable') and \
+               self._disposition_options.get('name')=='tramline_enable':
+            self.handle = self.handle_enable_vars
+            return
+        elif (filename is None or not filename) or \
+                 out.req.get_options().get('explicit_enable') and \
+                 self._disposition_options.get('name') not in self.vars_to_handle:
             self.handle = self.handle_data
             return
         fd, pathname, file_id = createUniqueFile()
@@ -283,6 +291,19 @@
         
         self._previous_line = None
         self.handle = self.handle_file_data
+
+    def handle_enable_vars(self, line, out):
+        out.write(line)
+        if line == self._boundary:
+            self.init_headers()
+            self.handle = self.handle_headers
+            self.vars_to_handle.append(self._enable_vars.strip())
+            self._enable_vars=''
+        elif line == self._last_boundary:
+            # we should be done
+            self.handle = None # shouldn't be called again
+        else:
+            self._enable_vars+=line
         
     def handle_data(self, line, out):
         out.write(line)
Index: tests/data/input6.txt
===================================================================
--- tests/data/input6.txt	(revision 0)
+++ tests/data/input6.txt	(revision 0)
@@ -0,0 +1,16 @@
+-----------------------------100323068321119442571506749230
+Content-Disposition: form-data; name="tramline_enable"
+
+test
+-----------------------------100323068321119442571506749230
+Content-Disposition: form-data; filename="test.txt"; name="test"
+Content-Type: application/octet-stream
+
+first line
+second line
+
+-----------------------------100323068321119442571506749230
+Content-Disposition: form-data; name="submit"
+
+submit data
+-----------------------------100323068321119442571506749230--
Index: tests/test_core.py
===================================================================
--- tests/test_core.py	(revision 25809)
+++ tests/test_core.py	(working copy)
@@ -11,6 +11,10 @@
         self.headers_out = {}
         self.main = None
         self.method = method
+        self.options = {}
+
+    def get_options(self):
+        return self.options
         
 class Filter:
     def __init__(self, input, output, is_last=True, method='POST'):
@@ -348,6 +352,39 @@
         self.assertEquals('foo',
                           d['filename'])
 
+    def test_explicit_enabling(self):
+        # first make sure we fail in normal mode when explicit
+        # enabling is enabled (boy this term needs something better)
+        input = open(get_data_path('input2.txt'), 'rb')
+        output = StringIO()
+        filter = Filter(input, output)
+        filter.req.options['explicit_enable'] = True
+        
+        inputfilter(filter)
+
+        input.close()
+
+        output_data = output.getvalue()
+
+        file_id = self.file_id(output_data)
+        self.assert_(not os.path.isfile(os.path.join(tramline_upload_path, file_id)))
+
+        # now go at it again and make sure it works this time
+        input = open(get_data_path('input6.txt'), 'rb')
+        output = StringIO()
+        filter = Filter(input, output)
+        filter.req.options['explicit_enable'] = True
+        
+        inputfilter(filter)
+
+        input.close()
+
+        output_data = output.getvalue()
+
+        file_id = self.file_id(output_data)
+        self.assert_(os.path.isfile(os.path.join(tramline_upload_path, file_id)))
+        
+
     def file_id(self, data, start=0):
         i = data.find('filename', start)
         if i == -1:


More information about the Tramline-dev mailing list