ds_recipe_lib.py 文件源码

python
阅读 104 收藏 0 点赞 0 评论 0

项目:esignature-recipes-python 作者: docusign 项目源码 文件源码
def url_origin(s, use_forwarded_host = False):

    # testing if Heroku includes forwarding host
    use_forwarded_host = True
    include_protocol = True

    ssl      = (('HTTPS' in s) and s['HTTPS'] == 'on')
    sp       = s['SERVER_PROTOCOL'].lower()
    protocol = sp[:sp.find('/')] + ('s' if ssl else '' )
    port     = s['SERVER_PORT']
    port     = '' if ((not ssl and port=='80') or (ssl and port=='443')) else (':' + port)
    host     = s['HTTP_X_FORWARDED_HOST'] if (use_forwarded_host and ('HTTP_X_FORWARDED_HOST' in s)) \
                 else (s['HTTP_HOST'] if ('HTTP_HOST' in s) else None)
    host     = host if (host != None) else (s['SERVER_NAME'] + port)

    # The protocol can easily be wrong if we're frontended by a HTTPS proxy
    # (Like the standard Heroku setup!)
    on_heroku = heroku_env in os.environ
    upgrade_insecure_request = request.headers.get('Upgrade-Insecure-Requests')
    upgrade_insecure_request = upgrade_insecure_request and upgrade_insecure_request == 1
    https_proto = request.headers.get('X-Forwarded-Proto')
    https_proto = https_proto and https_proto == 'https'
    use_https = on_heroku or upgrade_insecure_request or https_proto
    if use_https: # Special handling
        protocol = "https"
    return protocol + '://' + host
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号