def _really_load(self):
        """
        Implementation of the _really_load method. Basically, it just loads
        everything in the database, and maps it to cookies
        """
        try:
            with sqlite3.connect(self.filename, self.timeout) as con:
                con.row_factory = sqlite3.Row
                res = con.execute("SELECT * from cookie").fetchall()

                for cookie in res:

                    initial_dot = cookie["domain"].startswith(".")
                    c = Cookie( 0, cookie["name"], cookie["value"],
                                None, False,
                                cookie["domain"], initial_dot, initial_dot,
                                cookie["path"], cookie["path"]!="/",
                                cookie["secure"],
                                cookie["expiry"],
                                False,
                                None,
                                None,
                                {}
                               )

                    self.logger.info(
                        "LOADED cookie [domain: %s , name : %s, value: %s]" % (c.domain, c.name, c.value)
                    )

                    if not c.is_expired(time.time()):
                        self.set_cookie(c)

        except sqlite3.DatabaseError, e:
            self.logger.error("Loading cookies failed : could not access database")
            self.logger.error("Exception was : %s - %s" % (type(e).__name__, e))
    def _really_load(self, f, filename, ignore_discard, ignore_expires):
        now = time.time()

        magic = f.readline()
        if not re.search(self.magic_re, magic):
            f.close()
            raise LoadError(
                "%r does not look like a Netscape format cookies file" %
                filename)

        try:
            while True:
                line = f.readline()
                if line == "": break

                # last field may be absent, so keep any trailing tab
                if line.endswith("\n"): line = line[:-1]

                # skip comments and blank lines XXX what is $ for?
                if (line.strip().startswith(("#", "$")) or line.strip() == ""):
                    continue

                domain, domain_specified, path, secure, expires, name, value = \
                        line.split("\t")
                secure = (secure == "TRUE")
                domain_specified = (domain_specified == "TRUE")
                if name == "":
                    # cookies.txt regards 'Set-Cookie: foo' as a cookie
                    # with no name, whereas cookielib regards it as a
                    # cookie with no value.
                    name = value
                    value = None

                initial_dot = domain.startswith(".")
                assert domain_specified == initial_dot

                discard = False
                if expires == "":
                    expires = None
                    discard = True

                # assume path_specified is false
                c = Cookie(0, name, value, None, False, domain,
                           domain_specified, initial_dot, path, False, secure,
                           expires, discard, None, None, {})
                if not ignore_discard and c.discard:
                    continue
                if not ignore_expires and c.is_expired(now):
                    continue
                self.set_cookie(c)

        except IOError:
            raise
        except Exception:
            _warn_unhandled_exception()
            raise LoadError("invalid Netscape format cookies file %r: %r" %
                            (filename, line))
示例#3
0
    def load(self, loadStr, ignore_discard=True , ignore_expires=True):
        now = time.time()

        lines=loadStr.split('\n')
        for line in lines:
            if line == "": break

            # last field may be absent, so keep any trailing tab
            if line.endswith("\n"): line = line[:-1]

            # skip comments and blank lines XXX what is $ for?
            if (line.strip().startswith(("#", "$")) or
                line.strip() == ""):
                continue

            domain, domain_specified, path, secure, expires, name, value = \
                    line.split("\t")
            secure = (secure == "TRUE")
            domain_specified = (domain_specified == "TRUE")
            if name == "":
                # cookies.txt regards 'Set-Cookie: foo' as a cookie
                # with no name, whereas cookielib regards it as a
                # cookie with no value.
                name = value
                value = None

            initial_dot = domain.startswith(".")
            assert domain_specified == initial_dot

            discard = False
            if expires == "":
                expires = None
                discard = True

            # assume path_specified is false
            c = Cookie(0, name, value,
                           None, False,
                           domain, domain_specified, initial_dot,
                           path, False,
                           secure,
                           expires,
                           discard,
                           None,
                           None,
                           {})
            if not ignore_discard and c.discard:
                continue
            if not ignore_expires and c.is_expired(now):
                continue
            self.set_cookie(c)
