You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by Julie Larson <jl...@verity.com> on 2004/07/08 19:41:41 UTC

[users@httpd] Apache 2 module - per-directory config question

Hello,

I'd like desperately to set up Apache 2 to run multiple instances of a
dynamic module, but with a different configuration string for each
one.

Here is how I configured the httpd.conf file for the module

...

LoadModule thing_module modules/mod_thing.so

...

<LocationMatch /A>
        SetHandler thing-handler
        MyString "I'm the A module"
</LocationMatch>

<LocationMatch /B>
        SetHandler thing-handler
        MyString "I'm the B module"
</LocationMatch>


here is my code:  Basically it is a module that takes a string from
the configuration and dumps it to the error log

The problem is, when I try to set the per-directory config creator,
apache core dumps.  If I set the server-wide config creator, apache
starts up fine, but then the function MyString only takes the last
value in the file ("I'm the B module") and applies it to every module.


How can I get a per-module configuration without a core dump?

Thanks very much for any clues!
-Julie




#include "httpd.h"
#include "http_config.h"
#include "http_core.h"
#include "http_log.h"
#include "http_main.h"
#include "http_protocol.h"
#include "http_request.h"
#include "util_script.h"
#include "http_connection.h"

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/un.h>

#ifndef DEFAULT_THING_STRING
#define DEFAULT_THING_STRING "default thing string"
#endif

#define ERROR(mess, r)
ap_log_error(APLOG_MARK,APLOG_NOERRNO|APLOG_ERR, 0, r->server, mess );

typedef struct {
        char* m_szMyString;
} mod_thing_config;

module AP_MODULE_DECLARE_DATA thing_module;

thing_handler(request_rec *r)
{
        if (strcmp(r->handler, "thing-handler"))
                return DECLINED;


        ap_set_content_type(r, "text/html");

        if ( r->header_only )
        {
                return OK;
        }

        mod_thing_config* pConf =
ap_get_module_config(r->server->module_config, &thing_module);
        char* szMyString = pConf->m_szMyString;


        if ( ! szMyString || strlen( szMyString ) == 0 )
        {
                ERROR( "unable to read configuration", r );
                return OK;
        }
        ERROR( "my string is %s", szMyString, r );

        return OK;
}

static void* create_mod_thing_dir_config(apr_pool_t* p, char* d)
{
        mod_thing_config* conf = (mod_thing_config*) apr_pcalloc(p,
sizeof(mod_thing_config));
        char* szMemory = (char*) apr_pcalloc( p, 1025 );
        memset( szMemory, 0, 1025 );
        strcpy( szMemory, DEFAULT_THING_STRING );
        conf->m_szMyString = szMemory;
        return (void*) conf;
}


/* Each function our module provides to handle a particular hook is
 * specified here. See mod_example.c for more information about this
 * step. Suffice to say that we need to list all of our handlers in
 * here. */
static void x_register_hooks(apr_pool_t *p)
{
        ap_hook_handler(thing_handler, NULL, NULL, APR_HOOK_MIDDLE);
}

/** * Apache2 calls this function when our configuration node is parsed.  */
static const char* set_mod_thing_string(cmd_parms *parms, void *mconfig,
const char* szArg)
{
        mod_thing_config* s_cfg =
ap_get_module_config(parms->server->module_config, &thing_module);
        s_cfg->m_szMyString = (char*) szArg;
        // success
        return NULL;
}

/**
* Null terminated list of the possible configuration directives.
* Right now, we have only one for the string */

static const command_rec mod_thing_cmds[] =
{
  AP_INIT_TAKE1( "MyString", set_mod_thing_string, NULL, ACCESS_CONF,
                "MyString <some string> -- The string associated with this
module.  For example, 'foo bar'"),
        {NULL}
};

static void* create_server_config(apr_pool_t *p, server_rec *s)
{
        mod_thing_config* conf = (mod_thing_config*) apr_pcalloc(p,
sizeof(mod_thing_config));
        char* szMemory = (char*) apr_pcalloc( p, 1025 );
        memset( szMemory, 0, 1025 );
        strcpy( szMemory, DEFAULT_THING_STRING );
        conf->m_szMyString = szMemory;
        return (void*) conf;
}


/* Module definition for configuration. We list all callback routines
 * that we provide the static hooks into our module from the other parts
 * of the server. This list tells the server what function will handle a
 * particular type or stage of request. If we don't provide a function
 * to handle a particular stage / callback, use NULL as a placeholder as
 * illustrated below. */
module AP_MODULE_DECLARE_DATA thing_module =
{
        STANDARD20_MODULE_STUFF,
        NULL, /*create_mod_thing_dir_config, using this here causes core
dump! * per-directory config creator */
        NULL,           /* directory config merger */
        create_server_config,           /* server config creator */
        NULL,           /* server config merger */
        mod_thing_cmds,         /* command table */
        x_register_hooks,       /* other request processing hooks */
};


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Apache 2 module - per-directory config question

Posted by Robert Andersson <ro...@profundis.nu>.
Julie Larson wrote:
> The problem is, when I try to set the per-directory config creator,
> apache core dumps.  If I set the server-wide config creator, apache
> starts up fine, but then the function MyString only takes the last
> value in the file ("I'm the B module") and applies it to every module.

You should use per-directory configuration.

> How can I get a per-module configuration without a core dump?

You need to fix the bug(s).

I don't see any glaring error, but it has been a while since worked with C.
All those casted pointers and various pools makes it quite fragile. I
suggest you try to track down exactly where it goes wrong:
- Does it happen when your create_dir_config() function doesn't do anything?
- Does it work if the set_mod_thing_string() doesn' do anything?
- Etc...


Regards,
Robert Andersson


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org