轉載請說明該文章來自smallfish博客 http://hi.baidu.com/smallfish7788
前言:
擴展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 */
};
C語言擴展Apache模塊開發入門篇
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章
數據庫監控:nagios(check_mysql_health)
AMGYM
2019-02-24 13:05:14
MySQL数据库集群-PXC方案
wy53780
2020-04-23 13:55:07
sql語法、關係建模
死瘦宅
2019-02-24 22:35:22
MySQL 事務、函數、觸發器、數據庫備份、權限管理
死瘦宅
2019-02-24 22:35:12
《從0到1學習Flink》—— Flink 讀取 Kafka 數據批量寫入到 MySQL
zhisheng
2019-02-24 21:04:11
SpringBoot 填坑 (一) | CentOS7.4 環境下,表時間字段默認值設置失效
一個優秀的廢人
2019-02-24 15:33:55
mysql-存儲過程
雙子城
2019-02-24 13:43:57
jmeter學習指南之操作 mysql 數據庫
小強測試
2019-02-24 13:34:06
20190222全天的實驗
清歡難尋
2019-02-24 13:17:21
數據庫監控:zabbix(fpmmm)
AMGYM
2019-02-24 13:05:14
如何釋放已刪除的InnoDB磁盤空間
lvrenX
2019-02-24 13:04:33
MYSQL中視圖的使用
雙子城
2019-02-24 08:44:17
MySQL從刪庫到跑路
司木
2019-02-24 00:24:07
Oracle Database 監控:zabbix(一)
AMGYM
2019-02-24 13:05:14