You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modules-dev@httpd.apache.org by oh...@cox.net on 2012/06/30 22:33:34 UTC

How to access string in a module that is set in configuration directive?

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?

Thanks,
Jim


Re: How to access string in a module that is set in configuration directive?

Posted by oh...@cox.net.
---- Sorin Manolache <so...@gmail.com> wrote: 
> 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
> >
> 
> 


Hi Sorin,

I had been following info in a book, but either missed that part about creating the configuration object.  That's why I was having a hard time understanding how to actually use the configuration info in my module (e.g., how to get a reference to the object).

I've since found this tutorial:

http://threebit.net/tutorials/apache2_modules/tut2/tutorial2.html

and will be revising and testing my code, and will post back.

Thanks again,
Jim

Re: How to access string in a module that is set in configuration directive?

Posted by Sorin Manolache <so...@gmail.com>.
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
>



Re: How to access string in a module that is set in configuration directive?

Posted by oh...@cox.net.
---- Daniel Gruno <ru...@cord.dk> wrote: 
> On 06/30/2012 10:33 PM, ohaya@cox.net wrote:
> > Hi,.....
> > Can anyone tell me how, in my module code, I can access that "MyVar" string?
> > 
> > Thanks,
> > Jim
> > 
> 
> 
> You can either assign your configuration to a statically declared
> struct, or check out
> http://httpd.apache.org/docs/trunk/developer/modguide.html#context for
> some hints on how to use configurations.
> 
> With regards,
> Daniel.


Hi Daniel,

Thanks for the info and that link.  I hadn't seen that before.

Jim


Re: How to access string in a module that is set in configuration directive?

Posted by Daniel Gruno <ru...@cord.dk>.
On 06/30/2012 10:33 PM, ohaya@cox.net wrote:
> Hi,.....
> Can anyone tell me how, in my module code, I can access that "MyVar" string?
> 
> Thanks,
> Jim
> 


You can either assign your configuration to a statically declared
struct, or check out
http://httpd.apache.org/docs/trunk/developer/modguide.html#context for
some hints on how to use configurations.

With regards,
Daniel.