More Related Content
Similar to Apache Module(20)
Apache Module
- 13. 雛形の作成 % apxs -g -n hello Creating [DIR] hello Creating [FILE] hello/Makefile Creating [FILE] hello/modules.mk Creating [FILE] hello/mod_hello.c Creating [FILE] hello/.deps # cd hello/ # apxs –i –a –c mod_hello.c # /usr/local/apache2/bin/apachectl stop # /usr/local/apache2/bin/apachectl start <Location /hello> SetHandler hello </Location> httpd.conf
- 15. ソースコード 1 #include "httpd.h" #include "http_config.h" #include "http_protocol.h" #include "ap_config.h" /* コンテントハンドラ */ static int hello_handler(request_rec *r) { if (strcmp(r->handler, "hello")) { return DECLINED; } r->content_type = "text/html"; if (!r->header_only) ap_rputs("The sample page from mod_hello.c", r); return OK; }
- 21. ソースコード 2 static void hello_register_hooks(apr_pool_t *p) { ap_hook_handler(hello_handler, NULL, NULL, APR_HOOK_MIDDLE); } module AP_MODULE_DECLARE_DATA hello_module = { STANDARD20_MODULE_STUFF, NULL, /* create per-dir config */ NULL, /* merge per-dir config */ NULL, /* create per-server config */ NULL, /* merge per-server config */ NULL, /* config file commands */ hello_register_hooks /* register hooks */ };
- 25. 設定値を格納する構造体 typedef struct { char *msg; } hello_cfg; 構造体の宣言 static void *create_hello_cfg(apr_pool_t *p, char *dir) { hello_cfg *cfg = apr_palloc(p, sizeof(hello_cfg)); cfg->msg = "Hello World!"; return (void *)cfg; } 初期化
- 26. ディレクティブの定義 static const char *cmd_set_message(cmd_parms *cmd, void *c, const char *v) { hello_cfg *cfg = (hello_cfg *)c; cfg->msg = apr_pstrdup(cmd->pool, v); return NULL; } static const command_rec hello_cmds[] = { AP_INIT_TAKE1("HelloMessage", cmd_set_message, NULL, ACCESS_CONF, "Set mod_hello message."), {NULL} }; HelloMessage ディレクティブを定義
- 30. 設定値の利用 static int hello_handler(request_rec *r) { hello_cfg *cfg = ap_get_module_config(r->per_dir_config, &hello_module); if (strcmp(r->handler, "hello")) { return DECLINED; } r->content_type = "text/html"; if (!r->header_only) ap_rprintf(r, "%s", cfg->msg); return OK; } ap_get_module_config で設定値を参照