diff --git a/server.py b/server.py index 3b48b8a..4240a87 100644 --- a/server.py +++ b/server.py @@ -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'\n
The file that the Gemini server sent couldn\'t be decoded by Gem2Browser.
' + 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