示例#4
0
def init(cookieFile=None):
    if cookieFile is None:
        if 'COOKIE_FILE' not in os.environ:
            warning("cs.www.init(): no \$COOKIE_FILE")
            return
        cookieFile = os.environ['COOKIE_FILE']
    cookieHandler = HTTPCookieProcessor()
    cookieHandler.cookiejar = MozillaCookieJar()
    if not cookieFile.endswith('.sqlite'):
        # presume older mozilla cookie file
        cookieHandler.cookiejar.load(cookieFile)
    else:
        import sqlite3
        import time
        now = time.time()
        debug("SQLITE3 cookie file: %s" % cookieFile)
        db = sqlite3.connect(cookieFile)
        cursor = db.cursor()
        cursor.execute(
            'select id, name, value, host, path, expiry, isSecure, isHttpOnly from moz_cookies'
        )
        for id, name, value, host, path, expiry, isSecure, isHttpOnly in cursor:
            isSecure = bool(isSecure)
            isHttpOnly = bool(isHttpOnly)
            if name == "":
                # cookies.txt regards 'Set-Cookie: foo' as a cookie
                # with no name, whereas cookielib regards it as a
                # cookie with no value.
                name = value
                value = None
            initial_dot = host.startswith(".")
            domain_specified = initial_dot
            discard = False
            if expiry == "":
                expiry = None
                discard = True
            # assume path_specified is false
            c = Cookie(0, name, value, None, False, host, domain_specified,
                       initial_dot, path, False, isSecure, expiry, discard,
                       None, None, {})
            if c.is_expired(now):
                warning("skip expired cookie: name=%s, host=%s, path=%s", name,
                        host, path)
                continue
            warning("add cookie: name=%s, host=%s, path=%s", name, host, path)
            cookieHandler.cookiejar.set_cookie(c)
    install_opener(build_opener(cookieHandler))
示例#5
0
    def _really_load(self, f, filename, ignore_discard, ignore_expires):
        now = time.time()
        magic = f.readline()
        if not re.search(self.magic_re, magic):
            f.close()
            raise LoadError(
                '%r does not look like a Netscape format cookies file' %
                filename)
        try:
            while 1:
                line = f.readline()
                if line == '':
                    break
                if line.endswith('\n'):
                    line = line[:-1]
                if line.strip().startswith(('#', '$')) or line.strip() == '':
                    continue
                domain, domain_specified, path, secure, expires, name, value = line.split(
                    '\t')
                secure = secure == 'TRUE'
                domain_specified = domain_specified == 'TRUE'
                if name == '':
                    name = value
                    value = None
                initial_dot = domain.startswith('.')
                if not domain_specified == initial_dot:
                    raise AssertionError
                    discard = False
                    expires = expires == '' and None
                    discard = True
                c = Cookie(0, name, value, None, False, domain,
                           domain_specified, initial_dot, path, False, secure,
                           expires, discard, None, None, {})
                if not ignore_discard and c.discard:
                    continue
                if not ignore_expires and c.is_expired(now):
                    continue
                self.set_cookie(c)

        except IOError:
            raise
        except Exception:
            _warn_unhandled_exception()
            raise LoadError('invalid Netscape format cookies file %r: %r' %
                            (filename, line))

        return
示例#6
0
    def load(self, loadStr, ignore_discard=True, ignore_expires=True):
        now = time.time()

        lines = loadStr.split('\n')
        for line in lines:
            if line == "":
                break

            # last field may be absent, so keep any trailing tab
            if line.endswith("\n"): line = line[:-1]

            # skip comments and blank lines XXX what is $ for?
            if (line.strip().startswith(("#", "$")) or line.strip() == ""):
                continue

            domain, domain_specified, path, secure, expires, name, value = \
                line.split("\t")
            secure = (secure == "TRUE")
            domain_specified = (domain_specified == "TRUE")
            if name == "":
                # cookies.txt regards 'Set-Cookie: foo' as a cookie
                # with no name, whereas cookielib regards it as a
                # cookie with no value.
                name = value
                value = None

            initial_dot = domain.startswith(".")
            assert domain_specified == initial_dot

            discard = False
            if expires == "":
                expires = None
                discard = True

            # assume path_specified is false
            c = Cookie(0, name, value, None, False, domain, domain_specified,
                       initial_dot, path, False, secure, expires, discard,
                       None, None, {})
            if not ignore_discard and c.discard:
                continue
            if not ignore_expires and c.is_expired(now):
                continue
            self.set_cookie(c)
示例#7
0
  def load_cookie(self, c, ignore_discard=False, ignore_expires=False):
    expires = c['expires']
    discard = c['discard'] if expires is not None else True
    domain = c['domain']
    domain_specified = domain.startswith(".")
    c = Cookie(c['version'], c['name'], c['value'],
      c['port'], c['port_specified'],
      domain, domain_specified, c['domain_initial_dot'],
      c['path'], c['path_specified'],
      c['secure'],
      expires,
      discard,
      c['comment'],
      c['comment_url'],
      {})

    if not ignore_discard and c.discard: return
    if not ignore_expires and c.is_expired(): return

    self.set_cookie(c)
    def _really_load(self, f, filename, ignore_discard, ignore_expires):
        now = time.time()
        magic = f.readline()
        if not re.search(self.magic_re, magic):
            f.close()
            raise LoadError('%r does not look like a Netscape format cookies file' % filename)
        try:
            while 1:
                line = f.readline()
                if line == '':
                    break
                if line.endswith('\n'):
                    line = line[:-1]
                if line.strip().startswith(('#', '$')) or line.strip() == '':
                    continue
                domain, domain_specified, path, secure, expires, name, value = line.split('\t')
                secure = secure == 'TRUE'
                domain_specified = domain_specified == 'TRUE'
                if name == '':
                    name = value
                    value = None
                initial_dot = domain.startswith('.')
                if not domain_specified == initial_dot:
                    raise AssertionError
                    discard = False
                    expires = expires == '' and None
                    discard = True
                c = Cookie(0, name, value, None, False, domain, domain_specified, initial_dot, path, False, secure, expires, discard, None, None, {})
                if not ignore_discard and c.discard:
                    continue
                if not ignore_expires and c.is_expired(now):
                    continue
                self.set_cookie(c)

        except IOError:
            raise
        except Exception:
            _warn_unhandled_exception()
            raise LoadError('invalid Netscape format cookies file %r: %r' % (filename, line))

        return
示例#9
0
    def _really_load(self, f, filename, ignore_discard, ignore_expires):
        try:
            import sqlite3 as sqlite
            from cookielib import _warn_unhandled_exception, LoadError, Cookie

            con = sqlite.connect(filename)
            query_str = 'select name, value, host_key, path, expires_utc, secure from cookies'
            for r in con.execute(query_str):
                name, value, host, path, expires, secure = r

                name = str(name)
                value = str(value) or None
                domain = str(host)
                initial_dot = domain.startswith(".")
                domain_specified = initial_dot
                path = str(path) or None
                path_specified = (path is not None)
                secure = (secure != 0)
                discard = False
                if  not expires:
                    expires = None
                    discard = True

                c = Cookie(0, name, value,
                           None, False,
                           domain, domain_specified, initial_dot,
                           path, path_specified,
                           secure,
                           expires,
                           discard,
                           None,
                           None,
                           {})

                if not ignore_expires and not c.is_expired():
                    self.set_cookie(c)
        except IOError:
            raise
        except Exception:
            _warn_unhandled_exception()
            raise LoadError("invalid Chrome sqlite format cookies file %r" % filename)
示例#10
0
    def load(self, key, ignore_discard=False, ignore_expires=False):
        now = time.time()
        line = ''
        try:
            rvalue = self.redis.get(key)
            if rvalue is None: return
            lines = rvalue.split("\n")
            for line in lines:
                if line == '': break
                if line.endswith("\n"): line = line[:-1]
                #注释
                if (line.strip().startswith(("#", "$")) or line.strip() == ""):
                    continue
                domain, domain_specified, path, secure, expires, name, value = \
                    line.split("\t")
                secure = (secure == "TRUE")
                domain_specified = (domain_specified == "TRUE")
                if name == '':
                    name = value
                    value = None
                #
                initial_dot = domain.startswith(".")
                assert domain_specified == initial_dot

                discard = False
                if expires == "":
                    expires = None
                    discard = True
                # assume path_specified is false
                c = Cookie(0, name, value, None, False, domain,
                           domain_specified, initial_dot, path, False, secure,
                           expires, discard, None, None, {})
                if ignore_discard and c.discard:
                    continue
                if ignore_expires and c.is_expired(now):
                    continue
                self.set_cookie(c)
        except Exception:
            #_warn_unhandled_exception()
            raise LoadError("invalid format cookies redis %s: %r" %
                            (key, line))
