summaryrefslogtreecommitdiffstats
path: root/src/restRequest.py
blob: 493b4db54ca403ebf5143d2b1495316734665293 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#
# Copyright (C) 2020-2024 Soleta Networks <info@soleta.eu>
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the
# Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.

import email
import json
import logging

from io import StringIO

class restRequest:
	def __init__(self):
		self.request_line = None
		self.headers_alone = None
		self.headers = None
		self.host = None
		self.content_type = None
		self.content_len = None
		self.operation = None
		self.URI = None
		self.run = None
		self.partition = None
		self.disk = None
		self.cache = None
		self.cache_size = None
		self.partition_setup = None
		self.name = None
		self.repo = None
		self.type = None
		self.profile = None
		self.id = None
		self.inline = None
		self.code = None
		self.seq = None
		self.backup = None
		self.images = None

	def parser(self,data):
		self.request_line, self.headers_alone = data.split('\n', 1)
		self.headers = email.message_from_file(StringIO(self.headers_alone))

		if 'Host' in self.headers.keys():
			self.host = self.headers['Host']

		if 'Content-Type' in self.headers.keys():
			self.content_type = self.headers['Content-Type']

		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]

		if not self.content_len == 0:
			msgs = self.headers_alone.rstrip().split('\n')
			body = msgs[len(msgs) - 1]
			try:
				json_param = json.loads(body)
			except ValueError as e:
				logging.error("JSON message incomplete")
				return

			if "run" in json_param:
				self.run = json_param["run"]
				try:
					self.inline = json_param["inline"]
				except:
					pass

			if "images" in json_param:
				self.images = json_param["images"]

			if "image" in json_param:
				self.images = json_param["image"]

			if "disk" in json_param:
				self.disk = json_param["disk"]

			if "partition" in json_param:
				self.partition = json_param["partition"]

			if "cache" in json_param:
				self.cache = json_param["cache"]

			if "cache_size" in json_param:
				self.cache_size = json_param["cache_size"]

			if "partition_setup" in json_param:
				self.partition_setup = json_param["partition_setup"]

			if "name" in json_param:
				self.name = json_param["name"]

			if "repository" in json_param:
				self.repo = json_param["repository"]

			if "type" in json_param:
				self.type = json_param["type"]

			if "profile" in json_param:
				self.profile = json_param["profile"]

			if "id" in json_param:
				self.id = json_param["id"]

			if "code" in json_param:
				self.code = json_param["code"]

			if "backup" in json_param:
				self.backup = json_param["backup"]

	def get_method(self):
		return self.method

	def getImages(self):
		return self.images

	def get_uri(self):
		return self.URI

	def getrun(self):
		return self.run

	def getDisk(self):
		return self.disk

	def getPartition(self):
		return self.partition

	def getCache(self):
		return self.cache

	def getCacheSize(self):
		return self.cache_size

	def getPartitionSetup(self):
		return self.partition_setup

	def getName(self):
		return self.name

	def getRepo(self):
		return self.repo

	def getType(self):
		return self.type

	def getProfile(self):
		return self.profile

	def getId(self):
		return self.id

	def get_inline(self):
		return self.inline

	def getCode(self):
		return self.code

	def getBackup(self):
		return self.backup