summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJose M. Guisado <jguisado@soleta.eu>2023-06-13 10:51:42 +0200
committerJose M. Guisado <jguisado@soleta.eu>2023-06-14 14:50:55 +0200
commit0c03d82ca8792e5e0c41460f3aa5cdf6990a3412 (patch)
tree10e7e6d2433a46c945b6ffa3e7e574264b3295cb /src
parent926a73cf33896ae62f8255a8f2aca2e0d9a54038 (diff)
ogclient: add support for X-Sequence headerv1.3.0
Enable parsing of "X-Sequence" HTTP headers from incoming requests. Add "seq" field in restRequest class. Enable adding "X-Sequence" to outgoing responses. Add "seq" field inside restResponse class. Store current client sequence number inside ogClient class. Ideally, the restRequest object should be used to retrieve the sequence number but not all processing functions inside ogRest.py receive the request as parameter (eg: process_refresh). In the other hand, all processing functions receive the ogClient object.
Diffstat (limited to 'src')
-rw-r--r--src/ogClient.py3
-rw-r--r--src/ogRest.py49
-rw-r--r--src/restRequest.py5
3 files changed, 35 insertions, 22 deletions
diff --git a/src/ogClient.py b/src/ogClient.py
index 04b6d01..31ee5d3 100644
--- a/src/ogClient.py
+++ b/src/ogClient.py
@@ -47,6 +47,7 @@ class ogClient:
self.ip = self.CONFIG['opengnsys']['ip']
self.port = self.CONFIG['opengnsys']['port']
self.ogrest = ogRest(self.CONFIG)
+ self.seq = None
def get_socket(self):
return self.sock
@@ -144,6 +145,8 @@ class ogClient:
if 'Content-Length' in headers.keys():
self.content_len = int(headers['Content-Length'])
+ if 'X-Sequence' in headers.keys():
+ self.seq = int(headers['X-Sequence'])
self.trailer = True
# Add 4 because self.data.find("\r\n\r\n") does not count
diff --git a/src/ogRest.py b/src/ogRest.py
index 77a5d5d..5a89d27 100644
--- a/src/ogRest.py
+++ b/src/ogRest.py
@@ -39,7 +39,7 @@ class jsonBody():
return json.dumps(self.jsontree)
class restResponse():
- def __init__(self, response, json_body=None):
+ def __init__(self, response, json_body=None, seq=None):
self.msg = ''
if response == ogResponses.BAD_REQUEST:
self.msg = 'HTTP/1.0 400 Bad Request'
@@ -65,6 +65,11 @@ class restResponse():
self.msg += '\r\n'
+ if seq:
+ self.seq = seq
+ self.msg += 'X-Sequence: ' + str(seq)
+ self.msg += '\r\n'
+
if json_body:
self.msg += 'Content-Length: ' + str(len(json_body.dump()))
self.msg += '\r\nContent-Type: application/json'
@@ -80,7 +85,7 @@ class restResponse():
class ogThread():
def shellrun(client, request, ogRest):
if not request.getrun():
- response = restResponse(ogResponses.BAD_REQUEST)
+ response = restResponse(ogResponses.BAD_REQUEST, seq=client.seq)
client.send(response.get())
ogRest.state = ThreadState.IDLE
return
@@ -94,10 +99,10 @@ class ogThread():
if request.getEcho():
json_body = jsonBody()
json_body.add_element('out', shellout)
- response = restResponse(ogResponses.OK, json_body)
+ response = restResponse(ogResponses.OK, json_body, seq=client.seq)
client.send(response.get())
else:
- response = restResponse(ogResponses.OK)
+ response = restResponse(ogResponses.OK, seq=client.seq)
client.send(response.get())
ogRest.state = ThreadState.IDLE
@@ -116,7 +121,7 @@ class ogThread():
ogRest.send_internal_server_error(client, exc=e)
return
- response = restResponse(ogResponses.OK)
+ response = restResponse(ogResponses.OK, seq=client.seq)
client.send(response.get())
client.disconnect()
@@ -131,7 +136,7 @@ class ogThread():
json_body.add_element('partition', request.getPartition())
json_body.add_element('software', software)
- response = restResponse(ogResponses.OK, json_body)
+ response = restResponse(ogResponses.OK, json_body, seq=client.seq)
client.send(response.get())
ogRest.state = ThreadState.IDLE
@@ -145,7 +150,7 @@ class ogThread():
json_body = jsonBody()
json_body.add_element('hardware', result)
- response = restResponse(ogResponses.OK, json_body)
+ response = restResponse(ogResponses.OK, json_body, seq=client.seq)
client.send(response.get())
ogRest.state = ThreadState.IDLE
@@ -158,7 +163,7 @@ class ogThread():
json_body = jsonBody(out)
- response = restResponse(ogResponses.OK, json_body)
+ response = restResponse(ogResponses.OK, json_body, seq=client.seq)
client.send(response.get())
ogRest.state = ThreadState.IDLE
@@ -174,7 +179,7 @@ class ogThread():
json_body.add_element('partition', request.getPartition())
json_body.add_element('image_id', request.getId())
- response = restResponse(ogResponses.OK, json_body)
+ response = restResponse(ogResponses.OK, json_body, seq=client.seq)
client.send(response.get())
ogRest.state = ThreadState.IDLE
@@ -202,7 +207,7 @@ class ogThread():
json_body.add_element('filesystem', image_info.filesystem)
json_body.add_element('datasize', datasize)
- response = restResponse(ogResponses.OK, json_body)
+ response = restResponse(ogResponses.OK, json_body, seq=client.seq)
client.send(response.get())
ogRest.state = ThreadState.IDLE
@@ -215,7 +220,7 @@ class ogThread():
json_body = jsonBody(out)
- response = restResponse(ogResponses.OK, json_body)
+ response = restResponse(ogResponses.OK, json_body, seq=client.seq)
client.send(response.get())
ogRest.state = ThreadState.IDLE
@@ -260,7 +265,7 @@ class ogRest():
def send_internal_server_error(self, client, exc=None):
if exc:
logging.exception('Unexpected error')
- response = restResponse(ogResponses.INTERNAL_ERR)
+ response = restResponse(ogResponses.INTERNAL_ERR, seq=client.seq)
client.send(response.get())
self.state = ThreadState.IDLE
@@ -277,7 +282,7 @@ class ogRest():
if self.state == ThreadState.BUSY:
logging.warn('Request has been received '
'while ogClient is busy')
- response = restResponse(ogResponses.SERVICE_UNAVAILABLE)
+ response = restResponse(ogResponses.SERVICE_UNAVAILABLE, seq=client.seq)
client.send(response.get())
return
else:
@@ -295,7 +300,7 @@ class ogRest():
else:
logging.warn('Unsupported request: %s',
{URI[:ogRest.LOG_LENGTH]})
- response = restResponse(ogResponses.BAD_REQUEST)
+ response = restResponse(ogResponses.BAD_REQUEST, seq=client.seq)
client.send(response.get())
self.state = ThreadState.IDLE
elif ("POST" in method):
@@ -320,11 +325,11 @@ class ogRest():
else:
logging.warn('Unsupported request: %s',
URI[:ogRest.LOG_LENGTH])
- response = restResponse(ogResponses.BAD_REQUEST)
+ response = restResponse(ogResponses.BAD_REQUEST, seq=client.seq)
client.send(response.get())
self.state = ThreadState.IDLE
else:
- response = restResponse(ogResponses.BAD_REQUEST)
+ response = restResponse(ogResponses.BAD_REQUEST, seq=client.seq)
client.send(response.get())
self.state = ThreadState.IDLE
@@ -345,7 +350,7 @@ class ogRest():
self.state = ThreadState.IDLE
def process_reboot(self, client):
- response = restResponse(ogResponses.IN_PROGRESS)
+ response = restResponse(ogResponses.IN_PROGRESS, seq=client.seq)
client.send(response.get())
if self.mode != 'virtual':
@@ -356,7 +361,7 @@ class ogRest():
threading.Thread(target=ogThread.reboot, args=(self,)).start()
def process_poweroff(self, client):
- response = restResponse(ogResponses.IN_PROGRESS)
+ response = restResponse(ogResponses.IN_PROGRESS, seq=client.seq)
client.send(response.get())
if self.mode != 'virtual':
@@ -370,7 +375,7 @@ class ogRest():
try:
status = self.operations.probe(self)
except:
- response = restResponse(ogResponses.INTERNAL_ERR)
+ response = restResponse(ogResponses.INTERNAL_ERR, seq=client.seq)
client.send(response.get())
return
@@ -379,9 +384,9 @@ class ogRest():
json_body.add_element(k, v)
if self.state != ThreadState.BUSY:
- response = restResponse(ogResponses.OK, json_body)
+ response = restResponse(ogResponses.OK, json_body, seq=client.seq)
else:
- response = restResponse(ogResponses.IN_PROGRESS, json_body)
+ response = restResponse(ogResponses.IN_PROGRESS, json_body, seq=client.seq)
client.send(response.get())
@@ -398,7 +403,7 @@ class ogRest():
threading.Thread(target=ogThread.hardware, args=(client, self,)).start()
def process_schedule(self, client):
- response = restResponse(ogResponses.OK)
+ response = restResponse(ogResponses.OK, seq=client.seq)
client.send(response.get())
self.state = ThreadState.IDLE
diff --git a/src/restRequest.py b/src/restRequest.py
index 61d64c6..b14ad20 100644
--- a/src/restRequest.py
+++ b/src/restRequest.py
@@ -35,6 +35,7 @@ class restRequest:
self.id = None
self.echo = None
self.code = None
+ self.seq = None
def parser(self,data):
self.request_line, self.headers_alone = data.split('\n', 1)
@@ -49,6 +50,10 @@ class restRequest:
if 'Content-Length' in self.headers.keys():
self.content_len = int(self.headers['Content-Length'])
+ if 'X-Sequence' in self.headers.keys():
+ self.seq = int(self.headers['X-Sequence'])
+ logging.debug(f'Request with sequence number {self.seq}')
+
if (not self.request_line == None or not self.request_line == ''):
self.method = self.request_line.split('/', 1)[0]
self.URI = self.request_line.split('/', 1)[1]