示例#11
0
    def _add_cookie(self,
                    domain,
                    name,
                    value,
                    domain_specified="FALSE",
                    path="/",
                    secure="FALSE",
                    expires=0):
        secure = (secure == "TRUE")
        domain_specified = (domain_specified == "TRUE")
        if name == "":
            # cookies.txt regards 'Set-Cookie: foo' as a cookie
            # with no name, whereas cookielib regards it as a
            # cookie with no value.
            name = value
            value = None

        initial_dot = domain.startswith(".")
        # assert domain_specified == initial_dot
        discard = False
        if expires == "":
            expires = None
            discard = True

        # assume path_specified is false
        c = Cookie(0, name, value, None, False, domain, domain_specified,
                   initial_dot, path, False, secure, expires, discard, None,
                   None, {})
        if not self._ignore_discard and c.discard:
            #a session cookie
            #TODO deal with a session cookie
            pass
        if not self._ignore_expires and c.is_expired():
            #end of life, do not add it
            raise RuntimeError(
                "the cookie's life ended, try add it in ignore_expires mod.")

        self.set_cookie(c)
        return
示例#12
0
    def _add_cookie(self, domain, name, value, domain_specified="FALSE", path="/", secure="FALSE", expires=0):
        secure = (secure == "TRUE")
        domain_specified = (domain_specified == "TRUE")
        if name == "":
            # cookies.txt regards 'Set-Cookie: foo' as a cookie
            # with no name, whereas cookielib regards it as a
            # cookie with no value.
            name = value
            value = None

        initial_dot = domain.startswith(".")
        # assert domain_specified == initial_dot
        discard = False
        if expires == "":
            expires = None
            discard = True

        # assume path_specified is false
        c = Cookie(0, name, value,
                   None, False,
                   domain, domain_specified, initial_dot,
                   path, False,
                   secure,
                   expires,
                   discard,
                   None,
                   None,
                   {})
        if not self._ignore_discard and c.discard:
            #a session cookie
            #TODO deal with a session cookie
            pass
        if not self._ignore_expires and c.is_expired():
            #end of life, do not add it
            raise RuntimeError("the cookie's life ended, try add it in ignore_expires mod.")

        self.set_cookie(c)
        return
