From 47cbd5c2d2bae8fa94de15c1065edbd82eb96baa Mon Sep 17 00:00:00 2001 From: "Dustin C. Hatch" Date: Thu, 26 May 2011 02:00:13 +0000 Subject: [PATCH] Patch requests that try to use POST "emulate" some other verb like PUT --- src/milla/app.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/milla/app.py b/src/milla/app.py index 40ec801..e57eed6 100644 --- a/src/milla/app.py +++ b/src/milla/app.py @@ -65,11 +65,20 @@ class Application(object): else: 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', 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)} - if environ['REQUEST_METHOD'] == 'OPTIONS': + if request.method == 'OPTIONS': def options_response(request, *args, **kwargs): response = request.ResponseClass() response.headers = allow_header @@ -78,9 +87,6 @@ class Application(object): return HTTPMethodNotAllowed(headers=allow_header)(environ, start_response) - request = webob.Request(environ) - request.config = self.config.copy() - start_response_wrapper = StartResponseWrapper(start_response) request.start_response = start_response_wrapper try: