1 | import email |
---|
2 | from io import StringIO |
---|
3 | import json |
---|
4 | |
---|
5 | class HTTPParser: |
---|
6 | def __init__(self): |
---|
7 | self.requestLine = None |
---|
8 | self.headersAlone = None |
---|
9 | self.headers = None |
---|
10 | self.host = None |
---|
11 | self.contentType = None |
---|
12 | self.contentLen = None |
---|
13 | self.operation = None |
---|
14 | self.URI = None |
---|
15 | self.cmd = None |
---|
16 | self.partition = None |
---|
17 | self.disk = None |
---|
18 | self.cache = None |
---|
19 | self.cache_size = None |
---|
20 | self.partition_setup = None |
---|
21 | self.name = None |
---|
22 | self.repo = None |
---|
23 | self.type = None |
---|
24 | self.profile = None |
---|
25 | self.id = None |
---|
26 | |
---|
27 | def parser(self,data): |
---|
28 | self.requestLine, self.headersAlone = data.split('\n', 1) |
---|
29 | self.headers = email.message_from_file(StringIO(self.headersAlone)) |
---|
30 | |
---|
31 | if 'Host' in self.headers.keys(): |
---|
32 | self.host = self.headers['Host'] |
---|
33 | |
---|
34 | if 'Content-Type' in self.headers.keys(): |
---|
35 | self.contentType = self.headers['Content-Type'] |
---|
36 | |
---|
37 | if 'Content-Length' in self.headers.keys(): |
---|
38 | self.contentLen = int(self.headers['Content-Length']) |
---|
39 | |
---|
40 | if (not self.requestLine == None or not self.requestLine == ''): |
---|
41 | self.operation = self.requestLine.split('/', 1)[0] |
---|
42 | self.URI = self.requestLine.split('/', 1)[1] |
---|
43 | |
---|
44 | if not self.contentLen == 0: |
---|
45 | msgs = self.headersAlone.rstrip().split('\n') |
---|
46 | cmd = msgs[len(msgs) - 1] |
---|
47 | try: |
---|
48 | jsoncmd = json.loads(cmd) |
---|
49 | except ValueError as e: |
---|
50 | print ("Error: Json message incomplete") |
---|
51 | return |
---|
52 | |
---|
53 | if "run" in cmd: |
---|
54 | self.cmd = jsoncmd["run"] |
---|
55 | |
---|
56 | if "disk" in cmd: |
---|
57 | self.disk = jsoncmd["disk"] |
---|
58 | |
---|
59 | if "partition" in cmd: |
---|
60 | if not "partition_setup" in cmd: |
---|
61 | self.partition = jsoncmd["partition"] |
---|
62 | |
---|
63 | if "cache" in cmd: |
---|
64 | self.cache = jsoncmd["cache"] |
---|
65 | |
---|
66 | if "cache_size" in cmd: |
---|
67 | self.cache_size = jsoncmd["cache_size"] |
---|
68 | |
---|
69 | if "partition_setup" in cmd: |
---|
70 | self.partition_setup = jsoncmd["partition_setup"] |
---|
71 | |
---|
72 | if "name" in cmd: |
---|
73 | self.name = jsoncmd["name"] |
---|
74 | |
---|
75 | if "repository" in cmd: |
---|
76 | self.repo = jsoncmd["repository"] |
---|
77 | |
---|
78 | if "type" in cmd: |
---|
79 | self.type = jsoncmd["type"] |
---|
80 | |
---|
81 | if "profile" in cmd: |
---|
82 | self.profile = jsoncmd["profile"] |
---|
83 | |
---|
84 | if "id" in cmd: |
---|
85 | self.id = jsoncmd["id"] |
---|
86 | |
---|
87 | def getHeaderLine(self): |
---|
88 | return self.headersAlone |
---|
89 | |
---|
90 | def getRequestLine(self): |
---|
91 | return self.requestLine |
---|
92 | |
---|
93 | def getHeaderParsed(self): |
---|
94 | return self.headers |
---|
95 | |
---|
96 | def getHost(self): |
---|
97 | return self.host |
---|
98 | |
---|
99 | def getContentType(self): |
---|
100 | return self.contentType |
---|
101 | |
---|
102 | def getContentLen(self): |
---|
103 | return self.contentLen |
---|
104 | |
---|
105 | def getRequestOP(self): |
---|
106 | return self.operation |
---|
107 | |
---|
108 | def getURI(self): |
---|
109 | return self.URI |
---|
110 | |
---|
111 | def getCMD(self): |
---|
112 | return self.cmd |
---|
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 |
---|