-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathParser.py
277 lines (222 loc) · 7.66 KB
/
Parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import json
import gzip
class Parser(object):
@staticmethod
def parseJsonFile(fileName):
jsonFile = open(fileName, "r")
contents = jsonFile.read()
return json.loads(contents)
@staticmethod
def tokenizeByLine(message):
parsedData = []
line = ""
for character in message:
if (chr(character) == '\r'):
continue
if (chr(character) == '\n'):
parsedData.append(line)
line = ""
continue
line += chr(character)
return parsedData
@staticmethod
def getUrl(message):
REQUEST_LINE = 0
VALUE_INDEX = 1
try:
spaceIndex = message[REQUEST_LINE][VALUE_INDEX].find(" ")
return message[REQUEST_LINE][VALUE_INDEX][ : spaceIndex]
except:
return ""
@staticmethod
def getResponseLine(message):
line = ""
ID = "HTTP"
for character in message:
if (chr(character) == '\r'):
continue
if (chr(character) == '\n'):
break
line += chr(character)
if (line[ : len(ID)] == ID):
return line
else:
return None
@staticmethod
def getResponseHeader(message):
line = ""
ID = "HTTP"
isLastLine = False
for character in message:
if (chr(character) == '\r'):
continue
if (chr(character) == '\n'):
if (isLastLine):
break
line += "\n"
isLastLine = True
continue
isLastLine = False
line += chr(character)
if (line[ : len(ID)] == ID):
return line
else:
return None
@staticmethod
def getPragmaFlag(message):
line = ""
PRAGMA = "pragma: "
CACHE_CONTROL = "Cache-Control: "
for character in message:
if (chr(character) == '\r'):
continue
if (chr(character) == '\n'):
if (line[ : len(PRAGMA)] == PRAGMA):
return line[len(PRAGMA) : ]
if (line[ : len(CACHE_CONTROL)] == CACHE_CONTROL):
return line[len(CACHE_CONTROL) : ]
line = ""
continue
line += chr(character)
else:
return ""
@staticmethod
def parseHttpMessage(message):
DELIMITER = ":"
REQUEST_DELIMITER = " "
lines = Parser.tokenizeByLine(message)
parsedData = []
for index, line in enumerate(lines):
if (index == 0):
delmiterIndex = line.find(REQUEST_DELIMITER)
parsedData.append((line[ : delmiterIndex],\
line[delmiterIndex + 1 : ]))
continue
delmiterIndex = line.find(DELIMITER)
parsedData.append((line[ : delmiterIndex],\
line[delmiterIndex + 1 : ]))
return parsedData
@staticmethod
def getExpiryDate(message):
line = ""
EXPIRY = "Expires: "
EXPIRY_FORMAT = "Expires: XXX, "
GMT = " GMT"
for character in message:
if (chr(character) == '\r'):
continue
if (chr(character) == '\n'):
if (line[ : len(EXPIRY)] == EXPIRY):
return line[len(EXPIRY_FORMAT) : len(line) - len(GMT)]
line = ""
continue
line += chr(character)
else:
return ""
@staticmethod
def getRequestMessage(httpMessage):
message = ""
for i, x in enumerate(httpMessage):
if (i == 0):
message += x[0] + " " + x[1] + "\r\n"
else:
message += x[0] + ":" + x[1] + "\r\n"
message += "\r\n"
return message
@staticmethod
def getHostName(httpMessage):
HOSTNAME_LINE = 1
URL_INDEX = 1
SPACE_INDEX = 0
try:
return httpMessage[HOSTNAME_LINE][URL_INDEX][SPACE_INDEX + 1 : ]
except:
return None
@staticmethod
def getString(data):
string = ""
for character in data:
string += character
return string
@staticmethod
def changeTheContentLength(messageString, length):
line = ""
LENGTH_KEY = "Content-Length: "
if (LENGTH_KEY in messageString):
lengthIndex = messageString.find(LENGTH_KEY) + len(LENGTH_KEY)
newLineIndex = messageString[lengthIndex : ].find("\r\n")
messageString = messageString[ : lengthIndex] +\
str(int(messageString[lengthIndex : lengthIndex + newLineIndex]) + length) +\
messageString[lengthIndex + newLineIndex : ]
return messageString
return ""
@staticmethod
def getHeaderValue(message, header):
line = ""
DELIMITER = ": "
for character in message:
try:
if (chr(character) == '\r'):
continue
if (chr(character) == '\n'):
if (len(line) > len(header) and line[ : len(header)] == header):
return line[len(header) + len(DELIMITER) : ]
line = ""
continue
line += chr(character)
except:
pass
else:
return ""
@staticmethod
def getContent(completedData):
isLastLine = False
contentIndex = 0
for character in completedData:
contentIndex += 1
if (chr(character) == '\r'):
continue
if (chr(character) == '\n'):
if (isLastLine):
break
isLastLine = True
continue
isLastLine = False
if (len(completedData) > 0):
return completedData[contentIndex : ], contentIndex
return "", -1
@staticmethod
def getBody(data, injectedMessage):
BODY = "<body"
BR = "<br>"
encodingType = Parser.getHeaderValue(data, "Content-Encoding")
content, contentIndex = Parser.getContent(data)
header = data[ : contentIndex]
header = header.decode(errors = "ignore")
content = content.decode(errors = "ignore")
if (BODY in content):
header = Parser.changeTheContentLength(header,\
len(injectedMessage) + len(BR))
bodyIndex = content.index(BODY) + len(BODY)
bodyIndex += content[bodyIndex : ].index(">") + 1
content = content[ : bodyIndex] +\
injectedMessage + BR + content[bodyIndex : ]
content = bytes(content, 'utf-8')
if (encodingType == "gzip"):
content = gzip.compress(content)
header = bytes(header, 'utf-8')
return header + content
@staticmethod
def getUnzippedContent(content, encodingType):
if (encodingType == "gzip"):
return gzip.decompress(content)
return content
@staticmethod
def getUnzippedData(completedData):
encodingType = Parser.getHeaderValue(completedData, "Content-Encoding")
if (encodingType == ""):
return completedData
content, contentIndex = Parser.getContent(completedData)
newContent = Parser.getUnzippedContent(content, encodingType)
newData = completedData[ : contentIndex] + newContent
return newData