Skip to content
Myr

Merge pull request #3068 from pallets/method-override-encoding

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

remove encoding from method override pattern

authored by David Lord · committed by GitHub · parents ded7cbe, 2c29495 · Browse files · 1 file+5 added−6 removed
This commit

State

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

Diff stats

1 file+5 added−6 removed

  • other:+5 added−6 removed

1 file+5 added−6 removed

modified:docs/patterns/methodoverrides.rst+5 added-6 removed
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” 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``).9set the ``X-HTTP-Method-Override`` header. Then the method is replaced10with the header value before being passed to Flask.1112This can easily be accomplished with an HTTP middleware::12This can be accomplished with an HTTP middleware::1314 class HTTPMethodOverrideMiddleware(object):15 allowed_methods = frozenset([16 'GET',
11 unmodified lines
2829 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')32 environ['REQUEST_METHOD'] = method33 if method in self.bodyless_methods:34 environ['CONTENT_LENGTH'] = '0'35 return self.app(environ, start_response)3638To use this with Flask this is all that is necessary::37To use this with Flask, wrap the app object with the middleware::3839 from flask import Flask4041 app = Flask(__name__)
1 unmodified line