Created
December 28, 2009 12:58
-
-
Save asplake/264659 to your computer and use it in GitHub Desktop.
Easy 405's and OPTIONS in Pylons
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Changes to your config/middleware.py: | |
# -from pylons.wsgiapp import PylonsApp | |
# +from pylons.wsgiapp import PylonsApp as _PylonsApp | |
# +from webob.exc import HTTPMethodNotAllowed, HTTPNotFound, HTTPOk | |
# Then: | |
methods = ['GET', 'PUT', 'POST', 'DELETE'] | |
class PylonsApp(_PylonsApp): | |
def dispatch(self, controller, environ, start_response): | |
if not controller: | |
try: | |
old_method = environ['REQUEST_METHOD'] | |
allowed_methods=[] | |
mapper = config['routes.map'] | |
for method in methods: | |
environ['REQUEST_METHOD'] = method | |
if mapper.match(environ['PATH_INFO']): | |
allowed_methods.append(method) | |
if allowed_methods: | |
exc = HTTPOk() if old_method =='OPTIONS' else | |
HTTPMethodNotAllowed() | |
exc.allow = allowed_methods | |
return exc(environ, start_response) | |
finally: | |
environ['REQUEST_METHOD'] = old_method | |
return HTTPNotFound()(environ, start_response) | |
else: | |
return _PylonsApp.dispatch(self, controller, environ, | |
start_response) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment