feat(router): establish shared router/view/controller global variables
- add ssl warning
This commit is contained in:
parent
47cb58c9b1
commit
68d9240c0c
11 changed files with 121 additions and 18 deletions
|
|
@ -152,6 +152,7 @@ from httpaste.model import Config as ModelConfig
|
|||
from httpaste.backend import get_backend_config
|
||||
from httpaste.backend import Config as BackendConfig
|
||||
from httpaste.helper.config import get_configparser, CONFIGPATH_ENVIRON
|
||||
from httpaste.helper.url import url_upgrade_to_https
|
||||
from httpaste.helper.http import (
|
||||
BadRequestError,
|
||||
ForbiddenError,
|
||||
|
|
@ -238,6 +239,22 @@ def get_flask_app(config: Config) -> FlaskApp:
|
|||
response.headers['WWW-Authenticate'] = 'Basic realm="private"'
|
||||
return response
|
||||
|
||||
@application.app.before_request
|
||||
def before_request_func():
|
||||
from flask import request
|
||||
|
||||
request._view = {}
|
||||
|
||||
if config.server.request_ssl:
|
||||
|
||||
https_url = url_upgrade_to_https(request.url, config.server.ssl_port)
|
||||
|
||||
if https_url != request.url:
|
||||
|
||||
print('Hallo')
|
||||
|
||||
request._view['before_request__ssl_url'] = https_url
|
||||
|
||||
return application
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
from httpaste.helper.template import views
|
||||
from httpaste.helper.template import views, render_template_with_context
|
||||
from httpaste import __doc__ as man_page
|
||||
|
||||
from flask import current_app
|
||||
|
||||
def search(**kwargs):
|
||||
|
||||
template = views.get_template("viewport/ui/search.html")
|
||||
|
|
@ -13,4 +15,7 @@ def search(**kwargs):
|
|||
'delete_session_url': '/ui/user/session/delete'
|
||||
}
|
||||
|
||||
return template.render(**variables), 200
|
||||
with current_app.app_context():
|
||||
view_render = render_template_with_context(template, **variables)
|
||||
|
||||
return view_render, 200
|
||||
|
|
@ -2,8 +2,9 @@ from io import BytesIO
|
|||
from base64 import b64encode
|
||||
|
||||
from connexion import request
|
||||
from flask import current_app
|
||||
|
||||
from httpaste.helper.template import views
|
||||
from httpaste.helper.template import views, render_template_with_context
|
||||
from httpaste.helper.url import url_query_string, url_append_query_param
|
||||
from httpaste.helper.syntax import syntax_shortnames, format_shortnames
|
||||
from httpaste.helper.http import mime_types
|
||||
|
|
@ -23,7 +24,10 @@ def search(**kwargs):
|
|||
'delete_session_url': '/ui/user/session/delete'
|
||||
}
|
||||
|
||||
return template.render(**variables), 200
|
||||
with current_app.app_context():
|
||||
view_render = render_template_with_context(template, **variables)
|
||||
|
||||
return view_render, 200
|
||||
|
||||
|
||||
def post(**kwargs):
|
||||
|
|
@ -93,4 +97,7 @@ def get(**kwargs):
|
|||
'mime_types': mime_types()
|
||||
}
|
||||
|
||||
return template.render(**variables)
|
||||
with current_app.app_context():
|
||||
view_render = render_template_with_context(template, **variables)
|
||||
|
||||
return view_render, 200
|
||||
|
|
@ -1,4 +1,6 @@
|
|||
from httpaste.helper.template import views
|
||||
from flask import current_app
|
||||
|
||||
from httpaste.helper.template import views, render_template_with_context
|
||||
from httpaste.controller.ui.paste import post as post_proxy
|
||||
from httpaste.controller.ui.paste import get as get_proxy
|
||||
|
||||
|
|
@ -8,10 +10,12 @@ def search(**kwargs):
|
|||
|
||||
variables = {
|
||||
'paste_form_url': '/ui/paste/private',
|
||||
'user': kwargs.get('user')
|
||||
}
|
||||
|
||||
return template.render(**variables), 200
|
||||
with current_app.app_context():
|
||||
view_render = render_template_with_context(template, **variables)
|
||||
|
||||
return view_render, 200
|
||||
|
||||
|
||||
def post(**kwargs):
|
||||
|
|
|
|||
|
|
@ -1,4 +1,6 @@
|
|||
from httpaste.helper.template import views
|
||||
from flask import current_app
|
||||
|
||||
from httpaste.helper.template import views, render_template_with_context
|
||||
from httpaste.controller.ui.paste import post as post_proxy
|
||||
from httpaste.controller.ui.paste import get as get_proxy
|
||||
|
||||
|
|
@ -10,7 +12,10 @@ def search(**kwargs):
|
|||
'paste_form_url': '/ui/paste/public'
|
||||
}
|
||||
|
||||
return template.render(**variables), 200
|
||||
with current_app.app_context():
|
||||
view_render = render_template_with_context(template, **variables)
|
||||
|
||||
return view_render, 200
|
||||
|
||||
|
||||
def post(**kwargs):
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
from httpaste.helper.template import views
|
||||
from httpaste import __doc__ as man_page
|
||||
from flask import current_app
|
||||
|
||||
from httpaste.helper.template import views, render_template_with_context
|
||||
|
||||
def search(**kwargs):
|
||||
|
||||
|
|
@ -9,4 +10,7 @@ def search(**kwargs):
|
|||
'delete_session_url': '/ui/user/session/delete'
|
||||
}
|
||||
|
||||
return template.render(**variables), 200
|
||||
with current_app.app_context():
|
||||
view_render = render_template_with_context(template, **variables)
|
||||
|
||||
return view_render, 200
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
from httpaste.helper.template import views
|
||||
from httpaste.helper.template import views, render_template_with_context
|
||||
from httpaste.controller.user.session import delete as raw_delete
|
||||
|
||||
from connexion import request
|
||||
|
|
@ -7,11 +7,12 @@ def search(**kwargs):
|
|||
|
||||
template = views.get_template("viewport/ui/user/session/search.html")
|
||||
|
||||
print(request.path)
|
||||
|
||||
variables = {'session_delete_url': request.path + '/delete'}
|
||||
|
||||
return template.render(**variables), 200
|
||||
with current_app.app_context():
|
||||
view_render = render_template_with_context(template, **variables)
|
||||
|
||||
return view_render, 200
|
||||
|
||||
|
||||
def delete(**kwargs):
|
||||
|
|
|
|||
|
|
@ -1,6 +1,25 @@
|
|||
from jinja2 import Environment, PackageLoader, select_autoescape
|
||||
from collections import namedtuple
|
||||
|
||||
views = Environment(
|
||||
loader=PackageLoader("httpaste", "view"),
|
||||
autoescape=select_autoescape()
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def render_template_with_context(template: object, **kwargs):
|
||||
"""render a template with global context variables
|
||||
|
||||
the definition of a global context is abstract, it does neither apply
|
||||
to Flask, nor to Jinja2 and only acts as a bridge for passing
|
||||
variables from flask to jinja2, without having to define them within
|
||||
each controller.
|
||||
|
||||
:param template: jinja2 template object
|
||||
"""
|
||||
|
||||
from flask import request
|
||||
|
||||
return template.render(**{**kwargs, **{
|
||||
'flask': namedtuple('Flask', request._view.keys())(**request._view)
|
||||
}})
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
from typing import Optional
|
||||
from urllib.parse import urlparse, parse_qs
|
||||
|
||||
|
||||
|
|
@ -18,3 +19,22 @@ def url_append_query_param(url:str, name: str, value:str):
|
|||
|
||||
return urlcomps._replace(query=qs).geturl()
|
||||
|
||||
|
||||
def url_upgrade_to_https(url: str, port: Optional[int] = 443):
|
||||
|
||||
urlcomps = urlparse(url)
|
||||
|
||||
urlcomps = urlcomps._replace(scheme='https')
|
||||
|
||||
if url != urlcomps.geturl():
|
||||
|
||||
hostname = urlcomps.netloc.rsplit(':', 1)[0]
|
||||
|
||||
if port != 443:
|
||||
netloc = ':'.join((hostname, str(port)))
|
||||
else:
|
||||
netloc = hostname
|
||||
|
||||
urlcomps = urlcomps._replace(netloc=netloc)
|
||||
|
||||
return urlcomps.geturl()
|
||||
|
|
@ -10,6 +10,8 @@ class Config(NamedTuple):
|
|||
"""
|
||||
swagger_ui: bool = True
|
||||
bind_address: str = None
|
||||
request_ssl: bool = True
|
||||
ssl_port: int = 443
|
||||
|
||||
|
||||
def get_server_config(configIni: ConfigParser) -> Config:
|
||||
|
|
|
|||
|
|
@ -15,10 +15,29 @@
|
|||
margin-bottom: 1em;
|
||||
}
|
||||
|
||||
.blinking{
|
||||
animation:blinkingText 1.0s infinite;
|
||||
animation-timing-function: step-start;
|
||||
}
|
||||
|
||||
.warning {
|
||||
color: red;
|
||||
}
|
||||
|
||||
@keyframes blinkingText {
|
||||
0%{ color: red; }
|
||||
50%{ color: transparent; }
|
||||
100%{ color: red; }
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
{% if flask.before_request__ssl_url is defined %}
|
||||
<p>
|
||||
<strong class="blinking warning">WARNING:</strong> Communication not encrypted - SSL/TLS will not be enforced. Visit <a href="{{ flask.before_request__ssl_url }}">{{ flask.before_request__ssl_url }}</a>, for SSL/TLS encryption.
|
||||
</p>
|
||||
{% endif %}
|
||||
{% block header %}{% endblock %}
|
||||
</header>
|
||||
<hr/>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue