Commit 1997f2ce authored by Yann Garcia's avatar Yann Garcia

Validate MEC simulator for UETAG

parent 2a63e10d
......@@ -6,6 +6,10 @@ from mec_database import mec_database
class myHandler(http.server.BaseHTTPRequestHandler):
__db__ = mec_database()
def __init__(self, p_request, p_client_address, p_server):
super(http.server.BaseHTTPRequestHandler, self).__init__(p_request, p_client_address, p_server)
self.wfile._wbufsize = -1;
# Handler for the GET requests
def do_GET(self):
print('>>> do_GET: ', self.path)
......@@ -14,8 +18,9 @@ class myHandler(http.server.BaseHTTPRequestHandler):
if self.__check_http_headers__() == False:
resp = "{\"problemDetails\": {\t\"type\": \"Bad Request\",\t\"title\": \"N/A\",\t\"status\": 400,\t\"detail\": \"Wrong headers\",\t\"instance\": \"N/A\"}}"
self.send_response(400, 'Bad Request')
self.send_header('Host', self.headers.get('Host'))
self.send_header('Host', self.headers.get('Host')) # send_header() shall be after send_response()
self.send_header('Authorization', self.headers.get('Authorization'))
self.send_header('Connection', 'keep-alive')
self.send_header('Content-Type', 'application/problem+json')
l = len(resp)
self.send_header('Content-Length', str(l))
......@@ -27,8 +32,9 @@ class myHandler(http.server.BaseHTTPRequestHandler):
print('do_GET: resp= ', resp, ', Content_Type= ', content_type)
if (resp == None):
self.send_response(404, 'Not Found')
self.send_header('Host', self.headers.get('Host'))
self.send_header('Host', self.headers.get('Host')) # send_header() shall be after send_response()
self.send_header('Authorization', self.headers.get('Authorization'))
self.send_header('Connection', 'keep-alive')
self.send_header('Content-Type', content_type)
self.send_header('Content-Length', '0')
self.end_headers()
......@@ -41,13 +47,21 @@ class myHandler(http.server.BaseHTTPRequestHandler):
self.send_response(400, 'Bad Request')
else:
self.send_response(200, 'OK')
self.send_header('Host', self.headers.get('Host'))
self.send_header('Host', self.headers.get('Host')) # send_header() shall be after send_response()
self.send_header('Authorization', self.headers.get('Authorization'))
self.send_header('Connection', 'keep-alive')
self.send_header('Content-Type', content_type)
l = len(resp)
self.send_header('Content-Length', str(l))
self.end_headers()
self.wfile.write(bytes(resp, 'utf-8'))
#self.end_headers() # Unable to bufferize wfile, so calling end_headers() headers and body are sent in 2 different TCP packets
self._headers_buffer.append(b"\r\n")
self._headers_buffer.append(bytes(resp, 'utf-8'))
self.wfile.write(b"".join(self._headers_buffer))
self._headers_buffer = []
#self.wfile.write(bytes(resp, 'utf-8'))
#self.wfile.flush()
print("<<< do_GET")
return
# End of do_GET
# Handler for the POST requests
......@@ -61,6 +75,7 @@ class myHandler(http.server.BaseHTTPRequestHandler):
self.send_response(400, 'Bad Request')
self.send_header('Host', self.headers.get('Host'))
self.send_header('Authorization', self.headers.get('Authorization'))
self.send_header('Connection', 'keep-alive')
self.send_header('Content-Type', 'application/problem+json')
l = len(resp)
self.send_header('Content-Length', str(l))
......@@ -72,8 +87,9 @@ class myHandler(http.server.BaseHTTPRequestHandler):
print('do_POST: resp= ', resp, ', Content_Type= ', content_type)
if (resp == None):
self.send_response(404, 'Not Found')
self.send_header('Host', self.headers.get('Host'))
self.send_header('Host', self.headers.get('Host')) # send_header() shall be after send_response()
self.send_header('Authorization', self.headers.get('Authorization'))
self.send_header('Connection', 'keep-alive')
self.send_header('Content-Type', content_type)
self.send_header('Content-Length', '0')
self.end_headers()
......@@ -88,15 +104,76 @@ class myHandler(http.server.BaseHTTPRequestHandler):
self.send_response(201, 'Created')
else:
self.send_response(200, 'OK')
self.send_header('Host', self.headers.get('Host'))
self.send_header('Content-Type', content_type)
self.send_header('Host', self.headers.get('Host')) # send_header() shall be after send_response()
self.send_header('Authorization', self.headers.get('Authorization'))
self.send_header('Connection', 'keep-alive')
self.send_header('Content-Type', content_type)
l = len(resp)
self.send_header('Content-Length', str(l))
#self.end_headers() # Unable to bufferize wfile, so calling end_headers() headers and body are sent in 2 different TCP packets
self._headers_buffer.append(b"\r\n")
self._headers_buffer.append(bytes(resp, 'utf-8'))
self.wfile.write(b"".join(self._headers_buffer))
self._headers_buffer = []
#self.wfile.write(bytes(resp, 'utf-8'))
#self.wfile.flush()
print("<<< do_POST")
return
# End of do_POST
# Handler for the DELETE requests
def do_DELETE(self):
print('>>> do_DELETE: ', self.path)
self.protocol_version = self.request_version
# Check HTTP end_headers
if self.__check_http_headers__() == False:
resp = "{\"problemDetails\": {\t\"type\": \"Bad Request\",\t\"title\": \"N/A\",\t\"status\": 400,\t\"detail\": \"Wrong headers\",\t\"instance\": \"N/A\"}}"
self.send_response(400, 'Bad Request')
self.send_header('Host', self.headers.get('Host'))
self.send_header('Authorization', self.headers.get('Authorization'))
self.send_header('Connection', 'keep-alive')
self.send_header('Content-Type', 'application/problem+json')
l = len(resp)
self.send_header('Content-Length', str(l))
self.end_headers()
self.wfile.write(bytes(resp, 'utf-8'))
# End of do_POST
return
resp, content_type = self.__process__()
print('do_DELETE: resp= ', resp, ', Content_Type= ', content_type)
if (resp == None):
self.send_response(404, 'Not Found')
self.send_header('Host', self.headers.get('Host')) # send_header() shall be after send_response()
self.send_header('Authorization', self.headers.get('Authorization'))
self.send_header('Connection', 'keep-alive')
self.send_header('Content-Type', content_type)
self.send_header('Content-Length', '0')
self.end_headers()
else:
if resp.find('Not Found') != -1:
self.send_response(404, 'Not Found')
elif resp.find('Forbidden') != -1:
self.send_response(403, 'Forbidden')
elif resp.find('Bad Request') != -1:
self.send_response(400, 'Bad Request')
else:
self.send_response(200, 'OK')
self.send_header('Host', self.headers.get('Host')) # send_header() shall be after send_response()
self.send_header('Authorization', self.headers.get('Authorization'))
self.send_header('Connection', 'keep-alive')
self.send_header('Content-Type', content_type)
l = len(resp)
self.send_header('Content-Length', str(l))
#self.end_headers()
self._headers_buffer.append(b"\r\n")
self._headers_buffer.append(bytes(resp, 'utf-8'))
self.wfile.write(b"".join(self._headers_buffer))
self._headers_buffer = []
#self.wfile.write(bytes(resp, 'utf-8'))
#self.wfile.flush()
print("<<< do_DELETE")
return
# End of do_DELETE
# Check HTTP headers
def __check_http_headers__(self):
......
......@@ -10,7 +10,7 @@ import msvcrt
# MEC simulator IPv4 address
#ADDRESS_BINDING = '172.28.4.87'
#ADDRESS_BINDING = '192.168.1.21'
ADDRESS_BINDING = '192.168.0.5'
ADDRESS_BINDING = '192.168.0.17'
#ADDRESS_BINDING = '127.0.0.1'
# MEC simulator IPv4 listening port
PORT_NUMBER = 8081
......@@ -25,16 +25,20 @@ if __name__ == '__main__':
exit = False
try:
# Create a web server and define the handler to manage the incoming request
server = socketserver.TCPServer((ADDRESS_BINDING, PORT_NUMBER), mec_http_server.myHandler)
print ('Started httpserver on port ' , PORT_NUMBER)
# server = socketserver.TCPServer((ADDRESS_BINDING, PORT_NUMBER), mec_http_server.myHandler)
server = http.server.HTTPServer((ADDRESS_BINDING, PORT_NUMBER), mec_http_server.myHandler)
server.wbufsize = -1
print ('Started httpserver on port ', PORT_NUMBER)
# Wait forever for incoming http requests in background
#server.serve_forever()
#server.serve_forever(poll_interval=2)
threading.Thread(target=server.serve_forever).start()
# Use keybord entries to trigBger notifications
while exit == False:
ch = getch()
if ch == b'\x03':
exit = True
elif ch == b'q':
exit = True
else:
print(">>> ch= ", ch)
server.shutdown()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment