Search code examples
apachemodule

Can I have a custom apache module use different values for variables based upon Directory directives in the conf file?


I have a custom apache module written in C. It's job is to take the information from the api and send it to a server listening on a specific host/port and return the resulting json. I set the host/port in the conf file so that it is configurable. This is working well for one version of the api. My working conf file looks like this:

LoadModule Mine_module /etc/httpd/dev_modules/mine.so
<Directory "/var/www/html/api/v9.1">
  apiHost localhost
  apiPort 2024
  SetHandler mine
  LogLevel info
</Directory>

I use the apache AP_INIT_TAKE1 command to load the values into variables in the directives call. Now I want the same one module to be able to talk to multiple host/port combinations depending upon the directory. I tried to make the conf file like:

LoadModule Mine_module /etc/httpd/dev_modules/mine.so
<Directory "/var/www/html/api/v9.1">
  apiHost localhost
  apiPort 2024
  SetHandler mine
  LogLevel info
</Directory>
<Directory "/var/www/html/api/v9.2">
  apiHost host2
  apiPort 2025
  SetHandler mine
  LogLevel info
</Directory>

My hope had been since the directories were different any calls to /api/v9.1/user would talk to the server running on localhost/2024 and calls to /api/v9.2/user would call the server on host2/2025. However, any request to v9.1 or v9.2 results in the host/port combination that is listed last in the configuration file. This is a module specific conf file, but maybe it needs to be called a different way? The file is located in the conf.modules.d directory. Can someone please let me know what I am doing wrong and if there is a way for me to do this?


Solution

  • I found my problem. It was not in the config file; it was in my directives structure. I moved my population for the directives structure from the http line to the per directory line as follows:

    module AP_MODULE_DECLARE_DATA Mine_module = {
        STANDARD20_MODULE_STUFF,
        create_mine_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 */
        mine_directives,        /* table of config file commands       */
        Mine_register_hooks     /* register hooks                      */
    };
    

    After adding the call to create_mine_dir_config the module began picking up the correct values.