Updated Server to used buildListener for pings and updated the listener and firewall to handle definitions accordingly
This commit is contained in:
+83
-81
@@ -4,119 +4,121 @@ import logging
|
||||
|
||||
from http.server import HTTPServer, BaseHTTPRequestHandler
|
||||
|
||||
from .dispatcher import dispatchRequest
|
||||
from .createresponse import buildResponse
|
||||
from plugins.rmass.listeners import synapselistener
|
||||
|
||||
#from .dispatcher import dispatchRequest
|
||||
#from .createresponse import buildResponse
|
||||
from plugins.rmass.listeners import synapselistener, matrixpinglistener
|
||||
from core.server.createlistener import buildListener
|
||||
from plugins.rmass.actions import synapsesetbridgeonline
|
||||
#from plugins.rmass.actions import synapsesetbridgeonline
|
||||
|
||||
port = int(os.environ.get("SERVER_PORT", 19001))
|
||||
|
||||
|
||||
class RequestHandler(BaseHTTPRequestHandler):
|
||||
def do_POST(self):
|
||||
if self.path == "/_matrix/app/v1/ping":
|
||||
logging.debug("POST request received")
|
||||
|
||||
from core.execution.createthread import buildThread
|
||||
thread = buildThread(synapsesetbridgeonline)
|
||||
buildListener(matrixpinglistener, self)
|
||||
|
||||
self.send_response(200)
|
||||
self.send_header("Content-Type", "application/json")
|
||||
self.send_header("Content-Length", "2")
|
||||
self.end_headers()
|
||||
self.wfile.write(b"{}")
|
||||
return
|
||||
# if self.path == "/_matrix/app/v1/ping":
|
||||
|
||||
else:
|
||||
#lifecycles = {}
|
||||
print("POST request received")
|
||||
# from core.execution.createthread import buildThread
|
||||
# thread = buildThread(synapsesetbridgeonline)
|
||||
|
||||
max_request_size = 1024
|
||||
# self.send_response(200)
|
||||
# self.send_header("Content-Type", "application/json")
|
||||
# self.send_header("Content-Length", "2")
|
||||
# self.end_headers()
|
||||
# self.wfile.write(b"{}")
|
||||
# return
|
||||
|
||||
content_length = self.headers.get("Content-Length")
|
||||
# else:
|
||||
# print("POST request received")
|
||||
|
||||
# Check whether Content-Length exists
|
||||
if content_length is None:
|
||||
response = {
|
||||
"status": "error",
|
||||
"message": "Content length required"
|
||||
}
|
||||
status = 411
|
||||
# max_request_size = 1024
|
||||
|
||||
else:
|
||||
# Attempt to convert the header into an integer
|
||||
try:
|
||||
length = int(content_length)
|
||||
except ValueError:
|
||||
length = None
|
||||
# content_length = self.headers.get("Content-Length")
|
||||
|
||||
# Validate the converted length
|
||||
if length is None:
|
||||
response = {
|
||||
"status": "error",
|
||||
"message": "Invalid Content-Length"
|
||||
}
|
||||
status = 400
|
||||
# # Check whether Content-Length exists
|
||||
# if content_length is None:
|
||||
# response = {
|
||||
# "status": "error",
|
||||
# "message": "Content length required"
|
||||
# }
|
||||
# status = 411
|
||||
|
||||
elif length < 0:
|
||||
response = {
|
||||
"status": "error",
|
||||
"message": "Bad Request"
|
||||
}
|
||||
status = 400
|
||||
# else:
|
||||
# # Attempt to convert the header into an integer
|
||||
# try:
|
||||
# length = int(content_length)
|
||||
# except ValueError:
|
||||
# length = None
|
||||
|
||||
elif length > max_request_size:
|
||||
response = {
|
||||
"status": "error",
|
||||
"message": "Content exceeds maximum request size"
|
||||
}
|
||||
status = 413
|
||||
# # Validate the converted length
|
||||
# if length is None:
|
||||
# response = {
|
||||
# "status": "error",
|
||||
# "message": "Invalid Content-Length"
|
||||
# }
|
||||
# status = 400
|
||||
|
||||
else:
|
||||
# Content-Length is valid, so read the body
|
||||
data = self.rfile.read(length)
|
||||
# elif length < 0:
|
||||
# response = {
|
||||
# "status": "error",
|
||||
# "message": "Bad Request"
|
||||
# }
|
||||
# status = 400
|
||||
|
||||
try:
|
||||
request = json.loads(data)
|
||||
# elif length > max_request_size:
|
||||
# response = {
|
||||
# "status": "error",
|
||||
# "message": "Content exceeds maximum request size"
|
||||
# }
|
||||
# status = 413
|
||||
|
||||
except json.JSONDecodeError:
|
||||
response = {
|
||||
"status": "error",
|
||||
"message": "Invalid JSON received"
|
||||
}
|
||||
status = 400
|
||||
# else:
|
||||
# # Content-Length is valid, so read the body
|
||||
# data = self.rfile.read(length)
|
||||
|
||||
else:
|
||||
# Only valid JSON reaches the dispatcher
|
||||
thread, lifecycles, module, mode = dispatchRequest(
|
||||
request, lifecycles
|
||||
)
|
||||
# try:
|
||||
# request = json.loads(data)
|
||||
|
||||
response = buildResponse(module, thread, mode)
|
||||
status = 200
|
||||
# except json.JSONDecodeError:
|
||||
# response = {
|
||||
# "status": "error",
|
||||
# "message": "Invalid JSON received"
|
||||
# }
|
||||
# status = 400
|
||||
|
||||
# Serialize the response
|
||||
responsebytes = json.dumps(response).encode("utf-8")
|
||||
# else:
|
||||
# # Only valid JSON reaches the dispatcher
|
||||
# thread, lifecycles, module, mode = dispatchRequest(
|
||||
# request, lifecycles
|
||||
# )
|
||||
|
||||
# Send one HTTP response
|
||||
self.send_response(status)
|
||||
self.send_header("Content-Type", "application/json")
|
||||
self.send_header("Content-Length", str(len(responsebytes)))
|
||||
self.end_headers()
|
||||
# response = buildResponse(module, thread, mode)
|
||||
# status = 200
|
||||
|
||||
self.wfile.write(responsebytes)
|
||||
# # Serialize the response
|
||||
# responsebytes = json.dumps(response).encode("utf-8")
|
||||
|
||||
# # Send one HTTP response
|
||||
# self.send_response(status)
|
||||
# self.send_header("Content-Type", "application/json")
|
||||
# self.send_header("Content-Length", str(len(responsebytes)))
|
||||
# self.end_headers()
|
||||
|
||||
# self.wfile.write(responsebytes)
|
||||
|
||||
|
||||
def do_PUT(self):
|
||||
#print("PUT request received")
|
||||
logging.debug("PUT request received")
|
||||
|
||||
outputs = buildListener(synapselistener, self)
|
||||
buildListener(synapselistener, self)
|
||||
|
||||
def startServer():
|
||||
server = HTTPServer(("0.0.0.0", port), RequestHandler)
|
||||
|
||||
logging.info(f"Server listening on port {port}")
|
||||
#outputs = {}
|
||||
#outputs, response = buildAction(pingsynapse, outputs)
|
||||
logging.info(f"Server listening on port {port}")
|
||||
|
||||
server.serve_forever()
|
||||
Reference in New Issue
Block a user