Update server.py
This commit is contained in:
parent
c9b3921993
commit
d7228ccc13
1 changed files with 24 additions and 45 deletions
69
server.py
69
server.py
|
@ -38,59 +38,38 @@ def relay():
|
||||||
num = 0
|
num = 0
|
||||||
ok = False
|
ok = False
|
||||||
binary = False
|
binary = False
|
||||||
found = False
|
|
||||||
while True:
|
while True:
|
||||||
gemresponse = gemsocket.recv(1)
|
gemresponse = gemsocket.recv(2048)
|
||||||
if len(gemresponse) != 0:
|
if len(gemresponse) != 0:
|
||||||
gemraw.append(gemresponse[0])
|
for i in gemresponse:
|
||||||
|
gemraw.append(i)
|
||||||
else:
|
else:
|
||||||
break
|
break
|
||||||
|
found = False
|
||||||
|
gemraw = bytes(gemraw)
|
||||||
|
gemcontent_binary = bytearray()
|
||||||
|
mtype = bytearray()
|
||||||
|
for i in gemraw:
|
||||||
try:
|
try:
|
||||||
if num == 0:
|
if not found:
|
||||||
print(gemresponse)
|
mtype += i
|
||||||
try:
|
if "\n" in bytes(mtype).decode() and not found:
|
||||||
if "\r" in bytes(gemraw).decode() and not found:
|
ok = bytes(mtype).decode().split(" ")[0][0] == "2"
|
||||||
found = True
|
mimetype = bytes(mtype).decode().split(" ")[1].split(";")[0]
|
||||||
mimetype = bytes(gemraw).decode()[:-1].split(" ")[1].split(";")[0]
|
elif found:
|
||||||
ok = bytes(gemraw).decode()[:-1].split(" ")[0][0] == 2
|
gemcontent_binary += i
|
||||||
except:
|
|
||||||
pass
|
|
||||||
try:
|
|
||||||
if gemresponse.decode() != "":
|
|
||||||
received += gemresponse.decode()
|
|
||||||
else:
|
|
||||||
break
|
|
||||||
except:
|
|
||||||
print(traceback.format_exc())
|
|
||||||
binary = True
|
|
||||||
except:
|
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)
|
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:
|
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", "")
|
received = received.replace("\r", "")
|
||||||
firstline = True
|
firstline = True
|
||||||
redirected = False
|
redirected = False
|
||||||
|
|
Loading…
Add table
Reference in a new issue