文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>apache module hello world, linux平台使用c编写网页...

apache module hello world, linux平台使用c编写网页...

时间:2010-08-06  来源:www861168com

1) 创建文件apache_module_test.c,目录随意,以"/source/moduletest"为例

/*M------------------------------------------------------------------

 *M                           DEFINE HEADER

 *M------------------------------------------------------------------

 *M

 *M  Filename:      apache_module_test.c

 *M 

 *M  Path:             /source/moduletest

 *M

 *M  @ Copyright: TDF ( [email protected] )

 *M

 *M------------------------------------------------------------------

 *M                          FUN DESCRIPTION

 *M------------------------------------------------------------------

 *M

 *M              Test simple apache module only.

 *M

 *M------------------------------------------------------------------

 *M                         VERSION HISTORY 

 *M------------------------------------------------------------------

 *M

 *M     WHEN        BY        NO         DESCRIPTION

 *M     07-08-10   TDF      10123    Initial Version

 *M

 *M------------------------------------------------------------------*/


#include <httpd.h>
#include <http_protocol.h>

#include <http_config.h>

// 定义回调函数

static int helloworld_handler (request_rec *r)

{

      // 实现访问目录区分

      if (!r->handler || strcmp(r->handler, "myhelloworld"))

     {
                return DECLINED; 
     }

     // 判断请求方式GET/POST

     if (r->method_number != M_GET)

    {
                return HTTP_METHOD_NOT_ALLOWED; 
     }

     ap_set_content_type (r, "text/html; charset=utf-8");

     ap_rputs ("<html><body><h1>HELLO WORLD - Apache Module</h1></body></html>");

  

     return OK:

}

// 注册钩子函数

static void register_helloworld_hooks (apr_pool_t *pool)

{

     ap_hook_handler (helloworld_handler, NULL, NULL, APR_HOOK_MIDDLE);

}

// 定义module

modue AP_MODULE_DECLARE_DATA helloworld_module =

{

     STANDARD20_MODULE_STUFF,

     NULL,

     NULL,

     NULL,

     NULL,

     NULL,   //  使用command_rec创建其他命令集, 在此用不到, 比如my_cmds, 包括config

     register_helloworld_hooks // 注册回调函数

}

2) 创建文件 Makefile

   

CC=gcc

CCFLAGS=-I/usr/local/apache2/include

ModuleTest.so: ModuleTest.o

      [tab] $(CC) -shared -Wl  -o libModuleTest.so -lc ModuleTest.o

ModuleTest.o: apache_module_test.c

      [tab] $(CC) -g $(CCFLAGS) -fPIC -o ModuleTest.o -c $<

#使用[tab]的地方请替换为tab符, 必须使用tab制表符

3) 修改文件 httpd.conf,

增加: 

LoadModule myhellowolrd /source/moduletest/ModuleTest.so

<Location /aphelloworld>

    SetHandler myhellowolrd

</Location>

修改监听端口:

Listen 8090

4) 测试环境Linux, Apache2

编译并启动apache

>cd /source/moduletest

>make

>apachectl -k start  -f /source/moduletest/httpd.conf

在IE浏览器输入 http://xx.xx.xx.xx:8090/aphelloworld  验证效果。

P.S. 要写复杂的Web应用,靠C恐怕相当困难,我认为首先需要利用C++,在Module Handler里可调用C++函数来实现功能划分,其中一种比较快捷的方法是通过找地址的方式,除此之外,在我的设想中还需运用:

(1) Xml + Xslt, 有了xslt后才好在windows下把html框架页面调试好。

(2) 需要一个C#中的类XsltTransform在Linux C/C++下面的实现。

(3) 一个类似.Net中的DataSet,在Linux C/C++下的实现;并且有WriteXml()的功能。

有了Transfomer, 并有XML和XSLT, 两者一转译Html就OK了;另外还需解决接收post上来的数据问题。

相关阅读 更多 +
排行榜 更多 +
边境警察巡逻模拟中文版下载

边境警察巡逻模拟中文版下载

角色扮演 下载
数码宝贝世界复原手机版下载

数码宝贝世界复原手机版下载

角色扮演 下载
土地抢夺者中文手机版下载

土地抢夺者中文手机版下载

策略塔防 下载