Skip to content
Myr

Added HTTP override middleware to docs. This fixes #582

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent b2cf9e7 · Browse files · 2 files+44 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+44 added−0 removed

  • other:+44 added−0 removed

2 files+44 added−0 removed

modified:docs/patterns/index.rst+1 added
35 unmodified lines
36 mongokit37 favicon38 streaming39 deferredcallbacks40 methodoverrides
added:docs/patterns/methodoverrides.rst+43 added
1Adding HTTP Method Overrides2============================34Some HTTP proxies do not support arbitrary HTTP methods or newer HTTP5methods (such as PATCH). In that case it's possible to “proxy” HTTP6methods through another HTTP method in total violation of the protocol.78The way this works is by letting the client do an HTTP POST request and9set the ``X-HTTP-Method-Override`` header and set the value to the10intended HTTP method (such as ``PATCH``).1112This can easily be accomplished with an HTTP middleware::1314 class HTTPMethodOverrideMiddleware(object):15 allowed_methods = frozenset([16 'GET',17 'HEAD',18 'POST',19 'DELETE',20 'PUT',21 'PATCH',22 'OPTIONS'23 ])24 bodyless_methods = frozenset(['GET', 'HEAD', 'OPTIONS', 'DELETE'])2526 def __init__(self, app):27 self.app = app2829 def __call__(self, environ, start_response):30 method = environ.get('HTTP_X_HTTP_METHOD_OVERRIDE', '').upper()31 if method in self.allowed_methods:32 method = method.encode('ascii', 'replace')33 environ['REQUEST_METHOD'] = method34 if method in self.bodyless_methods:35 environ['CONTENT_LENGTH'] = '0'36 return self.app(environ, start_response)3738To use this with Flask this is all that is necessary::3940 from flask import Flask4142 app = Flask(__name__)43 app.wsgi_app = HTTPMethodOverrideMiddleware(app.wsgi_app)