在eJet系统退出时,会调用http_handle_clean来释放初始化过程分配的资源。
动态库在实现回调时,必须含有这三个函数名:http_handle_init、http_handle、http_handle_clean,其函数原型定义如下:
typedef void * HTTPCBInit (void * httpmgmt, int argc, char ** argv);typedef void HTTPCBClean (void * hcb);typedef int RequestHandler (void * cbobj, void * vmsg);
其中回调函数http_handle的第一个参数cbobj是由http_handle_init返回的结果对象,vmsg即是eJet系统的HTTPMsg实例对象。
13.4 回调函数使用HTTPMsg的成员函数
eJet系统通过传递HTTPMsg实例对象给回调函数,来处理HTTP请求。HTTP对象封装了HTTP请求的所有信息,回调函数在处理请求时,可以添加各种响应数据到HTTPMsg中,包括响应状态、响应头、响应体等。
访问请求头信息或添加响应数据的操作,既可以直接对HTTPMsg的成员变量进行数据读取或写入,也可以通过调用HTTPMsg内置的指针函数来进行处理,HTTPMsg中封装了很多函数调用,通过这些函数,基本可实现eJet系统HTTP请求处理的各种操作。这些例子函数如下:
......char * (*GetRootPath) (void * vmsg); int (*GetPath) (void * vmsg, char * path, int len);int (*GetRealPath) (void * vmsg, char * path, int len);int (*GetRealFile) (void * vmsg, char * path, int len);int (*GetLocFile) (void * vmsg, char * p, int len, char * f, int flen, char * d, int dlen); int (*GetQueryP) (void * vmsg, char ** pquery, int * plen);int (*GetQuery) (void * vmsg, char * query, int len);int (*GetQueryValueP) (void * vmsg, char * key, char ** pval, int * vallen);int (*GetQueryValue) (void * vmsg, char * key, char * val, int vallen);int (*GetReqContentP) (void * vmsg, void ** pform, int * plen); int (*GetReqFormJsonValueP) (void * vmsg, char * key, char ** ppval, int * vallen);int (*GetReqFormJsonValue) (void * vmsg, char * key, char * pval, int vallen);int (*SetStatus) (void * vmsg, int code, char * reason);int (*AddResHdr) (void * vmsg, char * na, int nlen, char * val, int vlen);int (*DelResHdr) (void * vmsg, char * name, int namelen); int (*SetResEtag) (void * vmsg, char * etag, int etaglen);int (*SetResContentType) (void * vmsg, char * type, int typelen);int (*SetResContentLength) (void * vmsg, int64 len);int (*AddResContent) (void * vmsg, void * body, int64 bodylen);int (*AddResContentPtr) (void * vmsg, void * body, int64 bodylen);int (*AddResFile) (void * vmsg, char * filename, int64 startpos, int64 len);int (*Reply) (void * vmsg);int (*RedirectReply) (void * vmsg, int status, char * redurl);......
eJet通过设置回调函数的两种接口机制,将客户端的HTTP请求转交给特定的应用程序来处理,充分利用Web开发的各种前端技术,扩展应用程序与用户前端的交互能力。