1 | # |
---|
2 | # Copyright (C) 2020 Soleta Networks <info@soleta.eu> |
---|
3 | # |
---|
4 | # This program is free software: you can redistribute it and/or modify it under |
---|
5 | # the terms of the GNU Affero General Public License as published by the |
---|
6 | # Free Software Foundation, version 3. |
---|
7 | # |
---|
8 | |
---|
9 | import email |
---|
10 | from io import StringIO |
---|
11 | import json |
---|
12 | |
---|
13 | class restRequest: |
---|
14 | def __init__(self): |
---|
15 | self.requestLine = None |
---|
16 | self.headersAlone = None |
---|
17 | self.headers = None |
---|
18 | self.host = None |
---|
19 | self.contentType = None |
---|
20 | self.contentLen = None |
---|
21 | self.operation = None |
---|
22 | self.URI = None |
---|
23 | self.run = None |
---|
24 | self.partition = None |
---|
25 | self.disk = None |
---|
26 | self.cache = None |
---|
27 | self.cache_size = None |
---|
28 | self.partition_setup = None |
---|
29 | self.name = None |
---|
30 | self.repo = None |
---|
31 | self.type = None |
---|
32 | self.profile = None |
---|
33 | self.id = None |
---|
34 | self.echo = None |
---|
35 | self.code = None |
---|
36 | |
---|
37 | def parser(self,data): |
---|
38 | self.requestLine, self.headersAlone = data.split('\n', 1) |
---|
39 | self.headers = email.message_from_file(StringIO(self.headersAlone)) |
---|
40 | |
---|
41 | if 'Host' in self.headers.keys(): |
---|
42 | self.host = self.headers['Host'] |
---|
43 | |
---|
44 | if 'Content-Type' in self.headers.keys(): |
---|
45 | self.contentType = self.headers['Content-Type'] |
---|
46 | |
---|
47 | if 'Content-Length' in self.headers.keys(): |
---|
48 | self.contentLen = int(self.headers['Content-Length']) |
---|
49 | |
---|
50 | if (not self.requestLine == None or not self.requestLine == ''): |
---|
51 | self.operation = self.requestLine.split('/', 1)[0] |
---|
52 | self.URI = self.requestLine.split('/', 1)[1] |
---|
53 | |
---|
54 | if not self.contentLen == 0: |
---|
55 | msgs = self.headersAlone.rstrip().split('\n') |
---|
56 | body = msgs[len(msgs) - 1] |
---|
57 | try: |
---|
58 | json_param = json.loads(body) |
---|
59 | except ValueError as e: |
---|
60 | print ("Error: Json message incomplete") |
---|
61 | return |
---|
62 | |
---|
63 | if "run" in body: |
---|
64 | self.run = json_param["run"] |
---|
65 | try: |
---|
66 | self.echo = json_param["echo"] |
---|
67 | except: |
---|
68 | pass |
---|
69 | |
---|
70 | if "disk" in body: |
---|
71 | self.disk = json_param["disk"] |
---|
72 | |
---|
73 | if "partition" in body: |
---|
74 | if not "partition_setup" in body: |
---|
75 | self.partition = json_param["partition"] |
---|
76 | |
---|
77 | if "cache" in body: |
---|
78 | self.cache = json_param["cache"] |
---|
79 | |
---|
80 | if "cache_size" in body: |
---|
81 | self.cache_size = json_param["cache_size"] |
---|
82 | |
---|
83 | if "partition_setup" in body: |
---|
84 | self.partition_setup = json_param["partition_setup"] |
---|
85 | |
---|
86 | if "name" in body: |
---|
87 | self.name = json_param["name"] |
---|
88 | |
---|
89 | if "repository" in body: |
---|
90 | self.repo = json_param["repository"] |
---|
91 | |
---|
92 | if "type" in body: |
---|
93 | self.type = json_param["type"] |
---|
94 | |
---|
95 | if "profile" in body: |
---|
96 | self.profile = json_param["profile"] |
---|
97 | |
---|
98 | if "id" in body: |
---|
99 | self.id = json_param["id"] |
---|
100 | |
---|
101 | if "code" in body: |
---|
102 | self.code = json_param["code"] |
---|
103 | |
---|
104 | def getHeaderLine(self): |
---|
105 | return self.headersAlone |
---|
106 | |
---|
107 | def getRequestLine(self): |
---|
108 | return self.requestLine |
---|
109 | |
---|
110 | def getHeaderParsed(self): |
---|
111 | return self.headers |
---|
112 | |
---|
113 | def getHost(self): |
---|
114 | return self.host |
---|
115 | |
---|
116 | def getContentType(self): |
---|
117 | return self.contentType |
---|
118 | |
---|
119 | def getContentLen(self): |
---|
120 | return self.contentLen |
---|
121 | |
---|
122 | def getRequestOP(self): |
---|
123 | return self.operation |
---|
124 | |
---|
125 | def getURI(self): |
---|
126 | return self.URI |
---|
127 | |
---|
128 | def getrun(self): |
---|
129 | return self.run |
---|
130 | |
---|
131 | def getDisk(self): |
---|
132 | return self.disk |
---|
133 | |
---|
134 | def getPartition(self): |
---|
135 | return self.partition |
---|
136 | |
---|
137 | def getCache(self): |
---|
138 | return self.cache |
---|
139 | |
---|
140 | def getCacheSize(self): |
---|
141 | return self.cache_size |
---|
142 | |
---|
143 | def getPartitionSetup(self): |
---|
144 | return self.partition_setup |
---|
145 | |
---|
146 | def getName(self): |
---|
147 | return self.name |
---|
148 | |
---|
149 | def getRepo(self): |
---|
150 | return self.repo |
---|
151 | |
---|
152 | def getType(self): |
---|
153 | return self.type |
---|
154 | |
---|
155 | def getProfile(self): |
---|
156 | return self.profile |
---|
157 | |
---|
158 | def getId(self): |
---|
159 | return self.id |
---|
160 | |
---|
161 | def getEcho(self): |
---|
162 | return self.echo |
---|
163 | |
---|
164 | def getCode(self): |
---|
165 | return self.code |
---|