NovaBryan/EVE_Dump
Log | Files | 📦 下载最新源码 (.zip) | Refs | README

_LWPCookieJar.py (4848B)


      1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\carbon\common\stdlib\_LWPCookieJar.py
      2 import time, re
      3 from cookielib import _warn_unhandled_exception, FileCookieJar, LoadError, Cookie, MISSING_FILENAME_TEXT, join_header_words, split_header_words, iso2time, time2isoz
      4 
      5 def lwp_cookie_str(cookie):
      6     h = [(cookie.name, cookie.value), ('path', cookie.path), ('domain', cookie.domain)]
      7     if cookie.port is not None:
      8         h.append(('port', cookie.port))
      9     if cookie.path_specified:
     10         h.append(('path_spec', None))
     11     if cookie.port_specified:
     12         h.append(('port_spec', None))
     13     if cookie.domain_initial_dot:
     14         h.append(('domain_dot', None))
     15     if cookie.secure:
     16         h.append(('secure', None))
     17     if cookie.expires:
     18         h.append(('expires', time2isoz(float(cookie.expires))))
     19     if cookie.discard:
     20         h.append(('discard', None))
     21     if cookie.comment:
     22         h.append(('comment', cookie.comment))
     23     if cookie.comment_url:
     24         h.append(('commenturl', cookie.comment_url))
     25     keys = cookie._rest.keys()
     26     keys.sort()
     27     for k in keys:
     28         h.append((k, str(cookie._rest[k])))
     29 
     30     h.append(('version', str(cookie.version)))
     31     return join_header_words([h])
     32 
     33 
     34 class LWPCookieJar(FileCookieJar):
     35 
     36     def as_lwp_str(self, ignore_discard = True, ignore_expires = True):
     37         now = time.time()
     38         r = []
     39         for cookie in self:
     40             if not ignore_discard and cookie.discard:
     41                 continue
     42             if not ignore_expires and cookie.is_expired(now):
     43                 continue
     44             r.append('Set-Cookie3: %s' % lwp_cookie_str(cookie))
     45 
     46         return '\n'.join(r + [''])
     47 
     48     def save(self, filename = None, ignore_discard = False, ignore_expires = False):
     49         if filename is None:
     50             if self.filename is not None:
     51                 filename = self.filename
     52             else:
     53                 raise ValueError(MISSING_FILENAME_TEXT)
     54         f = open(filename, 'w')
     55         try:
     56             f.write('#LWP-Cookies-2.0\n')
     57             f.write(self.as_lwp_str(ignore_discard, ignore_expires))
     58         finally:
     59             f.close()
     60 
     61     def _really_load(self, f, filename, ignore_discard, ignore_expires):
     62         magic = f.readline()
     63         if not re.search(self.magic_re, magic):
     64             msg = '%r does not look like a Set-Cookie3 (LWP) format file' % filename
     65             raise LoadError(msg)
     66         now = time.time()
     67         header = 'Set-Cookie3:'
     68         boolean_attrs = ('port_spec', 'path_spec', 'domain_dot', 'secure', 'discard')
     69         value_attrs = ('version', 'port', 'path', 'domain', 'expires', 'comment', 'commenturl')
     70         try:
     71             while 1:
     72                 line = f.readline()
     73                 if line == '':
     74                     break
     75                 if not line.startswith(header):
     76                     continue
     77                 line = line[len(header):].strip()
     78                 for data in split_header_words([line]):
     79                     name, value = data[0]
     80                     standard = {}
     81                     rest = {}
     82                     for k in boolean_attrs:
     83                         standard[k] = False
     84 
     85                     for k, v in data[1:]:
     86                         if k is not None:
     87                             lc = k.lower()
     88                         else:
     89                             lc = None
     90                         if lc in value_attrs or lc in boolean_attrs:
     91                             k = lc
     92                         if k in boolean_attrs:
     93                             if v is None:
     94                                 v = True
     95                             standard[k] = v
     96                         elif k in value_attrs:
     97                             standard[k] = v
     98                         else:
     99                             rest[k] = v
    100 
    101                     h = standard.get
    102                     expires = h('expires')
    103                     discard = h('discard')
    104                     if expires is not None:
    105                         expires = iso2time(expires)
    106                     if expires is None:
    107                         discard = True
    108                     domain = h('domain')
    109                     domain_specified = domain.startswith('.')
    110                     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)
    111                     if not ignore_discard and c.discard:
    112                         continue
    113                     if not ignore_expires and c.is_expired(now):
    114                         continue
    115                     self.set_cookie(c)
    116 
    117         except IOError:
    118             raise
    119         except Exception:
    120             _warn_unhandled_exception()
    121             raise LoadError('invalid Set-Cookie3 format file %r: %r' % (filename, line))