-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorker.py
More file actions
119 lines (101 loc) · 4.88 KB
/
Worker.py
File metadata and controls
119 lines (101 loc) · 4.88 KB
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
# -*- coding:ISO-8859-1 -*-
from threading import Thread
from methods.GET import GET # thanks to github@MVGOLOM for help
from methods.POST import POST
class Worker(Thread):
def __init__(self, conn, addr, data, servers, reqCount, upTime, IP, PORT):
Thread.__init__(self)
self.reqCount = reqCount
self.servers = servers
self.upTime = upTime
self.cookies = {}
self.data = data
self.conn = conn
self.PORT = PORT
self.addr = addr
self.IP = IP
def run(self): # when starter the thread, this def is execute
self.data = self.data.decode()
if(len(self.data) == 0): # The Google Chrome sending empty request
resp = "HTTP/1.1 405 Method Not Allowed\r\n\r\n"
self.conn.sendall(resp.encode())
return
print("CONNECTION ADDRESS ", self.addr, end=': ')
self.readFile()
self.methods()
print("Encerrou uma conexao")
self.conn.close()
def readFile(self):
print(self.data[:self.data.index('\r')])
self.i = 0
while(self.data[self.i] == '\r' and self.data[self.i+1] == '\n'): # the protocol allow that the first line in the request be \r\n
self.i += 2 # so, need ignore this lines
data = self.data[self.i:] # get whitout first '\r\n' lines
self.msgHTTP = data.split("\r\n") # splite the data in lines
line = self.msgHTTP[0].split() # get the first line. Ex: GET /file.ext HTTP/1.1
self.method = line[0] # get the Method: GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
self.resourcePath = line[1] # get the Path with the Query
self.resourcePathAndQuery(self.msgHTTP[0]) # separe the Query from the resourcePath
self.version = line[2] # get the version of HTTP
self.i = 1
line = self.msgHTTP[self.i].split(': ') # split the lines in 'key':'value'
self.headerFields = {}
while(self.i < len(self.msgHTTP)):
try: # some browers send the msgHTTP broken
if(line[0] == "Cookie"):
self.setCookies(line[1])
else:
self.headerFields[line[0]] = line[1]
self.i += 1
line = self.msgHTTP[self.i].split(': ') # split the lines in 'key':'value'
except IndexError:
return
def setCookies(self, cookieString):
cookieString = cookieString.split('; ')
i = 0
# sometimes, the Google Chrome send the cookies 'breaked'
try:
while(i < len(cookieString)):
cookiePair = cookieString[i].split("=")
self.cookies[cookiePair[0]] = cookiePair[1]
i += 1
except IndexError:
return
def resourcePathAndQuery(self, line):
if(self.resourcePath.find('htpasswd') != -1): # if the request is the file '.htpasswd'. NOT SHOULD return this file
self.resourcePath = '/' # then redirect to root
self.parent = '/'
self.query = 'R=N;O=C'
return
self.resourcePath = self.resourcePath.split('?') # split in the Query
if(len(self.resourcePath) == 2):
self.query = self.resourcePath[1]
else:
#self.query = 'R=N;O=C' # R=reference by 'N'ame, 'S'ize, 'L'astModifie; O=Order by 'C'rescent or 'D'ecreasing
self.query = ''
self.resourcePath = self.resourcePath[0] # only the path
if(self.resourcePath == '/' or self.resourcePath.rindex('/') == 0):
self.parent = '/'
return
self.parent = self.resourcePath[:self.resourcePath.rindex('/')] # get the parent path
#if the resource is in CGI but not have '?params'
try:
if(self.parent == 'CGI' and line.index('?') == -1):
self.query = ''
except ValueError:
self.query = ''
def methods(self):
if(self.method == 'GET'):
GET(self.resourcePath, self.headerFields, self.conn, self.cookies, self.query, self.parent, self.servers, self.reqCount, self.upTime).getResponse()
elif(self.method == 'HEAD'):
pass
elif(self.method == 'POST'):
POST(self.resourcePath, self.headerFields, self.conn, self.cookies, self.query, self.parent, self.servers, self.msgHTTP[self.i+1:], self.IP, self.PORT)
elif(self.method == 'PUT'):
pass
elif(self.method == 'DELETE'):
pass
elif(self.method == 'TRACE'):
pass
elif(self.method == 'CONNECT'):
pass