Skip to content

Commit

Permalink
fix WSGI root app (#26549)
Browse files Browse the repository at this point in the history
* fix WSGI root app

The `start_response()` callable expects a string in argument `status`.
See <https://peps.python.org/pep-0333/#the-start-response-callable>.

Signed-off-by: Steffen Prohaska <prohaska@zib.de>

* Better type WSGI middleware

This provides typing guardrails for the middleware callable.

Signed-off-by: Steffen Prohaska <prohaska@zib.de>
Co-authored-by: Tzu-ping Chung <uranusjr@gmail.com>
  • Loading branch information
sprohaska and uranusjr committed Sep 22, 2022
1 parent fe356be commit 39f99ec
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions airflow/www/extensions/init_wsgi_middlewares.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

from __future__ import annotations

from typing import TYPE_CHECKING, Iterable
from urllib.parse import urlparse

from flask import Flask
Expand All @@ -24,22 +26,24 @@

from airflow.configuration import conf

if TYPE_CHECKING:
from _typeshed.wsgi import StartResponse, WSGIEnvironment


def _root_app(_, resp):
resp(b'404 Not Found', [('Content-Type', 'text/plain')])
def _root_app(env: WSGIEnvironment, resp: StartResponse) -> Iterable[bytes]:
resp('404 Not Found', [('Content-Type', 'text/plain')])
return [b'Apache Airflow is not at this location']


def init_wsgi_middleware(flask_app: Flask):
def init_wsgi_middleware(flask_app: Flask) -> None:
"""Handle X-Forwarded-* headers and base_url support"""
# Apply DispatcherMiddleware
base_url = urlparse(conf.get('webserver', 'base_url'))[2]
if not base_url or base_url == '/':
base_url = ""
if base_url:
flask_app.wsgi_app = DispatcherMiddleware( # type: ignore
_root_app, mounts={base_url: flask_app.wsgi_app} # type: ignore
)
wsgi_app = DispatcherMiddleware(_root_app, mounts={base_url: flask_app.wsgi_app})
flask_app.wsgi_app = wsgi_app # type: ignore[assignment]

# Apply ProxyFix middleware
if conf.getboolean('webserver', 'ENABLE_PROXY_FIX'):
Expand Down

0 comments on commit 39f99ec

Please sign in to comment.