Procházet zdrojové kódy

Merge support for fallback_app argument to HTTPGitApplication.

Jelmer Vernooij před 12 roky
rodič
revize
c63ddf638b
3 změnil soubory, kde provedl 22 přidání a 4 odebrání
  1. 5 0
      NEWS
  2. 7 0
      dulwich/tests/test_web.py
  3. 10 4
      dulwich/web.py

+ 5 - 0
NEWS

@@ -5,9 +5,14 @@
   * dulwich.__init__ no longer imports client, protocol, repo and
     server modules. (Jelmer Vernooij)
 
+ FEATURES
+
   * ConfigDict now behaves more like a dictionary.
     (Adam 'Cezar' Jenkins, issue #58)
 
+  * HTTPGitApplication now takes an optional
+    `fallback_app` argument. (Jonas Haag, issue #67)
+
  TESTING
 
   * Make index entry tests a little bit less strict, to cope with

+ 7 - 0
dulwich/tests/test_web.py

@@ -443,6 +443,13 @@ class HTTPGitApplicationTestCase(TestCase):
         self._add_handler(self._app)
         self.assertEqual('output', self._app(self._environ, None))
 
+    def test_fallback_app(self):
+        def test_app(environ, start_response):
+            return 'output'
+
+        app = HTTPGitApplication('backend', fallback_app=test_app)
+        self.assertEqual('output', app(self._environ, None))
+
 
 class GunzipTestCase(HTTPGitApplicationTestCase):
     """TestCase for testing the GunzipFilter, ensuring the wsgi.input

+ 10 - 4
dulwich/web.py

@@ -318,10 +318,11 @@ class HTTPGitApplication(object):
       ('POST', re.compile('/git-receive-pack$')): handle_service_request,
     }
 
-    def __init__(self, backend, dumb=False, handlers=None):
+    def __init__(self, backend, dumb=False, handlers=None, fallback_app=None):
         self.backend = backend
         self.dumb = dumb
         self.handlers = dict(DEFAULT_HANDLERS)
+        self.fallback_app = fallback_app
         if handlers is not None:
             self.handlers.update(handlers)
 
@@ -339,8 +340,13 @@ class HTTPGitApplication(object):
             if mat:
                 handler = self.services[smethod, spath]
                 break
+
         if handler is None:
-            return req.not_found('Sorry, that method is not supported')
+            if self.fallback_app is not None:
+                return self.fallback_app(environ, start_response)
+            else:
+                return req.not_found('Sorry, that method is not supported')
+
         return handler(req, self.backend, mat)
 
 
@@ -382,11 +388,11 @@ class LimitedInputFilter(object):
         return self.app(environ, start_response)
 
 
-def make_wsgi_chain(backend, dumb=False, handlers=None):
+def make_wsgi_chain(*args, **kwargs):
     """Factory function to create an instance of HTTPGitApplication,
     correctly wrapped with needed middleware.
     """
-    app = HTTPGitApplication(backend, dumb, handlers)
+    app = HTTPGitApplication(*args, **kwargs)
     wrapped_app = GunzipFilter(LimitedInputFilter(app))
     return wrapped_app