summaryrefslogtreecommitdiffstats
path: root/src/HTTPParser.py
diff options
context:
space:
mode:
authorAlvaro Neira Ayuso <aneira@soleta.eu>2019-12-12 14:47:41 +0100
committerAlvaro Neira Ayuso <alvaroneay@gmail.com>2020-01-19 19:50:44 +0100
commitbfdeae840c95fb9da504af795179be65c6286326 (patch)
treea2b9f9fdf09eef5a4975ee295636ae5383cc9893 /src/HTTPParser.py
parent076e15bb299dbcc1f8cc0d6f260c26f39d81f983 (diff)
Add HTTP parser support
The new OpenGnsys support to communicate server and client side will be HTTP. This new class allows us the support for parsing all the message received from the server in HTTP format.
Diffstat (limited to 'src/HTTPParser.py')
-rw-r--r--src/HTTPParser.py54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/HTTPParser.py b/src/HTTPParser.py
new file mode 100644
index 0000000..bbced43
--- /dev/null
+++ b/src/HTTPParser.py
@@ -0,0 +1,54 @@
+from mimetools import Message
+from StringIO import StringIO
+
+class HTTPParser:
+ def __init__(self):
+ self.requestLine = None
+ self.headersAlone = None
+ self.headers = None
+ self.host = None
+ self.contentType = None
+ self.contentLen = None
+ self.operation = None
+ self.URI = None
+
+ def parser(self,data):
+ self.requestLine, self.headersAlone = data.split('\n', 1)
+ self.headers = Message(StringIO(self.headersAlone))
+
+ if 'host' in self.headers.keys():
+ self.host = self.headers['host']
+
+ if 'content-type' in self.headers.keys():
+ self.contentType = self.headers['content-type']
+
+ if 'content-length' in self.headers.keys():
+ self.contentLen = int(self.headers['content-length'])
+
+ if (not self.requestLine == None or not self.requestLine == ''):
+ self.operation = self.requestLine.split('/', 1)[0]
+ self.URI = self.requestLine.split('/', 1)[1]
+
+ def getHeaderLine(self):
+ return self.headersAlone
+
+ def getRequestLine(self):
+ return self.requestLine
+
+ def getHeaderParsed(self):
+ return self.headers
+
+ def getHost(self):
+ return self.host
+
+ def getContentType(self):
+ return self.contentType
+
+ def getContentLen(self):
+ return self.contentLen
+
+ def getRequestOP(self):
+ return self.operation
+
+ def getURI(self):
+ return self.URI