-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(http_method_override.py): Create the HTTPMethodOverrideMiddlewar…
…e, Responsible for creating support to accept PUT, DELETE and PATCH HTTP verbs.
- Loading branch information
Showing
16 changed files
with
347 additions
and
97 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from flask import Request | ||
from werkzeug.formparser import parse_form_data | ||
|
||
|
||
class HTTPMethodOverrideMiddleware: | ||
allowed_methods = frozenset( | ||
[ | ||
"GET", | ||
"POST", | ||
"DELETE", | ||
"PUT", | ||
"PATCH", | ||
] | ||
) | ||
bodyless_methods = frozenset(["GET", "HEAD", "OPTIONS", "DELETE"]) | ||
|
||
def __init__(self, app, input_name="_method"): | ||
self.app = app | ||
self.input_name = input_name | ||
|
||
def __call__(self, environ, start_response): | ||
if environ["REQUEST_METHOD"].upper() == "POST": | ||
stream, form, files = parse_form_data(environ) | ||
method = (form.get(self.input_name) or "").upper() | ||
|
||
if method in self.allowed_methods: | ||
environ["wsgi._post_form"] = form | ||
environ["wsgi._post_files"] = files | ||
environ["REQUEST_METHOD"] = method | ||
|
||
if method in self.bodyless_methods: | ||
environ["CONTENT_LENGTH"] = "0" | ||
|
||
return self.app(environ, start_response) | ||
|
||
|
||
class CustomRequest(Request): | ||
@property | ||
def form(self): | ||
if "wsgi._post_form" in self.environ: | ||
return self.environ["wsgi._post_form"] | ||
return super().form | ||
|
||
@property | ||
def files(self): | ||
if "wsgi._post_files" in self.environ: | ||
return self.environ["wsgi._post_files"] | ||
return super().files |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.