|
1 | 1 | from flask.views import MethodView, http_method_funcs
|
2 |
| -from flask import request |
| 2 | +from flask import request, abort |
3 | 3 | from werkzeug.wrappers import Response as ResponseBase
|
4 | 4 | from werkzeug.exceptions import BadRequest
|
5 | 5 |
|
@@ -107,19 +107,29 @@ def get_value(self):
|
107 | 107 | else: # Unless somehow an HTTP response isn't returned...
|
108 | 108 | return response
|
109 | 109 |
|
| 110 | + def _find_request_method(self): |
| 111 | + meth = getattr(self, request.method.lower(), None) |
| 112 | + if meth is None and request.method == "HEAD": |
| 113 | + meth = getattr(self, "get", None) |
| 114 | + |
| 115 | + # Handle the case of a GET request asking for WS upgrade where |
| 116 | + # no websocket method is defined on the view |
| 117 | + if request.method == "GET" and request.environ.get("wsgi.websocket"): |
| 118 | + ws_meth = getattr(self, "websocket", None) |
| 119 | + if ws_meth is None: |
| 120 | + abort(400, "Unable to upgrade websocket connection") |
| 121 | + return ws_meth |
| 122 | + |
| 123 | + return meth |
| 124 | + |
110 | 125 | def dispatch_request(self, *args, **kwargs):
|
111 | 126 | """
|
112 | 127 |
|
113 | 128 | :param *args:
|
114 | 129 | :param **kwargs:
|
115 | 130 |
|
116 | 131 | """
|
117 |
| - meth = getattr(self, request.method.lower(), None) |
118 |
| - |
119 |
| - # If the request method is HEAD and we don't have a handler for it |
120 |
| - # retry with GET. |
121 |
| - if meth is None and request.method == "HEAD": |
122 |
| - meth = getattr(self, "get", None) |
| 132 | + meth = self._find_request_method() |
123 | 133 |
|
124 | 134 | # Generate basic response
|
125 | 135 | return self.represent_response(meth(*args, **kwargs))
|
@@ -272,7 +282,7 @@ def dispatch_request(self, *args, **kwargs):
|
272 | 282 | :param **kwargs:
|
273 | 283 |
|
274 | 284 | """
|
275 |
| - meth = getattr(self, request.method.lower(), None) |
| 285 | + meth = self._find_request_method() |
276 | 286 |
|
277 | 287 | # Let base View handle non-POST requests
|
278 | 288 | if request.method != "POST":
|
@@ -414,12 +424,7 @@ def dispatch_request(self, *args, **kwargs):
|
414 | 424 | :param **kwargs:
|
415 | 425 |
|
416 | 426 | """
|
417 |
| - meth = getattr(self, request.method.lower(), None) |
418 |
| - |
419 |
| - # If the request method is HEAD and we don't have a handler for it |
420 |
| - # retry with GET. |
421 |
| - if meth is None and request.method == "HEAD": |
422 |
| - meth = getattr(self, "get", None) |
| 427 | + meth = self._find_request_method() |
423 | 428 |
|
424 | 429 | # POST and PUT methods can be used to write properties
|
425 | 430 | # In all other cases, ignore arguments
|
|
0 commit comments