示例#13
0
    def _really_load(self, f, filename, ignore_discard, ignore_expires):
        now = time.time()

        try:
            while 1:
                line = f.readline()
                if line == "": break

                # last field may be absent, so keep any trailing tab
                if line.endswith("\n"): line = line[:-1]

                # skip comments and blank lines XXX what is $ for?
                if (line.strip().startswith(("#", "$")) or line.strip() == ""):
                    continue

                # domain, domain_specified, path, secure, expires, name, value = \
                #        line.split("\t")

                # 2013-09-07 Firebug 导出的 cookies.txt 格式如下:
                #
                # .douban.fm    TRUE    /   FALSE   1403077533  undefined   openExpPan  Y
                #
                # 和原来相比多了 undefined 这一列(PS: 不太清楚是干什么的)
                # 与 curl 使用的 cookie 文件格式不兼容,需要去掉这一列才能用于 curl

                arr = line.strip().split("\t")

                domain = arr.pop(0)
                domain_specified = arr.pop(0)
                path = arr.pop(0)
                secure = arr.pop(0)
                expires = arr.pop(0)

                value = arr.pop()
                if arr:
                    name = arr.pop()
                else:
                    name = value
                    value = None

                secure = (secure == "TRUE")
                domain_specified = (domain_specified == "TRUE")

                initial_dot = domain.startswith(".")
                assert domain_specified == initial_dot

                discard = False
                if expires == None:
                    expires = None
                    discard = True

                # assume path_specified is false
                c = Cookie(0, name, value, None, False, domain,
                           domain_specified, initial_dot, path, False, secure,
                           expires, discard, None, None, {})
                if not ignore_discard and c.discard:
                    continue
                if not ignore_expires and c.is_expired(now):
                    continue
                self.set_cookie(c)

        except IOError:
            raise
        except Exception:
            _warn_unhandled_exception()
            raise LoadError("invalid Netscape format cookies file %r: %r" %
                            (filename, line))
    def _really_load(self, f, filename, ignore_discard, ignore_expires):
        now = time.time()

        magic = f.readline()
        if not re.search(self.magic_re, magic):
            f.close()
            raise LoadError(
                "%r does not look like a Netscape format cookies file" %
                filename)

        try:
            while 1:
                line = f.readline()
                if line == "": break

                # last field may be absent, so keep any trailing tab
                if line.endswith("\n"): line = line[:-1]

                # skip comments and blank lines XXX what is $ for?
                if (line.strip().startswith(("#", "$")) or
                    line.strip() == ""):
                    continue

                domain, domain_specified, path, secure, expires, name, value = \
                        line.split("\t")
                secure = (secure == "TRUE")
                domain_specified = (domain_specified == "TRUE")
                if name == "":
                    # cookies.txt regards 'Set-Cookie: foo' as a cookie
                    # with no name, whereas cookielib regards it as a
                    # cookie with no value.
                    name = value
                    value = None

                initial_dot = domain.startswith(".")
                assert domain_specified == initial_dot

                discard = False
                if expires == "":
                    expires = None
                    discard = True

                # assume path_specified is false
                c = Cookie(0, name, value,
                           None, False,
                           domain, domain_specified, initial_dot,
                           path, False,
                           secure,
                           expires,
                           discard,
                           None,
                           None,
                           {})
                if not ignore_discard and c.discard:
                    continue
                if not ignore_expires and c.is_expired(now):
                    continue
                self.set_cookie(c)

        except IOError:
            raise
        except Exception:
            _warn_unhandled_exception()
            raise LoadError("invalid Netscape format cookies file %r: %r" %
                            (filename, line))
    def _really_load(self, f, filename, ignore_discard, ignore_expires):
        now = time.time()

        magic = f.readline()
        if not re.search(self.magic_re, magic):
            f.close()
            raise LoadError(
                "The cookie does not appear to be in Netscape format. Check that the first line is: <br> # Netscape HTTP Cookie File")

        try:
            while 1:
                line = f.readline()
                if line == "": break
    
                # last field may be absent, so keep any trailing tab
                if line.endswith("\n"): line = line[:-1]

                # skip comments and blank lines XXX what is $ for?
                if (line.strip().startswith(("#", "$")) or
                    line.strip() == ""):
                    continue

                domain, domain_specified, path, secure, expires, name, value = \
                        line.split("\t")
                secure = (secure == "TRUE")
                domain_specified = (domain_specified == "TRUE")
                if name == "":
                    # cookies.txt regards 'Set-Cookie: foo' as a cookie
                    # with no name, whereas cookielib regards it as a
                    # cookie with no value.
                    name = value
                    value = None

                initial_dot = domain.startswith(".")
                assert domain_specified == initial_dot

                discard = False
                if expires == "":
                    expires = None
                    discard = True

                # assume path_specified is false
                c = Cookie(0, name, value,
                           None, False,
                           domain, domain_specified, initial_dot,
                           path, False,
                           secure,
                           expires,
                           discard,
                           None,
                           None,
                           {})
                if not ignore_discard and c.discard:
                    continue
                if not ignore_expires and c.is_expired(now):
                    continue
                self.set_cookie(c)

        except IOError:
            raise
        except AssertionError:
            raise AssertionError("Cookie does not follow Netscape format. Check that the values only are seperated by tab (\\t)" + self._parse_failed_cookie_message(line))
        except Exception as ex:
            raise Exception(ex.message + self._parse_failed_cookie_message(line))
示例#16
0
    def _really_load(self, f, filename, ignore_discard, ignore_expires):
        magic = f.readline()
        if not re.search(self.magic_re, magic):
            msg = '%r does not look like a Set-Cookie3 (LWP) format file' % filename
            raise LoadError(msg)
        now = time.time()
        header = 'Set-Cookie3:'
        boolean_attrs = ('port_spec', 'path_spec', 'domain_dot', 'secure', 'discard')
        value_attrs = ('version', 'port', 'path', 'domain', 'expires', 'comment', 'commenturl')
        try:
            while 1:
                line = f.readline()
                if line == '':
                    break
                if not line.startswith(header):
                    continue
                line = line[len(header):].strip()
                for data in split_header_words([line]):
                    name, value = data[0]
                    standard = {}
                    rest = {}
                    for k in boolean_attrs:
                        standard[k] = False

                    for k, v in data[1:]:
                        if k is not None:
                            lc = k.lower()
                        else:
                            lc = None
                        if lc in value_attrs or lc in boolean_attrs:
                            k = lc
                        if k in boolean_attrs:
                            if v is None:
                                v = True
                            standard[k] = v
                        elif k in value_attrs:
                            standard[k] = v
                        else:
                            rest[k] = v

                    h = standard.get
                    expires = h('expires')
                    discard = h('discard')
                    if expires is not None:
                        expires = iso2time(expires)
                    if expires is None:
                        discard = True
                    domain = h('domain')
                    domain_specified = domain.startswith('.')
                    c = Cookie(h('version'), name, value, h('port'), h('port_spec'), domain, domain_specified, h('domain_dot'), h('path'), h('path_spec'), h('secure'), expires, discard, h('comment'), h('commenturl'), rest)
                    if not ignore_discard and c.discard:
                        continue
                    if not ignore_expires and c.is_expired(now):
                        continue
                    self.set_cookie(c)

        except IOError:
            raise
        except Exception:
            _warn_unhandled_exception()
            raise LoadError('invalid Set-Cookie3 format file %r: %r' % (filename, line))

        return
