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 | import json |
---|
11 | import logging |
---|
12 | |
---|
13 | from io import StringIO |
---|
14 | |
---|
15 | class restRequest: |
---|
16 | def __init__(self): |
---|
17 | self.request_line = None |
---|
18 | self.headers_alone = None |
---|
19 | self.headers = None |
---|
20 | self.host = None |
---|
21 | self.content_type = None |
---|
22 | self.content_len = None |
---|
23 | self.operation = None |
---|
24 | self.URI = None |
---|
25 | self.run = None |
---|
26 | self.partition = None |
---|
27 | self.disk = None |
---|
28 | self.cache = None |
---|
29 | self.cache_size = None |
---|
30 | self.partition_setup = None |
---|
31 | self.name = None |
---|
32 | self.repo = None |
---|
33 | self.type = None |
---|
34 | self.profile = None |
---|
35 | self.id = None |
---|
36 | self.echo = None |
---|
37 | self.code = None |
---|
38 | |
---|
39 | def parser(self,data): |
---|
40 | self.request_line, self.headers_alone = data.split('\n', 1) |
---|
41 | self.headers = email.message_from_file(StringIO(self.headers_alone)) |
---|
42 | |
---|
43 | if 'Host' in self.headers.keys(): |
---|
44 | self.host = self.headers['Host'] |
---|
45 | |
---|
46 | if 'Content-Type' in self.headers.keys(): |
---|
47 | self.content_type = self.headers['Content-Type'] |
---|
48 | |
---|
49 | if 'Content-Length' in self.headers.keys(): |
---|
50 | self.content_len = int(self.headers['Content-Length']) |
---|
51 | |
---|
52 | if (not self.request_line == None or not self.request_line == ''): |
---|
53 | self.method = self.request_line.split('/', 1)[0] |
---|
54 | self.URI = self.request_line.split('/', 1)[1] |
---|
55 | |
---|
56 | if not self.content_len == 0: |
---|
57 | msgs = self.headers_alone.rstrip().split('\n') |
---|
58 | body = msgs[len(msgs) - 1] |
---|
59 | try: |
---|
60 | json_param = json.loads(body) |
---|
61 | except ValueError as e: |
---|
62 | logging.error("JSON message incomplete") |
---|
63 | return |
---|
64 | |
---|
65 | if "run" in json_param: |
---|
66 | self.run = json_param["run"] |
---|
67 | try: |
---|
68 | self.echo = json_param["echo"] |
---|
69 | except: |
---|
70 | pass |
---|
71 | |
---|
72 | if "disk" in json_param: |
---|
73 | self.disk = json_param["disk"] |
---|
74 | |
---|
75 | if "partition" in json_param: |
---|
76 | self.partition = json_param["partition"] |
---|
77 | |
---|
78 | if "cache" in json_param: |
---|
79 | self.cache = json_param["cache"] |
---|
80 | |
---|
81 | if "cache_size" in json_param: |
---|
82 | self.cache_size = json_param["cache_size"] |
---|
83 | |
---|
84 | if "partition_setup" in json_param: |
---|
85 | self.partition_setup = json_param["partition_setup"] |
---|
86 | |
---|
87 | if "name" in json_param: |
---|
88 | self.name = json_param["name"] |
---|
89 | |
---|
90 | if "repository" in json_param: |
---|
91 | self.repo = json_param["repository"] |
---|
92 | |
---|
93 | if "type" in json_param: |
---|
94 | self.type = json_param["type"] |
---|
95 | |
---|
96 | if "profile" in json_param: |
---|
97 | self.profile = json_param["profile"] |
---|
98 | |
---|
99 | if "id" in json_param: |
---|
100 | self.id = json_param["id"] |
---|
101 | |
---|
102 | if "code" in json_param: |
---|
103 | self.code = json_param["code"] |
---|
104 | |
---|
105 | def get_method(self): |
---|
106 | return self.method |
---|
107 | |
---|
108 | def get_uri(self): |
---|
109 | return self.URI |
---|
110 | |
---|
111 | def getrun(self): |
---|
112 | return self.run |
---|
113 | |
---|
114 | def getDisk(self): |
---|
115 | return self.disk |
---|
116 | |
---|
117 | def getPartition(self): |
---|
118 | return self.partition |
---|
119 | |
---|
120 | def getCache(self): |
---|
121 | return self.cache |
---|
122 | |
---|
123 | def getCacheSize(self): |
---|
124 | return self.cache_size |
---|
125 | |
---|
126 | def getPartitionSetup(self): |
---|
127 | return self.partition_setup |
---|
128 | |
---|
129 | def getName(self): |
---|
130 | return self.name |
---|
131 | |
---|
132 | def getRepo(self): |
---|
133 | return self.repo |
---|
134 | |
---|
135 | def getType(self): |
---|
136 | return self.type |
---|
137 | |
---|
138 | def getProfile(self): |
---|
139 | return self.profile |
---|
140 | |
---|
141 | def getId(self): |
---|
142 | return self.id |
---|
143 | |
---|
144 | def getEcho(self): |
---|
145 | return self.echo |
---|
146 | |
---|
147 | def getCode(self): |
---|
148 | return self.code |
---|