搬家系列(6)
iplanet(Sun Java System Web Server)下NSAPI 开发流程介绍(NSAPI系列一)
flw10000 发表于 2006-12-18 21:34:00
作者:冯磊 (flw10000) MAIL:flw10000 AT 163.com
1. 安装Sun Web Server6.1
安装过程中的注意事项
1.1 要设置一个Web Server 的管理员,此用户用于以后Web Server 的管理,请记录此用户的用户名,密码和端口。
1.2 新建服务器实例,用于提供web 服务,请记录此服务器实例的端口。
登录到管理画面,可以新建,启动或关闭服务器实例
2. 开发步骤
2.1 Windows 开发环境 (windows vc++)
* 编写源代码
* 编译
在 vc中新建( Win32 Dynamic-Link Library)工程,编写源代码
在 vc中配置 NSAPI中的库文件路径及头文件路径,增加编译参数 -DXP_WIN32和具体的库文件( ns-httpd40.lib)
* 修改配置文件
* 重新起动Web Server
* 测试
2.2 Linux 开发环境 (linux gcc)
* 编写源代码
* 编译
gcc -shared -DLINUX -D_REENTRANT -fPIC -DXP_UNIX –Ipath –Lpath source.c –o source
注:
-Ipath path 是NSAPI 中的头文件路径
-Lpath path 是NSAPI 中的库文件路径
source.c 为开发的源代码
-o source 生成的目标文件是source
* 修改配置文件
* 重新起动Web Server
* 测试
3. 下面是一个具体的例子
The example in this section demonstrates how to implement brief-log 。
Installing the Example
To load the shared object containing your functions, add the following line in the Init section of the magnus.conf file:
Init fn=load-modules shlib=yourlibrary funcs=brief-init,brief-log
To call brief-init to open the log file, add the following line to the Init section in magnus.conf. (This line must come after the one that loads the library containing brief-init.)
Init fn=brief-init file=/tmp/brief.log
To execute your custom SAF during the AddLog stage for some object, add the following line to that object in the obj.conf file:
AddLog fn=brief-log
Source Code
The source code is in addlog.c is in the nsapi/examples/ or plugins/nsapi/examples subdirectory within the server root directory.
#i nclude "nsapi.h" #i nclude "base/daemon.h" /* daemon_atrestart */ #i nclude "base/file.h" /* system_fopenWA, system_fclose */ #i nclude "base/util.h" /* sprintf */
/* File descriptor to be shared between the processes */
static SYS_FILE logfd = SYS_ERROR_FD;
#ifdef __cplusplus extern "C" #endif
NSAPI_PUBLIC void brief_terminate(void *parameter) { system_fclose(logfd); logfd = SYS_ERROR_FD; }
#ifdef __cplusplus extern "C" #endif
NSAPI_PUBLIC int brief_init(pblock *pb, Session *sn, Request *rq) { /* Parameter */ char *fn = pblock_findval("file", pb);
if(!fn) { pblock_nvinsert("error", "brief-init: please supply a file name", pb); return REQ_ABORTED; }
logfd = system_fopenWA(fn); if(logfd == SYS_ERROR_FD) { pblock_nvinsert("error", "brief-init: please supply a file name", pb); return REQ_ABORTED; }
/* Close log file when server is restarted */ daemon_atrestart(brief_terminate, NULL); return REQ_PROCEED; }
#ifdef __cplusplus extern "C" #endif
NSAPI_PUBLIC int brief_log(pblock *pb, Session *sn, Request *rq) { /* No parameters */
/* Server data */ char *method = pblock_findval("method", rq->reqpb); char *uri = pblock_findval("uri", rq->reqpb); char *ip = pblock_findval("ip", sn->client);
/* Temp vars */ char *logmsg; int len;
logmsg = (char *)MALLOC(strlen(ip) + 1 + strlen(method) + 1 + strlen(uri) + 1 + 1); len = util_sprintf(logmsg, "%s %s %s\n", ip, method, uri);
/* The atomic version uses locking to prevent interference */ system_fwrite_atomic(logfd, logmsg, len);
FREE(logmsg); return REQ_PROCEED; }
|
4. 参考
Sun ONE Web Server 6.1 NSAPI Programmer's Guide
(转载请保持文章的完整性,请注明作者和出处)
冯磊 2006.12.18
阅读(2199) | 评论(0) | 转发(0) |