//http.h #pragma once #include<stdio.h> #include<stdlib.h> #include<string.h> #include<sys/types.h> #include<sys/socket.h> #include<sys/sendfile.h> #include<sys/stat.h> #include<sys/epoll.h> #include<unistd.h> #include<fcntl.h> #include<netinet/in.h> #include<arpa/inet.h> #include<assert.h> #include<pthread.h> #include<errno.h> #define _DEF_PAGE_ "index.html" #define _SIZE_ 1024 typedef struct bf { char _buf[_SIZE_]; int _fd; int _cgi; char _method[_SIZE_/2]; char _path[_SIZE_]; union { char* _query_string; int _st_size; }; int _err; }bf_t,*bf_p; void printLog(const char* const str,const char* const fun,int line); void usage(const char* const proc); int startup(char* ip,int port); void response_err(bf_p bf,int eno); void echo_error(bf_p bf,int eno); int get_line(int sock,char* buf,int size); void clear_head(int sock); void execute_cgi(bf_p bf); void echo_html(bf_p bf); void accept_request(void* ptr); //http.c #include"http.h" void printLog(const char* const str,const char* const fun,int line) { printf("%s:%s:%d\n",str,fun,line); } void usage(const char* const proc) { assert(proc); printLog(proc,__FUNCTION__,__LINE__); } int startup(char* ip,int port) { assert(ip); int sock=socket(AF_INET,SOCK_STREAM,0); if(sock<0) { printLog(strerror(errno),__FUNCTION__,__LINE__); exit(1); } int opt=1; setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt)); struct sockaddr_in local; local.sin_family=AF_INET; local.sin_port=htons(port); if(strncmp(ip,"any",3)==0) { local.sin_addr.s_addr= INADDR_ANY; } else { local.sin_addr.s_addr=inet_addr(ip); } if(bind(sock,(struct sockaddr*)&local,sizeof(local))<0) { printLog(strerror(errno),__FUNCTION__,__LINE__); exit(1); } if(listen(sock,5)<0) { printLog(strerror(errno),__FUNCTION__,__LINE__); exit(1); } return sock; } void response_err(bf_p bf,int eno) { char state_line[_SIZE_]; char err_des[30];//error description memset(state_line,‘\0‘,sizeof(state_line)); memset(err_des,‘\0‘,sizeof(err_des)); switch(eno) { case 301: strcpy(err_des,"Removed"); break; case 404: strcpy(err_des,"Not Found"); break; case 403: strcpy(err_des,"Forbid"); break; case 500: strcpy(err_des,"Inner Error"); break; case 501: strcpy(err_des,"Not execute"); break; default: break; } sprintf(state_line,"HTTP/1.0 %d %s\r\n\r\n",eno,err_des); //printf("state_line:%s\n",state_line); strcpy(bf->_buf,state_line); } void echo_error(bf_p bf,int eno) { switch(eno) { case 301: response_err(bf,eno); break; case 404: response_err(bf,eno); break; case 403: response_err(bf,eno); break; case 500: response_err(bf,eno); break; case 501: response_err(bf,eno); break; default: break; } return; } int get_line(int sock,char* buf,int size) { assert(buf); int i=0; ssize_t _s=-1; char ch=‘\0‘; // printf("getLine"); while(i<size-1&&ch!=‘\n‘) { _s=recv(sock,&ch,1,0); if(_s) { if(ch==‘\r‘) { if((_s=recv(sock,&ch,1,MSG_PEEK))) { if(_s>0&&ch==‘\n‘) recv(sock,&ch,1,0); } } buf[i++]=ch; } else { buf[i++]=‘\n‘; break; } } // printf("endddddddddddd"); buf[i]=‘\0‘; return i; } void clear_head(int sock) { char buf[_SIZE_]; buf[0]=‘\0‘; ssize_t _s=1; while(_s>0&&strcmp(buf,"\n")!=0) { _s=get_line(sock,buf,sizeof(buf)); } } void execute_cgi(bf_p bf) { int content_len=0;//post method content-length ssize_t _s=-1; char buf[_SIZE_]; char method_env[30]; char query_env[_SIZE_]; char content_len_env[30]; memset(buf,‘\0‘,sizeof(buf)); memset(method_env,‘\0‘,sizeof(method_env)); memset(query_env,‘\0‘,sizeof(query_env)); memset(content_len_env,‘\0‘,sizeof(content_len_env)); //if method=get,query_string not null,if method=post,query_string is null sprintf(method_env,"REQUEST_METHOD=%s",bf->_method); putenv(method_env); //printf("method:%s,path:%s,query_string:%s\n",method,path,query_string); if(strcasecmp(bf->_method,"GET")==0) { sprintf(query_env,"QUERY_STRING=%s",bf->_query_string); putenv(query_env); } else { while((_s=get_line(bf->_fd,buf,sizeof(buf)))>1) { if(strncasecmp(buf,"Content-Length:",15)==0) { //printf("length::::::::::%s\n",buf); content_len=atoi(buf+15); // break;//not break!!!!!!!!!!!!!!! } } //printf("Content-Length:%d\n",content_len); sprintf(content_len_env,"CONTENT_LENGTH=%d",content_len); putenv(content_len_env); } //printf("ready env\n"); //ready pid_t id; int in_fds[2]; int out_fds[2]; if(pipe(in_fds)<0) { printLog(strerror(errno),__FUNCTION__,__LINE__); exit(1); } if(pipe(out_fds)<0) { printLog(strerror(errno),__FUNCTION__,__LINE__); exit(1); } // printf("exxxxxxxxxxxxxxx"); if((id=fork())<0) { printLog(strerror(errno),__FUNCTION__,__LINE__); exit(1); } else if(id==0)//child { close(in_fds[1]); close(out_fds[0]); dup2(in_fds[0],0); dup2(out_fds[1],1); execl(bf->_path,bf->_path,NULL); close(in_fds[0]); close(out_fds[1]); } else//father { close(in_fds[0]); close(out_fds[1]); char vals[1024]; memset(vals,‘\0‘,sizeof(vals)); ssize_t _s=-1; int index=0; char ch=‘0‘; if(strcasecmp(bf->_method,"POST")==0) { while(index<content_len&&read(bf->_fd,&ch,1)>0) { vals[index++]=ch; } } // printf("%s\n",vals); write(in_fds[1],vals,strlen(vals)); // printf("ffffffffffff"); memset(bf->_buf,‘\0‘,sizeof(bf->_buf)); char* status_line="HTTP/1.0 200 ok\r\n\r\n"; sprintf(bf->_buf,status_line,strlen(status_line)); // printf("response\n"); int i=strlen(bf->_buf); while(read(out_fds[0],&ch,1)>0) { bf->_buf[i++]=ch; } bf->_buf[i]=‘\0‘; close(in_fds[1]); close(out_fds[0]); waitpid(id); } } void echo_html(bf_p bf) { char* status_line="HTTP/1.0 200 ok\r\n\r\n"; sprintf(bf->_buf,status_line,strlen(status_line)); //printf("%s!!!!!!!!!!!!!!!!\n",bf->_buf); } void accept_request(void* ptr) { bf_p bf=(bf_p)ptr; bf->_err=0; int ret=-1; int i=0,j=0; char url[_SIZE_]; memset(bf->_method,‘\0‘,sizeof(bf->_method)); memset(bf->_buf,‘\0‘,sizeof(bf->_buf)); memset(bf->_path,‘\0‘,sizeof(bf->_path)); memset(url,‘\0‘,sizeof(url)); if(get_line(bf->_fd,bf->_buf,sizeof(bf->_buf))==0) { printLog("errno",__FUNCTION__,__LINE__); return; } i=j=0; while(‘\0‘!=bf->_buf[i]&&!isspace(bf->_buf[i])&&i<strlen(bf->_buf)&&j<sizeof(bf->_method)-1) { bf->_method[j]=bf->_buf[i];//get method ++i; ++j; } bf->_method[j]=‘\0‘; //printf("method:%s\n",bf->_method); j=0; while(‘\0‘!=bf->_buf[i]&&isspace(bf->_buf[i])) { ++i; } while(‘\0‘!=bf->_buf[i]&&!isspace(bf->_buf[i])&&i<strlen(bf->_buf)&&j<sizeof(url)-1) { url[j]=bf->_buf[i]; ++j; ++i; } url[j]=‘\0‘; //printf("url:%s\n",url); bf->_cgi=0; if(strcasecmp(bf->_method,"POST")!=0&&strcasecmp(bf->_method,"GET")!=0) { echo_error(bf,500); bf->_err=1; //return; } bf->_query_string=NULL; if(strcasecmp(bf->_method,"POST")==0) { bf->_cgi=1; bf->_query_string=NULL; } if(strcasecmp(bf->_method,"GET")==0) { bf->_query_string=url; while(*bf->_query_string!=‘\0‘&&*bf->_query_string!=‘?‘) { ++bf->_query_string; } //printf("query_string::::::::::::::::\n"); if(*bf->_query_string==‘?‘) { *bf->_query_string=‘\0‘; bf->_cgi=1; ++bf->_query_string; } } sprintf(bf->_path,"htdocs%s",url); struct stat st; if(stat(bf->_path,&st)<0)//not exist { echo_error(bf,404); bf->_err=1; //return; } else if(S_IFDIR&st.st_mode)//dir { if(strcmp(bf->_path,"htdocs/")!=0) strcpy(bf->_path,"htdocs/"); strcat(bf->_path,_DEF_PAGE_); } else if((st.st_mode&S_IXUSR)||(st.st_mode&S_IXGRP)||(st.st_mode&S_IXOTH)) { bf->_cgi=1; } // printf("%d:%s\n",bf->_cgi,bf->_path); if(bf->_err==0) { if(bf->_cgi) { execute_cgi(bf); } else { clear_head(bf->_fd); bf->_st_size=st.st_size; echo_html(bf); } } } //cgi_math.h #include<iostream> #include<string> #include<string.h> #include<stdlib.h> #include<errno.h> #include<vector> #include<assert.h> using namespace std; #define INT_64 long long #define UN_INIT 0xcccccccccccccccc//不能分开 #define MIN_INT (unsigned)0xffffffff #define MAX_INT (unsigned)0x7fffffff class BigData { public: BigData() :_data(0) ,_pdata("") {} BigData(INT_64 data) :_data(data) { int tmp=_data; char symbol=‘+‘; if(tmp<0) { symbol=‘-‘; tmp=0-tmp; } _pdata.append(1,symbol); while(tmp) { _pdata.append(1,tmp%10+‘0‘); tmp/=10; } char* str=(char*)_pdata.c_str()+1; while(*str==‘0‘) { ++str; } char* left=str; char* right=(char*)(_pdata.c_str()+_pdata.size()-1); char temp; while(left<right) { temp=*left; *left=*right; *right=temp; ++left; --right; } } BigData(const char* str) { assert(str); //cout<<str<<endl; char* src=const_cast<char*>(str); INT_64 ret=0; char symbol=‘+‘; if(*src==‘+‘||*src==‘-‘) symbol=*src++; _pdata.append(1,symbol); while(‘0‘==*src) ++src; while(*src>=‘0‘&&*src<=‘9‘) { if (ret <= MAX_INT) ret = ret * 10 + *src-‘0‘; _pdata.append(1,*src); ++src; } // cout<<_pdata<<endl; if(symbol==‘-‘) ret=0-ret; _data=ret; } BigData operator+(BigData db) { if(!IsINT64OverFlow()&&!db.IsINT64OverFlow()) { if(_pdata[0]!=db._pdata[0]) return BigData(_data+db._data); else { if(_pdata[0]==‘+‘&&MAX_INT-_data>=db._data||_pdata[0]==‘-‘&&_data>=MIN_INT-db._data) return BigData(_data+db._data); } } BigData tmp(*this); return BigData(Add(tmp._pdata,db._pdata).c_str()); } BigData operator-(BigData db) { if(!IsINT64OverFlow()&&!db.IsINT64OverFlow()) { if(_pdata[0]==db._pdata[0]) return BigData(_data-db._data); else { if(_pdata[0]==‘+‘&&MAX_INT+db._data>=_data||_pdata[0]==‘-‘&&_data>=MIN_INT+db._data) return BigData(_data-db._data); } } string ret; BigData tmp(*this); //cout<<tmp; if(_pdata[0]==db._pdata[0]) ret=Sub(tmp._pdata,db._pdata); else { ret=Add(tmp._pdata,db._pdata); ret[0]=_pdata[0]; } return BigData(ret.c_str()); } BigData operator*(BigData db) { if(!IsINT64OverFlow()&&!db.IsINT64OverFlow()) { if(_pdata[0]==db._pdata[0]) { if(_pdata[0]==‘+‘&&_data<=MAX_INT/db._data||_pdata[0]==‘-‘&&_data>=MAX_INT/db._data) return BigData(_data*db._data); } else { if(_pdata[0]==‘+‘&&MIN_INT/_data<=db._data||_pdata[0]==‘-‘&&_data>=MIN_INT/db._data) return BigData(_data*db._data); } } BigData tmp(*this); return BigData(Mul(tmp._pdata,db._pdata).c_str()); } BigData operator/(BigData db) { if(_data==0||db._data==0) return BigData((INT_64)0); if(!IsINT64OverFlow()&&!db.IsINT64OverFlow()) { return BigData(_data/db._data); } BigData tmp(*this); return BigData(Div(tmp._pdata,db._pdata).c_str()); } friend ostream& operator<<(ostream& out,BigData& db) { if(!db.IsINT64OverFlow()) { out<<db._data; return out; } if(db._pdata[0]==‘-‘) out<<&db._pdata[0]; char* str=(char*)db._pdata.c_str()+1; while(*str==‘0‘) ++str; out<<str; return out; } protected: bool IsINT64OverFlow() { if(_data<=MAX_INT&&_data>=MIN_INT) return false; return true; } string Add(string& s1,string& s2) { int leftSize=s1.size(); int rightSize=s2.size(); char symbol=‘+‘; string ret; if(s1[0]==s2[0]) { if(s1[0]==‘-‘) symbol=‘-‘; } else { if(s1[0]==‘+‘&&strcmp(s1.c_str()+1,s2.c_str()+1)<0||s1[0]==‘-‘&&strcmp(s1.c_str()+1,s2.c_str()+1)>0) { symbol=‘-‘; } } if(leftSize<rightSize) { swap(s1,s2); swap(leftSize,rightSize); } ret.resize(leftSize+1); memset((void*)ret.c_str(),‘0‘,ret.size()*sizeof(char)); ret[0]=symbol; char cret,cstep=0; for(int dx=1;dx<leftSize;++dx) { cret=s1[leftSize-dx]-‘0‘+cstep; if(dx<rightSize) cret+=(s2[rightSize-dx]-‘0‘); cstep=cret/10; ret[leftSize-dx+1]=cret%10+‘0‘; } ret[1]=cstep+‘0‘; return ret; } string Sub(string& s1,string& s2) { int leftSize=s1.size(); int rightSize=s2.size(); char symbol=s1[0]; if(leftSize<rightSize||leftSize==rightSize&&strcmp(s1.c_str()+1,s2.c_str()+1)<0) { swap(leftSize,rightSize); swap(s1,s2); if(symbol=‘+‘) symbol=‘-‘; else symbol=‘+‘; } //cout<<s1<<s2<<endl; string ret; ret.resize(leftSize); memset((void*)ret.c_str(),‘0‘,ret.size()*sizeof(char)); ret[0]=symbol; char cret; //cout<<ret<<endl; //cout<<s1<<s2<<endl; for(int dx=1;dx<leftSize;++dx) { cret=s1[leftSize-dx]-‘0‘; if(dx<rightSize) { cret-=(s2[rightSize-dx]-‘0‘); } if(cret<0) { s1[leftSize-dx-1]-=1; cret+=10; } // cout<<cret+‘0‘; ret[leftSize-dx]=cret+‘0‘; } return ret; } string Mul(string& s1,string& s2) { int leftSize=s1.size(); int rightSize=s2.size(); if(leftSize<rightSize) { swap(leftSize,rightSize); swap(s1,s2); } string ret; ret.resize(leftSize+rightSize-1); memset((void*)ret.c_str(),‘0‘,ret.size()*sizeof(char)); char symbol=‘+‘; if(s1[0]!=s2[0]) symbol=‘-‘; ret[0]=symbol; int dataLen=ret.size(); int offSet=0; for(int dx=1;dx<rightSize;++dx) { char c1=s2[rightSize-dx]-‘0‘; char cstep=0; if(c1==0) { ++offSet; continue; } for(int ldx=1;ldx<leftSize;++ldx) { char cret=c1*(s1[leftSize-ldx]-‘0‘); cret+=(cstep+ret[dataLen-ldx-offSet]-‘0‘); ret[dataLen-ldx-offSet]=cret%10+‘0‘; cstep=cret/10; } ret[dataLen-offSet-leftSize]=cstep+‘0‘; ++offSet; } // cout<<ret.c_str(); return ret; } string Div(string& s1,string& s2) { int leftSize=s1.size(); int rightSize=s2.size(); char symbol=‘+‘; if(s1[0]!=s2[0]) symbol=‘-‘; if(leftSize<rightSize||leftSize==rightSize&&strcmp(s1.c_str()+1,s2.c_str()+1)<0) { return "0"; } else if(s2=="1"||s2=="-1") { s1[0]=symbol; return s1; } char* left=(char*)s1.c_str()+1; char* right=(char*)s2.c_str()+1; int dataLen=rightSize-1; string ret; ret[0]=symbol; // cout<<left<<right<<endl; for(int dx=0;dx<leftSize-rightSize+1;++dx) { if(!leftStrRight(left,dataLen,right,rightSize-1)) { ++dataLen; ret.append(1,‘0‘); continue; } else { ret.append(1,subLoop(left,dataLen,right,rightSize-1)); dataLen+=1; } } //cout<<ret.c_str()<<"RRRR"; return ret; } char subLoop(char*& left,int& leftSize,char* right,int rightSize) { assert(left&&right); char cret=‘0‘; while(1) { if (!leftStrRight(left, leftSize, right, rightSize)) break; int lLen=leftSize-1; int rLen=rightSize-1; while(lLen>=0&&rLen>=0) { if(left[lLen]<right[rLen]) { if(lLen<1) break; left[lLen-1]-=1; left[lLen]+=10; } left[lLen]-=right[rLen]; left[lLen]+=‘0‘; --lLen; --rLen; } while(*left==‘0‘) { ++left; --leftSize; } cret++; } return cret; } bool leftStrRight(char* left,int leftLen,char* right,int rightLen) { assert(left&&right); if(leftLen>rightLen||leftLen==rightLen&&strcmp(left,right)>0) return true; return false; } private: INT_64 _data; string _pdata; }; void analy_query(char * query_string,vector<string>& v); void obtain_query_string(string& query_string); //cgi_math.cpp #include"cgi_math.h" void analy_query(char * query_string,vector<string>& v) { int i=strlen(query_string); for(;i>0;--i)//obtain { if(query_string[i-1]==‘=‘) { v.push_back(&query_string[i]);//i-1+1 } else if(query_string[i-1]==‘&‘) { query_string[i-1]=‘\0‘; } } } void obtain_query_string(string& query_string) { char method[1024]; char buf[1024]; memset(buf,‘\0‘,sizeof(buf)); int content_length=0; if(getenv("REQUEST_METHOD")) { strcpy(method,getenv("REQUEST_METHOD")); } else { cout<<strerror(errno)<<endl; return; } if(strcasecmp(method,"GET")==0) { query_string+=getenv("QUERY_STRING"); } else//POST { ssize_t _s=-1; if((_s=read(0,buf,sizeof(buf)))>0) { buf[_s]=‘\0‘; query_string+=buf; } } //cout<<query_string; //num1=100&num2=200 } //add.cpp #include"cgi_math.h" int main() { string query_string; obtain_query_string(query_string); vector<string> v; analy_query((char*)(query_string.c_str()),v); BigData b1(v[1].c_str()); BigData b2(v[0].c_str()); BigData ret=b1+b2; cout<<b1<<"+"<<b2<<"="<<ret<<endl; } //sub.cpp #include"cgi_math.h" int main() { string query_string; obtain_query_string(query_string); vector<string> v; analy_query((char*)query_string.c_str(),v); BigData b1(v[1].c_str()); BigData b2(v[0].c_str()); BigData ret=b1-b2; cout<<b1<<"-"<<b2<<"="<<ret<<endl; } //mul.cpp #include"cgi_math.h" int main() { string query_string; obtain_query_string(query_string); vector<string> v; analy_query((char*)query_string.c_str(),v); BigData b1(v[1].c_str()); BigData b2(v[0].c_str()); //BigData b1("1234"); //BigData b2("2344"); BigData ret=b1*b2; cout<<b1<<"*"<<b2<<"="<<ret<<endl; } //div.cpp #include"cgi_math.h" int main() { string query_string; obtain_query_string(query_string); vector<string> v; analy_query((char*)query_string.c_str(),v); BigData b1(v[1].c_str()); BigData b2(v[0].c_str()); //BigData b1("1234"); //BigData b2("12"); BigData ret=b1/b2; cout<<b1<<"/"<<b2<<"="<<ret<<endl; } //remain.cpp #include"cgi_math.h" int main() { string query_string; obtain_query_string(query_string); vector<string> v; analy_query((char*)query_string.c_str(),v); BigData num1(v[1].c_str()); BigData num2(v[0].c_str()); //BigData num1(1234); //BigData num2(12); BigData num4=num1/num2; //cout<<num4<<endl; BigData num5=num4*num2; //cout<<num5<<endl; BigData num6=num1-num5; //cout<<num3<<num5; cout<<num1<<"%"<<num2<<"="<<num6<<endl; } //Makefile .PHONY:all all:cgi_add cgi_sub cgi_mul cgi_div cgi_remain cgi_add:cgi_add.cpp cgi_math.cpp g++ -o [email protected] $^ cgi_sub:cgi_sub.cpp cgi_math.cpp g++ -o [email protected] $^ cgi_mul:cgi_mul.cpp cgi_math.cpp g++ -o [email protected] $^ cgi_div:cgi_div.cpp cgi_math.cpp g++ -o [email protected] $^ cgi_remain:cgi_remain.cpp cgi_math.cpp g++ -o [email protected] $^ .PHONY:clean clean: rm -f cgi_add cgi_sub cgi_mul cgi_div cgi_remain //总的Makefile ROOT_PATH=$(shell pwd) LDFLAGS=-lpthread FLAGS=#_DEBUG1_ CC=gcc BIN=httpd CGI_BIN=htdocs/cgi_bin SRC=$(shell ls *.c) OBJ=$(SRC:.c=.o) $(BIN):$(OBJ) @echo "Linking [$^] to [[email protected]]" @$(CC) -o [email protected] $^ $(LDFLAGS) @echo "Linking done..." %.o:%.c @echo "Compling [$<] to [[email protected]]" @$(CC) -c $< @echo "Compling done..." .PHONY:clean clean: @rm -rf *.o $(BIN) output @for name in `echo $(CGI_BIN)`; do cd $$name; make clean; cd -; done .PHONY:cgi cgi: @for name in `echo $(CGI_BIN)`; do cd $$name; make; cd -; done .PHONY:output output:$(BIN) cgi @mkdir -p output/log @cp -rf htdocs output @mkdir -p output/htdocs/cgi_bin @cp -f httpd output/ @cp -f start.sh output/ @cp -rf conf output @for name in `echo $(CGI_BIN)`; do cd $$name; make output; cd -; done //main.c #include"http.h" int main(int argc,char* argv[]) { if(argc!=3) { usage(argv[0]); return 1; } char* ip=argv[1]; int port=atoi(argv[2]); int listen_sock=startup(ip,port); struct sockaddr_in client; socklen_t len=sizeof(client); int epo_fd=epoll_create(256); if(epo_fd<0)//success:not 0 fd/error:-1 { printLog(strerror(errno),__FUNCTION__,__LINE__); return -1; } bf_p fd_bf=(bf_p)malloc(sizeof(bf_t)); memset(fd_bf->_buf,‘\0‘,sizeof(fd_bf->_buf)); fd_bf->_fd=listen_sock; struct epoll_event ev; ev.events=EPOLLIN; ev.data.fd=listen_sock; if(epoll_ctl(epo_fd,EPOLL_CTL_ADD,listen_sock,&ev)<0)//success:0 fail:-1 { printLog(strerror(errno),__FUNCTION__,__LINE__); return -1; } struct epoll_event evfds[_SIZE_];//_SIZE_ 1024 int _timeout=5000; int ret=-1; int i=0; while(1) { switch((ret=epoll_wait(epo_fd,evfds,_SIZE_,_timeout))) { case -1://error printLog(strerror(errno),__FUNCTION__,__LINE__); break; case 0://time out printf("time out...\n"); break; default://normal { for(i=0;i<ret;++i) { if(evfds[i].data.fd==listen_sock&&evfds[i].events&EPOLLIN) { int new_sock=accept(listen_sock,(struct sockaddr*)&client,&len); if(new_sock<0) { printLog(strerror(errno),__FUNCTION__,__LINE__); continue; } bf_p _bf=(bf_p)malloc(sizeof( bf_t)); memset(_bf->_buf,‘\0‘,sizeof(_bf->_buf)); _bf->_fd=new_sock; ev.events=EPOLLIN; ev.data.ptr=_bf; epoll_ctl(epo_fd,EPOLL_CTL_ADD,new_sock,&ev); } else if(((bf_p)(evfds[i].data.ptr))->_fd>0&&evfds[i].events&EPOLLIN) { accept_request(evfds[i].data.ptr); ev.events=EPOLLOUT; ev.data.ptr=evfds[i].data.ptr; epoll_ctl(epo_fd,EPOLL_CTL_MOD,((bf_p)(evfds[i].data.ptr))->_fd,&ev); } else if(((bf_p)(evfds[i].data.ptr))->_fd>0&&evfds[i].events&EPOLLOUT) { bf_p _bf=(bf_p)evfds[i].data.ptr; if(_bf->_err) { send(_bf->_fd,_bf->_buf,strlen(_bf->_buf),0); } else if(_bf->_cgi)//cgi=1 { send(_bf->_fd,_bf->_buf,strlen(_bf->_buf),0); } else { char* path=_bf->_path; int fd=open(path,O_RDONLY); if(fd<0) { printLog(strerror(errno),__FUNCTION__,__LINE__); exit(1); } send(_bf->_fd,_bf->_buf,strlen(_bf->_buf),0); if(sendfile(_bf->_fd,fd,NULL,_bf->_st_size)<0) { printf("error"); } close(fd); } epoll_ctl(epo_fd,EPOLL_CTL_DEL,_bf->_fd,NULL); close(_bf->_fd); free(_bf); } } break; } } } return 0; }
时间: 2024-10-25 20:52:45