示例#17
0
    def _really_load(self, f, filename, ignore_discard, ignore_expires):
        now = time.time()

        try:
            while 1:
                line = f.readline()
                if line == "": break

                # last field may be absent, so keep any trailing tab
                if line.endswith("\n"): line = line[:-1]

                # skip comments and blank lines XXX what is $ for?
                if (line.strip().startswith(("#", "$")) or line.strip() == ""):
                    continue

                # domain, domain_specified, path, secure, expires, name, value = \
                #        line.split("\t")

                # 2013-09-07 Firebug 导出的 cookies.txt 格式如下:
                #
                # .douban.fm    TRUE    /   FALSE   1403077533  undefined   openExpPan  Y
                #
                # 和原来相比多了 undefined 这一列(PS: 不太清楚是干什么的)
                # 与 curl 使用的 cookie 文件格式不兼容,需要去掉这一列才能用于 curl

                arr = line.strip().split("\t")

                domain = arr.pop(0)
                domain_specified = arr.pop(0)
                path = arr.pop(0)
                secure = arr.pop(0)
                expires = arr.pop(0)

                value = arr.pop()
                if arr:
                    name = arr.pop()
                else:
                    name = value
                    value = None

                secure = (secure == "TRUE")
                domain_specified = (domain_specified == "TRUE")

                initial_dot = domain.startswith(".")
                assert domain_specified == initial_dot

                discard = False
                if expires == None:
                    expires = None
                    discard = True

                # assume path_specified is false
                c = Cookie(0, name, value, None, False, domain,
                           domain_specified, initial_dot, path, False, secure,
                           expires, discard, None, None, {})
                if not ignore_discard and c.discard:
                    continue
                if not ignore_expires and c.is_expired(now):
                    continue
                self.set_cookie(c)

        except IOError:
            raise
        except Exception:
            _warn_unhandled_exception()
            raise LoadError("invalid Netscape format cookies file %r: %r" %
                            (filename, line))
示例#18
0
    def _really_load(self, f, filename, ignore_discard, ignore_expires):
        now = time.time()

        try:
            while 1:
                line = f.readline()
                if line == "": break

                # last field may be absent, so keep any trailing tab
                if line.endswith("\n"): line = line[:-1]

                # skip comments and blank lines XXX what is $ for?
                if (line.strip().startswith(("#", "$")) or
                    line.strip() == ""):
                    continue

#                domain, domain_specified, path, secure, expires, name, value = \
#                        line.split("\t")

                arr = line.strip().split("\t")

                domain = arr.pop(0)
                domain_specified = arr.pop(0)
                path = arr.pop(0)
                secure = arr.pop(0)
                expires = arr.pop(0)

                name = arr.pop(0)
                if arr:
                    value = arr.pop(0)
                else:
                    value = None

                secure = (secure == "TRUE")
                domain_specified = (domain_specified == "TRUE")

                initial_dot = domain.startswith(".")
                assert domain_specified == initial_dot

                discard = False
                if expires == None:
                    expires = None
                    discard = True

                # assume path_specified is false
                c = Cookie(0, name, value,
                           None, False,
                           domain, domain_specified, initial_dot,
                           path, False,
                           secure,
                           expires,
                           discard,
                           None,
                           None,
                           {})
                if not ignore_discard and c.discard:
                    continue
                if not ignore_expires and c.is_expired(now):
                    continue
                self.set_cookie(c)

        except IOError:
            raise
        except Exception:
            _warn_unhandled_exception()
            raise LoadError("invalid Netscape format cookies file %r: %r" %
                            (filename, line))
