diff options
author | Jose M. Guisado <jguisado@soleta.eu> | 2023-06-13 10:51:42 +0200 |
---|---|---|
committer | Jose M. Guisado <jguisado@soleta.eu> | 2023-06-14 14:50:55 +0200 |
commit | 0c03d82ca8792e5e0c41460f3aa5cdf6990a3412 (patch) | |
tree | 10e7e6d2433a46c945b6ffa3e7e574264b3295cb /src/ogRest.py | |
parent | 926a73cf33896ae62f8255a8f2aca2e0d9a54038 (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/ogRest.py')
-rw-r--r-- | src/ogRest.py | 49 |
1 files changed, 27 insertions, 22 deletions
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 |