Update server.py

This commit is contained in:
Nova Cat 2025-01-14 15:39:13 -08:00
parent c9b3921993
commit d7228ccc13

View file

@ -38,59 +38,38 @@ def relay():
num = 0
ok = False
binary = False
found = False
while True:
gemresponse = gemsocket.recv(1)
gemresponse = gemsocket.recv(2048)
if len(gemresponse) != 0:
gemraw.append(gemresponse[0])
for i in gemresponse:
gemraw.append(i)
else:
break
found = False
gemraw = bytes(gemraw)
gemcontent_binary = bytearray()
mtype = bytearray()
for i in gemraw:
try:
if num == 0:
print(gemresponse)
try:
if "\r" in bytes(gemraw).decode() and not found:
found = True
mimetype = bytes(gemraw).decode()[:-1].split(" ")[1].split(";")[0]
ok = bytes(gemraw).decode()[:-1].split(" ")[0][0] == 2
except:
pass
try:
if gemresponse.decode() != "":
received += gemresponse.decode()
else:
break
except:
print(traceback.format_exc())
binary = True
if not found:
mtype += i
if "\n" in bytes(mtype).decode() and not found:
ok = bytes(mtype).decode().split(" ")[0][0] == "2"
mimetype = bytes(mtype).decode().split(" ")[1].split(";")[0]
elif found:
gemcontent_binary += i
except:
return f'<!DOCTYPE html>\n<html><head><meta charset="UTF-8"><link rel="stylesheet" href="/style.css"><title>Something went wrong...</title></head><body><h1>Something went wrong...</h1><p>The file that the Gemini server sent couldn\'t be decoded by Gem2Browser.</p></body></html>'
binary = True
print(traceback.format_exc())
try:
if not binary:
recived = gemraw.decode()
except:
binary = True
print(traceback.format_exc())
print(mimetype)
if num == 1:
print(mimetype)
for i in bytes(gemraw):
try:
if i.decode() != "\r":
mimetyp += i.decode()
else:
break
except:
print(traceback.format_exc())
break
gr2 = bytearray()
if not mimetype in ["text/gemini", ""]:
frline = True
for i in gemraw:
try:
if frline and i.decode() != "\n":
pass
else:
frline = False
except:
print(traceback.format_exc())
pass
if binary:
return Response(bytes(gemraw), mimetype="text/gemini" if mimetype=="" else mimetype)
return Response(bytes(gemcontent_binary), mimetype="text/gemini" if mimetype=="" else mimetype)
received = received.replace("\r", "")
firstline = True
redirected = False