Commits

Dustin Hatch  committed 4851b25

Respect the allowed methods of controller callables

  • Participants
  • Parent commits 852abea

Comments (0)

Files changed (1)

File src/milla/app.py

 
 from milla.controllers import FaviconController
 from milla.util import asbool
-from webob.exc import HTTPNotFound, WSGIHTTPException
+from webob.exc import HTTPNotFound, WSGIHTTPException, HTTPMethodNotAllowed
 import milla.dispatch
 import webob
 
        configuration is copied and assigned to ``request.config``.
     '''
 
+    DEFAULT_ALLOWED_METHODS = ['GET', 'HEAD', 'OPTIONS']
+
     def __init__(self, dispatcher):
         self.dispatcher = dispatcher
         self.config = {'milla.favicon': True}
             else:
                 return HTTPNotFound()(environ, start_response)
 
+        allowed_methods = getattr(func, 'allowed_methods',
+                                  self.DEFAULT_ALLOWED_METHODS)
+        if environ['REQUEST_METHOD'] not in allowed_methods:
+            allow_header = {'Allow': ', '.join(allowed_methods)}
+            if environ['REQUEST_METHOD'] == 'OPTIONS':
+                def options_response(request, *args, **kwargs):
+                    response = request.ResponseClass()
+                    response.headers = allow_header
+                    return response
+                func = options_response
+            return HTTPMethodNotAllowed(headers=allow_header)(environ,
+                                                              start_response)
+
         request = webob.Request(environ)
         request.config = self.config.copy()
 
 
         if not start_response_wrapper.called:
             start_response(response.status, response.headerlist)
-        return response.app_iter
+        if not environ['REQUEST_METHOD'] == 'HEAD':
+            return response.app_iter
 
 class StartResponseWrapper():