Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/websocket/_cookiejar.py: 21%

33 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 06:48 +0000

1import http.cookies 

2 

3""" 

4_cookiejar.py 

5websocket - WebSocket client library for Python 

6 

7Copyright 2022 engn33r 

8 

9Licensed under the Apache License, Version 2.0 (the "License"); 

10you may not use this file except in compliance with the License. 

11You may obtain a copy of the License at 

12 

13 http://www.apache.org/licenses/LICENSE-2.0 

14 

15Unless required by applicable law or agreed to in writing, software 

16distributed under the License is distributed on an "AS IS" BASIS, 

17WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

18See the License for the specific language governing permissions and 

19limitations under the License. 

20""" 

21 

22 

23class SimpleCookieJar: 

24 def __init__(self) -> None: 

25 self.jar = dict() 

26 

27 def add(self, set_cookie: str) -> None: 

28 if set_cookie: 

29 simpleCookie = http.cookies.SimpleCookie(set_cookie) 

30 

31 for k, v in simpleCookie.items(): 

32 domain = v.get("domain") 

33 if domain: 

34 if not domain.startswith("."): 

35 domain = "." + domain 

36 cookie = self.jar.get(domain) if self.jar.get(domain) else http.cookies.SimpleCookie() 

37 cookie.update(simpleCookie) 

38 self.jar[domain.lower()] = cookie 

39 

40 def set(self, set_cookie: str) -> None: 

41 if set_cookie: 

42 simpleCookie = http.cookies.SimpleCookie(set_cookie) 

43 

44 for k, v in simpleCookie.items(): 

45 domain = v.get("domain") 

46 if domain: 

47 if not domain.startswith("."): 

48 domain = "." + domain 

49 self.jar[domain.lower()] = simpleCookie 

50 

51 def get(self, host: str) -> str: 

52 if not host: 

53 return "" 

54 

55 cookies = [] 

56 for domain, simpleCookie in self.jar.items(): 

57 host = host.lower() 

58 if host.endswith(domain) or host == domain[1:]: 

59 cookies.append(self.jar.get(domain)) 

60 

61 return "; ".join(filter( 

62 None, sorted( 

63 ["%s=%s" % (k, v.value) for cookie in filter(None, cookies) for k, v in cookie.items()] 

64 )))