httpcache icon indicating copy to clipboard operation
httpcache copied to clipboard

expires_from_cache_control: TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

Open TimurNurlygayanov opened this issue 5 years ago • 0 comments

Hi, I've found the following error in function expires_from_cache_control:

we setting the duration to None and then trying to convert None to int, and it doesn't work properly. We need to fix this code:

def expires_from_cache_control(header, current_time):
    """
    Given a Cache-Control header, builds a Python datetime object corresponding
    to the expiry time (in UTC). This function should respect all relevant
    Cache-Control directives.

    Takes current_time as an argument to ensure that 'max-age=0' generates the
    correct behaviour without being special-cased.

    Returns None to indicate that a request must not be cached.
    """
    # Cache control header values are made of multiple comma separated fields.
    # Splitting them like this is probably a bad idea, but I'm going to roll with
    # it for now. We'll come back to it.
    fields = header.split(', ')
    duration = None

    for field in fields:
        # Right now we don't handle no-cache applied to specific fields. To be
        # as 'nice' as possible, treat any no-cache as applying to the whole
        # request. Bail early, because there's no reason to stick around.
        if field.startswith('no-cache') or field == 'no-store':
            return None

        if field.startswith('max-age'):
            _, duration = field.split('=')
            duration = int(duration)

    interval = timedelta(seconds=int(duration))

    return current_time + interval

and add something like this:


    if no duration:
        return None

    interval = timedelta(seconds=int(duration))

    return current_time + interval

TimurNurlygayanov avatar Jul 03 '20 11:07 TimurNurlygayanov