mirror of
https://github.com/postgres/pgweb.git
synced 2025-08-13 13:12:42 +00:00

This can break things (d'uh). Do this by introducing a new decorator, @ssl_optional. When this is present, no SSL redirection will happen, regardless of whether the access comes in over http or https. This decorator overrides @ssl_required, but for redability's sake, never use both at the same time.
35 lines
1.1 KiB
Python
35 lines
1.1 KiB
Python
import datetime
|
|
from functools import wraps
|
|
|
|
def ssl_required(fn):
|
|
def _require_ssl(request, *_args, **_kwargs):
|
|
return fn(request, *_args, **_kwargs)
|
|
_require_ssl.ssl_required = True
|
|
# wraps retains original function attributes such as __name__, csrf_exempt, etc
|
|
return wraps(_require_ssl)(fn)
|
|
|
|
def ssl_optional(fn):
|
|
def _optional_ssl(request, *_args, **_kwargs):
|
|
return fn(request, *_args, **_kwargs)
|
|
_optional_ssl.ssl_optional = True
|
|
# wraps retains original function attributes such as __name__, csrf_exempt, etc
|
|
return wraps(_optional_ssl)(fn)
|
|
|
|
def nocache(fn):
|
|
def _nocache(request, *_args, **_kwargs):
|
|
resp = fn(request, *_args, **_kwargs)
|
|
resp['Cache-Control'] = 's-maxage=0'
|
|
return resp
|
|
return _nocache
|
|
|
|
def cache(days=0, hours=0, minutes=0, seconds=0):
|
|
"Set the server to cache object a specified time. td must be a timedelta object"
|
|
def _cache(fn):
|
|
def __cache(request, *_args, **_kwargs):
|
|
resp = fn(request, *_args, **_kwargs)
|
|
td = datetime.timedelta(hours=hours, minutes=minutes, seconds=seconds)
|
|
resp['Cache-Control'] = 's-maxage=%s' % (td.days*3600*24 + td.seconds)
|
|
return resp
|
|
return __cache
|
|
return _cache
|