Minor restructuring.
This commit is contained in:
parent
de3c418051
commit
104ba07a05
28
av98.py
28
av98.py
|
@ -228,14 +228,6 @@ class GeminiClient(cmd.Cmd):
|
||||||
header = header.decode("UTF-8").strip()
|
header = header.decode("UTF-8").strip()
|
||||||
self._debug("Response header: %s." % header)
|
self._debug("Response header: %s." % header)
|
||||||
body = f.read()
|
body = f.read()
|
||||||
status, mime = header.split("\t")
|
|
||||||
mime, mime_options = cgi.parse_header(mime)
|
|
||||||
if "charset" in mime_options:
|
|
||||||
try:
|
|
||||||
codecs.lookup(mime_options["charset"])
|
|
||||||
except LookupError:
|
|
||||||
print("Header declared unknown encoding %s" % value)
|
|
||||||
return
|
|
||||||
|
|
||||||
# Catch network errors which may be recoverable if a redundant
|
# Catch network errors which may be recoverable if a redundant
|
||||||
# mirror is specified
|
# mirror is specified
|
||||||
|
@ -268,17 +260,34 @@ Slow internet connection? Use 'set timeout' to be more patient.""")
|
||||||
print("ERROR: " + str(err))
|
print("ERROR: " + str(err))
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# Look at what we got
|
||||||
|
status, mime = header.split("\t")
|
||||||
|
# Handle different statuses.
|
||||||
|
# Everything other than 200 should return here
|
||||||
|
if status in ("2", "200"):
|
||||||
|
if mime == "":
|
||||||
|
mime = "text/gemini; charset=utf-8"
|
||||||
|
mime, mime_options = cgi.parse_header(mime)
|
||||||
|
if "charset" in mime_options:
|
||||||
|
try:
|
||||||
|
codecs.lookup(mime_options["charset"])
|
||||||
|
except LookupError:
|
||||||
|
print("Header declared unknown encoding %s" % value)
|
||||||
|
return
|
||||||
# Handle redirects
|
# Handle redirects
|
||||||
# (Temporarily accepting unofficial conman status codes)
|
# (Temporarily accepting unofficial conman status codes)
|
||||||
if status in ("3", "301"):
|
elif status in ("3", "301"):
|
||||||
self._debug("Following redirect to %s." % mime)
|
self._debug("Following redirect to %s." % mime)
|
||||||
new_gi = GeminiItem(gi.host, gi.port, mime, None)
|
new_gi = GeminiItem(gi.host, gi.port, mime, None)
|
||||||
self._go_to_gi(new_gi)
|
self._go_to_gi(new_gi)
|
||||||
return
|
return
|
||||||
|
# Not found
|
||||||
elif status in ("4", "404"):
|
elif status in ("4", "404"):
|
||||||
print("Path %s does not exist at %s:%d" % (gi.path, gi.host, gi.port))
|
print("Path %s does not exist at %s:%d" % (gi.path, gi.host, gi.port))
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# If we're still here, this is a 200
|
||||||
|
|
||||||
# Save the result in a temporary file
|
# Save the result in a temporary file
|
||||||
## Delete old file
|
## Delete old file
|
||||||
if self.tmp_filename:
|
if self.tmp_filename:
|
||||||
|
@ -295,7 +304,6 @@ Slow internet connection? Use 'set timeout' to be more patient.""")
|
||||||
else:
|
else:
|
||||||
mode = "wb"
|
mode = "wb"
|
||||||
encoding = None
|
encoding = None
|
||||||
|
|
||||||
## Write
|
## Write
|
||||||
tmpf = tempfile.NamedTemporaryFile(mode, encoding=encoding, delete=False)
|
tmpf = tempfile.NamedTemporaryFile(mode, encoding=encoding, delete=False)
|
||||||
size = tmpf.write(body)
|
size = tmpf.write(body)
|
||||||
|
|
Loading…
Reference in New Issue