Patch requests that try to use POST "emulate" some other verb like PUT

master 0.1
Dustin C. Hatch 2011-05-26 02:00:13 +00:00
parent b13bbe9abb
commit 47cbd5c2d2
1 changed files with 11 additions and 5 deletions

View File

@ -65,11 +65,20 @@ class Application(object):
else: else:
return HTTPNotFound()(environ, start_response) return HTTPNotFound()(environ, start_response)
request = webob.Request(environ)
request.config = self.config.copy()
# Sometimes, hacky applications will try to "emulate" some HTTP
# method like POST or DELETE by specifying an _method parameter
# in a POST request.
if request.method == 'POST' and '_method' in request.POST:
request.method = request.POST.pop('_method')
allowed_methods = getattr(func, 'allowed_methods', allowed_methods = getattr(func, 'allowed_methods',
self.DEFAULT_ALLOWED_METHODS) self.DEFAULT_ALLOWED_METHODS)
if environ['REQUEST_METHOD'] not in allowed_methods: if request.method not in allowed_methods:
allow_header = {'Allow': ', '.join(allowed_methods)} allow_header = {'Allow': ', '.join(allowed_methods)}
if environ['REQUEST_METHOD'] == 'OPTIONS': if request.method == 'OPTIONS':
def options_response(request, *args, **kwargs): def options_response(request, *args, **kwargs):
response = request.ResponseClass() response = request.ResponseClass()
response.headers = allow_header response.headers = allow_header
@ -78,9 +87,6 @@ class Application(object):
return HTTPMethodNotAllowed(headers=allow_header)(environ, return HTTPMethodNotAllowed(headers=allow_header)(environ,
start_response) start_response)
request = webob.Request(environ)
request.config = self.config.copy()
start_response_wrapper = StartResponseWrapper(start_response) start_response_wrapper = StartResponseWrapper(start_response)
request.start_response = start_response_wrapper request.start_response = start_response_wrapper
try: try: