轉(zhuǎn)載請(qǐng)說(shuō)明該文章來(lái)自smallfish博客 [url=http://hi.baidu.com/smallfish7788]http://hi.baidu.com/smallfish7788
前言:
擴(kuò)展Apache模塊開(kāi)發(fā)網(wǎng)上大部分教程都是圍繞Perl語(yǔ)言記性,老外的《Writing Apache Modules with Perl and C》可以算是經(jīng)典之作了,可惜一直都是針對(duì)老版本開(kāi)發(fā),而且主力語(yǔ)言是Perl,C語(yǔ)言部分只是略有介紹。不過(guò)相比較而言用Perl來(lái)擴(kuò)展模塊功能確實(shí)比 C語(yǔ)言來(lái)的快速以及便捷多了,也簡(jiǎn)單容易。我自己也在工作里應(yīng)用了一部分,主要是在防盜鏈上面寫(xiě)了兩個(gè)簡(jiǎn)單都模塊,可以參考我寫(xiě)的另外兩篇文章:[url=http://hi.baidu.com/smallfish7788/blog/item/2a1137fb9497a318a8d31123.html]apache+mod_perl防盜鏈以及[url=http://hi.baidu.com/smallfish7788/blog/item/3ccbca1a7405edfcaf51335b.html]apache+mod_perl實(shí)現(xiàn)url rewrite。說(shuō)了那么多題外話(huà),回到正題,這里只是用C語(yǔ)言實(shí)現(xiàn)一個(gè)簡(jiǎn)單的hello模塊,模塊功能是查詢(xún)MySQL自帶mysql數(shù)據(jù)庫(kù)里都user表。
系統(tǒng)環(huán)境:
ArchLinux Apache2.2 MySQL 5.0
具體開(kāi)發(fā)步驟:
1.利用Apache自帶都apxs建立hello模塊:
[root#localhost] apxs -g -n hello
這樣就會(huì)在當(dāng)前目錄下新建一個(gè)hello模塊的文件目錄,可以看到里面有:Makefile mod_hello.c modules.mk這樣的文件,具體apxs路徑查詢(xún)下本機(jī)apache/bin目錄。
2.預(yù)覽下mod_hello.c,可以看到里面apxs自動(dòng)幫你生成一堆代碼了,我們需要的只是修改里面的代碼部分,先簡(jiǎn)單都介紹下里面的函數(shù)說(shuō)明。
include 部分就是引入了一些必要都頭文件
hello_handler 這個(gè)就是hello模塊都主體部分,所有的顯示、處理請(qǐng)求什么的都在這里。
hello_register_hooks hello_module 這倆個(gè)是需要導(dǎo)出的函數(shù)所必須的,先可以不管他們,按照生成的不動(dòng)即可。
3.修改hello_handler函數(shù),里面可以看到request_rec *r,r有很多函數(shù)和變量,具體要參見(jiàn)文檔了。里面的ap_rputs是輸出,可以簡(jiǎn)單的理解為把字符串輸出到r。
static int hello_handler(request_rec *r)
{
if (strcmp(r->handler, "hello")) { // 判斷apache配置文件里handler是否等于hello,不是就跳過(guò)
return DECLINED;
}
r->content_type = "text/html"; // 設(shè)置content-type
if (!r->header_only)
ap_rputs("The sample page from mod_hello.c\n", r); // 輸出一段文字
return OK;// 返回 200 OK狀態(tài)
}
增加#include "mysq.h",查詢(xún)需要用到這個(gè)頭文件。
具體代碼參見(jiàn)本文結(jié)尾部分。
4.編譯模塊
[root#localhost] apxs -c -a -i -I/usr/include/mysql/ -lmysqlclient mod_hello.c
可以看到一堆編譯指令,加上-I和-l是編譯mysql必須的,編譯完會(huì)自動(dòng)在httpd.conf加上 LoadModule hello_module modules/mod_hello.so
5.修改httpd.conf
<Location /hello>
SetHandler hello
</Location >
6.重啟apache,訪(fǎng)問(wèn)http://localhost/hello,看是否成功。
=================================================================================
完整代碼:
#include "httpd.h"
#include "http_config.h"
#include "http_protocol.h"
#include "ap_config.h"
/* 頭文件,本文用到了ap_rprintf函數(shù) */
#include "apr.h"
#include "apr_lib.h"
#include "apr_strings.h"
#include "apr_want.h"
#include "mysql.h"
/* 定義mysql數(shù)據(jù)變量 */
const char *host = "localhost";
const char *user = "root";
const char *pass = "smallfish";
const char *db = "mysql";
/* The sample content handler */
static int hello_handler(request_rec *r)
{
if (strcmp(r->handler, "hello")) {
return DECLINED;
}
r->content_type = "text/html";
/* 定義mysql變量 */
MYSQL mysql;
MYSQL_RES *rs;
MYSQL_ROW row;
mysql_init(&mysql); /* 初始化 */
if (!mysql_real_connect(&mysql, host, user, pass, db, 0, NULL, 0)) {/* 連接數(shù)據(jù)庫(kù) */
ap_rprintf(r, "<li>Error : %d %s</li>\n", mysql_errno(&mysql), mysql_error(&mysql));
return OK;
}
char *sql = "select host,user from user order by rand()";
if (mysql_query(&mysql, sql)!=0) { /* 查詢(xún) */
ap_rprintf(r, "<li>Error : %d %s</li>\n", mysql_errno(&mysql), mysql_error(&mysql));
return OK;
}
rs = mysql_store_result(&mysql); /* 獲取查詢(xún)結(jié)果 */
while ((row = mysql_fetch_row(rs))) { /* 獲取每一行記錄 */
ap_rprintf(r, "<li>%s - %s</li>\n", row[0], row[1]);
}
mysql_free_result(rs); /* 釋放結(jié)果集 */
mysql_close(&mysql); /* 關(guān)閉連接 */
return OK;
}
static void hello_register_hooks(apr_pool_t *p)
{
ap_hook_handler(hello_handler, NULL, NULL, APR_HOOK_MIDDLE);
}
/* Dispatch list for API hooks */
module AP_MODULE_DECLARE_DATA hello_module = {
STANDARD20_MODULE_STUFF,
NULL, /* create per-dir config structures */
NULL, /* merge per-dir config structures */
NULL, /* create per-server config structures */
NULL, /* merge per-server config structures */
NULL, /* table of config file commands */
hello_register_hooks /* register hooks */
};
[ 本帖最后由 xiaoyu9805119 于 2008-8-28 08:52 編輯 ]