From ffc9adab4456dc320b02e6f26bdbd0c0da77ec3c Mon Sep 17 00:00:00 2001
From: Martijn Vermaat <martijn@vermaat.name>
Date: Wed, 26 Jan 2011 13:48:49 +0000
Subject: [PATCH] Fixed maximum size for uploaded genbank files. Added maximum
 size for batch checker input files (5MB).

mutalyzer.conf:
src/Modules/Config.py:
- Add maximum size for batch checker input files.

src/wsgi.py:
templates/batch.html:
- Fixed maximum size for uploaded genbank files. Added maximum size for
  batch checker input files (5MB).

tests/test_wsgi.py:
- Added test for a too big batch input file.



git-svn-id: https://humgenprojects.lumc.nl/svn/mutalyzer/trunk@167 eb6bd6ab-9ccd-42b9-aceb-e2899b4a52f1
---
 mutalyzer.conf         |  3 +++
 src/Modules/Config.py  |  2 ++
 src/tests/test_wsgi.py | 39 +++++++++++++++++++++++++++++++++++++++
 src/wsgi.py            | 20 ++++++++++++++++++--
 templates/batch.html   |  1 +
 5 files changed, 63 insertions(+), 2 deletions(-)

diff --git a/mutalyzer.conf b/mutalyzer.conf
index 2a35880d..1cd39d23 100644
--- a/mutalyzer.conf
+++ b/mutalyzer.conf
@@ -117,6 +117,9 @@ resultsDir = "./var/cache"
 # Location of the PID file.
 PIDfile = "./var/batch.pid"
 
+# Maximum size for uploaded batch input files in megabytes.
+batchInputMaxSize = 5
+
 # The output header for NameChecking
 nameCheckOutHeader = "Input", "Errors | Messages", "AccNo", "Genesymbol", "Variant", "Reference Sequence Start Descr.", "Coding DNA Descr.", "Protein Descr.", "GeneSymbol Coding DNA Descr.", "GeneSymbol Protein Descr.", "Genomic Reference", "Coding Reference", "Protein Reference", "Affected Transcripts", "Affected Proteins"
 
diff --git a/src/Modules/Config.py b/src/Modules/Config.py
index f0662721..62f329c4 100644
--- a/src/Modules/Config.py
+++ b/src/Modules/Config.py
@@ -108,6 +108,7 @@ class Config() :
 
         Public variables:
             - PIDfile     ; Location of the PID file.
+            - batchInputMaxSize ; Max size for batch input files in bytes.
         """
 
         pass
@@ -208,6 +209,7 @@ class Config() :
 
         # Set thte variables neede for the Batch module.
         self.Batch.PIDfile = config["PIDfile"]
+        self.Batch.batchInputMaxSize = int(config["batchInputMaxSize"]) * 1048576
 
         # Set the variables needed by the File module.
         self.File.bufSize = int(config["bufSize"])
diff --git a/src/tests/test_wsgi.py b/src/tests/test_wsgi.py
index 559cd0c9..759ffc23 100755
--- a/src/tests/test_wsgi.py
+++ b/src/tests/test_wsgi.py
@@ -361,6 +361,45 @@ class TestWSGI(unittest.TestCase):
                     size=len(variants),
                     header='Input\tStatus')
 
+    def test_batch_syntaxchecker_oldstyle(self):
+        """
+        Submit the batch syntax checker form with old style input file.
+        """
+        variants = ['AccNo\tGenesymbol\tMutation',
+                    'AB026906.1\tSDHD\tg.7872G>T',
+                    'NM_003002.1\t\tc.3_4insG',
+                    'AL449423.14\tCDKN2A_v002\tc.5_400del']
+        self._batch('SyntaxChecker',
+                    file='\n'.join(variants),
+                    size=len(variants)-1,
+                    header='Input\tStatus')
+
+    def test_batch_syntaxchecker_toobig(self):
+        """
+        Submit the batch syntax checker with a too big input file.
+        """
+        seed = """
+Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy
+nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi
+enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis
+nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in
+hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu
+feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui
+blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla
+facilisi."""
+        file = seed
+        # Very crude way of creating something at least 6MB in size
+        while len(file) < 6000000:
+            file += file
+        r = self.app.get('/batch')
+        form = r.forms[0]
+        form['batchType'] = 'SyntaxChecker'
+        form['batchEmail'] = 'm.vermaat.hg@lumc.nl'
+        form.set('batchFile', ('test_batch_toobig.txt',
+                               file))
+        r = form.submit(status=413)
+        self.assertEqual(r.content_type, 'text/plain')
+
     def test_download_py(self):
         """
         Download a Python example client for the webservice.
diff --git a/src/wsgi.py b/src/wsgi.py
index 3bac3d00..cb5059bb 100644
--- a/src/wsgi.py
+++ b/src/wsgi.py
@@ -792,9 +792,12 @@ class BatchChecker:
         """
         O = Output.Output(__file__, C.Output)
 
+        maxUploadSize = C.Batch.batchInputMaxSize
+
         attr = {"messages"      : [],
                 "errors"        : [],
                 "debug"         : [],
+                "maxSize"       : float(maxUploadSize) / 1048576,
                 "batchTypes"    : ["NameChecker",
                                    "SyntaxChecker",
                                    "PositionConverter",
@@ -823,6 +826,17 @@ class BatchChecker:
         # to the truth value False, so 'if inFile: ...' is not useful.
 
         if email and isEMail(email) and not inFile == None and inFile.file:
+
+            # Todo: These error messages could be delivered trough a template
+            if not 'CONTENT_LENGTH' in web.ctx.environ.keys():
+                web.header('Content-Type', 'text/plain')
+                web.ctx.status = '411 Length required'
+                return 'Content length required'
+            if int(web.ctx.environ.get('CONTENT_LENGTH')) > maxUploadSize:
+                web.header('Content-Type', 'text/plain')
+                web.ctx.status = '413 Request entity too large'
+                return 'Sorry, only files up to %s megabytes are accepted.' % (float(maxUploadSize) / 1048576)
+
             D = Db.Batch(C.Db)
             S = Scheduler.Scheduler(C.Scheduler, D)
             FileInstance = File.File(C.File, O)
@@ -976,11 +990,13 @@ class Uploader:
 
         try:
             if i.invoermethode == "file" :
-                if not 'Content-Length' in web.ctx.environ:
+                if not 'CONTENT_LENGTH' in web.ctx.environ.keys():
+                    web.header('Content-Type', 'text/plain')
                     web.ctx.status = '411 Length required'
                     return 'Content length required.'
                 #if
-                if int(web.ctx.environ['Content-Length']) > maxUploadSize :
+                if int(web.ctx.environ.get('CONTENT_LENGTH')) > maxUploadSize :
+                    web.header('Content-Type', 'text/plain')
                     web.ctx.status = '413 Request entity too large'
                     return 'Upload limit exceeded.'
                 #if
diff --git a/templates/batch.html b/templates/batch.html
index a83ef982..2143f8c5 100644
--- a/templates/batch.html
+++ b/templates/batch.html
@@ -16,6 +16,7 @@
           <li>Microsoft Excel file</li>
           <li>OpenOffice ODS file</li>
       </ul>
+      and the maximum size is <span tal:content = "maxSize"></span> megabytes.
       </p>
     <h5>We accept two types of input files, you can download examples below</h5>
     <h5>Old Style: 
-- 
GitLab