Restructure redirect logic so that things like hostname lookup errors don't trigger an infinite loop.
This commit is contained in:
parent
e948ce0a13
commit
1668ed1468
|
@ -40,17 +40,14 @@ while True:
|
||||||
print("Sorry, Gemini links only.")
|
print("Sorry, Gemini links only.")
|
||||||
continue
|
continue
|
||||||
# Do the Gemini transaction, following redirects
|
# Do the Gemini transaction, following redirects
|
||||||
while True:
|
|
||||||
try:
|
try:
|
||||||
|
while True:
|
||||||
s = socket.create_connection((parsed_url.netloc, 1965))
|
s = socket.create_connection((parsed_url.netloc, 1965))
|
||||||
context = ssl.SSLContext()
|
context = ssl.SSLContext()
|
||||||
context.check_hostname = False
|
context.check_hostname = False
|
||||||
context.verify_mode = ssl.CERT_NONE
|
context.verify_mode = ssl.CERT_NONE
|
||||||
s = context.wrap_socket(s, server_hostname = parsed_url.netloc)
|
s = context.wrap_socket(s, server_hostname = parsed_url.netloc)
|
||||||
s.sendall((parsed_url.path + '\r\n').encode("UTF-8"))
|
s.sendall((parsed_url.path + '\r\n').encode("UTF-8"))
|
||||||
except:
|
|
||||||
print("Network error!")
|
|
||||||
continue
|
|
||||||
# Get header and check for redirects
|
# Get header and check for redirects
|
||||||
fp = s.makefile("rb")
|
fp = s.makefile("rb")
|
||||||
header = fp.readline()
|
header = fp.readline()
|
||||||
|
@ -62,6 +59,9 @@ while True:
|
||||||
# Follow the redirect
|
# Follow the redirect
|
||||||
url = absolutise_url(url, mime)
|
url = absolutise_url(url, mime)
|
||||||
parsed_url = urllib.parse.urlparse(url)
|
parsed_url = urllib.parse.urlparse(url)
|
||||||
|
except Exception as err:
|
||||||
|
print(err)
|
||||||
|
continue
|
||||||
# Fail if transaction was not successful
|
# Fail if transaction was not successful
|
||||||
if not status.startswith("2"):
|
if not status.startswith("2"):
|
||||||
print("Error %s: %s" % (status, mime))
|
print("Error %s: %s" % (status, mime))
|
||||||
|
|
Loading…
Reference in New Issue