- 論壇徽章:
- 0
|
想用c寫一個(gè)Httpserver,大家給點(diǎn)思路。!
早些年寫的,供參考。
- #include <system.h>;
- #include <stdio.h>;
- #include <string.h>;
- #include <stdlib.h>;
- #include <sys/stat.h>;
- #ifndef WINDOW_SYS
- #include <unistd.h>;
- #include <sys/socket.h>;
- #include <netinet/in.h>;
- #include <arpa/inet.h>;
- #include <sys/select.h>;
- #include <sys/times.h>;
- #include <sys/time.h>;
- #include <strings.h>;
- #include <signal.h>;
- #endif
- #include <asyncapi.h>;
- #include <tbase.h>;
- #include <paramgr.h>;
- #include <cini.h>;
- char * Gen_Head[] =
- {"Cache-Control", // Section 14.9
- "Connection", // Section 14.10
- "Date", // Section 14.19
- "Pragma", // Section 14.32
- "Transfer-Encoding", // Section 14.40
- "Upgrade", // Section 14.41
- "Via"}; // Section 14.44
- #define Gen_Head_Num 7
- int GetGen_HeadType(char * type)
- {
- int id;
- for(id=0;id<Gen_Head_Num;id++)
- if(!strcmp(Gen_Head[id],type))
- return id;
- return -1;
- }
- char * Req_Method[] =
- {"OPTIONS", // Section 9.2
- "GET", // Section 9.3
- "HEAD", // Section 9.4
- "POST", // Section 9.5
- "PUT", // Section 9.6
- "DELETE", // Section 9.7
- "TRACE"}; // Section 9.8
- #define Req_Method_Num 7
- int GetReq_MethodType(char * type)
- {
- int id;
- for(id=0;id<Req_Method_Num;id++)
- if(!strcmp(Req_Method[id],type))
- return id;
- return -1;
- }
- char * Req_Head[] =
- {"Accept", // Section 14.1
- "Accept-Charset", // Section 14.2
- "Accept-Encoding", // Section 14.3
- "Accept-Language", // Section 14.4
- "Authorization", // Section 14.8
- "From", // Section 14.22
- "Host", // Section 14.23
- "If-Modified-Since", // Section 14.24
- "If-Match", // Section 14.25
- "If-None-Match", // Section 14.26
- "If-Range", // Section 14.27
- "If-Unmodified-Since", // Section 14.28
- "Max-Forwards", // Section 14.31
- "Proxy-Authorization", // Section 14.34
- "Range", // Section 14.36
- "Referer", // Section 14.37
- "User-Agent"}; // Section 14.42
- #define Req_Head_Num 17
- int GetReq_HeadType(char * type)
- {
- int id;
- for(id=0;id<Req_Head_Num;id++)
- if(!strcmp(Req_Head[id],type))
- return id;
- return -1;
- }
- char * Res_Head[] =
- {"Age", // Section 14.6
- "Location", // Section 14.30
- "Proxy-Authenticate", // Section 14.33
- "Public", // Section 14.35
- "Retry-After", // Section 14.38
- "Server", // Section 14.39
- "Vary", // Section 14.43
- "Warning", // Section 14.45
- "WWW-Authenticate"}; // Section 14.46
- #define Res_Head_Num 17
- int GetRes_HeadType(char * type)
- {
- int id;
- for(id=0;id<Res_Head_Num;id++)
- if(!strcmp(Res_Head[id],type))
- return id;
- return -1;
- }
- char * Res_Entity[] =
- {"Allow", // Section 14.7
- "Content-Base", // Section 14.11
- "Content-Encoding", // Section 14.12
- "Content-Language", // Section 14.13
- "Content-Length", // Section 14.14
- "Content-Location", // Section 14.15
- "Content-MD5", // Section 14.16
- "Content-Range", // Section 14.17
- "Content-Type", // Section 14.18
- "ETag", // Section 14.20
- "Expires", // Section 14.21
- "Last-Modified"}; // Section 14.29
- #define Res_Entity_Num 12
- int GetRes_EntityType(char * type)
- {
- int id;
- for(id=0;id<Res_Entity_Num;id++)
- if(!strcmp(Res_Entity[id],type))
- return id;
- return -1;
- }
- typedef struct
- {
- int Code;
- char Msg[40];
- }StatusCodeStru;
- StatusCodeStru StatusCode[]=
- {{100,"Continue"},
- {101,"Switching Protocols"},
- {200,"OK"}, /* 成功 */
- {201,"Created"}, /* 成功 新的資源建立(POST) */
- {202,"Accepted"}, /* 請(qǐng)求被接受,但處理未完成 */
- {203,"Non-Authoritative Information"},
- {204,"No Content"}, /* 成功,但無(wú)信息返回 */
- {205,"Reset Content"},
- {206,"Partial Content"},
- {300,"Multiple Choices"},
- {301,"Moved Permanently"}, /* 所請(qǐng)求的資源已經(jīng)被指派為新的固定 URL */
- {302,"Moved Temporarily"},
- {303,"See Other"}, /* 文檔沒(méi)有修改 (GET) */
- {304,"Not Modified"},
- {305,"Use Proxy"},
- {400,"Bad Request"}, /* 錯(cuò)誤的請(qǐng)求 */
- {401,"Unauthorized"}, /* 未被授權(quán),該請(qǐng)求要求用戶認(rèn)證 */
- {402,"Payment Required"},
- {403,"Forbidden"}, /* 不明原因的禁止 */
- {404,"Not Found"}, /* 沒(méi)有找到 */
- {405,"Method Not Allowed"},
- {406,"Not Acceptable"},
- {407,"Proxy Authentication Required"},
- {408,"Request Time-out"},
- {409,"Conflict"},
- {410,"Gone"},
- {411,"Length Required"},
- {412,"Precondition Failed"},
- {413,"Request Entity Too Large"},
- {414,"Request-URI Too Large"},
- {415,"Unsupported Media Type"},
- {500,"Internal Server Error"}, /* 內(nèi)部服務(wù)器差錯(cuò) */
- {501,"Not Implemented"}, /* 沒(méi)有實(shí)現(xiàn) */
- {502,"Bad Gateway"}, /* 錯(cuò)誤的網(wǎng)關(guān) */
- {503,"Service Unavailable"}, /* 服務(wù)暫時(shí)失效 */
- {504,"Gateway Time-out"},
- {505,"HTTP Version not supported"}};
- //定義文件類型配對(duì)信息
- typedef struct
- {
- char Ext[8];
- char Type[32];
- }FileTypeStru;
- FileTypeStru File_Type[]={
- {"","*/*"},
- {"htm","text/html"},
- {"txt","text/text"},
- {"html","text/html"},
- {"gif","image/gif"},
- {"jpg","image/jpg"},
- {"png","image/png"},
- };
- #define File_Type_Num 7
- char * GetFile_Type(char * ext)
- {
- int id;
- for(id=0;id<File_Type_Num;id++)
- if(!strcmp(File_Type[id].Ext,ext))
- return File_Type[id].Type;
- return File_Type[0].Type;
- }
- /*定義配置信息*/
- struct HttpSetStru
- {
- /* 存放信息的路徑 */
- char DocPath[128];
- /* 基礎(chǔ)路徑 */
- char BasePath[128];
- /* 顯示的默認(rèn)文檔 */
- char DefaultDoc[128];
- /* 用于存放臨時(shí)文件的路徑 */
- char TmpPath[128];
- /* 用于存放CGI程序的路徑 */
- char CgiPath[128];
- /* 服務(wù)端口編號(hào) */
- short Port;
- /* 主機(jī)名稱 */
- char HostName[128];
- }HttpSet;
- /*
- 函數(shù)功能:判斷該路徑是否CGI路徑
- 傳入?yún)?shù):
- path: 要判斷的路徑
- 返回參數(shù):
- 1:是CGI路徑
- 0:不是CGI路徑
- */
- int IsCgiPath(char * path)
- {
- if(!strcmp(path,HttpSet.CgiPath)) return 1;
- return 0;
- }
- /*定義HTTP請(qǐng)求頭結(jié)構(gòu)*/
- class TRequest_HTTP
- {
- public:
- /* 客戶端的連接 */
- int mClientSocket;
- /* 客戶端的 IP 地址 */
- char mClientIp[30];
- /* 存儲(chǔ)的信息 */
- char mLine[5120];
- /* 存儲(chǔ)的字節(jié)數(shù) */
- char mBytes;
- /* 請(qǐng)求類型 */
- char mType[30];
- /* 請(qǐng)求的 URI */
- char mUri[300];
- /* 版本標(biāo)識(shí) */
- char mVersion[200];
- /* 存放請(qǐng)求頭傳入的參數(shù) */
- TParaMgr mReqHead;
- /* 存放返回的參數(shù) */
- TParaMgr mRetHead;
- /* 狀態(tài)編碼 */
- int mStatusCode;
- /* 錯(cuò)誤編碼 */
- int mErrorCode;
- private:
- /* 是不是第一行 */
- int mIsFirstLine;
- /* 文件名稱 */
- char mFileName[200];
- /* 路徑名稱 */
- char mPathName[200];
- public:
- /* 構(gòu)造函數(shù) */
- TRequest_HTTP();
- /* 返回信息給客戶端 */
- void RetToClient();
- /* 添加信息 */
- void BrowseDataCome(char * line,int bytes);
- /* 分析每個(gè)實(shí)體域 */
- int AnalysisHeadLine(char * line);
- /* 分析請(qǐng)求的信息 */
- int AnalysisRequestLine(char * line);
- /* 分析URI */
- void AnalysisUri(char * uri);
- /* 設(shè)置狀態(tài)碼及信息 */
- void SetStatusCode(int code,char * msg);
- /* 設(shè)置客戶端的信 息 */
- void SetClientInfo(int socket,char * ip);
- /* 壓縮生成返回的頭 */
- char * PackRetHead(int * size);
- /* 處理 GET 的請(qǐng)求句柄 */
- void Process_Get();
- /* 處理請(qǐng)求的句柄 */
- void Process_Request();
- };
- class T_Http
- {
- public:
- TMemList * mSiteLst;
- public:
- T_Http();
- ~T_Http();
- TRequest_HTTP * GetSite(int socketid);
- int AddSite(int socketid,char * address);
- int DeleteSite(int socketid);
- int Disconnect_Handle(int sock,char * ip);
- int DataArrived_Handle(int sock,char * buf,int length);
- int ConnectIn_Handle(int sock,char * ip,int servsocket);
- int LoadHttpSet();
- void ShowHttpSet();
- };
- T_Http IHttp;
- /*******************************************************************
- * 下面是T_Http類的成員函數(shù)
- *******************************************************************/
- T_Http::T_Http()
- {
- mSiteLst=new TMemList();
- };
- T_Http::~T_Http()
- {
- TRequest_HTTP * obj;
- while(mSiteLst->;GoHead())
- {
- obj = (TRequest_HTTP * )mSiteLst->;GetPtr();
- mSiteLst->;Remove(obj);
- delete obj;
- };
- delete mSiteLst;
- };
- /*
- 函數(shù)功能:獲取1個(gè)存儲(chǔ)信息的數(shù)組編號(hào),如果目前不存在,則把它加進(jìn)去,當(dāng)然不應(yīng)當(dāng)超過(guò)限制
- 傳入?yún)?shù):
- 返回參數(shù):
- */
- TRequest_HTTP * T_Http::GetSite(int socketid)
- {
- TRequest_HTTP * obj;
- mSiteLst->;GoHead();
- while(1)
- {
- obj = (TRequest_HTTP * )mSiteLst->;GetPtr();
- if(obj==NULL) break;
- if(obj->;mClientSocket==socketid)
- return obj;
- if(!mSiteLst->;Next())
- break;
- };
- return NULL;
- };
- int T_Http::AddSite(int socketid,char * address)
- {
- TRequest_HTTP * obj;
- obj=new TRequest_HTTP();
- obj->;mClientSocket=socketid;
- strcpy(obj->;mClientIp,address);
- mSiteLst->;AddTail(obj);
- return 1;
- };
- /*
- 函數(shù)功能:刪除一個(gè)連接
- 傳入?yún)?shù):
- 返回參數(shù):
- */
- int T_Http::DeleteSite(int socketid)
- {
- TRequest_HTTP * obj;
- obj=GetSite(socketid);
- if(obj==NULL)
- return 0;
- mSiteLst->;Remove(obj);
- delete obj;
- return 1;
- };
- int T_Http::Disconnect_Handle(int sock,char * ip)
- {
- return DeleteSite(sock);
- };
- int T_Http::DataArrived_Handle(int sock,char * buf,int length)
- {
- TRequest_HTTP * obj;
- obj=GetSite(sock);
- if(obj==NULL)
- return 0;
- obj->;BrowseDataCome(buf,length);
- return 1;
- };
- int T_Http::ConnectIn_Handle(int sock,char * ip,int servsocket)
- {
- return AddSite(sock,ip);
- };
- /*
- 函數(shù)功能:提取webserver的配置參數(shù)
- 傳入?yún)?shù):
- 返回參數(shù):
- 1:成功
- 0:失敗
- [system]
- port = 88
- docpath = doc
- basepath = /bill1/goddess/goddess/http
- tmppath = tmp
- cgipath = cgi
- defaultdoc = index.html
- */
- int T_Http::LoadHttpSet()
- {
- int port;
- TIni ini;
- if(!ini.LoadFile("webserver.ini"))
- return 0;
- sscanf(ini.GetKey("system","port"),"%d",&port);
- HttpSet.Port=(short)port;
- strcpy(HttpSet.DocPath,ini.GetKey("system","docpath"));
- strcpy(HttpSet.BasePath,ini.GetKey("system","basepath"));
- strcpy(HttpSet.DefaultDoc,ini.GetKey("system","defaultdoc"));
- strcpy(HttpSet.TmpPath,ini.GetKey("system","tmppath"));
- strcpy(HttpSet.CgiPath,ini.GetKey("system","cgipath"));
- return 1;
- }
- /*
- 函數(shù)功能:顯示webserver的配置參數(shù)
- 傳入?yún)?shù):
- 返回參數(shù):
- */
- void T_Http::ShowHttpSet()
- {
- printf("PORT: %d\n",HttpSet.Port);
- printf("DOC PATH:%s\n",HttpSet.DocPath);
- printf("BASE PATH:%s\n",HttpSet.BasePath);
- printf("DEFAULT DOC:%s\n",HttpSet.DefaultDoc);
- printf("TMP PATH:%s\n",HttpSet.TmpPath);
- printf("CGI PATH:%s\n",HttpSet.CgiPath);
- }
- /*******************************************************************
- * 下面是TRequest_HTTP類的成員函數(shù)
- *******************************************************************/
- /*
- 函數(shù)功能:構(gòu)造函數(shù)
- 傳入?yún)?shù):
- 返回參數(shù):
- */
- TRequest_HTTP::TRequest_HTTP()
- {
- mBytes = 0;
- mIsFirstLine = 1;
- mRetHead.SetValidCheck(0);
- }
- /*
- 函數(shù)功能:請(qǐng)求信息的處理
- 每一行都已0x0d0x0a結(jié)尾,連續(xù)的2個(gè)0x0d0xa表示客戶端請(qǐng)求數(shù)據(jù)完畢
- 傳入?yún)?shù):
- line: 到達(dá)的請(qǐng)求數(shù)據(jù)
- bytes:到達(dá)數(shù)據(jù)的字節(jié)數(shù)
- 返回參數(shù):
- */
- void TRequest_HTTP::BrowseDataCome(char * line,int bytes)
- {
- int id;
- for(id=0;id<bytes;id++)
- {
- //凡是0x0d都忽略掉
- if(line[id]==0x0d)
- continue;
- if(mBytes==0)
- {
- mLine[mBytes++] = line[id];
- continue;
- };
- //如果是0x0a則需要判斷是否是請(qǐng)求結(jié)束標(biāo)志
- if(line[id]==0x0a)
- {
- if(mLine[0]==0x0a)
- {
- //連續(xù)的2個(gè)0x0a,這里已經(jīng)是最后一行了
- Process_Request();
- //處理結(jié)束
- mBytes = 0;
- mIsFirstLine = 1;
- return;
- };
- //一行結(jié)束,進(jìn)行處理
- mLine[mBytes] = 0;
- if(mIsFirstLine)
- {
- //第一行中包含了請(qǐng)求類型
- AnalysisRequestLine(mLine);
- mIsFirstLine = 0;
- }
- else
- {
- //后面行中為其它信息
- AnalysisHeadLine(mLine);
- };
- mBytes=0;
- mLine[mBytes++] = line[id];
- continue;
- };
- if(mLine[0]==0x0a)
- mBytes=0;
- mLine[mBytes++] = line[id];
- };
- }
- /*
- 函數(shù)功能:設(shè)置客戶端的信 息
- 傳入?yún)?shù):
- 返回參數(shù):
- */
- void TRequest_HTTP::SetClientInfo(int socket,char * ip)
- {
- mClientSocket = socket;
- strcpy(mClientIp,ip);
- }
- /*
- 函數(shù)功能:設(shè)置狀態(tài)碼及信息
- 傳入?yún)?shù):
- 返回參數(shù):
- */
- void TRequest_HTTP::SetStatusCode(int code,char * msg)
- {
- mStatusCode = code;
- }
- /*
- 函數(shù)功能:解析URI
- 傳入?yún)?shù):
- uri: 要解析的文件名
- 返回參數(shù):
- */
- void TRequest_HTTP::AnalysisUri(char * uri)
- {
- char * pos;
- char tmpbuf[512];
- printf("uri is '%s'\n",uri);
- //pos=uri;
- /*
- pos = strstr(uri,HttpSet.HostName);
- if(pos==NULL)
- pos = uri;
- else
- {
- pos += strlen(HttpSet.HostName);
- sprintf(tmpbuf,"%s/%s",HttpSet.BasePath,pos);
- };
- pos = strstr(tmpbuf,HttpSet.CgiPath);
- if(pos!=NULL)
- {
- strcpy(mPathName,HttpSet.CgiPath);
- strcpy(mFileName,pos+strlen(HttpSet.CgiPath));
- return;
- };
- pos = strstr(tmpbuf,HttpSet.DocPath);
- if(pos!=NULL)
- {
- strcpy(mPathName,HttpSet.DocPath);
- strcpy(mFileName,pos+strlen(HttpSet.DocPath));
- return;
- };
- */
- if(*uri=='/')
- {
- strcpy(mPathName,HttpSet.DocPath);
- strcpy(mFileName,uri+1);
- };
- if(mFileName[0]==0)
- strcpy(mFileName,HttpSet.DefaultDoc);
- printf("path is '%s'\n",mPathName);
- printf("file is '%s'\n",mFileName);
- printf("------------------------------------\n");
- }
- /*
- 函數(shù)功能:分析每個(gè)實(shí)體域
- 傳入?yún)?shù):
- 返回參數(shù):
- */
- int TRequest_HTTP::AnalysisHeadLine(char * line)
- {
- char * pos;
- char type[201];
- int id=0;
- printf("head line: '%s'\n",line);
- pos = line;
- while(*pos==' '||*pos==9)
- pos++;
- while(*pos!=':'&&*pos!=0&&id<200)
- type[id++]=*pos++;
- type[id]=0;
- while(*pos==':')
- pos++;
- while(*pos==' ')
- pos++;
- if(GetGen_HeadType(type))
- {
- mReqHead.PushPara(type,pos);
- return 1;
- };
- if(GetReq_HeadType(type))
- mReqHead.PushPara(type,pos);
- else
- return 0;
- return 1;
- }
- /*
- 函數(shù)功能:分析請(qǐng)求的信息
- 傳入?yún)?shù):
- 返回參數(shù):
- */
- int TRequest_HTTP::AnalysisRequestLine(char * line)
- {
- char type[120],uri[300],version[100];
- sscanf(line,"%s %s %s",type,uri,version);
- // 請(qǐng)求類型
- if(GetReq_MethodType(type)<0)
- {
- SetStatusCode(400,"Bad Request");
- return 0;
- };
- strcpy(mType,type);
- strcpy(mUri,uri);
- AnalysisUri(mUri);
- strcpy(mVersion,version);
- return 1;
- }
- /*
- 函數(shù)功能: 壓縮生成返回的頭
- 傳入?yún)?shù):
- size: 返回型參數(shù),用于存放生成的buf的大小
- 返回參數(shù):
- 壓縮生成的內(nèi)存空間指針
- */
- char * TRequest_HTTP::PackRetHead(int * size)
- {
- char * pos=NULL,tmpbuf[300],paraval[300],* end;
- int poolsize,len,id;
- *size=0;
- poolsize=2000;
- pos=(char *)IMem.Malloc(poolsize);
- end=pos;
- for(id=0;id<mRetHead.mParaNum;id++)
- {
- mRetHead.GetParaValue(id,paraval);
- if(id==0)
- sprintf(tmpbuf,"%s%s",mRetHead.GetParaName(id),paraval);
- else
- sprintf(tmpbuf,"%s: %s",mRetHead.GetParaName(id),paraval);
- len = strlen(tmpbuf);
- printf("len is %d tmp is '%s'\n",len,tmpbuf);
- if((*size+len+2) >; poolsize)
- {
- char * newbuf;
- newbuf=(char *)IMem.Malloc(poolsize+2000);
- memcpy(newbuf,pos,poolsize);
- IMem.Free(pos);
- pos=newbuf;
- poolsize+=2000;
- end = pos+(*size);
- };
- *size += (len+2);
- strcpy(end,tmpbuf);
- end += len;
- *end = 0x0d;
- end ++;
- *end = 0x0a;
- end ++;
- };
- //最后加上0x0d0x0a表示結(jié)束
- *size += (2);
- *end = 0x0d;
- end ++;
- *end = 0x0a;
- end ++;
- return pos;
- }
- /*
- 函數(shù)功能:處理客戶端 GET 請(qǐng)求的函數(shù)
- 需要判斷是否調(diào)用 CGI 的目錄路徑
- 傳入?yún)?shù):
- 返回參數(shù):
- */
- void TRequest_HTTP::Process_Get()
- {
- char fullname[512],cgicmd[512];
- char type[20];
- char ext[220],*headpool;
- struct stat statbuf;
- int size,id;
- FILE * fp;
- #define MAXDATALEN 2038 //最大包長(zhǎng)
- int datalen;
- char databuf[MAXDATALEN];
- printf("path is '%s'\n",mPathName);
- printf("file is '%s'\n",mFileName);
- #ifndef UNIX_SYS
- IString.ReplaceStr(mPathName,"/","\\");
- IString.ReplaceStr(mFileName,"/","\\");
- #endif
- #ifdef UNIX_SYS
- sprintf(fullname,"%s/%s",mPathName,mFileName);
- #else
- sprintf(fullname,"%s\\%s",mPathName,mFileName);
- #endif
- size=strlen(mFileName);
- headpool=mFileName;
- headpool+=(size-1);
- for(id=size;id>;0;id--)
- {
- if(*headpool=='.'||*headpool=='/'||headpool==mFileName)
- break;
- headpool--;
- };
- if(*headpool=='.')
- headpool++;
- strcpy(ext,headpool);
- if(IsCgiPath(mPathName))
- {
- sprintf(cgicmd,"system %s >;%s/%d.html",HttpSet.TmpPath,mClientSocket);
- system(cgicmd);
- #ifdef UNIX_SYS
- sprintf(fullname,"%s/%d.html",HttpSet.TmpPath,mClientSocket);
- #else
- sprintf(fullname,"%s\\%d.html",HttpSet.TmpPath,mClientSocket);
- #endif
- }
- else
- #ifdef UNIX_SYS
- sprintf(fullname,"%s/%s",mPathName,mFileName);
- #else
- sprintf(fullname,"%s\\%s",mPathName,mFileName);
- #endif
- if(stat(fullname,&statbuf))
- {
- SetStatusCode(404,"Not Found");
- mRetHead.PushPara("HTTP","/1.0 404 Not Found");
- return;
- }
- mRetHead.PushPara("HTTP","/1.0 200 OK");
- SetStatusCode(200,"OK");
- printf("cont leng is %d\n",statbuf.st_size);
- strcpy(type,GetFile_Type(ext));
- mRetHead.PushPara("Server","Orient WebServer");
- mRetHead.PushPara("Connection","keep-alive");
- mRetHead.PushPara("Content-Type",type);
- mRetHead.PushPara("Accept-Ranges","bytes");
- mRetHead.PushPara("Content-Length",statbuf.st_size);
- headpool=PackRetHead(&size);
- NetItf.SendData(mClientSocket,headpool,size);
- IMem.Free(headpool);
- fp=fopen(fullname,"rb");
- while((datalen=fread(databuf,1,MAXDATALEN,fp))>;0)
- {
- NetItf.SendData(mClientSocket,databuf,datalen);
- if(datalen<MAXDATALEN)
- {
- //所有數(shù)據(jù)都已經(jīng)發(fā)送完了
- break;
- };
- }
- fclose(fp);
- }
- /*
- 函數(shù)功能:所有請(qǐng)求的處理句柄
- 現(xiàn)在只處理GET請(qǐng)求
- 傳入?yún)?shù):
- 返回參數(shù):
- */
- void TRequest_HTTP::Process_Request()
- {
- Process_Get();
- mReqHead.ResetPara();
- mRetHead.ResetPara();
- }
- #include <asyncapi.h>;
- int SocketDisconnect_Handle_Http(int sock,char * ip)
- {
- return IHttp.DeleteSite(sock);
- }
- int DataArrived_Handle_Http(int sock,char * buf,int length)
- {
- return IHttp.DataArrived_Handle(sock,buf,length);
- }
- int SocketConnectIn_Handle_Http(int sock,char * ip,int servsocket)
- {
- IHttp.AddSite(sock,ip);
- NetItf.AddClient(sock,ip);
- NetItf.SetDisconnectHandle(sock,SocketDisconnect_Handle_Http);
- NetItf.SetDataArriveHandle(sock,DataArrived_Handle_Http);
- NetItf.SetRecvBufSize(sock,0);
- return 1;
- }
- void Exit_Handle_Http(int para)
- {
- exit(0);
- }
- int main_Web(int argc, char **argv)
- {
- int socket;
- IHttp.LoadHttpSet();
- IHttp.ShowHttpSet();
- /*顯示版權(quán)信息*/
- printf("*******************************************************************************\n");
- printf(" Orient Simple Web Server startup\n");
- printf(" www.onlyit.cn\n");
- printf(" Version 1.0\n");
- printf("*******************************************************************************\n");
- socket=INet.Server(HttpSet.Port);
- if(socket<0)
- {
- puts("Unable to create sock for http serv");
- exit(0);
- };
- NetItf.AddServer(socket,"127.0.0.1");
- NetItf.BeginSocketAsync(socket);
- NetItf.SetRecvBufSize(socket,16384);
- NetItf.SetConnectInHandle(socket,SocketConnectIn_Handle_Http);
- NetItf.SetDisconnectHandle(socket,SocketDisconnect_Handle_Http);
- NetItf.SetDataArriveHandle(socket,DataArrived_Handle_Http);
- NetItf.NetEventMgr_Balance();
- return 1;
- }
復(fù)制代碼 |
|