Respect the allowed methods of controller callables
parent
92ef80fd61
commit
4c4ecbfe52
|
@ -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,6 +126,7 @@ 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)
|
||||||
|
if not environ['REQUEST_METHOD'] == 'HEAD':
|
||||||
return response.app_iter
|
return response.app_iter
|
||||||
|
|
||||||
class StartResponseWrapper():
|
class StartResponseWrapper():
|
||||||
|
|
Loading…
Reference in New Issue