← Back to team overview

dulwich-users team mailing list archive

[PATCH 2/9] web: Handle empty-string CONTENT_LENGTH.

 

From: Dave Borowitz <dborowitz@xxxxxxxxxx>

Change-Id: I02711c5ed03633489809a94659397676920056af
---
 NEWS                      |    4 ++++
 dulwich/tests/test_web.py |   18 ++++++++++--------
 dulwich/web.py            |    5 +++--
 3 files changed, 17 insertions(+), 10 deletions(-)

diff --git a/NEWS b/NEWS
index d953112..c710b83 100644
--- 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)
diff --git a/dulwich/tests/test_web.py b/dulwich/tests/test_web.py
index 0ac31fb..6a2f7c1 100644
--- a/dulwich/tests/test_web.py
+++ b/dulwich/tests/test_web.py
@@ -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'
diff --git a/dulwich/web.py b/dulwich/web.py
index b74f9d8..d7ac60b 100644
--- a/dulwich/web.py
+++ b/dulwich/web.py
@@ -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()
-- 
1.7.1




References