This repository has been archived by the owner on May 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathflask_modus.py
62 lines (46 loc) · 1.71 KB
/
flask_modus.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# -*- coding: utf-8 -*-
from werkzeug import url_decode
class Middleware(object):
""" WSGI Method Overriding Middleware """
allowed_methods = frozenset([
'GET',
'HEAD',
'POST',
'DELETE',
'PUT',
'PATCH',
'OPTIONS'
])
bodyless_methods = frozenset(['GET', 'HEAD', 'OPTIONS', 'DELETE'])
def __init__(self, app=None, input_name=''):
self.app = app
self.input_name = input_name
def __call__(self, environ, response):
""" Extract the new method from the Query String """
def set_method(method):
if method in self.allowed_methods:
method = method.encode('ascii', 'replace')
environ['REQUEST_METHOD'] = method
if method in self.bodyless_methods:
environ['CONTENT_LENGTH'] = '0'
# Handle our Header Method
method = environ.get('HTTP_X_HTTP_METHOD_OVERRIDE', '').upper()
if method in self.allowed_methods:
set_method(method)
return self.app(environ, response)
# Handle our Query String Method setter
if '_method' in environ.get('QUERY_STRING', ''):
args = url_decode(environ['QUERY_STRING'])
method = args.get('_method', '').upper()
if method in self.allowed_methods:
set_method(method)
return self.app(environ, response)
class Modus(object):
""" Enables Flask Method Overriding """
def __init__(self, app=None):
self.app = app
if self.app:
self.init_app(self.app)
def init_app(self, app):
""" Configures the Flask Overriding Middleware """
app.wsgi_app = Middleware(app.wsgi_app)