-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_handling.py
More file actions
137 lines (114 loc) · 4.5 KB
/
file_handling.py
File metadata and controls
137 lines (114 loc) · 4.5 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import os
import json
import hashlib
import base64
class ClientInfo:
def __init__(self, data):
self.request = str(data).split()[0] # GET or POST request
try:
self.path = str(data).split()[1] # The path that is being requested
except IndexError:
print("Error with path!")
self.cookies = [] # The cookies that are present for the client
self.form = [] # The contents of a submitted form
self.length = 0 # Length of the contents of the form
self.web_socket_key = False
x = ""
x = str(data).find("Cookie")
if x != -1:
x = str(data)[x:]
end = x.find('\\r\\n')
x = x[:end]
if "jpeg" not in str(data):
for i in x.split(): # This parses and stores all of the cookies
self.cookies.append(str(i))
x = str(data).find("Content-Length:")
if x != -1:
x = str(data)[x:]
end = x.find('\\r\\n')
x = x[:end]
x = x[len("Content-Length:"):]
self.length = x
x = str(data).find("Sec-WebSocket-Key:")
if x != -1:
x = data[x:]
end = x.find(b'\r\n')
x = x[:end]
x = x.replace(b"Sec-WebSocket-Key: ", b"")
x += b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
m = hashlib.sha1()
m.update(x)
self.web_socket_key = m.hexdigest()
if "POST" in self.request and "jpeg" not in str(data) and ".json" not in self.path: # If it is a POST request there is more to do
walker = str(data)
while "Content-Disposition:" in walker:
x = walker.find("name=")
walker = walker[x:]
x = walker[:walker.find("\\r\\n\\r\\n")]
name = x.split("=")[1]
name = name.replace('"', '')
x = walker.find("\\r\\n\\r\\n")
walker = walker[x + 8:]
content = walker.split("\\r\\n")[0]
self.form.append((name, content))
elif ".json" in self.path:
walker = str(data)
x = walker.find("\\r\\n\\r\\n")
y = walker.find("}")
walker = walker[x + 8: y + 1]
info = json.loads(walker)
with open("data/ajax_file.txt", "a") as file:
for x in info:
file.write("The contents of " + x + " is: " + info[x]+'\r\n')
file.close()
print(walker)
def build_html(file, info):
while "{{loop}}" in file:
start = file.find("{{loop}}")
end = file.find("{{end_loop}}")
temp_file = file[start + 8: end]
swap = ""
for i in info:
if ("{{" + i[0] + "}}") in temp_file:
for j in i[1]:
swap += temp_file
swap = swap.replace(("{{" + i[0] + "}}"), j)
temp_file = "{{loop}}" + temp_file + "{{end_loop}}"
file = file.replace(temp_file, swap)
for i in info:
if ("{{" + i[0] + "}}") in file:
file = file.replace(("{{" + i[0] + "}}"), i[1])
return file
def response_builder(code, file_type, length):
response = "HTTP/1.1 "
if code == 101:
response = bytes(response.encode('utf-8'))
temp = base64.b64encode(bytes(file_type.encode('utf-8')))
response += b"101 Switching Protocols\r\n"
response += b"Upgrade: websocket\r\n"
response += b"Connection: Upgrade\r\n"
response += b"Sec-WebSocket-Accept:" + temp + b"\r\n\r\n"
return response
elif code == 200:
response += "200 OK\r\n"
elif code == 301:
response += "301 Moved Permanently\r\nLocation: " + file_type + "\r\n\r\n"
return response
else:
response += "404 Not Found\r\n"
response += "Content-Type: "
if file_type == "css":
response += "text/css;charset=UTF-8;X-Content-Type-Options: nosniff"
elif file_type == "js":
response += "text/javascript;charset=UTF-8;X-Content-Type-Options: nosniff"
elif file_type == "html":
response += "text/html;charset=UTF-8;X-Content-Type-Options: nosniff\r\nSet-Cookie: id=100"
elif file_type == "jpeg":
response += "image/jpeg;"
elif file_type == "txt":
response += "text/plain;charset=UTF-8;X-Content-Type-Options: nosniff"
response += "\r\nContent-Length: " + str(length) + "\r\n\r\n"
return response
def getFileSize(file):
f = os.stat(file)
return f.st_size