c apache module 開發入門

chinaunix地址:http://www.chinaunix.net/index.php?uid=386791&url=http://bbs.chinaunix.net/viewthread.php?tid=1255594

前言:
擴展Apache模塊開發網上大部分教程都是圍繞Perl語言記性,老外的《Writing Apache Modules with Perl and C》可以算是經典之作了,可惜一直都是針對老版本開發,而且主力語言是Perl,C語言部分只是略有介紹。不過相比較而言用Perl來擴展模塊功能確實比 C語言來的快速以及便捷多了,也簡單容易。我自己也在工作裏應用了一部分,主要是在防盜鏈上面寫了兩個簡單都模塊,可以參考我寫的另外兩篇文章:apache+mod_perl防盜鏈 以及apache+mod_perl實現url rewrite 。說了那麼多題外話,回到正題,這裏只是用C語言實現一個簡單的hello模塊,模塊功能是查詢MySQL自帶mysql數據庫裏都user表。

系統環境:
ArchLinux Apache2.2 MySQL 5.0

具體開發步驟:
1.利用Apache自帶都apxs建立hello模塊:
[root#localhost] apxs -g -n hello
這樣就會在當前目錄下新建一個hello模塊的文件目錄,可以看到裏面有:Makefile mod_hello.c modules.mk這樣的文件,具體apxs路徑查詢下本機apache/bin目錄。

2.預覽下mod_hello.c ,可以看到裏面apxs自動幫你生成一堆代碼了,我們需要的只是修改裏面的代碼部分,先簡單都介紹下里面的函數說明。
include 部分就是引入了一些必要都頭文件
hello_handler 這個就是hello模塊都主體部分,所有的顯示、處理請求什麼的都在這裏。
hello_register_hooks hello_module 這倆個是需要導出的函數所必須的,先可以不管他們,按照生成的不動即可。

3.修改
hello_handler函數, 裏面可以看到request_rec *r,r有很多函數和變量,具體要參見文檔了。裏面的ap_rputs是輸出,可以簡單的理解爲把字符串輸出到r。
static int hello_handler(request_rec *r)
{
     if (strcmp(r->handler, "hello")) { // 判斷apache配置文件裏handler是否等於hello,不是就跳過
          return DECLINED;
     }
     r->content_type = "text/html"; // 設置content-type
     if (!r->header_only)
          ap_rputs("The sample page from mod_hello.c/n", r); // 輸出一段文字
     return OK;// 返回 200 OK狀態
}

增加#include "mysq.h",查詢需要用到這個頭文件。
具體代碼參見本文結尾部分。

4.編譯模塊

[root#localhost]
apxs -c -a -i -I/usr/include/mysql/ -lmysqlclient mod_hello.c
可以看到一堆編譯指令,加上-I和-l是編譯mysql必須的,編譯完會自動在httpd.conf加上 LoadModule hello_module modules/mod_hello.so

5.修改httpd.conf
<Location /hello>
     SetHandler hello
</
Location >

6.重啓apache, 訪問http://localhost/hello,看是否成功。


=================================================================================

完整代碼:
#include "httpd.h"
#include "http_config.h"
#include "http_protocol.h"
#include "ap_config.h"
/* 頭文件,本文用到了
ap_rprintf函數 */
#include "apr.h"
#include "apr_lib.h"
#include "apr_strings.h"
#include "apr_want.h"
#include "mysql.h"

/* 定義mysql數據變量 */
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)) {/* 連接 數據庫 */
          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) { /* 查詢 */
          ap_rprintf(r, "<li>Error : %d %s</li>/n", mysql_errno(&mysql), mysql_error(&mysql));
          return OK;
     }
     rs = mysql_store_result(&mysql); /* 獲取查詢結果 */
     while ((row = mysql_fetch_row(rs))) { /* 獲取每一行記錄 */
          ap_rprintf(r, "<li>%s - %s</li>/n", row[0], row[1]);
     }
     mysql_free_result(rs); /* 釋放結果集 */
     mysql_close(&mysql); /* 關閉連接 */
     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                      */
};
發佈了44 篇原創文章 · 獲贊 3 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章