/*
** mod_myparams.c
** ○httpd.conf におけるパラメータ設定のサンプル
** 以下のパラメータを定義する:
** ・MYPARAMSw: スイッチ (ON/OFF)
** ・MYPARAMStr: 文字列
** ・MYPARAMNum: 整数値
** ○ビルド
** $ apxs -c -l libhttpd.lib -l libapr-1.lib mod_myparams.c
** ○インストール
** $ copy mod_myparams.so /your_httpd_path/modules
** ○httpd.conf の編集。追加内容:
** LoadModule myparams_module modules/mod_myparams.so
**
** SetHandler myparams
**
** MYPARAMSw On
** MYPARAMStr hello
** MYPARAMNum 123456
*/
#include "httpd.h"
#include "http_config.h"
#include "http_protocol.h"
#include "ap_config.h"
#include "util_script.h"
// ServerRoot の値を保持するグローバル変数の参照
AP_DECLARE_DATA extern const char *ap_server_root;
module AP_MODULE_DECLARE_DATA myparams_module;
// パラメータ格納用構造体
typedef struct myparams_conf_t {
int sw1; /* ON:1, OFF:0*/
char *str1;
int num1;
} myparams_conf_t;
// apr_table_do から使用されるコールバック関数
int print_table_cb (void *ctx, const char *key, const char *value) {
request_rec *r = (request_rec*)ctx;
ap_rprintf(r, "
[%s]=[%s]\n", key, value);
return TRUE;/* TRUE:continue iteration. FALSE:stop iteration */
}
/* HTML を出力する関数 */
static int myparams_handler(request_rec *r)
{
myparams_conf_t *ptr = (myparams_conf_t *) ap_get_module_config(
r->server->module_config, &myparams_module);
if (strcmp(r->handler, "myparams")) {
return DECLINED;
}
r->content_type = "text/html";
if (!r->header_only) {
ap_add_common_vars(r);
ap_add_cgi_vars(r);
ap_rprintf(r, "\n\nmod_myparams\n\n");
ap_rprintf(r, "\n\n- ServerRoot: %s\n", ap_server_root);
ap_rprintf(r, "
- MYPARAMSwitch: %d\n", ptr->sw1);
ap_rprintf(r, "
- MYPARAMStr: %s\n", ptr->str1);
ap_rprintf(r, "
- MYPARAMNum: %d\n", ptr->num1);
if (r->subprocess_env) {
ap_rprintf(r, "
- subprocess_env\n
\n");
apr_table_do(print_table_cb, r, r->subprocess_env, NULL);
ap_rprintf(r, "
\n");
}
ap_rprintf(r, "
\n");
}
return OK;
}
// パラメータ格納用構造体を作成し初期化する関数
static void *myparams_create_sconfig(apr_pool_t *p, server_rec *s)
{
myparams_conf_t *ptr = apr_pcalloc(p, sizeof *ptr);
ptr->sw1 = 0;
ptr->str1 = NULL;
ptr->num1 = -1;
return ptr;
}
static void myparams_register_hooks(apr_pool_t *p)
{
ap_hook_handler(myparams_handler, NULL, NULL, APR_HOOK_MIDDLE);
}
// MYPARAMSw の値を構造体にセットする関数
static const char *myparams_set_sw(cmd_parms *cmd, void *dummy, int arg)
{
myparams_conf_t *ptr = (myparams_conf_t *) ap_get_module_config(
cmd->server->module_config, &myparams_module);
ptr->sw1 = arg;
return NULL;
}
// MYPARAMStr の値を構造体にセットする関数
static const char *myparams_set_str(cmd_parms *cmd, void *dummy,
const char *arg)
{
char *str;
myparams_conf_t *ptr = (myparams_conf_t *) ap_get_module_config(
cmd->server->module_config, &myparams_module);
if ((str = ap_getword_conf(cmd->pool, &arg))) {
ptr->str1 = str;
} else {
return "MYPARAMStr requires string value";
}
return NULL;
}
// MYPARAMNum の値を構造体にセットする関数
static const char *myparams_set_num(cmd_parms *cmd, void *dummy,
const char *arg)
{
char *str;
myparams_conf_t *ptr = (myparams_conf_t *) ap_get_module_config(
cmd->server->module_config, &myparams_module);
if ((str = ap_getword_conf(cmd->pool, &arg))) {
ptr->num1 = atoi(str);
} else {
return "MYPARAMNum requires integer value";
}
return NULL;
}
// パラメータの定義
static const command_rec myparams_cmds[] = {
AP_INIT_FLAG( "MYPARAMSw", myparams_set_sw, NULL, RSRC_CONF,
"MYPARAMS Switch"),
AP_INIT_TAKE1("MYPARAMStr", myparams_set_str, NULL, RSRC_CONF,
"MYPARAMS String"),
AP_INIT_TAKE1("MYPARAMNum", myparams_set_num, NULL, RSRC_CONF,
"MYPARAMS Number"),
{ NULL }
};
/* Dispatch list for API hooks */
module AP_MODULE_DECLARE_DATA myparams_module = {
STANDARD20_MODULE_STUFF,
NULL, /* create per-dir config structures */
NULL, /* merge per-dir config structures */
myparams_create_sconfig, /* create per-server config structures */
NULL, /* merge per-server config structures */
myparams_cmds, /* table of config file commands */
myparams_register_hooks /* register hooks */
};