From c387626c30e7898fd52431e8be03c78be69e1a03 Mon Sep 17 00:00:00 2001 From: erickson Date: Fri, 5 Aug 2005 15:37:58 +0000 Subject: [PATCH] added apache config file reading, yay... more to come git-svn-id: svn://svn.open-ils.org/ILS/trunk@1617 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- Open-ILS/src/apachemods/Makefile | 9 ++- Open-ILS/src/apachemods/mod_xmltools.c | 93 ++++++++++++++++++-------- Open-ILS/src/apachemods/xmltools.c | 63 ----------------- 3 files changed, 73 insertions(+), 92 deletions(-) diff --git a/Open-ILS/src/apachemods/Makefile b/Open-ILS/src/apachemods/Makefile index 1932c7efed..0a717e7fa1 100644 --- a/Open-ILS/src/apachemods/Makefile +++ b/Open-ILS/src/apachemods/Makefile @@ -1,7 +1,14 @@ +# --------------------------------------------------------------------------------- +CC_OPTS = -I /usr/include/libxml2 -I /opt/include -g +APXS2 = /home/erickson/sandbox/apache2/bin/apxs +# --------------------------------------------------------------------------------- + LD_OPTS += -lxml2 -lc_utils all: mod_xmltools.so +install: mod_xmltools-install + mod_xmltools.so: apachetools.o xmltools.o $(CC) -c $(CC_OPTS) mod_xmltools.c $(CC) $(LD_OPTS) -shared -W1 apachetools.o xmltools.o mod_xmltools.o -o $@ @@ -13,7 +20,7 @@ xmltools.o: xmltools.c xmltools.h $(CC) -c $(CC_OPTS) xmltools.c -o $@ -install: +mod_xmltools-install: $(APXS2) -i -a -n mod_xmltools mod_xmltools.so @echo "-----------------------------------------------"; @echo -e "* Important * : Change httpd.conf from this: \n \ diff --git a/Open-ILS/src/apachemods/mod_xmltools.c b/Open-ILS/src/apachemods/mod_xmltools.c index 41caeb6b83..e96c78144e 100644 --- a/Open-ILS/src/apachemods/mod_xmltools.c +++ b/Open-ILS/src/apachemods/mod_xmltools.c @@ -1,44 +1,57 @@ -#include "apachetools.h" -#include "xmltools.h" +#include "mod_xmltools.h" -#define MODULE_NAME "mod_xmltools" /* our module name */ -#define PARAM_LOCALE "locale" /* the URL param for the local directory */ -#define LANG_DTD "lang.dtd" /* the DTD for the test entities */ -/* these should be config directives */ -#define LOCALE_DIR "/home/erickson/sandbox/apachemods/locale" /* The root directory where the local files are stored */ -#define DEFAULT_LOCALE "en-US" /* If no locale data is provided */ - - -/* Child Init */ -static void mod_xmltools_child_init(apr_pool_t *p, server_rec *s) { +/* Configuration handlers -------------------------------------------------------- */ +static const char* mod_xmltools_set_locale_dir(cmd_parms *parms, void *config, const char *arg) { + mod_xmltools_config *cfg = ap_get_module_config(parms->server->module_config, &mod_xmltools); + cfg->locale_dir = (char*) arg; + return NULL; } -/* allocates a char* to hold the name of the DTD language file - Prints to stderr and returns NULL if there was an error loading the file - */ - -static char* get_dtd_lang_file(string_array* params) { +static const char* mod_xmltools_set_default_locale(cmd_parms *parms, void *config, const char *arg) { + mod_xmltools_config *cfg = ap_get_module_config(parms->server->module_config, &mod_xmltools); + cfg->default_locale = (char*) arg; + return NULL; +} - char* localedir = apacheGetFirstParamValue(params, PARAM_LOCALE); - if(!localedir) localedir = strdup(DEFAULT_LOCALE); +/* tell apache about our commands */ +static const command_rec mod_xmltools_cmds[] = { + AP_INIT_TAKE1( "XMLToolsDefaultLocale", mod_xmltools_set_default_locale, + NULL, RSRC_CONF, "XMLToolsDefaultLocale - Test"), + AP_INIT_TAKE1( "XMLToolsLocaleDir", mod_xmltools_set_locale_dir, + NULL, RSRC_CONF, "XMLToolsLocaleDir - Test"), + {NULL} +}; - int len = strlen(LANG_DTD) + strlen(localedir) + strlen(LOCALE_DIR) + 1; - char dtdfile[len]; - bzero(dtdfile, len); +/* build the config object */ +static void* mod_xmltools_create_config( apr_pool_t* p, server_rec* s) { + mod_xmltools_config* cfg = + (mod_xmltools_config*) apr_palloc(p, sizeof(mod_xmltools_config)); + cfg->default_locale = DEFAULT_LOCALE; + cfg->locale_dir = DEFAULT_LOCALE_DIR; + return (void*) cfg; +} - if(localedir) - sprintf(dtdfile, "%s/%s/%s", LOCALE_DIR, localedir, LANG_DTD ); - return strdup(dtdfile); +/* Child Init handler ----------------------------------------------------------- */ +static void mod_xmltools_child_init(apr_pool_t *p, server_rec *s) { } + +/* Request handler -------------------------------------------------------------- */ static int mod_xmltools_handler (request_rec* r) { /* make sure we're needed first thing*/ if (strcmp(r->handler, MODULE_NAME )) return DECLINED; + mod_xmltools_config *cfg = ap_get_module_config(r->server->module_config, &mod_xmltools); + char* locale_dir = cfg->locale_dir; + char* default_locale = cfg->default_locale; + + fprintf(stderr, "%s : %s\n", locale_dir, default_locale ); + fflush(stderr); + /* we accept get/post requests */ r->allowed |= (AP_METHOD_BIT << M_GET); r->allowed |= (AP_METHOD_BIT << M_POST); @@ -48,7 +61,7 @@ static int mod_xmltools_handler (request_rec* r) { string_array* params = apacheParseParms(r); char* file = r->filename; - char* dtdfile = get_dtd_lang_file(params); + char* dtdfile = get_dtd_lang_file(params, default_locale, locale_dir ); xmlDocPtr doc; @@ -99,18 +112,42 @@ static int mod_xmltools_handler (request_rec* r) { } +/* register callbacks */ static void mod_xmltools_register_hooks (apr_pool_t *p) { ap_hook_handler(mod_xmltools_handler, NULL, NULL, APR_HOOK_MIDDLE); ap_hook_child_init(mod_xmltools_child_init,NULL,NULL,APR_HOOK_MIDDLE); } + +/* finally, flesh the module */ module AP_MODULE_DECLARE_DATA mod_xmltools = { STANDARD20_MODULE_STUFF, NULL, NULL, + mod_xmltools_create_config, NULL, - NULL, - NULL, + mod_xmltools_cmds, mod_xmltools_register_hooks, }; + + +/* UTILITY FUNCTIONS ----------------------------------------------------- */ +char* get_dtd_lang_file(string_array* params, char* default_locale, char* locale_dir) { + + /* if no locale is provided via URL, we use the default */ + char* locale = apacheGetFirstParamValue(params, PARAM_LOCALE); + if(!locale) locale = default_locale; + if(!locale) return NULL; + + int len = strlen(LANG_DTD) + strlen(locale) + strlen(locale_dir) + 1; + char dtdfile[len]; + bzero(dtdfile, len); + + if(locale) + sprintf(dtdfile, "%s/%s/%s", locale_dir, locale, LANG_DTD ); + + return strdup(dtdfile); +} + + diff --git a/Open-ILS/src/apachemods/xmltools.c b/Open-ILS/src/apachemods/xmltools.c index fb92b89532..c9f6c97f44 100644 --- a/Open-ILS/src/apachemods/xmltools.c +++ b/Open-ILS/src/apachemods/xmltools.c @@ -1,65 +1,5 @@ #include "xmltools.h" -#define TEXT_DTD "test2.dtd" - - - -/* -int main( int argc, char* argv[] ) { - - char* file = argv[1]; - char* localedir = argv[2]; - - int len = strlen(TEXT_DTD) + strlen(localedir) + 1; - char dtdfile[len]; - bzero(dtdfile, len); - - if(localedir) - sprintf(dtdfile, "%s/%s", localedir, TEXT_DTD ); - - - if(access(dtdfile, R_OK)) { - fprintf(stderr, "Unable to open DTD file %s\n", dtdfile); - fflush(stderr); - return HTTP_INTERNAL_SERVER_ERROR; - } - - - xmlDocPtr doc; - - xmlSubstituteEntitiesDefault(0); - - - if( (doc = xmlParseFile(file)) == NULL) { - fprintf(stderr, "\n ^-- Error parsing XML file %s\n", file); - fflush(stderr); - return HTTP_INTERNAL_SERVER_ERROR; - } - - if( xmlXIncludeProcess(doc) < 0 ) { - fprintf(stderr, "\n ^-- Error processing XIncludes for file %s\n", file); - fflush(stderr); - return HTTP_INTERNAL_SERVER_ERROR; - } - - xmlReplaceDtd(doc, dtdfile); - - doc = xmlProcessDtdEntities(doc); - - char* xml = xmlDocToString(doc, 0); - - printf("\n%s\n", xml); - - free(xml); - xmlFreeDoc(doc); - xmlCleanupCharEncodingHandlers(); - xmlCleanupParser(); - - return 0; - -} -*/ - xmlDocPtr xmlProcessDtdEntities(xmlDocPtr doc) { char* xml = xmlDocToString(doc, 1); xmlFreeDoc(doc); @@ -90,9 +30,6 @@ int xmlReplaceDtd(xmlDocPtr doc, char* dtdfile) { return -1; } - fprintf(stderr, "2\n"); - fflush(stderr); - dtd->name = xmlStrdup((xmlChar*)"x"); doc->extSubset = dtd; dtd->doc = doc; -- 2.43.2