Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

_cookiejar.py 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import http.cookies
  2. """
  3. _cookiejar.py
  4. websocket - WebSocket client library for Python
  5. Copyright 2023 engn33r
  6. Licensed under the Apache License, Version 2.0 (the "License");
  7. you may not use this file except in compliance with the License.
  8. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. """
  16. class SimpleCookieJar:
  17. def __init__(self) -> None:
  18. self.jar = dict()
  19. def add(self, set_cookie: str) -> None:
  20. if set_cookie:
  21. simpleCookie = http.cookies.SimpleCookie(set_cookie)
  22. for k, v in simpleCookie.items():
  23. domain = v.get("domain")
  24. if domain:
  25. if not domain.startswith("."):
  26. domain = "." + domain
  27. cookie = self.jar.get(domain) if self.jar.get(domain) else http.cookies.SimpleCookie()
  28. cookie.update(simpleCookie)
  29. self.jar[domain.lower()] = cookie
  30. def set(self, set_cookie: str) -> None:
  31. if set_cookie:
  32. simpleCookie = http.cookies.SimpleCookie(set_cookie)
  33. for k, v in simpleCookie.items():
  34. domain = v.get("domain")
  35. if domain:
  36. if not domain.startswith("."):
  37. domain = "." + domain
  38. self.jar[domain.lower()] = simpleCookie
  39. def get(self, host: str) -> str:
  40. if not host:
  41. return ""
  42. cookies = []
  43. for domain, simpleCookie in self.jar.items():
  44. host = host.lower()
  45. if host.endswith(domain) or host == domain[1:]:
  46. cookies.append(self.jar.get(domain))
  47. return "; ".join(filter(
  48. None, sorted(
  49. ["%s=%s" % (k, v.value) for cookie in filter(None, cookies) for k, v in cookie.items()]
  50. )))