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

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

Include echo option for returning shell output

This patch adds a new echo option in /shell/run command. In case that the option
is set up to true, the server will receive in the response a json with the shell
output. Otherwise, the server will receive a response message without json
body.

A side effect of this change is that the command /shell/output/ disapears.

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