示例#19
0
    def _really_load(self, f, filename, ignore_discard, ignore_expires):
        magic = f.readline()
        if not re.search(self.magic_re, magic):
            msg = '%r does not look like a Set-Cookie3 (LWP) format file' % filename
            raise LoadError(msg)
        now = time.time()
        header = 'Set-Cookie3:'
        boolean_attrs = ('port_spec', 'path_spec', 'domain_dot', 'secure',
                         'discard')
        value_attrs = ('version', 'port', 'path', 'domain', 'expires',
                       'comment', 'commenturl')
        try:
            while 1:
                line = f.readline()
                if line == '':
                    break
                if not line.startswith(header):
                    continue
                line = line[len(header):].strip()
                for data in split_header_words([line]):
                    name, value = data[0]
                    standard = {}
                    rest = {}
                    for k in boolean_attrs:
                        standard[k] = False

                    for k, v in data[1:]:
                        if k is not None:
                            lc = k.lower()
                        else:
                            lc = None
                        if lc in value_attrs or lc in boolean_attrs:
                            k = lc
                        if k in boolean_attrs:
                            if v is None:
                                v = True
                            standard[k] = v
                        elif k in value_attrs:
                            standard[k] = v
                        else:
                            rest[k] = v

                    h = standard.get
                    expires = h('expires')
                    discard = h('discard')
                    if expires is not None:
                        expires = iso2time(expires)
                    if expires is None:
                        discard = True
                    domain = h('domain')
                    domain_specified = domain.startswith('.')
                    c = Cookie(h('version'), name, value, h('port'),
                               h('port_spec'), domain, domain_specified,
                               h('domain_dot'), h('path'), h('path_spec'),
                               h('secure'), expires, discard, h('comment'),
                               h('commenturl'), rest)
                    if not ignore_discard and c.discard:
                        continue
                    if not ignore_expires and c.is_expired(now):
                        continue
                    self.set_cookie(c)

        except IOError:
            raise
        except Exception:
            _warn_unhandled_exception()
            raise LoadError('invalid Set-Cookie3 format file %r: %r' %
                            (filename, line))

        return
示例#20
0
def _really_load(self, f, filename, ignore_discard, ignore_expires):
    """
    This function is required to monkey patch MozillaCookieJar's _really_load
    function which does not understand the curl format cookie file created
    by ecp-cookie-init. It patches the code so that #HttpOnly_ get loaded.

    https://bugs.python.org/issue2190
    https://bugs.python.org/file37625/httponly.patch
    """
    now = time.time()

    magic = f.readline()
    if not re.search(self.magic_re, magic):
        f.close()
        raise LoadError(
            "%r does not look like a Netscape format cookies file" %
            filename)

    try:
        while 1:
            line = f.readline()
            if line == "": break

            # last field may be absent, so keep any trailing tab
            if line.endswith("\n"): line = line[:-1]

            sline = line.strip()
            # support HttpOnly cookies (as stored by curl or old Firefox).
            if sline.startswith("#HttpOnly_"):
                line = sline[10:]
            # skip comments and blank lines XXX what is $ for?
            elif (sline.startswith(("#", "$")) or sline == ""):
                continue

            domain, domain_specified, path, secure, expires, name, value = \
                    line.split("\t")
            secure = (secure == "TRUE")
            domain_specified = (domain_specified == "TRUE")
            if name == "":
                # cookies.txt regards 'Set-Cookie: foo' as a cookie
                # with no name, whereas cookielib regards it as a
                # cookie with no value.
                name = value
                value = None

            initial_dot = domain.startswith(".")
            assert domain_specified == initial_dot

            discard = False
            if expires == "":
                expires = None
                discard = True

            # assume path_specified is false
            c = Cookie(0, name, value,
                       None, False,
                       domain, domain_specified, initial_dot,
                       path, False,
                       secure,
                       expires,
                       discard,
                       None,
                       None,
                       {})
            if not ignore_discard and c.discard:
                continue
            if not ignore_expires and c.is_expired(now):
                continue
            self.set_cookie(c)

    except IOError:
        raise
    except Exception:
        _warn_unhandled_exception()
        raise LoadError("invalid Netscape format cookies file %r: %r" %
                        (filename, line))
