Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/websocket/_cookiejar.py: 24%
34 statements
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-25 06:34 +0000
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-25 06:34 +0000
1import http.cookies
3from typing import Optional
5"""
6_cookiejar.py
7websocket - WebSocket client library for Python
9Copyright 2023 engn33r
11Licensed under the Apache License, Version 2.0 (the "License");
12you may not use this file except in compliance with the License.
13You may obtain a copy of the License at
15 http://www.apache.org/licenses/LICENSE-2.0
17Unless required by applicable law or agreed to in writing, software
18distributed under the License is distributed on an "AS IS" BASIS,
19WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20See the License for the specific language governing permissions and
21limitations under the License.
22"""
25class SimpleCookieJar:
26 def __init__(self) -> None:
27 self.jar = dict()
29 def add(self, set_cookie: Optional[str]) -> None:
30 if set_cookie:
31 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 cookie = self.jar.get(domain) if self.jar.get(domain) else http.cookies.SimpleCookie()
39 cookie.update(simpleCookie)
40 self.jar[domain.lower()] = cookie
42 def set(self, set_cookie: str) -> None:
43 if set_cookie:
44 simpleCookie = http.cookies.SimpleCookie(set_cookie)
46 for k, v in simpleCookie.items():
47 domain = v.get("domain")
48 if domain:
49 if not domain.startswith("."):
50 domain = "." + domain
51 self.jar[domain.lower()] = simpleCookie
53 def get(self, host: str) -> str:
54 if not host:
55 return ""
57 cookies = []
58 for domain, simpleCookie in self.jar.items():
59 host = host.lower()
60 if host.endswith(domain) or host == domain[1:]:
61 cookies.append(self.jar.get(domain))
63 return "; ".join(filter(
64 None, sorted(
65 ["%s=%s" % (k, v.value) for cookie in filter(None, cookies) for k, v in cookie.items()]
66 )))