feat: added body support

This commit is contained in:
Elias Renman
2023-05-27 00:58:06 +02:00
parent f17050c7f4
commit fa5a808c45

View File

@@ -39,16 +39,41 @@ class Endpoint:
if endpoint != self.path and self.path != '*' or method != self.method:
return
try:
body = get_body(request)
if body:
kwargs = dict(**kwargs, body=body)
val = function(instance, *args, **kwargs)
respond(cl, 200, val)
return val
except HttpError as e:
respond(cl, e.status,
e.message)
except Exception as e:
print("Some other unkown error occured:", e)
return wrapper
"""
Please note that this only supports content-type 'application/json'
"""
def get_body(request: str):
req = request.split("\r\n")
content_type_index = -10
for index in range(len(req)):
item = req[index]
if item.find("Content-Type") != -1:
if not item.find("application/json"):
return
content_type_index = index
if (content_type_index + 4) == index:
return item
class ServerHandler(object):
def __init__(self, s: socket.socket):
not_found_endpoints = (self.__post_not_found,