apache module 读取配置文件

#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 "

// 该模块的配置信息定义一个结构。
typedef  struct  
{
  
short  IsAll;
  
const    char   * gpfpath;
}auth_jira_conf;

// 声明模块名称
module AP_MODULE_DECLARE_DATA pathtest_module;

//  测试用的handler实际。输出从配置文件中读入的配置信息。
static   int  pathtest_handler(request_rec  * r)
{
   r
-> content_type  =   " text/html " ;
   
// 取conf数据
  auth_jira_conf  * conf  =  ap_get_module_config(r -> per_dir_config,
    
& pathtest_module);
  ap_rprintf(r, 
" IsAll:%d\n " ,conf -> IsAll);
  ap_rprintf(r, 
" path:%s " ,conf -> gpfpath);
   
return  OK;
}

static   void  pathtest_register_hooks(apr_pool_t  * p)
{
     ap_hook_handler(pathtest_handler, NULL, NULL, APR_HOOK_MIDDLE);
}

/*  设置IsAll字段的读放法  */
static   const   char   * set_authjira_is_all_check(cmd_parms  * cmd,
                                             
void   * mconfig,
                                             
int  arg)
{
  auth_jira_conf 
* conf  =  (auth_jira_conf  * ) mconfig;
  conf
-> IsAll  =  arg;
  
return  NULL;
}
/*  读取设置某个文件的字符串方法,对于文件,判断文件是否有效  */
static   const   char   * set_jira_group_file(cmd_parms  * cmd,
                                          
void   * mconfig,
                                          
const   char   * name)
{
  auth_jira_conf 
* conf  =  (auth_jira_conf  * ) mconfig;
  
/* ap_configfile_t *f = NULL;
  apr_status_t status;

  if(strlen(name) <= 0)
    return "File can not be null.";

  status = ap_pcfg_openfile(&f, cmd->pool, name);

  if (status != APR_SUCCESS) {
    return “Can not open given  file.”;
  }
  ap_cfg_closefile(f);
*/

  conf
-> gpfpath  =  apr_pstrdup(cmd -> pool, name);

  
return  NULL;
}
/*  init per dir  */
static   void   * create_authjira_dir_config(apr_pool_t  * p,  char   * d)
{
  auth_jira_conf 
* conf  =  (auth_jira_conf  * )apr_pcalloc(p,  sizeof ( * conf));
  
if (conf  ==  NULL)  return  NULL;
  conf
-> gpfpath          =  d;
  
return  conf;
}

// 对应的http.conf的命令读到方法。
static   const  command_rec authn_jira_cmds[]  =
{
  AP_INIT_FLAG(
" IsAllCheck " , set_authjira_is_all_check, NULL, OR_FILEINFO,
  
" enable authSVN or not. " ),
  AP_INIT_TAKE1(
" filepath " , set_jira_group_file,   NULL, OR_FILEINFO,
  
" set jira group file. " ),
  { NULL }
};

/*  Dispatch list for API hooks  */
module AP_MODULE_DECLARE_DATA pathtest_module 
=  {
          STANDARD20_MODULE_STUFF,
         create_authjira_dir_config, 
/*  create per-dir    config structures  */
          NULL,                  
/*  merge per-dir    config structures  */
          NULL,                  
/*  create per-server config structures  */
          NULL,                  
/*  merge per-server config structures  */
          authn_jira_cmds,                  
/*  table of config file commands        */
          pathtest_register_hooks 
/*  register hooks                       */
};


然后在配置文件中添加

 

<Location />
IsAllCheck on
filepath /usr/local/abc
SetHandler pathtest

</Location>

通过apxs -a -c -i /path_to/mod_pathtest.c 添加该模块。

重启apache ,打开http://localhost/

就可以看到读取到了配置文件信息。

 

 参考网址:http://www.apachetutor.org/dev/config

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值