Exit from retries of unauthorized

main2
Bob Mottram 2019-10-23 19:44:03 +01:00
parent ef7135cd98
commit 21ce09e192
2 changed files with 14 additions and 8 deletions

View File

@ -1012,14 +1012,18 @@ def threadSendPost(session,postJsonStr: str,federationList: [],\
sendIntervalSec=30 sendIntervalSec=30
for attempt in range(20): for attempt in range(20):
postResult=None postResult=None
unauthorized=False
try: try:
postResult = \ postResult,unauthorized = \
postJsonString(session,postJsonStr,federationList, \ postJsonString(session,postJsonStr,federationList, \
inboxUrl,signatureHeaderJson, \ inboxUrl,signatureHeaderJson, \
"inbox:write",debug) "inbox:write",debug)
except Exception as e: except Exception as e:
print('ERROR: postJsonString failed') print('ERROR: postJsonString failed '+str(e))
print(e) if unauthorized==True:
print(postJsonStr)
print('threadSendPost: Post is unauthorized')
break
if postResult: if postResult:
logStr='Success on try '+str(tries)+': '+postJsonStr logStr='Success on try '+str(tries)+': '+postJsonStr
else: else:

View File

@ -75,9 +75,10 @@ def postJsonString(session,postJsonStr: str, \
inboxUrl: str, \ inboxUrl: str, \
headers: {}, \ headers: {}, \
capability: str, \ capability: str, \
debug: bool) -> bool: debug: bool) -> (bool,bool):
"""Post a json message string to the inbox of another person """Post a json message string to the inbox of another person
Supplying a capability, such as "inbox:write" Supplying a capability, such as "inbox:write"
The second boolean returned is true if the send is unauthorized
NOTE: Here we post a string rather than the original json so that NOTE: Here we post a string rather than the original json so that
conversions between string and json format don't invalidate conversions between string and json format don't invalidate
the message body digest of http signatures the message body digest of http signatures
@ -88,7 +89,7 @@ def postJsonString(session,postJsonStr: str, \
# check that we are posting to a permitted domain # check that we are posting to a permitted domain
if not urlPermitted(inboxUrl,federationList,capability): if not urlPermitted(inboxUrl,federationList,capability):
print('postJson: '+inboxUrl+' not permitted by capabilities') print('postJson: '+inboxUrl+' not permitted by capabilities')
return None return None,None
postResult = session.post(url = inboxUrl, data = postJsonStr, headers=headers) postResult = session.post(url = inboxUrl, data = postJsonStr, headers=headers)
if postResult.status_code<200 or postResult.status_code>202: if postResult.status_code<200 or postResult.status_code>202:
@ -97,13 +98,14 @@ def postJsonString(session,postJsonStr: str, \
# postResult = session.post(url = inboxUrl, data = postJsonStr, headers=headers) # postResult = session.post(url = inboxUrl, data = postJsonStr, headers=headers)
# if not (postResult.status_code<200 or postResult.status_code>202): # if not (postResult.status_code<200 or postResult.status_code>202):
# return True # return True
if postResult.status_code==401: if postResult.status_code>=400 and postResult.status_code<=405:
print('WARN: >>> Post to '+inboxUrl+' is unauthorized <<<') print('WARN: >>> Post to '+inboxUrl+' is unauthorized <<<')
return False,True
else: else:
print('WARN: Failed to post to '+inboxUrl+' with headers '+str(headers)) print('WARN: Failed to post to '+inboxUrl+' with headers '+str(headers))
print('status code '+str(postResult.status_code)) print('status code '+str(postResult.status_code))
return False return False,False
return True return True,False
def postImage(session,attachImageFilename: str,federationList: [],inboxUrl: str,headers: {},capability: str) -> str: def postImage(session,attachImageFilename: str,federationList: [],inboxUrl: str,headers: {},capability: str) -> str:
"""Post an image to the inbox of another person or outbox via c2s """Post an image to the inbox of another person or outbox via c2s