-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.c
More file actions
429 lines (372 loc) · 11.9 KB
/
Copy pathproxy.c
File metadata and controls
429 lines (372 loc) · 11.9 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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#include <stdio.h>
#include "csapp.h"
#include "sbuf.h"
/* Recommended max cache and object sizes */
#define MAX_CACHE_SIZE 1049000
#define MAX_OBJECT_SIZE 102400
#define MAX_CACHE 10
#define SBUFSIZE 16 // buffer maxsize
#define NTHREADS 4
/* You won't lose style points for including this long line in your code */
static const char *user_agent_hdr = "User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:10.0.3) Gecko/20120305 Firefox/10.0.3\r\n";
struct URI{
char hostname[MAXLINE];// hostname
char port[MAXLINE]; // port
char path[MAXLINE]; //resource path
};
// cache unit structure
typedef struct
{
/* data */
char obj[MAX_OBJECT_SIZE];
char uri[MAXLINE];
int LRU;
int isEmpty;
int read_cnt;
sem_t w;
sem_t mutex;
}block;
// cache structure
typedef struct
{
/* data */
block data[MAX_CACHE];
int num;
}Cache;
Cache cache;
void doit(int fd);
void read_requesthdrs(rio_t *rp);
void parse_uri(char *uri, struct URI *uri_data);
void serve_static(int fd, char *filename, int filesize);
void get_filetype(char *filename, char *filetype);
void serve_dynamic(int fd, char *filename, char *cgiargs);
void clienterror(int fd, char *cause, char *errnum,
char *shortmsg, char *longmsg);
void request_to_proxy(int fd );
int send_to_server(int fd, struct URI *uri_data);
void *thread(void *vargp);
void init_Cache();
int get_Cache(char *uri);
int get_Index();
void update_LRU(int index);
void write_Cache(char *uri, char *buf);
sbuf_t sbuf;
int main(int argc, char **argv){
int listenfd, connfd;
char hostname[MAXLINE], port[MAXLINE];
socklen_t clientlen;
struct sockaddr_storage clientaddr;
pthread_t tid;
init_Cache();
/* Check command line args */
if (argc != 2) {
fprintf(stderr, "usage: %s <port>\n", argv[0]);
exit(1);
}
/* listen from client*/
listenfd = Open_listenfd(argv[1]);
sbuf_init(&sbuf, SBUFSIZE);
for(int i = 0 ; i < NTHREADS; i++){
Pthread_create(&tid, NULL, thread, NULL);
}
while (1) {
clientlen = sizeof(clientaddr);
connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen); //proxy connected with client
Getnameinfo((SA *) &clientaddr, clientlen, hostname, MAXLINE, port, MAXLINE, 0);
printf("Accepted connection from (%s, %s)\n", hostname, port);
// request_to_proxy(connfd); //client request to proxy, proxy redirect to server
// Close(connfd); //line:netp:tiny:close
sbuf_insert(&sbuf, connfd);
}
return 0;
}
void *thread(void *vargp){
Pthread_detach(pthread_self());
while(1){
int connfd = sbuf_remove(&sbuf);
request_to_proxy(connfd);
close(connfd);
}
}
/*
handle request line and header from client, store, wait to connect with server
*/
void request_to_proxy(int fd ){
char buf[MAXLINE], method[MAXLINE], uri[MAXLINE], version[MAXLINE];
rio_t rio, server_rio;
Rio_readinitb(&rio, fd);
if (!Rio_readlineb(&rio, buf, MAXLINE)) //store in buf
return;
printf("buf: %s", buf);
fflush(stdout);
sscanf(buf, "%s %s %s", method, uri, version); // client request: method, uri, version
char cache_tag[MAXLINE];
strcpy(cache_tag, uri);
if (strcasecmp(method, "GET")) { //only handle GET request
clienterror(fd, method, "501", "Not Implemented",
"Tiny does not implement this method");
return;
}
struct URI *uri_data = (struct URI *)malloc(sizeof(struct URI*));
int i;
if ((i = get_Cache(cache_tag)) != -1)
{
//加锁
P(&cache.data[i].mutex);
cache.data[i].read_cnt++;
if (cache.data[i].read_cnt == 1)
// printf("P before: cache.data[%d].w = %d\n", i, cache.data[i].w);
// fflush(stdout);
P(&cache.data[i].w);
// printf("P after: cache.data[%d].w = %d\n", i, cache.data[i].w);
// fflush(stdout);
V(&cache.data[i].mutex);
Rio_writen(fd, cache.data[i].obj, strlen(cache.data[i].obj));
printf("cache hit\n");
fflush(stdout);
P(&cache.data[i].mutex);
cache.data[i].read_cnt--;
if (cache.data[i].read_cnt == 0)
V(&cache.data[i].w);
// printf("V: cache.data[%d].w = %d\n", i, cache.data[i].w);
// fflush(stdout);
V(&cache.data[i].mutex);
return;
}
printf("cache not hit\n");
fflush(stdout);
parse_uri(uri, uri_data);//parse uri into uri_data,(hostname, port, resource path)
printf("new uri: %s, new port: %s, resourse located: %s\n", uri_data->hostname, uri_data->port, uri_data->path);
fflush(stdout);
// proxy as a client
int connfd;
connfd = send_to_server(fd, uri_data);
printf("have sent to server\n");
fflush(stdout);
// save message from server,transfer to browers
char cache_buf[MAX_OBJECT_SIZE];
rio_t rio_server_to_proxy;
char buf_proxy[MAXLINE];
size_t n ;
int size_buf = 0;
Rio_readinitb(&rio_server_to_proxy, connfd);
while( (n = Rio_readlineb(&rio_server_to_proxy, buf_proxy, MAXLINE))!= 0){
// printf("recieve from server\n");
// fflush(stdout);
size_buf += n;
if(size_buf < MAX_OBJECT_SIZE)
strcat(cache_buf, buf_proxy);
printf("read %d bytes from server\n", (int)n);
fflush(stdout);
Rio_writen(fd, buf_proxy, n);
// printf("send to client: %s", buf_proxy);
// fflush(stdout);
}
Close(connfd);
if(size_buf < MAX_OBJECT_SIZE){
write_Cache(cache_tag, cache_buf);
}
}
/* parse uri, split host, port and path into uri_data*/
void parse_uri(char *uri, struct URI *uri_data){
char *default_hostname = "localhost";
char *hostpose = strstr(uri, "//");
// printf("hostpose: %s\n", hostpose);
// fflush(stdout);
if (hostpose == NULL){
char *pathpose = strstr(uri, "/");
if (pathpose != NULL){
strcpy(uri_data->path, pathpose);
}
return;
}
else{
char *portpose = strstr(hostpose + 2, ":");
if (portpose != NULL){
int tmp;
// after port: path
sscanf(portpose + 1, "%d%s", &tmp, uri_data->path);
sprintf(uri_data->port, "%d", tmp);
*portpose = '\0';
}
else{
char *pathpose = strstr(hostpose + 2, "/");
if (pathpose != NULL){
strcpy(uri_data->path, pathpose);
*pathpose = '\0';
}
}
strcpy(uri_data->hostname, hostpose + 2);
}
return ;
}
// request to server, server's hostname as same with proxy
// proxy has already had listened port -> server has listend port,do not create listened port again
int send_to_server(int fd, struct URI *uri_data ){
int server_connectfd;
server_connectfd = Open_clientfd(uri_data->hostname, uri_data->port);
if(server_connectfd < 0){
printf("connection failed \n");
fflush(stdout);
}
char buf_client[MAXLINE]; // all request lines and request headers buf
rio_t rio_server;//read response
Rio_readinitb(&rio_server, server_connectfd);
printf("HTTP request start\n");
/* send request line, headers */
/* send request line */
sprintf(buf_client, "GET %s HTTP/1.0\r\n", uri_data->path);
Rio_writen(server_connectfd, buf_client, strlen(buf_client));
/* send request headers*/
sprintf(buf_client, "Host: %s\r\n", uri_data->hostname);
Rio_writen(server_connectfd, buf_client, strlen(buf_client));
sprintf(buf_client, "%s",user_agent_hdr);
Rio_writen(server_connectfd, buf_client, strlen(buf_client));
sprintf(buf_client, "Connection: close\r\n");
Rio_writen(server_connectfd, buf_client, strlen(buf_client));
sprintf(buf_client, "Proxy-Connection: close\r\n");
Rio_writen(server_connectfd, buf_client, strlen(buf_client));
sprintf(buf_client, "\r\n");
Rio_writen(server_connectfd, buf_client, strlen(buf_client));
return server_connectfd;
}
void clienterror(int fd, char *cause, char *errnum,
char *shortmsg, char *longmsg)
{
char buf[MAXLINE];
/* Print the HTTP response headers */
sprintf(buf, "HTTP/1.0 %s %s\r\n", errnum, shortmsg);
Rio_writen(fd, buf, strlen(buf));
sprintf(buf, "Content-type: text/html\r\n\r\n");
Rio_writen(fd, buf, strlen(buf));
/* Print the HTTP response body */
sprintf(buf, "<html><title>Tiny Error</title>");
Rio_writen(fd, buf, strlen(buf));
sprintf(buf, "<body bgcolor=""ffffff"">\r\n");
Rio_writen(fd, buf, strlen(buf));
sprintf(buf, "%s: %s\r\n", errnum, shortmsg);
Rio_writen(fd, buf, strlen(buf));
sprintf(buf, "<p>%s: %s\r\n", longmsg, cause);
Rio_writen(fd, buf, strlen(buf));
sprintf(buf, "<hr><em>The Tiny Web server</em>\r\n");
Rio_writen(fd, buf, strlen(buf));
}
void init_Cache()
{
cache.num = 0;
int i;
for (i = 0; i < MAX_CACHE; i++)
{
cache.data[i].LRU = 0;
cache.data[i].isEmpty = 1;
// w, mutex均初始化为1
Sem_init(&cache.data[i].w, 0, 1);
Sem_init(&cache.data[i].mutex, 0, 1);
cache.data[i].read_cnt = 0;
}
}
//从Cache中找到内容
int get_Cache(char *url)
{
int i;
for (i = 0; i < MAX_CACHE; i++)
{
//读者加锁
P(&cache.data[i].mutex);
cache.data[i].read_cnt++;
if (cache.data[i].read_cnt == 1)
P(&cache.data[i].w);
V(&cache.data[i].mutex);
if ((cache.data[i].isEmpty == 0) && (strcmp(url, cache.data[i].uri) == 0))
break;
P(&cache.data[i].mutex);
cache.data[i].read_cnt--;
if (cache.data[i].read_cnt == 0)
V(&cache.data[i].w);
V(&cache.data[i].mutex);
}
if (i >= MAX_CACHE)
return -1;
return i;
}
//找到可以存放的缓存行
int get_Index()
{
int min = __INT_MAX__;
int minindex = 0;
int i;
for (i = 0; i < MAX_CACHE; i++)
{
//读锁
P(&cache.data[i].mutex);
cache.data[i].read_cnt++;
if (cache.data[i].read_cnt == 1)
P(&cache.data[i].w);
V(&cache.data[i].mutex);
if (cache.data[i].isEmpty == 1)
{
minindex = i;
P(&cache.data[i].mutex);
cache.data[i].read_cnt--;
if (cache.data[i].read_cnt == 0)
V(&cache.data[i].w);
V(&cache.data[i].mutex);
break;
}
if (cache.data[i].LRU < min)
{
minindex = i;
P(&cache.data[i].mutex);
cache.data[i].read_cnt--;
if (cache.data[i].read_cnt == 0)
V(&cache.data[i].w);
V(&cache.data[i].mutex);
continue;
}
P(&cache.data[i].mutex);
cache.data[i].read_cnt--;
if (cache.data[i].read_cnt == 0)
V(&cache.data[i].w);
V(&cache.data[i].mutex);
}
return minindex;
}
//更新LRU
void update_LRU(int index)
{
printf("start update_LRU,index = %d\n", index);
fflush(stdout);
for (int i = 0; i < MAX_CACHE; i++)
{
if (!cache.data[i].isEmpty&& i != index)
{
// printf("update_LRU, cache.data[%d].w = %d\t", i, cache.data[i].w);
// fflush(stdout);
P(&cache.data[i].w);
// printf("update_LRU, cache.data[i].obj = %s\n", cache.data[i].obj);
// fflush(stdout);
cache.data[i].LRU--;
V(&cache.data[i].w);
}
}
printf("end update_LRU\n");
fflush(stdout);
}
//写缓存
void write_Cache(char *uri, char *buf)
{
int i = get_Index();
//加写锁
P(&cache.data[i].w);
//写入内容
strcpy(cache.data[i].obj, buf);
strcpy(cache.data[i].uri, uri);
cache.data[i].isEmpty = 0;
cache.data[i].LRU = __INT_MAX__;
printf("need write in cache[%d] (update others)\n", i);
fflush(stdout);
update_LRU(i);
printf("updated cache\n");
fflush(stdout);
V(&cache.data[i].w);
}