proxyWord下载.docx
《proxyWord下载.docx》由会员分享,可在线阅读,更多相关《proxyWord下载.docx(14页珍藏版)》请在冰豆网上搜索。
typedefstruct{
intserverPort;
charmethod[MAXLINE],url[MAXLINE];
charversion[MAXLINE],hostname[MAXLINE],path[MAXLINE];
}requestHeader;
structcacheSet{
charhostname[MAXLINE];
//hostname
charpath[MAXLINE];
//path
structcacheSet*next;
//pointertonextcacheset
char*buffer;
//bufferpointer
inttime;
//timestamp
intlen;
//buffersize
};
/*-----------*/
void*doit(void*);
structcacheSet*cacheSearch(char*hostname,char*path);
structcacheSet*cacheFindLRU(intlen);
intcacheAdd(char*hostname,char*path,char*buf,intlen);
intglobalTime=0;
structcacheSet*cache;
sem_tmutex_time;
intreadcnt=0;
sem_tmutex,w;
sem_tmutexHostName;
/*-----------------*/
intmain(intargc,char*argv[])
{
intfd;
if((fd=open("
output.txt"
O_RDWR,0))>
0)
dup2(fd,1);
pthread_ttid;
structsockaddr_inclientaddr;
intlistenfd;
int*connfdp;
intport;
socklen_tclientLen=sizeof(structsockaddr_in);
//initilizecache
cache=malloc(sizeof(structcacheSet));
if(cache==NULL){
fprintf(stderr,"
failtomalloccache.\n"
);
exit
(1);
}
strcpy(cache->
hostname,"
"
path,"
cache->
next=NULL;
buffer=NULL;
time=0;
len=0;
//initilizesemaphore
sem_init(&
mutex_time,0,1);
sem_init(&
mutex,0,1);
w,0,1);
mutexHostName,0,1);
/*examtheportnumber*/
if(argc!
=2){
Noportnumber!
\n"
/*ignoretheSIGPIPE*/
Signal(SIGPIPE,SIG_IGN);
port=atoi(argv[1]);
if((listenfd=open_listenfd(port))<
0){
fprintf(stderr,"
failtoopenlistenfd!
exit(-1);
/*createanewthread*/
while
(1){
if((connfdp=(int*)malloc(sizeof(int)))==NULL){
failtogetspacefromheap!
!
continue;
}
if((*connfdp=accept(listenfd,(SA*)&
clientaddr,&
clientLen))<
failtoacceptaconnfdprequest!
if((pthread_create(&
tid,NULL,doit,connfdp))!
=0){
failtocreateathread!
return0;
}
/*
*parsetheheader
*/
intparse_uri(requestHeader*header,rio_t*clientRio)
{
char*ptr;
inti=0,j=0;
intlength;
charport[MAXLINE]="
80"
;
if(!
strncasecmp(header->
url,"
http:
//"
7)){
/*surpass"
*/
char*first=header->
url+7;
char*as=header->
url;
while(*first)
{
*as=*first;
as++;
first++;
}
*as='
\0'
/*whetherpathistherootdirectory*/
(ptr=strchr(header->
url,'
/'
)))
strcpy(header->
path,"
/"
/*getthepath*/
else{
path,ptr);
/*getthehostname*/
length=strlen(header->
url);
for(i=0;
i<
length;
i++){
if(header->
url[i]=='
)
break;
:
'
header->
hostname[i]=header->
url[i];
hostname[i]='
/*getportnumber*/
if(header->
){
i++;
url+i);
for(j=0;
j++,i++){
port[j]=header->
port[j]='
serverPort=atoi(port);
*servethesingleclient
void*doit(void*vargp)
rio_tclientRio,serverRio;
intconnfd=*(int*)vargp;
charbuf[MAXLINE],recBuf[MAX_OBJECT_SIZE];
requestHeaderheader;
intconnfd2server;
structcacheSet*set;
intlenSum=0;
free(vargp);
tid=pthread_self();
if((pthread_detach(tid))!
failtodetachthread:
%d\n"
(int)tid);
close(connfd);
pthread_exit(NULL);
/*readthefirstlineofheaderintherequest*/
rio_readinitb(&
clientRio,connfd);
if(rio_readlineb(&
clientRio,buf,MAXLINE)<
0){
failtoreadthefirstlinefromtherequest!
dbg_printf("
buf=%s\n"
buf);
sscanf(buf,"
%s%s%s"
header.method,header.url,header.version);
dbg_printf("
method=%s,url=%s,version=%s\n"
/*onlysurpportGETmethod*/
if(strcmp(header.method,"
GET"
)){
wecannotforward%smethod!
header.method);
if(parse_uri(&
header,&
clientRio)<
failtoparsetheheader)!
serverPort=%d,hostname=%s\n"
header.serverPort,header.hostnam