utils.py 文件源码

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

项目:http-observatory 作者: mozilla 项目源码 文件源码
def parse_http_equiv_headers(html: str) -> CaseInsensitiveDict:
    http_equiv_headers = CaseInsensitiveDict()

    # Try to parse the HTML
    try:
        soup = bs(html, 'html.parser')
    except:
        return http_equiv_headers

    # Find all the meta tags
    metas = soup.find_all('meta')

    for meta in metas:
        if meta.has_attr('http-equiv') and meta.has_attr('content'):
            # Add support for multiple CSP policies specified via http-equiv
            # See issue: https://github.com/mozilla/http-observatory/issues/266
            # Note that this is so far only done for CSP and not for other types
            # of http-equiv
            if (meta.get('http-equiv', '').lower().strip() == 'content-security-policy' and
               'Content-Security-Policy' in http_equiv_headers):
                http_equiv_headers['Content-Security-Policy'] += '; ' + meta.get('content')
            else:
                http_equiv_headers[meta.get('http-equiv')] = meta.get('content')

        # Technically not HTTP Equiv, but I'm treating it that way
        elif meta.get('name', '').lower().strip() == 'referrer' and meta.has_attr('content'):
            http_equiv_headers['Referrer-Policy'] = meta.get('content')

    return http_equiv_headers
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号