日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

Apache模块开发helloworld无错版

發布時間:2023/11/27 生活经验 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Apache模块开发helloworld无错版 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

環境:CentOS 5.4

第一步:安裝Apache的apxs

首先來介紹下apache的一個工具apxs。apxs是一個為Apache HTTP服務器編譯和安裝擴展模塊的工具,用于編譯一個或多個源程序或目標代碼文件為動態共享對象,使之可以用由mod_so提供的LoadModule指令在運行時加載到Apache服務器中。

?

apxs可參考官方文檔

http://lamp.linux.gov.cn/Apache/ApacheMenu/programs/apxs.html

?

輸入命令查看是否有httpd-devel這個包,如果沒有需要安裝

#rpm -qa|grep httpd?

# yum -y install httpd-devel

利用指令確認其已經安裝

# which apxs
/use/sbin/apxs

也可以這樣查找全部

#find / | grep apxs

?

第二步:apxs -g -n helloworld

上面的命令可以幫助我們產生一個模塊名字為helloworld的模板。
上面的命令會產生以下代碼

?

C代碼

  1. #include?"httpd.h" ??
  2. #include?"http_config.h" ??
  3. #include?"http_protocol.h" ??
  4. #include?"ap_config.h" ??
  5. ??
  6. /*?The?sample?content?handler?*/??
  7. static?int?helloworld_handler(request_rec?*r) ??
  8. { ??
  9. ????if?(strcmp(r->handler,?"helloworld"))?{ ??
  10. ????????return?DECLINED; ??
  11. ????} ??
  12. ????r->content_type?=?"text/html";?????? ??
  13. ??
  14. ????if?(!r->header_only) ??
  15. ????????ap_rputs("The?sample?page?from?mod_helloworld.c\n",?r); ??
  16. ????return?OK; ??
  17. } ??
  18. ??
  19. static?void?helloworld_register_hooks(apr_pool_t?*p) ??
  20. { ??
  21. ????ap_hook_handler(helloworld_handler,?NULL,?NULL,?APR_HOOK_MIDDLE); ??
  22. } ??
  23. /*?Dispatch?list?for?API?hooks?*/??
  24. module?AP_MODULE_DECLARE_DATA?helloworld_module?=?{ ??
  25. ????STANDARD20_MODULE_STUFF,?//用于編譯后的模塊產生版本信息 ??
  26. ????NULL,??????????????????/*?創建目錄配置結構*/??
  27. ????NULL,??????????????????/*?合并目錄配置結構?*/??
  28. ????NULL,??????????????????/*?創建主機配置結構?*/??
  29. ????NULL,??????????????????/*?合并主機配置結構?*/??
  30. ????NULL,??????????????????/*?為模塊配置相關指令???????*/??
  31. ????helloworld_register_hooks??/*?注冊模塊的鉤子函數??????????????????????*/??
  32. };??

#include "httpd.h"
#include "http_config.h"
#include "http_protocol.h"
#include "ap_config.h"

/* The sample content handler */
static int helloworld_handler(request_rec *r)
{
if (strcmp(r->handler, "helloworld")) {
return DECLINED;
}
r->content_type = "text/html";

if (!r->header_only)
ap_rputs("The sample page from mod_helloworld.c\n", r);
return OK;
}

static void helloworld_register_hooks(apr_pool_t *p)
{
ap_hook_handler(helloworld_handler, NULL, NULL, APR_HOOK_MIDDLE);
}
/* Dispatch list for API hooks */
module AP_MODULE_DECLARE_DATA helloworld_module = {
STANDARD20_MODULE_STUFF, //用于編譯后的模塊產生版本信息
NULL, /* 創建目錄配置結構*/
NULL, /* 合并目錄配置結構 */
NULL, /* 創建主機配置結構 */
NULL, /* 合并主機配置結構 */
NULL, /* 為模塊配置相關指令 */
helloworld_register_hooks /* 注冊模塊的鉤子函數 */
};

