cookieManager.py (3685B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\carbonui\control\browser\cookieManager.py 2 import Cookie 3 import urlparse 4 import blue 5 import log 6 import re 7 ip = re.compile('[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+') 8 9 class CookieManager: 10 11 def __init__(self): 12 self.cookies = [] 13 14 def ProcessCookies(self, httpResponse): 15 host, path = urlparse.urlsplit(httpResponse.url.encode('ascii'))[1:3] 16 for each in httpResponse.info().headers: 17 if not each.lower().startswith('set-cookie:'): 18 continue 19 raw = each[len('set-cookie:'):] 20 cookie = Cookie.SimpleCookie(raw) 21 for morsel in cookie.values(): 22 for oldmorsel in self.cookies[:]: 23 if morsel.key == oldmorsel.key and (morsel['domain'] or host == oldmorsel['domain'] or oldmorsel.__host) and morsel['path'] == oldmorsel['path']: 24 self.cookies.remove(oldmorsel) 25 26 if morsel['max-age'] == '0': 27 continue 28 if not morsel['path'] or not path.startswith(morsel['path']): 29 log.LogWarn('browser', 'No proper path; rejecting cookie:', morsel.output(header='')) 30 continue 31 if morsel['domain']: 32 if not morsel['domain'].startswith('.'): 33 log.LogWarn('browser', "Domain doesn't begin with a dot; rejecting cookie:", morsel.output(header='')) 34 continue 35 if morsel['domain'][1:-1].find('.') == -1: 36 log.LogWarn('browser', 'Domain contains no embedded dots; rejecting cookie:', morsel.output(header='')) 37 continue 38 if not self.DomainMatches(host, morsel['domain']): 39 log.LogWarn('browser', "Response-host doesn't match domain; rejecting cookie:", morsel.output(header='')) 40 continue 41 if not ip.match(host) and host[:-len(morsel['domain'])].find('.') != -1: 42 log.LogWarn('browser', 'HD case; rejecting cookie:', morsel.output(header='')) 43 continue 44 morsel.__host = host 45 morsel.__time = blue.os.GetWallclockTime() 46 self.cookies.append(morsel) 47 48 def GetCookie(self, url): 49 url = url.encode('ascii') 50 morsels = [] 51 host, path = urlparse.urlsplit(url)[1:3] 52 for morsel in self.cookies[:]: 53 if morsel['max-age'] and blue.os.TimeDiffInMs(morsel.__time) / 1000 > morsel['max-age']: 54 self.cookies.remove(morsel) 55 continue 56 if not self.DomainMatches(host, morsel['domain'] or morsel.__host): 57 continue 58 if not path.startswith(morsel['path']): 59 continue 60 morsels.append(morsel) 61 62 morsels.sort(lambda a, b: cmp(len(a['path'] or ''), len(b['path'] or ''))) 63 morsels.reverse() 64 parts = [] 65 for morsel in morsels: 66 s = '%s=%s' % (morsel.key, morsel.coded_value) 67 if morsel['path']: 68 s = '%s;$Path=%s' % (s, morsel['path']) 69 if morsel['domain']: 70 s = '%s;$Domain=%s' % (s, morsel['domain']) 71 parts.append(s) 72 73 if parts: 74 return '$Version=%s;%s' % (min([ each['version'] and int(each['version']) or 0 for each in morsels ]), ';'.join(parts)) 75 else: 76 return None 77 78 def DomainMatches(self, d1, d2): 79 if d2.startswith('.'): 80 return d1.endswith(d2) and d1 != d2 81 else: 82 return d1 == d2