source: ogClient-Git/src/HTTPParser.py @ fdd4ba5

Last change on this file since fdd4ba5 was 05b1088, checked in by Alvaro Neira Ayuso <alvaroneay@…>, 5 years ago

Include License header

  • Property mode set to 100644
File size: 3.2 KB
Line 
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
9import email
10from io import StringIO
11import json
12
13class HTTPParser:
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.cmd = 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
36        def parser(self,data):
37                self.requestLine, self.headersAlone = data.split('\n', 1)
38                self.headers = email.message_from_file(StringIO(self.headersAlone))
39
40                if 'Host' in self.headers.keys():
41                        self.host = self.headers['Host']
42
43                if 'Content-Type' in self.headers.keys():
44                        self.contentType = self.headers['Content-Type']
45
46                if 'Content-Length' in self.headers.keys():
47                        self.contentLen = int(self.headers['Content-Length'])
48
49                if (not self.requestLine == None or not self.requestLine == ''):
50                        self.operation = self.requestLine.split('/', 1)[0]
51                        self.URI = self.requestLine.split('/', 1)[1]
52
53                if not self.contentLen == 0:
54                        msgs = self.headersAlone.rstrip().split('\n')
55                        cmd = msgs[len(msgs) - 1]
56                        try:
57                                jsoncmd = json.loads(cmd)
58                        except ValueError as e:
59                                print ("Error: Json message incomplete")
60                                return
61
62                        if "run" in cmd:
63                                self.cmd = jsoncmd["run"]
64                                try:
65                                        self.echo = jsoncmd["echo"]
66                                except:
67                                        pass
68
69                        if "disk" in cmd:
70                                self.disk = jsoncmd["disk"]
71
72                        if "partition" in cmd:
73                                if not "partition_setup" in cmd:
74                                        self.partition = jsoncmd["partition"]
75
76                        if "cache" in cmd:
77                                self.cache = jsoncmd["cache"]
78
79                        if "cache_size" in cmd:
80                                self.cache_size = jsoncmd["cache_size"]
81
82                        if "partition_setup" in cmd:
83                                self.partition_setup = jsoncmd["partition_setup"]
84
85                        if "name" in cmd:
86                                self.name = jsoncmd["name"]
87
88                        if "repository" in cmd:
89                                self.repo = jsoncmd["repository"]
90
91                        if "type" in cmd:
92                                self.type = jsoncmd["type"]
93
94                        if "profile" in cmd:
95                                self.profile = jsoncmd["profile"]
96
97                        if "id" in cmd:
98                                self.id = jsoncmd["id"]
99
100        def getHeaderLine(self):
101                return self.headersAlone
102
103        def getRequestLine(self):
104                return self.requestLine
105
106        def getHeaderParsed(self):
107                return self.headers
108
109        def getHost(self):
110                return self.host
111
112        def getContentType(self):
113                return self.contentType
114
115        def getContentLen(self):
116                return self.contentLen
117
118        def getRequestOP(self):
119                return self.operation
120
121        def getURI(self):
122                return self.URI
123
124        def getCMD(self):
125                return self.cmd
126
127        def getDisk(self):
128                return self.disk
129
130        def getPartition(self):
131                return self.partition
132
133        def getCache(self):
134                return self.cache
135
136        def getCacheSize(self):
137                return self.cache_size
138
139        def getPartitionSetup(self):
140                return self.partition_setup
141
142        def getName(self):
143                return self.name
144
145        def getRepo(self):
146                return self.repo
147
148        def getType(self):
149                return self.type
150
151        def getProfile(self):
152                return self.profile
153
154        def getId(self):
155                return self.id
156
157        def getEcho(self):
158                return self.echo
Note: See TracBrowser for help on using the repository browser.