<commit_before>from sheared import error
from sheared.protocol.http import splitHeaderList

import re

# Overrides for Accept-header.
user_agent_overrides = [
    (re.compile('^Mozilla/4.0 \\(compatible; MSIE '),
                'text/html, */*; q=0.1'),
]

def parse_accepts_header(value):
    """parse_accepts_header(header_value) -> widgets

    Parse a HTTP Accept(-.*)? header into a list of acceptable widgets,
    in decreasing order of preference (e.g. [("text/html", 1.0),
    ("text/plain", 0.2)])."""

    widgets = []
    for header in splitHeaderList(value):
        all = header.split(';')
        if len(all) == 1:
            gizmos, = all
            qval = 1.0
        elif len(all) == 2:
            gizmos, qval = all
            qval = qval.strip()
            if not qval.startswith('q='):
                raise ValueError, 'bad parameter in Accept-header: %s' % qval
            qval = float(qval[2:])
        else:
            raise ValueError, 'bad Accept-header: %s' % value

        for gizmo in gizmos.split(','):
            widget = gizmo.strip(), qval
            widgets.append(widget)

    widgets.sort(lambda a, b: cmp(a[1], b[1]))
    return widgets
    
def chooseContentType(request, content_types):
    """chooseContentType(request, content_types) -> content_type
    
    Find the preferred content type for a given request, among a list of
    possible content types. Or, if none of the possible content types
    are acceptable raise sheared.error.web.NotAcceptable."""

    def is_acceptable(widget, gizmo):
        return (widget == gizmo) or \
               (gizmo.endswith('/*') and widget.startswith(gizmo[:-1])) or \
               (gizmo == '*/*') or \
               (gizmo == '*')

    ua = request.headers.get('User-Agent', '')
    if ua:
        for ex, val in user_agent_overrides:
            if ex.match(ua):
                request.headers.setHeader('Accept', val)
                break

    if request.headers.has_key('Accept'):
        chosen = None
        acceptable = parse_accepts_header(request.headers['Accept'])
        for content_type in content_types:<commit_msg>Fudge Accept header like Apache does.<commit_after>from sheared import error
from sheared.protocol.http import splitHeaderList

import re

def parse_accepts_header(value):
    """parse_accepts_header(header_value) -> widgets

    Parse a HTTP Accept header into a list of acceptable widgets, in
    decreasing order of preference (e.g. [("text/html", 1.0),
    ("text/plain", 0.2)])."""

    widgets = []
    for header in splitHeaderList(value):
        all = header.split(';')
        if len(all) == 1:
            gizmo, = all
            qval = 1.0
        elif len(all) == 2:
            gizmo, qval = all
            qval = qval.strip()
            if not qval.startswith('q='):
                raise ValueError, 'bad parameter in Accept-header: %s' % qval
            qval = float(qval[2:])
        else:
            raise ValueError, 'bad Accept-header: %s' % value

        gizmo = gizmo.strip()
        if gizmo == '*/*':
            qval = 0.0001
        elif gizmo.endswith('/*'):
            qval = 0.001            
        widget = gizmo, qval
        widgets.append(widget)

    widgets.sort(lambda a, b: cmp(a[1], b[1]))
    return widgets
    
def chooseContentType(request, content_types):
    """chooseContentType(request, content_types) -> content_type
    
    Find the preferred content type for a given request, among a list of
    possible content types. Or, if none of the possible content types
    are acceptable raise sheared.error.web.NotAcceptable."""

    def is_acceptable(widget, gizmo):
        return (widget == gizmo) or \
               (gizmo.endswith('/*') and widget.startswith(gizmo[:-1])) or \
               (gizmo == '*/*') or \
               (gizmo == '*')

    if request.headers.has_key('Accept'):
        chosen = None
        acceptable = parse_accepts_header(request.headers['Accept'])
        for content_type in content_types:
{'fraction_non_alphanumeric': 0.12462612163509472, 'fraction_numerical': 0.009222333000997009, 'mean_word_length': 2.212970376301041, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1749267', 'n_tokens_mistral': 1257, 'n_tokens_neox': 1210, 'n_words': 333}