\n"
title = "Something went wrong..."
try:
gsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
fulladdr = "gemini://" + url + ("?" + queries if queries != None else "")
gemsocket = ssl._create_unverified_context().wrap_socket(gsocket, server_hostname=urlparse(fulladdr).hostname)
gemsocket.connect((urlparse(fulladdr).hostname, 1965))
gemsocket.send(bytes(fulladdr + "\r\n", "UTF-8"))
received = ""
mimetype = ""
gemraw = bytearray()
num = 0
while True:
gemresponse = gemsocket.recv(2048)
try:
if num = 0:
try:
mimetype = gemresponse.decode().replace("\r","").split("\n")[0].split(" ")[1].split(";")[0]
if mimetype != "text/gemini".lower() and gemresponse.decode().replace("\r","").split("\n")[0].split(" ")[0][0] != "2":
print("Non-ordinary type! " + mimetype)
except:
pass
elif num = 1 and not mimetype in ["text/gemini", ""]:
pass
elif num >= 2 and not mimetype in ["text/gemini", ""]:
for i in gemresponse:
gemraw.append(i)
if gemresponse.decode() != "":
received += gemresponse.decode()
else:
break
num+=1
except:
return f'\nSomething went wrong...
Something went wrong...
The file that the Gemini server sent couldn\'t be decoded by Gem2Browser.
'
if not mimetype.lower() in ["text/gemini", ""]:
return bytes(gemraw)
received = received.replace("\r", "")
firstline = True
redirected = False
gemtext = True
code = ""
escaped = False
for i in received.split("\n"):
i = i.replace("<", "<")
if firstline:
if i.split(" ")[0][0] == "3":
return redirect("/gem?gemini=" + quote(i.split(" ")[1][9:], safe=''))
elif i.split(" ")[0][0] == "1":
return f'\nInput required
Input required
The specified Gemini server wants more data:
{i}
'
elif i.split(" ")[0][0] != "2":
return f'\nSomething went wrong...
Something went wrong...
The specified Gemini server returned a status of: {i}
'
else:
firstline = False
if i.split(" ")[1].split(";")[0] != "text/gemini":
print("Unrecognised type: " + i.split(" ")[1])
return Response(" ".join(received.split("\n")[1:]), mimetype=i.split(" ")[1])
else:
if escaped:
if i[0:3] == "```":
code += "\n"
escaped = False
else:
code += i + "\n"
else:
if i[0:2] == "# ":
if title == "Something went wrong...":
title = i[2:]
temp = i[2:]
code += f"