<commit_before># websocket_server -- WebSocket server library
# https://github.com/CylonicRaider/websocket-server

"""
Cookie management utilities.

(Mostly) compliant to RFC 6265. The alternatives present in the
standard library are, frankly, utterly insufficient.
"""

import re
import time
from . import tools
from .compat import bytes, unicode

try:
    from urllib.parse import quote, unquote, urlsplit, urlunsplit, parse_qs
except ImportError:
    from urllib import quote, unquote
    from urlparse import urlsplit, urlunsplit, parse_qs

__all__ = ['Cookie', 'CookieJar', 'FileCookieJar', 'CookieLoadError']

SECURE_SCHEMES = ['https', 'wss']
HTTP_SCHEMES = ['http', 'https', 'ws', 'wss']

def domains_match(base, probe):
    """
    domains_match(base, probe) -> bool

    Return whether probe should "match" base if both are interpreted
    as domain names. Implements RFC6265, Section 5.1.3.
    """
    if probe == base:
        return True
    elif not probe.endswith(base) or not probe[:-len(base)].endswith('.'):
        return False
    elif probe.replace('.', '').isdigit() or ':' in probe:
        # Crude heuristic to match IP addresses.
        return False
    else:
        return True

def paths_match(base, probe):
    """
    paths_match(base, probe) -> bool

    Return whether probe is considered to "match" base if both are
    interpreted as path names. Implements RFC6265, Section 5.1.4.
    """
    return (probe == base or probe.startswith(base) and
            (base.endswith('/') or probe[len(base):].startswith('/')))

def parse_url(url):
    """
    parse_url(url) -> (str, str, str)

    Parse url and return the scheme, host, and path normalized as
    convenient for Cookie.match() et al.
    """
    purl = urlsplit(url)
    return (purl.scheme.lower(), (purl.hostname or '').lower(),
            (purl.path or '/'))

class CookieLoadError(Exception):
    """
    Raised if trying to load cookies from a badly formatted file.
    """
    pass

class Cookie:
    """
    Cookie(name, value, url=None, **attributes) -> new instance

    A single HTTP cookie. name and value are, respectively, the name
    and value of the cookie. url is the URL the cookie references, or
    None; if present, it is used to derive default values for certain
    attributes (as demanded by the RFC). attributes are additional
    key-value pairs containing meta-information about the cookie.
    Cookie implements a bare-bones mapping interface (enough to pass
    the dict constructor and collections.MutableMapping); use that to
    access/modify cookie attributes. Lookup and modification is
    case-insensitive; iteration preserves the case at the time of
    insertion (delete and re-add an attribute to force a specific
    case).

    Make sure to choose only appropriate names/values; Cookie does not
    empoly any means of automatic escaping.
    """

    @classmethod
    def create(cls, name, value, url):
        """
        create(name, value, url) -> new instance

        Create a cookie with the given name and value, initializing
        attributes from the given URL. The host name from it is used
        for the Domain attribute, the path for the Path, query string
        parameters are converted into additional attributes.
        """
        purl = urlsplit(url)
        attrs = {}
        if purl.hostname: attrs['Domain'] = purl.hostname
        attrs['Path'] = purl.path or '/'
        for key, value in parse_qs(purl.query, True).items():
            if value:
                attrs[key] = value
            else:
                attrs[key] = None
        return cls(name, value, url, **attrs)

    @classmethod
    def parse(cls, string, url=None, parse_attr=None, make_url=None):
        """
        parse(string, url=None, parse_attr=None, make_url=None)
            -> new instance
<commit_msg>Automatically create Secure attribute<commit_after># websocket_server -- WebSocket server library
# https://github.com/CylonicRaider/websocket-server

"""
Cookie management utilities.

(Mostly) compliant to RFC 6265. The alternatives present in the
standard library are, frankly, utterly insufficient.
"""

import re
import time
from . import tools
from .compat import bytes, unicode

try:
    from urllib.parse import quote, unquote, urlsplit, urlunsplit, parse_qs
except ImportError:
    from urllib import quote, unquote
    from urlparse import urlsplit, urlunsplit, parse_qs

__all__ = ['Cookie', 'CookieJar', 'FileCookieJar', 'CookieLoadError']

SECURE_SCHEMES = ['https', 'wss']
HTTP_SCHEMES = ['http', 'https', 'ws', 'wss']

def domains_match(base, probe):
    """
    domains_match(base, probe) -> bool

    Return whether probe should "match" base if both are interpreted
    as domain names. Implements RFC6265, Section 5.1.3.
    """
    if probe == base:
        return True
    elif not probe.endswith(base) or not probe[:-len(base)].endswith('.'):
        return False
    elif probe.replace('.', '').isdigit() or ':' in probe:
        # Crude heuristic to match IP addresses.
        return False
    else:
        return True

def paths_match(base, probe):
    """
    paths_match(base, probe) -> bool

    Return whether probe is considered to "match" base if both are
    interpreted as path names. Implements RFC6265, Section 5.1.4.
    """
    return (probe == base or probe.startswith(base) and
            (base.endswith('/') or probe[len(base):].startswith('/')))

def parse_url(url):
    """
    parse_url(url) -> (str, str, str)

    Parse url and return the scheme, host, and path normalized as
    convenient for Cookie.match() et al.
    """
    purl = urlsplit(url)
    return (purl.scheme.lower(), (purl.hostname or '').lower(),
            (purl.path or '/'))

class CookieLoadError(Exception):
    """
    Raised if trying to load cookies from a badly formatted file.
    """
    pass

class Cookie:
    """
    Cookie(name, value, url=None, **attributes) -> new instance

    A single HTTP cookie. name and value are, respectively, the name
    and value of the cookie. url is the URL the cookie references, or
    None; if present, it is used to derive default values for certain
    attributes (as demanded by the RFC). attributes are additional
    key-value pairs containing meta-information about the cookie.
    Cookie implements a bare-bones mapping interface (enough to pass
    the dict constructor and collections.MutableMapping); use that to
    access/modify cookie attributes. Lookup and modification is
    case-insensitive; iteration preserves the case at the time of
    insertion (delete and re-add an attribute to force a specific
    case).

    Make sure to choose only appropriate names/values; Cookie does not
    empoly any means of automatic escaping.
    """

    @classmethod
    def create(cls, name, value, url):
        """
        create(name, value, url) -> new instance

        Create a cookie with the given name and value, initializing
        attributes from the given URL. The host name from it is used
        for the Domain attribute, the path for the Path, query string
        parameters are converted into additional attributes.
        """
        purl = urlsplit(url)
        attrs = {}
        if purl.scheme in SECURE_SCHEMES: attrs['Secure'] = None
        if purl.hostname: attrs['Domain'] = purl.hostname
        attrs['Path'] = purl.path or '/'
        for key, value in parse_qs(purl.query, True).items():
            if value:
                attrs[key] = value
            else:
                attrs[key] = None
        return cls(name, value, url, **attrs)

    @classmethod
    def parse(cls, string, url=None, parse_attr=None, make_url=None):
        """
        parse(string, url=None, parse_attr=None, make_url=None)
            -> new instance

{'fraction_non_alphanumeric': 0.09926801078720945, 'fraction_numerical': 0.0046230897649929365, 'mean_word_length': 2.8707753479125246, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 2, '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': '1048371', 'n_tokens_mistral': 2247, 'n_tokens_neox': 2170, 'n_words': 839}