-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPOST.py
134 lines (104 loc) · 4.33 KB
/
POST.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
from pathlib import Path
import os
import datetime
import calendar
import get_date
import cookie
import headers
import GET
import log_file_entry as log
document_root = GET.document_root
images_folder = GET.images_folder
LOCAL_TIMEZONE = datetime.datetime.now(datetime.timezone.utc).astimezone().tzinfo
def post(req, address, msg):
#get all the files and folders in the root
files = os.listdir(document_root)
request = req[0].split(' ')
uri = request[1]
cur_time = datetime.datetime.now()
cur_day = get_date.getday(str(cur_time.day) + " " + str(cur_time.month) + " " + str(cur_time.year))
cur_month = get_date.getMonth(str(cur_time.month))
cur_day = str(cur_day)
#validate the header
host, type, proxy_auth, range, encoding, if_mod_since, if_unmod_since = headers.check_headers(req)
if host == 0:
data = "HTTP/1.1 400 Bad Request\r\n"
status_code = 400
data += "Date: " + cur_day[0:3] + ", " + str(cur_time.day) + " " + cur_month + " " + str(cur_time.year) + " " + str(cur_time.hour) + ":" + str(cur_time.minute) + ":" + str(cur_time.second) + " " + str(LOCAL_TIMEZONE) + "\r\n"
data += "Server: Aayush/0.1\r\n"
data += "Connection: Closed\r\n"
data += '\r\n'
log.make_entry(address, req[0], cur_time, cur_month, status_code, 0, '', 'error', 'Bad request')
return data
#check if the request contains cookie header
cookie_id = cookie.generate_cookie(req)
if cookie_id == False:
set_cookie_header = 0
else:
set_cookie_header = 1
if uri == '/':
data = "HTTP/1.1 200 OK\r\n"
status_code = 200
data += "Date: " + cur_day[0:3] + ", " + str(cur_time.day) + " " + cur_month + " " + str(cur_time.year) + " " + str(cur_time.hour) + ":" + str(cur_time.minute) + ":" + str(cur_time.second) + " " + str(LOCAL_TIMEZONE) + "\r\n"
if set_cookie_header == 1:
data += "Set-Cookie: " + str(cookie_id) + '\r\n'
data += "Server: Aayush/0.1\r\n"
data += "Connection: Closed\r\n"
data += "\r\n"
log.make_entry(address, req[0], cur_time, cur_month, status_code, len(msg), msg)
return data
uri = uri.split('/')
uri_page = uri[len(uri) - 1]
uri_page += ".html"
#check if the requested URL exists or not(files)
for file in files:
if uri_page != str(file):
found_file = 0
found = 0
else:
found_file = 1
found = 1
break
if found_file == 0:
uri_page = uri_page.replace('.html', '')
#check for text files
for file in files:
if uri_page != str(file):
found_file = 0
found = 0
else:
found_file = 1
found = 1
break
image_files = os.listdir(images_folder)
#check for the images
if found_file == 0:
for image in image_files:
if uri_page == str(image):
found = 1
uri_page = "images/" + uri_page
break
else:
found = 0
if found == 0:
data = "HTTP/1.1 404 Not Found\r\n"
status_code = 404
data += "Date: " + cur_day[0:3] + ", " + str(cur_time.day) + " " + cur_month + " " + str(cur_time.year) + " " + str(cur_time.hour) + ":" + str(cur_time.minute) + ":" + str(cur_time.second) + " " + str(LOCAL_TIMEZONE) + "\r\n"
if set_cookie_header == 1:
data += "set-Cookie: " + str(cookie_id) + '\r\n'
data += "Server: Aayush/0.1\r\n"
data += "Connection: Closed\r\n"
data += "\r\n"
log.make_entry(address, req[0], cur_time, cur_month, status_code, len(msg))
return data
else:
data = "HTTP/1.1 200 OK\r\n"
status_code = 200
data += "Date: " + cur_day[0:3] + ", " + str(cur_time.day) + " " + cur_month + " " + str(cur_time.year) + " " + str(cur_time.hour) + ":" + str(cur_time.minute) + ":" + str(cur_time.second) + " " + str(LOCAL_TIMEZONE) + "\r\n"
if set_cookie_header == 1:
data += "Set-Cookie: " + str(cookie_id) + '\r\n'
data += "Server: Aayush/0.1\r\n"
data += "Connection: Closed\r\n"
data += "\r\n"
log.make_entry(address, req[0], cur_time, cur_month, status_code, len(msg), msg)
return data