diff --git a/src/milla/app.py b/src/milla/app.py index ad25c5f..40ec801 100644 --- a/src/milla/app.py +++ b/src/milla/app.py @@ -23,7 +23,7 @@ Please give me a docstring! 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 @@ -48,6 +48,8 @@ class Application(object): 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} @@ -63,6 +65,19 @@ class Application(object): 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() @@ -111,7 +126,8 @@ class Application(object): 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():