source: ogClient-Git/src/HTTPParser.py @ 44a4662

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

(Clean-Up) Rename cmd variable to body

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