Skip to content

Commit

Permalink
cache bugfix + support empty 200 response from .well-known
Browse files Browse the repository at this point in the history
  • Loading branch information
caronc committed Sep 14, 2024
1 parent c804def commit 43a9bb5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
38 changes: 30 additions & 8 deletions apprise/plugins/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -1300,14 +1300,14 @@ def _fetch(self, path, payload=None, params={}, attachment=None,
timeout=self.request_timeout,
)

# Store status code
status_code = r.status_code

self.logger.debug(
'Matrix Response: code=%d, %s' % (
r.status_code, str(r.content)))
response = loads(r.content)

# Store status code
status_code = r.status_code

if r.status_code == requests.codes.too_many_requests:
wait = self.default_wait_ms / 1000
try:
Expand Down Expand Up @@ -1616,7 +1616,7 @@ def server_discovery(self):
self.store.get(self.discovery_identity_key),
)

if base_url is not None and identity_url is not None:
if not (base_url is None and identity_url is None):
# We can use our cached value and return early
return base_url

Expand All @@ -1641,7 +1641,7 @@ def server_discovery(self):
self.logger.debug(
'Matrix Well-Known Base URI not found at %s', verify_url)

# Clear our keys out for fast recall later on
# Set our keys out for fast recall later on
self.store.set(
self.discovery_base_key, '',
expires=self.discovery_cache_length_sec)
Expand All @@ -1657,6 +1657,20 @@ def server_discovery(self):
'%s - %s returned error code: %d', msg, verify_url, code)
raise MatrixDiscoveryException(msg, error_code=code)

if not wk_response:
# This is an acceptable response; we simply do nothing
self.logger.debug(
'Matrix Well-Known Base URI not defined %s', verify_url)

# Set our keys out for fast recall later on
self.store.set(
self.discovery_base_key, '',
expires=self.discovery_cache_length_sec)
self.store.set(
self.discovery_identity_key, '',
expires=self.discovery_cache_length_sec)
return ''

#
# Parse our m.homeserver information
#
Expand Down Expand Up @@ -1695,8 +1709,7 @@ def server_discovery(self):
#
# Phase 2: Handle m.identity_server IF defined
#
if isinstance(wk_response, dict) \
and 'm.identity_server' in wk_response:
if 'm.identity_server' in wk_response:
try:
identity_url = \
wk_response['m.identity_server']['base_url'].rstrip('/')
Expand Down Expand Up @@ -1733,7 +1746,14 @@ def server_discovery(self):
# Update our cache
self.store.set(
self.discovery_identity_key, identity_url,
expires=self.discovery_cache_length_sec)
# Add 2 seconds to prevent this key from expiring before base
expires=self.discovery_cache_length_sec + 2)
else:
# No identity server
self.store.set(
self.discovery_identity_key, '',
# Add 2 seconds to prevent this key from expiring before base
expires=self.discovery_cache_length_sec + 2)

# Update our cache
self.store.set(
Expand All @@ -1758,6 +1778,8 @@ def base_url(self):
self.discovery_base_key, self.discovery_identity_key)
raise

# If we get hear, we need to build our URL dynamically based on what
# was provided to us during the plugins initialization
default_port = 443 if self.secure else 80

return '{schema}://{hostname}{port}'.format(
Expand Down
13 changes: 12 additions & 1 deletion test/test_plugin_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -1192,12 +1192,23 @@ def test_plugin_matrix_discovery_service(mock_post, mock_get):
assert NotifyMatrix.discovery_base_key not in obj.store
assert NotifyMatrix.discovery_identity_key not in obj.store

# Enforce cleanup
# Test an empty block response
response.status_code = requests.codes.ok
response.content = ''
mock_get.return_value = response
mock_get.side_effect = None
mock_post.return_value = response
mock_post.side_effect = None
obj.store.clear(
NotifyMatrix.discovery_base_key, NotifyMatrix.discovery_identity_key)

assert obj.base_url == 'https://example.com'
assert obj.identity_url == 'https://example.com'

# Verify cache saved
assert NotifyMatrix.discovery_base_key in obj.store
assert NotifyMatrix.discovery_identity_key in obj.store

del obj


Expand Down

0 comments on commit 43a9bb5

Please sign in to comment.