示例#21
0
    def _really_load(self, f, filename, ignore_discard, ignore_expires):
        """
        Override this method in order to provide better error handling.
        """
        now = time.time()

        magic = f.readline()
        if not re.search(self.magic_re, magic):
            f.close()

            msg = "%r does not look like a Netscape format cookies file"
            raise LoadError(msg % filename)

        try:
            while 1:
                line = f.readline()
                if line == "":
                    break

                # last field may be absent, so keep any trailing tab
                if line.endswith("\n"):
                    line = line[:-1]

                # skip comments and blank lines XXX what is $ for?
                if line.strip().startswith(("#", "$")) or line.strip() == "":
                    continue

                split_values = line.split("\t")

                if len(split_values) != 7:
                    msg = 'Expected seven tab delimited fields, got %s in %s: %s'
                    args = (len(split_values), filename, line)
                    raise LoadError(msg % args)

                domain, domain_specified, path, secure, expires, name, value = split_values

                secure = (secure == "TRUE")
                domain_specified = (domain_specified == "TRUE")
                if name == "":
                    # cookies.txt regards 'Set-Cookie: foo' as a cookie
                    # with no name, whereas cookielib regards it as a
                    # cookie with no value.
                    name = value
                    value = None

                initial_dot = domain.startswith(".")

                if domain_specified != initial_dot:
                    if domain_specified:
                        msg = (
                            'The second tab delimited field (domain_specified) is'
                            ' set to %s and the domain does NOT start with a dot (%s).'
                            ' This is not acceptable by the Mozilla Cookie format.'
                            ' Issue found at %s: %s')
                        args = (domain_specified, domain, filename, line)
                        raise LoadError(msg % args)

                    else:
                        msg = (
                            'The second tab delimited field (domain_specified) is'
                            ' set to %s and the domain starts with a dot (%s).'
                            ' This is not acceptable by the Mozilla Cookie format.'
                            ' Issue found at %s: %s')
                        args = (domain_specified, domain, filename, line)
                        raise LoadError(msg % args)

                discard = False
                if expires == "":
                    expires = None
                    discard = True

                # assume path_specified is false
                c = Cookie(0, name, value, None, False, domain,
                           domain_specified, initial_dot, path, False, secure,
                           expires, discard, None, None, {})
                if not ignore_discard and c.discard:
                    continue
                if not ignore_expires and c.is_expired(now):
                    continue
                self.set_cookie(c)

        except IOError:
            raise
        except Exception:
            _warn_unhandled_exception()
            raise LoadError("invalid Netscape format cookies file %r: %r" %
                            (filename, line))
示例#22
-2
    def _really_load(self, f, filename, ignore_discard, ignore_expires):
        magic = f.readline()
        if not re.search(self.magic_re, magic):
            msg = ("%r does not look like a Set-Cookie3 (LWP) format "
                   "file" % filename)
            raise LoadError(msg)

        now = time.time()

        header = "Set-Cookie3:"
        boolean_attrs = ("port_spec", "path_spec", "domain_dot",
                         "secure", "discard")
        value_attrs = ("version",
                       "port", "path", "domain",
                       "expires",
                       "comment", "commenturl")

        try:
            while 1:
                line = f.readline()
                if line == "": break
                if not line.startswith(header):
                    continue
                line = line[len(header):].strip()

                for data in split_header_words([line]):
                    name, value = data[0]
                    standard = {}
                    rest = {}
                    for k in boolean_attrs:
                        standard[k] = False
                    for k, v in data[1:]:
                        if k is not None:
                            lc = k.lower()
                        else:
                            lc = None
                        # don't lose case distinction for unknown fields
                        if (lc in value_attrs) or (lc in boolean_attrs):
                            k = lc
                        if k in boolean_attrs:
                            if v is None: v = True
                            standard[k] = v
                        elif k in value_attrs:
                            standard[k] = v
                        else:
                            rest[k] = v

                    h = standard.get
                    expires = h("expires")
                    discard = h("discard")
                    if expires is not None:
                        expires = iso2time(expires)
                    if expires is None:
                        discard = True
                    domain = h("domain")
                    domain_specified = domain.startswith(".")
                    c = Cookie(h("version"), name, value,
                               h("port"), h("port_spec"),
                               domain, domain_specified, h("domain_dot"),
                               h("path"), h("path_spec"),
                               h("secure"),
                               expires,
                               discard,
                               h("comment"),
                               h("commenturl"),
                               rest)
                    if not ignore_discard and c.discard:
                        continue
                    if not ignore_expires and c.is_expired(now):
                        continue
                    self.set_cookie(c)

        except IOError:
            raise
        except Exception:
            _warn_unhandled_exception()
            raise LoadError("invalid Set-Cookie3 format file %r: %r" %
                            (filename, line))