-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProxy.c
340 lines (297 loc) · 12 KB
/
Proxy.c
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// ************************************ Server Code ****************************************************
// Authors : Sabyasachi Gupta ([email protected])
// Organisation : Texas A&M University, CS for ECEN 602 Assignment 4
// Description : Implementation of a simple TCP HTTP Proxy Server. Establishes an IPv4/IPv6 socket
// connection with a client and caches incoming requests or forwards them to the
// appropriate web server, eventually forwarding the requested HTTP content to the
// client. Multiple clients can be supported.
// Last_Modified : 11/27/2017
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#include <dirent.h>
#include <pthread.h>
#include <time.h>
#include <utils.h>
struct Cache {
char URL[256];
char Last_Modified[50];
char Access_Date[50];
char Expires[50];
char *body;
};
static const struct Cache Clear_Entry;
int num_cache_entries = 0;
struct Cache Proxy_Cache[MAX_CACHE_ENTRY];
int Update_Cache(char *URL, char *buf, int flag, int x) {
int j=0;
int p=0;
if (flag == 1) { // New entry
if (num_cache_entries==MAX_CACHE_ENTRY){
Proxy_Cache[0] = Clear_Entry; // Popping LRU
for (j=0; j<MAX_CACHE_ENTRY; j++){
if (j+1!=MAX_CACHE_ENTRY)
Proxy_Cache[j] = Proxy_Cache[j+1];
else {
// Add new entry at the head (latest)
memset(&Proxy_Cache[j], 0, sizeof(struct Cache));
memcpy(Proxy_Cache[j].URL,URL,256);
Proxy_Cache[j].body = (char *) malloc(strlen(buf));
memcpy(Proxy_Cache[j].body,buf,strlen(buf));
parseHDR("Expires:", buf, Proxy_Cache[j].Expires);
parseHDR("Last-Modified:", buf, Proxy_Cache[j].Last_Modified);
parseHDR("Date:", buf, Proxy_Cache[j].Access_Date);
}
}
}
else { // If cache has not reached max allowed capacity (MAX_CACHE_ENTRY)
Proxy_Cache[num_cache_entries] = Clear_Entry;
memcpy(Proxy_Cache[num_cache_entries].URL,URL,256);
parseHDR("Expires:", buf, Proxy_Cache[num_cache_entries].Expires);
parseHDR("Last-Modified:", buf, Proxy_Cache[num_cache_entries].Last_Modified);
parseHDR("Date:", buf, Proxy_Cache[num_cache_entries].Access_Date);
Proxy_Cache[num_cache_entries].body = (char *) malloc(strlen(buf));
memcpy(Proxy_Cache[num_cache_entries].body,buf,strlen(buf));
num_cache_entries++;
}
}
else { // Existing entry
struct Cache tmp;
memset(&tmp, 0, sizeof(struct Cache));
tmp = Proxy_Cache[x];
for (j=x; j<num_cache_entries; j++){
if (j==num_cache_entries-1)
break;
Proxy_Cache[j] = Proxy_Cache[j+1];
}
Proxy_Cache[num_cache_entries -1] = tmp;
struct tm tmp_t;
time_t nw = time(NULL);
tmp_t = *gmtime(&nw);
const char* op_tmp = "%a, %d %b %Y %H:%M:%S GMT";
strftime (Proxy_Cache[num_cache_entries - 1].Access_Date, 50, op_tmp, &tmp_t);
}
}
int Cache_Display () {
int t = 0;
if (num_cache_entries == 0)
printf("Cache is unoccupied currently\n");
else {
printf("Cache count: %d\n", num_cache_entries);
for (t=0; t<num_cache_entries; t++) {
if (strcmp(Proxy_Cache[t].Expires, "") != 0 && strcmp(Proxy_Cache[t].Last_Modified, "") != 0)
printf("Index: %d | URL: %s | Access Date: %s | Expires: %s | Last_Modified: %s\n\n", t, Proxy_Cache[t].URL, Proxy_Cache[t].Access_Date, Proxy_Cache[t].Expires, Proxy_Cache[t].Last_Modified);
else if (strcmp(Proxy_Cache[t].Expires, "") == 0 && strcmp(Proxy_Cache[t].Last_Modified, "") == 0)
printf("Index: %d | URL: %s | Access Date: %s | Expires: N/A | Last_Modified: N/A\n\n", t, Proxy_Cache[t].URL, Proxy_Cache[t].Access_Date);
else if (strcmp(Proxy_Cache[t].Expires, "") == 0)
printf("Index: %d | URL: %s | Access Date: %s | Expires: N/A | Last_Modified: %s\n\n", t, Proxy_Cache[t].URL, Proxy_Cache[t].Access_Date, Proxy_Cache[t].Last_Modified);
else if (strcmp(Proxy_Cache[t].Last_Modified, "") == 0)
printf("Index: %d | URL: %s | Access Date: %s | Expires: %s | Last_Modified: N/A\n\n", t, Proxy_Cache[t].URL, Proxy_Cache[t].Access_Date, Proxy_Cache[t].Expires);
}
}
return 0;
}
int Fresh (int cache_ptr) {
struct tm tmp_t;
time_t nw = time(NULL);
tmp_t = *gmtime(&nw);
struct tm EXPIRES;
if (strcmp(Proxy_Cache[cache_ptr].Expires, "") != 0) {
strptime(Proxy_Cache[cache_ptr].Expires, "%a, %d %b %Y %H:%M:%S %Z", &EXPIRES);
time_t EXP = mktime(&EXPIRES);
time_t NOW = mktime(&tmp_t);
if (difftime (NOW, EXP) < 0)
return 1;
else
return -1;
}
else
return -1;
}
int Cache_Element(char *URL) {
int b=0;
for (b=0; b<MAX_CACHE_ENTRY; b++) {
if (strcmp(Proxy_Cache[b].URL, URL)==0) {
return b;
}
}
return -1;
}
int WebS_Socket (char *host) {
struct addrinfo dynamic_addr, *ai, *p;
int ret_val = 0;
int webs_sockfd = 0;
memset(&dynamic_addr, 0, sizeof dynamic_addr);
dynamic_addr.ai_family = AF_INET;
dynamic_addr.ai_socktype = SOCK_STREAM;
if ((ret_val = getaddrinfo(host, "http", &dynamic_addr, &ai)) != 0) {
fprintf(stderr, "SERVER: %s\n", gai_strerror(ret_val));
exit(1);
}
for(p = ai; p != NULL; p = p->ai_next) {
webs_sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if (webs_sockfd >= 0 && (connect(webs_sockfd, p->ai_addr, p->ai_addrlen) >= 0))
break;
}
if (p == NULL)
webs_sockfd = -1;
freeaddrinfo(ai);
return webs_sockfd;
}
int Proxy_Server(int client_fd) {
int webs_sockfd;
char *msg;
char forward_client_msg[MAX_LEN] = {0};
int ret;
int cache_el = 0;
//char resp[1024] = {0};
char *resp = NULL;
//char to_client[10240] = {0};
char *to_client = NULL;
//string Method;
//string Protocol;
char path[256];
char hostname[64];
int port = 80;
char URL[256] = {0};
char Method[8] = {0};
char Protocol[16] = {0};
char cond_msg[256] = {0};
char url_parse[256] = {0};
int check = 0;
//memset(&url, 0, sizeof url);
msg = (char *) malloc (MAX_LEN);
ret = read(client_fd, msg, MAX_LEN);
printf("SERVER: Request retrieved from client: \n%s", msg);
if (ret < 0)
err_sys ("SERVER: Error in extracting message request from client");
sscanf(msg, "%s %s %s", Method, URL, Protocol);
//free (msg);
//printf("SERVER: URL extracted: %s\n", URL);
if ((cache_el = Cache_Element (URL)) != -1 && (Fresh (cache_el) == 1)) {
//printf("Cache_el: %d\n", cache_el);
printf ("SERVER: Requested URL: %s is in cache and is fresh\n", URL);
Update_Cache(URL, NULL, 0, cache_el);
to_client = (char *) malloc(strlen(Proxy_Cache[cache_el].body));
memcpy(to_client, Proxy_Cache[cache_el].body, strlen(Proxy_Cache[cache_el].body));
}
else { // Either URL is not cached or it is stale
memset(hostname, 0, 64);
memset(path, 0, 256);
memcpy(&url_parse[0], &URL[0], 256);
parse_URL (url_parse, hostname, &port, path);
if ((webs_sockfd = WebS_Socket (hostname)) == -1)
err_sys ("SERVER: Error in connecting with web server");
printf ("SERVER: Successfully connected to web server %d\n", webs_sockfd);
if (cache_el != -1) { // If cache entry exists but has expired
printf ("SERVER: Requested URL: %s is in cache but is expired\n", URL);
//split_URL (URL, split_url);
if (strcmp(Proxy_Cache[cache_el].Expires, "") != 0 && strcmp(Proxy_Cache[cache_el].Last_Modified, "") != 0)
snprintf(cond_msg, MAX_LEN, "%s %s %s\r\nHost: %s\r\nUser-Agent: HTTPTool/1.0\r\nIf-Modified_Since: %s\r\n\r\n", Method, path, Protocol, hostname, Proxy_Cache[cache_el].Expires);
else if (strcmp(Proxy_Cache[cache_el].Expires, "") == 0 && strcmp(Proxy_Cache[cache_el].Last_Modified, "") == 0)
snprintf(cond_msg, MAX_LEN, "%s %s %s\r\nHost: %s\r\nUser-Agent: HTTPTool/1.0\r\nIf-Modified_Since: %s\r\n\r\n", Method, path, Protocol, hostname, Proxy_Cache[cache_el].Access_Date);
else if (strcmp(Proxy_Cache[cache_el].Expires, "") == 0)
snprintf(cond_msg, MAX_LEN, "%s %s %s\r\nHost: %s\r\nUser-Agent: HTTPTool/1.0\r\nIf-Modified_Since: %s\r\n\r\n", Method, path, Protocol, hostname, Proxy_Cache[cache_el].Last_Modified);
else if (strcmp(Proxy_Cache[cache_el].Last_Modified, "") == 0)
snprintf(cond_msg, MAX_LEN, "%s %s %s\r\nHost: %s\r\nUser-Agent: HTTPTool/1.0\r\nIf-Modified_Since: %s\r\n\r\n", Method, path, Protocol, hostname, Proxy_Cache[cache_el].Expires);
printf("Conditional GET Generated: \n%s", cond_msg);
write(webs_sockfd, cond_msg, MAX_LEN);
//resp = malloc (10240); // FIXME: May be needed to increase allocation
//memset(resp, 0, 1024);
resp = (char *) malloc (100000);
check = Extract_Read(webs_sockfd, resp);
//printf("Checking: %d\n", check);
//Extract_Read(webs_sockfd, resp);
to_client = (char *) malloc(strlen(resp));
if (strstr(resp, "304 Not Modified") != NULL) {
printf("'304 Not Modified' received. Sending file in cache\n");
memcpy(to_client, Proxy_Cache[cache_el].body, strlen(Proxy_Cache[cache_el].body));
Update_Cache(URL, NULL, 0, cache_el);
}
else {
printf("SERVER: File was modified\n");
memcpy(to_client, resp, strlen(resp));
Update_Cache(URL, NULL, 0, cache_el); // move to head (LRU) of the queue
Proxy_Cache[--num_cache_entries] = Clear_Entry; // Popping LRU
Update_Cache(URL, resp, 1, 0); // treat like a new entry as it was modified
}
}
else { // document is not cached
printf ("SERVER: Requested URL is not in cache\n");
memset(forward_client_msg, 0, MAX_LEN);
snprintf(forward_client_msg, MAX_LEN, "%s %s %s\r\nHost: %s\r\nUser-Agent: HTTPTool/1.0\r\n\r\n", Method, path, Protocol, hostname);
printf("SERVER: Request generated: \n%s", forward_client_msg);
write(webs_sockfd, forward_client_msg, MAX_LEN);
resp = (char *) malloc (100000);
check = Extract_Read(webs_sockfd, resp);
to_client = (char *) malloc(strlen(resp));
memcpy(to_client, resp, strlen(resp));
Update_Cache(URL, resp, 1, 0);
}
}
Cache_Display();
write(client_fd, to_client, strlen(to_client) + 1);
}
int Extract_Read(int fd, char *msg) { // Extracts message body from read socket
int total = 0;
char buffer[MAX_LEN] = {0};
int cnt = 1;
int h;
while(cnt>0) {
memset(buffer, 0, sizeof(buffer));
cnt = read(fd, buffer, MAX_LEN);
if (cnt == 0) break;
strcat(msg, buffer);
total = total + cnt;
if (buffer[cnt - 1] == EOF) {
strncpy(msg,msg,(strlen(msg)-1));
total--;
break;
}
}
return total;
}
int main (int argc, char *argv[])
{
int sockfd, comm_fd, bind_fd, listen_fd;
int port_number ;
struct sockaddr_storage remoteaddr;
socklen_t addrlen;
if (argc != 3){
err_sys ("USAGE: ./proxy <Server IP Address> <Port_Number>");
return 0;
}
port_number = atoi(argv[2]);
struct sockaddr_in servaddr;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
err_sys ("ERR: Socket Error");
bzero( &servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr(argv[1]);
servaddr.sin_port = htons(port_number);
bind_fd = bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
if (bind_fd < 0)
err_sys ("ERR: Bind Error");
listen_fd = listen(sockfd, 10);
if (listen_fd < 0)
err_sys ("ERR: Listen Error");
memset(Proxy_Cache,0,MAX_CACHE_ENTRY*sizeof(struct Cache));
addrlen = sizeof remoteaddr;
pthread_t x;
printf("\nPROXY SERVER is online\n\n");
while(1)
{
comm_fd = accept(sockfd, (struct sockaddr*)&remoteaddr,&addrlen);
pthread_create(&x, NULL, (void *)(&Proxy_Server), (void *)(intptr_t)comm_fd);
}
}