Apache 模組開發

2021-04-30 07:03:00 字數 3772 閱讀 4595

一.目的

寫乙個apache2.0的mod模組,讀取配置,並對所有字尾為.hello的請求進行處理。

二.步驟

建立乙個mod_hello.c檔案

1.  定義乙個模組。

2.  定義介面。

module ap_module_declare_data hello_module =

;說明:

其中create_modhello_config函式為用來為自定義的結構分配空間,mod_hello_cmds定義了引數序列和引數的讀取函式。mod_hello_register_hooks定義了請求處理函式

3.  初始化配置,讀取配置。

配置結構的定義:

typedef struct modhello_config;

引數的定義:

static const command_rec mod_hello_cmds =

};引數結構的建立,由apache在裝載模組時候呼叫。

static void *create_modhello_config(apr_pool_t *p, server_rec *s)

引數讀取函式

static const char *set_modhello_string(cmd_parms *parms, void *mconfig, const char *arg)

else if(!strcmp(parms->cmd->name,"modulemaxprocess"))

// success

return null;

}4.  處理請求。

註冊請求。

static void mod_hello_register_hooks (apr_pool_t *p)

請求處理函式

static int mod_hello_method_handler (request_rec *r)

三.安裝。

1.       編譯。

看makefile.

all:    mod_hello.c

gcc -g -i/home/wee/apache2/include/ -fpic -o mod_hello.o -c mod_hello.c

gcc -shared -i/home/wee/apache2/include/ -o libmodhello.so -lc mod_hello.o

cp *.so /home/wee/apache2/modules/

clean:

rm *.o *.so

2.       配置。

修改httpd.conf。

增加處理:

loadmodule hello_module        modules/libmodhello.so

addhandler hello-s cript .hello

增加引數:

welcome  "hello,world"

modulemaxprocess   5

3.       安裝

gcc -v

reading specs from /usr/lib/gcc-lib/i386-redhat-linux/2.96/specs

gcc version 2.96 20000731 (red hat linux 7.3 2.96-110)

make.

四.測試。

訪問http://***.***.***.***/a.hello, 螢幕上列印出

「hello,world」,同時log中也有列印資訊。

五.參考資料

apache mod 開發 step 2 獲取使用者輸入

apache mod 開發 step 2 獲取使用者輸入

1. 了解apr_table_t結構

可以把他理解為乙個hash表,可以對他進行取值賦值操作,常用的有

apr_table_add

apr_table_set

apr_table_get

例如:char *slen=apr_table_get(r->headers_in, "content-length");

2. 了解request_rec

這是乙個最重要的結構,定義在httpd.h第682行.

注意到handle函式的唯一的引數

static int mod_hello_method_handler (request_rec *r);你可以認為從這個結構裡面你可以得到所有一切.

重要的幾個結構成員

apr_pool_t *pool;

/** the connection to the client */

conn_rec *connection;

/** the virtual host for this request */

server_rec *server;

int method_number; //提交資訊的型別,get或者post

char *args; //存放get的引數

apr_table_t *headers_in; //提交資訊的頭資訊的儲存位置

const char *handler;

//處理的型別

3. 讀取http頭

資訊在r->headers_in裡面,那麼就是

char *slen=apr_table_get(r->headers_in, "content-length");

4. 獲得get方法傳遞的資料

資訊在r->args裡面.注意,這裡的資料是沒有經過解析的,也就是說url編碼過的,如果你不使用類似libapreq2而自行解析的話,需要自行編碼.

ap_log_rerror(aplog_mark, aplog_err,0,r,"get query string:%s",r->args);

5. 獲得post方法傳遞的資料

資料在request_rec關聯的bucket裡面.bucket的解釋將在下一步解釋,那麼我們簡單的使用ap_get_client_block來讀取吧.其實這個函式裡面也是呼叫了bucket操作.

6. 簡單的例子.

if(strcmp("hello-s cript",r->handler)) return declined;

//get the comand.

if(r->method_number==m_get)else if(r->method_number==m_post)else

handle_post 函式

void handle_post(request_rec *r)}}

7. 小結

現在對模組有了簡單的理解,知道如何寫乙個模組,知道資料在哪.知道如何處理輸入輸出,下面要知道apahce內部是如何的運作,apr執行庫的常用函式,然後是利用apache的服務框架完成更多的工作.要把apache看做乙個socket伺服器,而不僅僅是乙個web伺服器

Apache模組開發

參考 第一步使用採用apache只帶的工具apxs產生乙個模板helloword模組 apxs g n hello 這樣就會在當前目錄產生乙個hello資料夾 進入hello資料夾,修改mod hello.c檔案 裡面有hello handler函式,即為對請求進行處理的函式。可以再這裡修改處理請求...

apache模組開發例項

該例子是在 usr local apache2 logs error log檔案中寫入一條訪問記錄 apache2 mod tut1 a request was made.執行下面指令 在當前目錄下生成apache2 modules目錄 進入apache2 modules自目錄tut1 執行下面指令...

apache鉤子模組開發

背景 現專案下,有這樣乙個需求,當使用者訪問的是特定網域名稱時跳轉到登陸介面輸入密碼,驗證成功後,在40分鐘內可以正常瀏覽介面,以此迴圈。首先需要前端做好相關介面,且後台要提供乙個校驗密碼的介面,後台在校驗成功需寫入相關記錄。在此基礎上開始開發apache相關模組。apache的鉤子函式挺多的,在此...