Repository navigation
Expand file tree
/
Copy pathhttpserver
More file actions
170 lines (137 loc) · 4.96 KB
/
Copy pathhttpserver
File metadata and controls
170 lines (137 loc) · 4.96 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
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
#!/usr/bin/env python3
import os
import socket as sock
import sys
import urllib.request
import subprocess
print("Running HTTP server", flush=True)
args = sys.argv
argc = len(args)
port_num = ""
origin_server = ""
host_ip = ""
http_ver = "HTTP/1.1"
origin_socket = sock.socket(sock.AF_INET, sock.SOCK_DGRAM)
origin_port = 8080
dns_ip = "173.255.237.185"
cache_file_frequency_map = {}
max_disk_usage = 18.5 * 1000000
disk_size_used = sum(os.path.getsize(f) for f in os.listdir('.') if os.path.isfile(f))
def parse_args():
port = ""
origin = ""
for i in range(0, argc-1):
if args[i] == "-p":
port = args[i+1]
elif args[i] == '-o':
origin = args[i+1]
if port == "" or origin == "":
print("Usage: ./httpserver - p < port > -o < origin >", flush=True)
exit(1)
return port, origin
def get_host_ip():
origin_socket.connect((origin_server, int(origin_port)))
host = origin_socket.getsockname()[0]
origin_socket.shutdown(sock.SHUT_RDWR)
origin_socket.close()
return host
def file_in_cache(file):
try:
f = open("cache{}".format(file), "r")
return f.read()
except:
return ""
def get_from_origin(file):
url = "http://{}:{}".format(origin_server, origin_port) + file
try:
response = urllib.request.urlopen(url)
return response.read().decode()
except:
return ""
def remove_file_from_disk(file_path):
cache_file_frequency_map.pop(file_path)
os.remove("cache{}".format(file_path))
def remove_least_frequent_entry(data, file):
global disk_size_used
file_path = min(cache_file_frequency_map, key=cache_file_frequency_map.get)
disk_size_used -= os.path.getsize("cache{}".format(file_path))
remove_file_from_disk(file_path)
add_to_cache(data, file)
def add_to_cache(data, file):
global disk_size_used
if disk_size_used < max_disk_usage:
try:
f = open("cache{}".format(file), "w+")
f.write(data)
disk_size_used += os.path.getsize("cache{}".format(file))
except EnvironmentError:
print("Inside Exception")
remove_least_frequent_entry(data, file)
else:
print("Not enough space in disk")
remove_least_frequent_entry(data, file)
# Tried implementing rtt calculation but failed to do so because ping doesn't work on a lot of clients like my laptop.
# def calculate_rtt_of_client(ip_to_ping):
# scamper_command = "scamper -i {} -c 'ping -c 1'".format(ip_to_ping)
# process = subprocess.Popen(scamper_command.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# out, err = process.communicate()
# print("Scamper communicate thing ", out)
# print("Scamper error thing ", err)
# rtt = out.split("/")[4]
# return rtt
def run_http_server():
serv_sock = sock.socket(sock.AF_INET, sock.SOCK_STREAM)
print("HTTP server ", serv_sock, flush=True)
serv_sock.setsockopt(sock.SOL_SOCKET, sock.SO_REUSEADDR, 1)
serv_sock.bind((host_ip, int(port_num)))
serv_sock.listen(1)
while True:
conn, client_ip = serv_sock.accept()
print("CLIENT IP :",client_ip)
request = conn.recv(4096).decode()
print(request)
request_split = request.split("\n")
file_path = ""
ip_to_ping = ""
for line in request_split:
if line.__contains__("GET"):
line_split = line.split()
file_path = line_split[1]
break
print("File path: ", file_path, flush=True)
origin_response = ""
if file_path == "":
print("Empty GET request", flush=True)
conn.close()
continue
elif file_path == "/grading/beacon":
server_response = 'HTTP/1.1 204 No Content\r\nContent-Type: text/html\r\n\r\n'
conn.sendall(server_response.encode())
conn.close()
continue
file_path_2 = "/"+file_path[1:].replace("/", '_')
origin_response = file_in_cache(file_path_2[1:])
in_cache = True
if origin_response == "":
origin_response = get_from_origin(file_path)
in_cache = False
print("Not found in Cache", flush=True)
if origin_response == "":
server_response = 'HTTP/1.1 404 NOT FOUND\r\nContent-Type: text/html\r\n\r\n File Not Found'
conn.sendall(server_response.encode())
conn.close()
continue
server_response = 'HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n' + origin_response
conn.sendall(server_response.encode())
conn.close()
if not in_cache:
add_to_cache(origin_response, file_path_2[1:])
cache_file_frequency_map[file_path_2[1:]] = 1
else:
cache_file_frequency_map[file_path_2[1:]] += 1
serv_sock.close()
port_num, origin_server = parse_args()
host_ip = ""
host_ip = get_host_ip()
print("HTTP server: ", host_ip, flush=True)
run_http_server()