web: Handle empty-string CONTENT_LENGTH.
authorDave Borowitz <dborowitz@google.com>
Mon, 26 Jul 2010 17:17:27 +0000 (19:17 +0200)
committerJelmer Vernooij <jelmer@samba.org>
Mon, 26 Jul 2010 17:17:27 +0000 (19:17 +0200)
NEWS
dulwich/tests/test_web.py
dulwich/web.py

diff --git a/NEWS b/NEWS
index d953112cc0c9b652622c3460a326fe34466dc65b..c710b833894ff9d7afb0025608d531424cb6af8e 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -1,5 +1,9 @@
 0.6.2  UNRELEASED
 
+ BUG FIXES
+
+  * HTTP server correctly handles empty CONTENT_LENGTH. (Dave Borowitz)
+
  FEATURES
 
   * Use slots for core objects to save up on memory. (Jelmer Vernooij)
index 0ac31fbc33a6028194790c6d27a050834cd1a015..6a2f7c101920ffff5f35456388cf9d6135d976a9 100644 (file)
@@ -192,8 +192,10 @@ class SmartHandlersTestCase(WebTestCase):
         list(handle_service_request(self._req, 'backend', mat))
         self.assertEquals(HTTP_FORBIDDEN, self._status)
 
-    def test_handle_service_request(self):
+    def _run_handle_service_request(self, content_length=None):
         self._environ['wsgi.input'] = StringIO('foo')
+        if content_length is not None:
+            self._environ['CONTENT_LENGTH'] = content_length
         mat = re.search('.*', '/git-upload-pack')
         output = ''.join(handle_service_request(self._req, 'backend', mat))
         self.assertEqual('handled input: foo', output)
@@ -202,14 +204,14 @@ class SmartHandlersTestCase(WebTestCase):
         self.assertFalse(self._handler.advertise_refs)
         self.assertTrue(self._handler.stateless_rpc)
 
+    def test_handle_service_request(self):
+        self._run_handle_service_request()
+
     def test_handle_service_request_with_length(self):
-        self._environ['wsgi.input'] = StringIO('foobar')
-        self._environ['CONTENT_LENGTH'] = 3
-        mat = re.search('.*', '/git-upload-pack')
-        output = ''.join(handle_service_request(self._req, 'backend', mat))
-        self.assertEqual('handled input: foo', output)
-        response_type = 'application/x-git-upload-pack-response'
-        self.assertTrue(('Content-Type', response_type) in self._headers)
+        self._run_handle_service_request(content_length='3')
+
+    def test_handle_service_request_empty_length(self):
+        self._run_handle_service_request(content_length='')
 
     def test_get_info_refs_unknown(self):
         self._environ['QUERY_STRING'] = 'service=git-evil-handler'
index b74f9d898ac014c0bea20e48217b5a9c939ad14d..d7ac60b15e50199f9bf2a5aa22842ba74da237d0 100644 (file)
@@ -238,8 +238,9 @@ def handle_service_request(req, backend, mat):
     # Unfortunately, there's no way to tell that at this point.
     # TODO: git may used HTTP/1.1 chunked encoding instead of specifying
     # content-length
-    if 'CONTENT_LENGTH' in req.environ:
-        input = _LengthLimitedFile(input, int(req.environ['CONTENT_LENGTH']))
+    content_length = req.environ.get('CONTENT_LENGTH', '')
+    if content_length:
+        input = _LengthLimitedFile(input, int(content_length))
     proto = ReceivableProtocol(input.read, output.write)
     handler = handler_cls(backend, [url_prefix(mat)], proto, stateless_rpc=True)
     handler.handle()