httpd-modules-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sorin Manolache <sor...@gmail.com>
Subject Re: How to access string in a module that is set in configuration directive?
Date Sat, 30 Jun 2012 21:15:48 GMT
On 2012-06-30 22:33, ohaya@cox.net wrote:
> Hi,
>
> I got my 1st module working and now I need to add support for a configuration directive
that sets a string that my module needs in various places, i.e., if my directive is "SetMyVar",
and httpd.conf has:
>
> SetMyVar    "foo123"
>
> then, in my module, I need to able to access the string, "foo123", that got set via the
"SetMyVar" directive.  This directive would only be used at the server level.
>
> I think that I know how to do the code to set the variable into my module, but the question
that I have is how do I *access* it after it is set?
>
> Here's the code I have to handle the directive:
>
> /*
>   * Stuff for Directive handling
>   */
>
> // Struct for Directives
> typedef struct txt_cfg {
>      const char * MyVar;
> } txt_cfg;
>
>
> // Function to get GxInterceptorURL
> static const char * txt_set_MyVar(cmd_parms* cmd, void* cfg, const char* val) {
>      printf("In set_MyVar: Setting MyVar to [%s]\n", val);
>      ((txt_cfg*) cfg)->MyVar = val;
> } // end txt_set_MyVar()
>
>
> //
> static const command_rec txt_cmds[] = {
>      AP_INIT_TAKE1("SetMyVar", txt_set_MyVar, NULL, OR_ALL,
>              "This is MyVar"),
>      { NULL }
> };
> .
> .
> .
> module AP_MODULE_DECLARE_DATA my_module =
> {
>      STANDARD20_MODULE_STUFF,
>      NULL,                       /* dir config creater */
>      NULL,                       /* dir merger --- default is to override */
>      NULL,                       /* server config */
>      NULL,                       /* merge server configs */
>      txt_cmds,                       /* command apr_table_t */
>      register_hooks              /* register hooks */
> };
>
>
> Can anyone tell me how, in my module code, I can access that "MyVar" string?
>

You don't get a valid configuration object (txt_cfg) in the cfg argument 
of the txt_set_MyVar unless you create that configuration object. As 
your code looks now, cfg in txt_set_MyVar should be null.

You'll have to set a callback function for creating the configuration 
object. The configuration object configuration functions are 
dir_config_creater (first after STANDARD20_MODULE_STUFF in my_module) 
and server_config (third after  STANDARD20_MODULE_STUFF).

The dir_config creator will put your configuration object in 
r->per_dir_config and you get it with

txt_cfg *cfg = (txt_cfg *)ap_get_module_config(r->per_dir_config, 
&my_module);

The server_config_creator will put your configuration object in 
r->server->module_config and you get it with

txt_cfg *cfg = (txt_cfg *)ap_get_module_config(r->server->module_config, 
&my_module);


Have a look in include/http_config.h at all those OR_* macros and 
especially at RSRC_CONF and ACCESS_CONF.

RSRC_CONF is used mainly to define server-wide directives. ACCESS_CONF 
is used to define directory-wide configuration directives.

In your cfg argument of the txt_set_MyVar configuration directive 
handler, you'll get the _directory-wide_ configuration object (if you've 
created one by using the dir_config_creator in module my_module). If you 
want to set your variable just for the directory, you can keep 
txt_set_MyVar as it is now. Later in the module, you can retrieve the 
value as I showed above, i.e. from r->per_dir_config.

If you want to set it server-wide, then don't use cfg. Write

txt_cfg *srv_cfg = (txt_cfg 
*)ap_get_module_config(cmd->server->module_config, &my_module).

Later in the module, you can retrieve the value as I showed above, i.e. 
from r->server->module_config.

Remember that in any case, you have to create the respective 
configuration object (via the callbacks in my_module).

S


> Thanks,
> Jim
>



Mime
View raw message