1 | # |
---|
2 | # Copyright (C) 2020-2021 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; either version 3 of the License, or |
---|
7 | # (at your option) any later version. |
---|
8 | |
---|
9 | import email |
---|
10 | from io import StringIO |
---|
11 | import json |
---|
12 | |
---|
13 | class restRequest: |
---|
14 | def __init__(self): |
---|
15 | self.request_line = None |
---|
16 | self.headers_alone = None |
---|
17 | self.headers = None |
---|
18 | self.host = None |
---|
19 | self.content_type = None |
---|
20 | self.content_len = 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.request_line, self.headers_alone = data.split('\n', 1) |
---|
39 | self.headers = email.message_from_file(StringIO(self.headers_alone)) |
---|
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.content_type = self.headers['Content-Type'] |
---|
46 | |
---|
47 | if 'Content-Length' in self.headers.keys(): |
---|
48 | self.content_len = int(self.headers['Content-Length']) |
---|
49 | |
---|
50 | if (not self.request_line == None or not self.request_line == ''): |
---|
51 | self.method = self.request_line.split('/', 1)[0] |
---|
52 | self.URI = self.request_line.split('/', 1)[1] |
---|
53 | |
---|
54 | if not self.content_len == 0: |
---|
55 | msgs = self.headers_alone.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 json_param: |
---|
64 | self.run = json_param["run"] |
---|
65 | try: |
---|
66 | self.echo = json_param["echo"] |
---|
67 | except: |
---|
68 | pass |
---|
69 | |
---|
70 | if "disk" in json_param: |
---|
71 | self.disk = json_param["disk"] |
---|
72 | |
---|
73 | if "partition" in json_param: |
---|
74 | self.partition = json_param["partition"] |
---|
75 | |
---|
76 | if "cache" in json_param: |
---|
77 | self.cache = json_param["cache"] |
---|
78 | |
---|
79 | if "cache_size" in json_param: |
---|
80 | self.cache_size = json_param["cache_size"] |
---|
81 | |
---|
82 | if "partition_setup" in json_param: |
---|
83 | self.partition_setup = json_param["partition_setup"] |
---|
84 | |
---|
85 | if "name" in json_param: |
---|
86 | self.name = json_param["name"] |
---|
87 | |
---|
88 | if "repository" in json_param: |
---|
89 | self.repo = json_param["repository"] |
---|
90 | |
---|
91 | if "type" in json_param: |
---|
92 | self.type = json_param["type"] |
---|
93 | |
---|
94 | if "profile" in json_param: |
---|
95 | self.profile = json_param["profile"] |
---|
96 | |
---|
97 | if "id" in json_param: |
---|
98 | self.id = json_param["id"] |
---|
99 | |
---|
100 | if "code" in json_param: |
---|
101 | self.code = json_param["code"] |
---|
102 | |
---|
103 | def get_method(self): |
---|
104 | return self.method |
---|
105 | |
---|
106 | def get_uri(self): |
---|
107 | return self.URI |
---|
108 | |
---|
109 | def getrun(self): |
---|
110 | return self.run |
---|
111 | |
---|
112 | def getDisk(self): |
---|
113 | return self.disk |
---|
114 | |
---|
115 | def getPartition(self): |
---|
116 | return self.partition |
---|
117 | |
---|
118 | def getCache(self): |
---|
119 | return self.cache |
---|
120 | |
---|
121 | def getCacheSize(self): |
---|
122 | return self.cache_size |
---|
123 | |
---|
124 | def getPartitionSetup(self): |
---|
125 | return self.partition_setup |
---|
126 | |
---|
127 | def getName(self): |
---|
128 | return self.name |
---|
129 | |
---|
130 | def getRepo(self): |
---|
131 | return self.repo |
---|
132 | |
---|
133 | def getType(self): |
---|
134 | return self.type |
---|
135 | |
---|
136 | def getProfile(self): |
---|
137 | return self.profile |
---|
138 | |
---|
139 | def getId(self): |
---|
140 | return self.id |
---|
141 | |
---|
142 | def getEcho(self): |
---|
143 | return self.echo |
---|
144 | |
---|
145 | def getCode(self): |
---|
146 | return self.code |
---|