Mercurial > p > roundup > code
comparison roundup/cgi/client.py @ 6813:6b636fb29740
Refactor client.py session cookie code. Remove session db access.
The original code did a session_db.exists test followed by a
session_db.getall.
Refactor does a getall and if a KeyError is thrown, handles the
error. Most likely the session key will be found so exception handling
won't be triggered.
Added test case to test the exception code path and minor
rearrangement of setup code.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Wed, 03 Aug 2022 17:34:58 -0400 |
| parents | 9a1f5e496e6c |
| children | d9c9f5b81d4d |
comparison
equal
deleted
inserted
replaced
| 6812:d7905a78ab8a | 6813:6b636fb29740 |
|---|---|
| 172 # parse cookies for session id | 172 # parse cookies for session id |
| 173 self.cookie_name = 'roundup_session_%s' % \ | 173 self.cookie_name = 'roundup_session_%s' % \ |
| 174 re.sub('[^a-zA-Z]', '', client.instance.config.TRACKER_NAME) | 174 re.sub('[^a-zA-Z]', '', client.instance.config.TRACKER_NAME) |
| 175 cookies = LiberalCookie(client.env.get('HTTP_COOKIE', '')) | 175 cookies = LiberalCookie(client.env.get('HTTP_COOKIE', '')) |
| 176 if self.cookie_name in cookies: | 176 if self.cookie_name in cookies: |
| 177 if not self.session_db.exists(cookies[self.cookie_name].value): | 177 try: |
| 178 self._sid = cookies[self.cookie_name].value | |
| 179 self._data = self.session_db.getall(self._sid) | |
| 180 except KeyError: | |
| 178 self._sid = None | 181 self._sid = None |
| 179 # remove old cookie | 182 # remove old cookie |
| 180 self.client.add_cookie(self.cookie_name, None) | 183 self.client.add_cookie(self.cookie_name, None) |
| 181 else: | |
| 182 self._sid = cookies[self.cookie_name].value | |
| 183 self._data = self.session_db.getall(self._sid) | |
| 184 | 184 |
| 185 def _gen_sid(self): | 185 def _gen_sid(self): |
| 186 """ generate a unique session key """ | 186 """ generate a unique session key """ |
| 187 while 1: | 187 while 1: |
| 188 s = b2s(binascii.b2a_base64(random_.token_bytes(32)).strip()).rstrip('=') | 188 s = b2s(binascii.b2a_base64(random_.token_bytes(32)).strip()).rstrip('=') |
