Respect the allowed methods of controller callables

master
Dustin C. Hatch 2011-04-16 11:57:14 -05:00
parent 92ef80fd61
commit 4c4ecbfe52
1 changed files with 18 additions and 2 deletions

View File

@ -23,7 +23,7 @@ Please give me a docstring!
from milla.controllers import FaviconController from milla.controllers import FaviconController
from milla.util import asbool from milla.util import asbool
from webob.exc import HTTPNotFound, WSGIHTTPException from webob.exc import HTTPNotFound, WSGIHTTPException, HTTPMethodNotAllowed
import milla.dispatch import milla.dispatch
import webob import webob
@ -48,6 +48,8 @@ class Application(object):
configuration is copied and assigned to ``request.config``. configuration is copied and assigned to ``request.config``.
''' '''
DEFAULT_ALLOWED_METHODS = ['GET', 'HEAD', 'OPTIONS']
def __init__(self, dispatcher): def __init__(self, dispatcher):
self.dispatcher = dispatcher self.dispatcher = dispatcher
self.config = {'milla.favicon': True} self.config = {'milla.favicon': True}
@ -63,6 +65,19 @@ class Application(object):
else: else:
return HTTPNotFound()(environ, start_response) 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 = webob.Request(environ)
request.config = self.config.copy() request.config = self.config.copy()
@ -111,7 +126,8 @@ class Application(object):
if not start_response_wrapper.called: if not start_response_wrapper.called:
start_response(response.status, response.headerlist) start_response(response.status, response.headerlist)
return response.app_iter if not environ['REQUEST_METHOD'] == 'HEAD':
return response.app_iter
class StartResponseWrapper(): class StartResponseWrapper():