-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMessageModifier.py
81 lines (67 loc) · 2.76 KB
/
MessageModifier.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
from Parser import Parser
DAYNAMES = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
class MessageModifier(object):
def __init__(self, userAgent, enable):
self.userAgent = userAgent
self.enable = enable
def getLineIndex(self, httpMessage, field):
FIELD_PART = 0
index = [i for i, x in enumerate(httpMessage) if(x[FIELD_PART] == field)]
if (len(index) == 0):
return -1
return index[0]
def changeUrl(self, httpMessage):
REQUEST_LINE = 0
HOST_NAME_LINE = 1
FIRST_PART = 0
URL_PART = 1
HTTP_PART = "http:/"
try:
spaceIndex = httpMessage[REQUEST_LINE][URL_PART].find(" ")
httpMessage[REQUEST_LINE] = (httpMessage[REQUEST_LINE][FIRST_PART],\
httpMessage[REQUEST_LINE][URL_PART][len(HTTP_PART) +\
len(httpMessage[HOST_NAME_LINE][URL_PART]) : ])
except:
pass
def removeProxyHeader(self, httpMessage):
index = self.getLineIndex(httpMessage, "Proxy-Connection")
try:
httpMessage.pop(index)
except:
pass
def changeHttpVersion(self, httpMessage):
REQUEST_LINE = 0
FIRST_PART = 0
HTTP_VERSION_PART = 1
HTTP_VERSION = "HTTP/1.0"
try:
httpVersionIndex = httpMessage[REQUEST_LINE][HTTP_VERSION_PART].find(" ") + 1
httpMessage[REQUEST_LINE] = (\
httpMessage[REQUEST_LINE][FIRST_PART],\
httpMessage[REQUEST_LINE][HTTP_VERSION_PART][ : httpVersionIndex] +\
HTTP_VERSION + httpMessage[REQUEST_LINE][HTTP_VERSION_PART][\
httpVersionIndex + len(HTTP_VERSION) : ])
except:
pass
def changeUserAgent(self, httpMessage):
if (self.enable):
FIRST_PART = 0
userAgentIndex = self.getLineIndex(httpMessage, "User-Agent")
try:
httpMessage[userAgentIndex] = (
httpMessage[userAgentIndex][FIRST_PART], self.userAgent)
except:
pass
def changeIfModifiedSinceHeader(self, date, httpMessage):
IF_MODIFIED_SINCE = "If-Modified-Since"
HEADER_INDEX = 0
VALUE_INDEX = 0
formatedDate = DAYNAMES[date.weekday()] + ", " + str(date.day) + " " +\
str(date.month) + " " + str(date.year) + " " + str(date.hour) + ":" +\
str(date.minute) + ":" + str(date.second) + " GMT"
for headerLine in httpMessage:
if (headerLine[HEADER_INDEX] == IF_MODIFIED_SINCE):
headerLine[VALUE_INDEX] = formatedDate
break
else:
httpMessage.append((IF_MODIFIED_SINCE, formatedDate))