[7548870] | 1 | import email |
---|
| 2 | from io import StringIO |
---|
[e20daf6] | 3 | import json |
---|
[bfdeae8] | 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 |
---|
[e20daf6] | 15 | self.cmd = None |
---|
[2fa8aa4] | 16 | self.partition = None |
---|
| 17 | self.disk = None |
---|
[efbe8a7] | 18 | self.cache = None |
---|
| 19 | self.cache_size = None |
---|
| 20 | self.partition_setup = None |
---|
[cc11d8f] | 21 | self.name = None |
---|
| 22 | self.repo = None |
---|
| 23 | self.type = None |
---|
| 24 | self.profile = None |
---|
| 25 | self.id = None |
---|
[a9d81ad] | 26 | self.echo = None |
---|
[bfdeae8] | 27 | |
---|
| 28 | def parser(self,data): |
---|
| 29 | self.requestLine, self.headersAlone = data.split('\n', 1) |
---|
[7548870] | 30 | self.headers = email.message_from_file(StringIO(self.headersAlone)) |
---|
[bfdeae8] | 31 | |
---|
[e20daf6] | 32 | if 'Host' in self.headers.keys(): |
---|
| 33 | self.host = self.headers['Host'] |
---|
[bfdeae8] | 34 | |
---|
[e20daf6] | 35 | if 'Content-Type' in self.headers.keys(): |
---|
| 36 | self.contentType = self.headers['Content-Type'] |
---|
[bfdeae8] | 37 | |
---|
[e20daf6] | 38 | if 'Content-Length' in self.headers.keys(): |
---|
| 39 | self.contentLen = int(self.headers['Content-Length']) |
---|
[bfdeae8] | 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 | |
---|
[e20daf6] | 45 | if not self.contentLen == 0: |
---|
| 46 | msgs = self.headersAlone.rstrip().split('\n') |
---|
| 47 | cmd = msgs[len(msgs) - 1] |
---|
[db7cc0d] | 48 | try: |
---|
| 49 | jsoncmd = json.loads(cmd) |
---|
| 50 | except ValueError as e: |
---|
| 51 | print ("Error: Json message incomplete") |
---|
| 52 | return |
---|
| 53 | |
---|
[e20daf6] | 54 | if "run" in cmd: |
---|
| 55 | self.cmd = jsoncmd["run"] |
---|
[a9d81ad] | 56 | try: |
---|
| 57 | self.echo = jsoncmd["echo"] |
---|
| 58 | except: |
---|
| 59 | pass |
---|
[e20daf6] | 60 | |
---|
[2fa8aa4] | 61 | if "disk" in cmd: |
---|
| 62 | self.disk = jsoncmd["disk"] |
---|
| 63 | |
---|
| 64 | if "partition" in cmd: |
---|
[efbe8a7] | 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"] |
---|
[2fa8aa4] | 76 | |
---|
[cc11d8f] | 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 | |
---|
[bfdeae8] | 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 |
---|
[e20daf6] | 115 | |
---|
| 116 | def getCMD(self): |
---|
| 117 | return self.cmd |
---|
[2fa8aa4] | 118 | |
---|
| 119 | def getDisk(self): |
---|
| 120 | return self.disk |
---|
| 121 | |
---|
| 122 | def getPartition(self): |
---|
| 123 | return self.partition |
---|
[efbe8a7] | 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 |
---|
[cc11d8f] | 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 |
---|
[a9d81ad] | 148 | |
---|
| 149 | def getEcho(self): |
---|
| 150 | return self.echo |
---|