我們來看下helloworld_module這個結構體,它沒個成員的具體作用請看注釋。
它最關鍵的參數為最后一個,這個參數是一個注冊鉤子函數指針,也就是說當我們把模塊加入到apache里面去的時候,他會執行這個注冊函數。在這個函數里面我們將會注冊我們所要添加的鉤子。
本例子中我們用的是

?

C代碼

  1. ap_hook_handler(helloworld_handler,?NULL,?NULL,?APR_HOOK_MIDDLE);??

ap_hook_handler(helloworld_handler, NULL, NULL, APR_HOOK_MIDDLE);

這個處理函數,這個處理函數注冊了helloworld_handler這個函數。這個函數用于處理我們的請求。
我們來講下執行的順序,模塊加載-》執行helloworld_register_hooks函數-》注冊helloworld_handler這個函數到鉤子上去。
這樣一來:當一個http請求來的時候,我們就會自動去執行helloworld_handler這個函數。本例子是一個非常簡單的內容生成器。

?

C代碼

  1. if?(strcmp(r->handler,?"helloworld"))?{//判斷是否是這個helloworld??handler ??
  2. ????????return?DECLINED;// ??
  3. ????} ??
  4. ????r->content_type?=?"text/html";?????? ??
  5. ????if?(!r->header_only) ??
  6. ????????ap_rputs("The?sample?page?from?mod_helloworld.c\n",?r);//內容生成 ??
  7. ????return?OK;??

if (strcmp(r->handler, "helloworld")) {//判斷是否是這個helloworld handler
return DECLINED;//
}
r->content_type = "text/html";
if (!r->header_only)
ap_rputs("The sample page from mod_helloworld.c\n", r);//內容生成
return OK;

?

第三步:編譯

# apxs -c mod_helloworld.c
/usr/lib/apr-1/build/libtool --silent --mode=compile gcc -prefer-pic -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m32 -march=i386 -mtune=generic -fasynchronous-unwind-tables -fno-strict-aliasing? -DLINUX=2 -D_REENTRANT -D_GNU_SOURCE -D_LARGEFILE64_SOURCE -pthread -I/usr/include/httpd? -I/usr/include/apr-1?? -I/usr/include/apr-1?? -c -o mod_helloworld.lo mod_helloworld.c && touch mod_helloworld.slo
/usr/lib/apr-1/build/libtool --silent --mode=link gcc -o mod_helloworld.la? -rpath /usr/lib/httpd/modules -module -avoid-version??? mod_helloworld.lo

ls后發現目錄下的確多了幾個文件,其中就有一個mod_helloworld.la的,于是再調用

#?apxs -i mod_helloworld.la

apache的Module目錄下就多了一個mod_helloworld.so

?

再在httpd.conf中加入這一Module:

LoadModule helloworld_module /usr/lib/httpd/modules/mod_helloworld.so

<Location /helloworld>
??????? SetHandler helloworld
</Location>

重啟apache 然后輸入 http://loacalhost/helloworld 就可以看到
The sample page from mod_helloworld.c

當然這里這里只是輸出一句話,我們也可以打印很多html信息,就類似于servlet一樣。

這樣一來一個簡單的apache內容生成器模塊已經開發好了,當然應用比較廣泛的是過濾器模塊的開發,最近項目主要也是用過濾器來實現的。

apache 可以開發出一些功能非常強大的模塊來,可以為我們定制更好的apache,比如容器中應用的流量統計,cpu統計等。

?

?

網上還有幾篇寫的可參考的:

http://www.cnblogs.com/ithurricane/archive/2009/01/01/1366312.html?

http://www.cnblogs.com/DeadKnight/archive/2010/04/08/1707444.html

http://hi.baidu.com/kylelee/blog/item/f13fef2c7366bceb8b1399c9.html

總結

以上是生活随笔為你收集整理的Apache模块开发helloworld无错版的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。