/
proxy_bk.c
399 lines (355 loc) · 9.83 KB
/
proxy_bk.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
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
/* Ming-Hsiung Hsueh msheuh
* Shu-Hao Yu shuhaoy
*/
#include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<string.h>
#include<netdb.h>
#include<pthread.h>
#include "csapp.h"
#include "cache.h"
#define MAXHEADERSIZE 8192
#define MAXHOSTSIZE 1024
#define MAXBUFFERSIZE 4096
int readcnt;
sem_t mutex,w;
char ** headerSet;
/* get url information*/
int takeurlname(const char* buff,char** urlname){
char* first=strstr(buff,"GET ");
char* urlend;
int urlnameLength;
if(first == NULL)
return 0;
first+=strlen("GET ");
if(strncmp(first,"http:",5) == 0 ){
first += strlen("http://");
urlend = strstr(first," ");
// urlnameLength = strlen(first) - strlen(urlend);
urlnameLength = urlend - first;
*urlname = (char*)malloc((urlnameLength)*sizeof(char));
strncpy(*urlname,first,urlnameLength);
*(*urlname + urlnameLength) = '\0';
return 1;
}
return 0;
}
/*get to hostname information*/
int takehostname(const char* buff,char* hostname){
char* first=strstr(buff,"Host: ");
if(first==NULL)
return 0;
first+=strlen("Host: ");
char* last=strstr(first,"\r\n");
strncpy(hostname,first,strlen(first)-strlen(last));
hostname[strlen(first)-strlen(last)]='\0';
return 1;
}
int setGETheader(const char* buff, char* request, char* hostname){
char* first = strstr(buff,"GET");
char* hostend;
char* uriend;
char* uri;
char temp_host[100];
char temp_buffer[MAXBUFFERSIZE];
char temp_get[MAXBUFFERSIZE];
char fullhost[MAXBUFFERSIZE];
int urilength;
if(first == NULL){
perror("No GET in HEADER\n");
return 0;
}
first+=strlen("GET ");
if(strncmp(first,"http:",5) == 0 ){
first += strlen("http://");
hostend = strstr(first,"/");
strncpy(hostname,first,strlen(first)-strlen(hostend));
hostname[strlen(first)-strlen(hostend)] = '\0';
uriend = strstr(hostend," ");
urilength = (int)(uriend - hostend);
// urilength = strlen(hostend) - strlen(uriend);
printf("urilength :%d\n",urilength);
fflush(stdout);
uri = (char*)malloc((urilength+1)*sizeof(char));
strncpy(uri,hostend,urilength);
uri[urilength] = '\0';
sprintf(temp_get,"GET %s HTTP/1.0\r\n",uri);
free(uri);
}
/* Not http request, return zero*/
else{
return 0;
}
/* Check if the browser has already specified the Host name */
if( takehostname(buff,temp_host) ){
sprintf(fullhost,"Host: %s\r\n",temp_host);
sprintf(request,"%s%s",temp_get,fullhost);
return 1;
}
else{
sprintf(fullhost,"Host: %s\r\n",hostname);
sprintf(request,"%s%s",temp_get,fullhost);
return 1;
}
}
/*convert the Proxy-connection:keep-alive to close*/
int settherequest(char* request,char* newRequestHeader, char* hostname){
char* pch;
char *first;
int i = 0 ;
int flag[5] = {0};
bzero(newRequestHeader,sizeof(newRequestHeader));
//Get host name and rewrite GET header
if( setGETheader(request,newRequestHeader,hostname) == 0){
perror("Error in Header\n");
return 0;
}
printf("set GET header dont\n");
first=request;
pch=strstr(request,"\n");
if(pch==NULL)
return 0 ;
else
*pch='\0';
while(1){
/*GET and HOST line has been set*/
if(strncmp(first,"GET",3) == 0){
}
else if(strncmp(first,"Host",4) == 0){
}
else if(strncmp(first,"\r",1) == 0){
break;
}
/* else if(strncmp(first,"User-Agent:",11) == 0){
strcat(newRequestHeader,headerSet[0]);
flag[0] = 1;
}
else if(strncmp(first,"Accept:",7) == 0){
strcat(newRequestHeader,headerSet[1]);
flag[1] = 1;
}
else if(strncmp(first,"Accept-Encoding:",16) == 0){
strcat(newRequestHeader,headerSet[2]);
flag[2] = 1;
}
*/ else if(strncmp(first,"Connection:",11) == 0){
strcat(newRequestHeader,headerSet[3]);
flag[3] = 1;
}
else if(strncmp(first,"Proxy-Connection:",17) == 0){
strcat(newRequestHeader,headerSet[4]);
flag[4] = 1;
}
else{
strcat(newRequestHeader,first);
strcat(newRequestHeader,"\n");
}
first=pch+1;
pch=strstr(first,"\n");
if(pch==NULL)
break;
else
*pch='\0';
}
for(i = 3 ; i < 5 ;i++){
if(flag[i] == 0 ){
strcat(newRequestHeader,headerSet[i]);
flag[i] = 1;
}
}
strcat(newRequestHeader,"\r\n");
return 1;
}
void* request (void* data){
pthread_detach(pthread_self());
int serverfd=*(int*)data;
int clientfd;
char headerBuff[MAXHEADERSIZE];
char RequestHeader[MAXHEADERSIZE];
char hostname[MAXHOSTSIZE];
char Buffer[MAXBUFFERSIZE];
char* urlname;
char* cache_buff;
int cache_size = 0;
int error;
int byte;
int getbyte;
int cachehit = 0;
Signal(SIGPIPE, SIG_IGN);
free(data);
rio_t serverrio_t;
// Rio_readinitb(&serverrio_t,serverfd);
/*set the structure, and set port to 80*/
printf("to Start read %u\n",pthread_self());
byte=Rio_readn(serverfd,headerBuff,sizeof(headerBuff));
// byte=read(serverfd,headerBuff,sizeof(headerBuff));
printf("finish read byte:%d %u\n",byte,pthread_self());
if(byte<=0){
close(serverfd);
pthread_exit(NULL);
}
printf("%s\n",headerBuff);
fflush(stdout);
/*Check if the webpage is cached. if cache hit, no need to connect to server*/
if(takeurlname(headerBuff,&urlname) == 0){
perror("Cannot find url name\n");
close(serverfd);
pthread_exit(NULL);
}
printf("Try to get cached data: %u\n",pthread_self());
/* Try to get cached data, must check no thread writing the cache,
* and use FILO architecture to lock when there threads reading cache*/
/*
P(&mutex);
readcnt++;
if(readcnt == 1)
P(&w);
V(&mutex);
if( Get_cachedata(urlname,serverfd) > 0){
cachehit = 1;
}
P(&mutex);
readcnt--;
if(readcnt == 0)
V(&w);
V(&mutex);
if(cachehit){
close(serverfd);
pthread_exit(NULL);
}
*/
printf("No cached data try connect: %u\n",pthread_self());
/* Webpage not cached, retrieve information from web server */
if( (clientfd=socket(AF_INET,SOCK_STREAM,0))<0){
perror("socket");
close(serverfd);
pthread_exit(NULL);
}
// Rio_readinitb(&clientrio_t,clientfd);
/* Set the request to the server */
if( settherequest(headerBuff,RequestHeader,hostname) == 0){
perror("Set Request Error\n");
close(serverfd);
pthread_exit(NULL);
}
printf("%s\n",RequestHeader);
fflush(stdout);
/*get server's address info*/
struct addrinfo *hostinfo;
if( (error=getaddrinfo(hostname,"http",NULL,&hostinfo)) >0){
perror("hostinfo");
close(serverfd);
pthread_exit(NULL);
}
/* connect to the server*/
if( (connect(clientfd,hostinfo->ai_addr,hostinfo->ai_addrlen))<0){
perror("connect");
close(serverfd);
return NULL;
}
printf("Ready to transmitt data: %u\n",pthread_self());
/*pass the request from browser to server*/
Rio_writen(clientfd,RequestHeader,byte);
/*read the information from web server and send it to browser*/
cache_buff = (char*) malloc(MAX_OBJECT_SIZE*sizeof(char));
bzero(cache_buff,MAX_OBJECT_SIZE);
while(1){
// getbyte=read(clientfd,Buffer,sizeof(Buffer));
getbyte=Rio_readn(clientfd,Buffer,sizeof(Buffer));
if(getbyte<=0)
break;
if(cache_size + getbyte < MAX_OBJECT_SIZE){
memcpy(cache_buff + cache_size,Buffer,getbyte);
}
cache_size += getbyte;
Rio_writen(serverfd,Buffer,getbyte);
}
printf("Finish transmitt data: %u\n",pthread_self());
/* if size fit in cache, write into cache, else free the content*/
/*
if(cache_size < MAX_OBJECT_SIZE){
char* new_cache_buff = realloc(cache_buff,cache_size);
free(cache_buff);
if(new_cache_buff == NULL){
perror("Realloc fail\n");
close(clientfd);
close(serverfd);
pthread_exit(NULL);
}
P(&w);
Create_cache(urlname,new_cache_buff,cache_size);
Cache_checker();
V(&w);
}
else{
free(cache_buff);
}
*/ printf("Finish cached data: %u\n",pthread_self());
close(clientfd);
close(serverfd);
pthread_exit(NULL);
}
void proxy_init(){
int i;
int lengthSet[5] = {86,73,32,19,25};
/* Initial locks to be 1 */
Sem_init(&mutex,0,1);
Sem_init(&w,0,1);
/* Initial Cache space */
/*
Cache_init();
*/
/* Initial pre-set header */
headerSet = (char**)malloc(5*sizeof(char*));
for(i = 0 ; i < 5 ;i++){
headerSet[i] = (char*)malloc(lengthSet[i]*sizeof(char));
}
strcpy(headerSet[0],"User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:10.0.3) Gecko/20120305 Firefox/10.0.3\r\n");
strcpy(headerSet[1],"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n");
strcpy(headerSet[2],"Accept-Encoding: gzip, deflate\r\n");
strcpy(headerSet[3],"Connection: close\r\n");
strcpy(headerSet[4],"Proxy-Connection: close\r\n");
}
int main(int argc,char* argv[]){
int serverfd;
int sockfd;
struct sockaddr_in serv;
struct sockaddr_in server_addr;
socklen_t length_ptr;
pthread_t th;
Signal(SIGPIPE, SIG_IGN);
proxy_init();
sockfd=socket(AF_INET,SOCK_STREAM,0);
bzero(&serv,sizeof(serv));
/*set the structure and port defined as argv[1]*/
serv.sin_family=AF_INET;
serv.sin_port=htons(atoi(argv[1]));
serv.sin_addr.s_addr = INADDR_ANY;
// inet_pton(AF_INET,"",&serv.sin_addr);
bind(sockfd,(struct sockaddr*)&serv,sizeof(serv));
if(listen(sockfd,1000)<0){
perror("listen");
exit(-1);
}
/*continously get the request from the browser
* and build the thread to run each request*/
while(1){
serverfd=accept(sockfd,(struct sockaddr*)&server_addr,&length_ptr);
if(serverfd<=0){
perror("Error accept");
break;
}
printf("serverfd:%d\n",serverfd);
int *valp = (int*)malloc(sizeof(int));
*valp = serverfd;
pthread_create(&th,NULL,request,valp);
}
printf("serverfd :%d\n",serverfd);
fflush(stdout);
close(sockfd);
return 0;
}