]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/apachemods/mod_xmltools.c
added apache config file reading, yay... more to come
[working/Evergreen.git] / Open-ILS / src / apachemods / mod_xmltools.c
1 #include "mod_xmltools.h"
2
3
4 /* Configuration handlers -------------------------------------------------------- */
5 static const char* mod_xmltools_set_locale_dir(cmd_parms *parms, void *config, const char *arg) {
6         mod_xmltools_config  *cfg = ap_get_module_config(parms->server->module_config, &mod_xmltools);
7         cfg->locale_dir = (char*) arg;
8         return NULL;
9 }
10
11 static const char* mod_xmltools_set_default_locale(cmd_parms *parms, void *config, const char *arg) {
12         mod_xmltools_config *cfg = ap_get_module_config(parms->server->module_config, &mod_xmltools);
13         cfg->default_locale = (char*) arg;
14         return NULL;
15 }
16
17 /* tell apache about our commands */
18 static const command_rec mod_xmltools_cmds[] = {
19         AP_INIT_TAKE1( "XMLToolsDefaultLocale", mod_xmltools_set_default_locale,
20                 NULL, RSRC_CONF, "XMLToolsDefaultLocale - Test"),
21         AP_INIT_TAKE1( "XMLToolsLocaleDir", mod_xmltools_set_locale_dir,
22                 NULL, RSRC_CONF, "XMLToolsLocaleDir - Test"),
23         {NULL}
24 };
25
26 /* build the config object */
27 static void* mod_xmltools_create_config( apr_pool_t* p, server_rec* s) {
28         mod_xmltools_config* cfg = 
29                 (mod_xmltools_config*) apr_palloc(p, sizeof(mod_xmltools_config));
30         cfg->default_locale = DEFAULT_LOCALE;
31         cfg->locale_dir = DEFAULT_LOCALE_DIR;
32         return (void*) cfg;
33 }
34
35
36 /* Child Init handler  ----------------------------------------------------------- */
37 static void mod_xmltools_child_init(apr_pool_t *p, server_rec *s) {
38 }
39
40
41 /* Request handler  -------------------------------------------------------------- */
42 static int mod_xmltools_handler (request_rec* r) {
43
44         /* make sure we're needed first thing*/
45         if (strcmp(r->handler, MODULE_NAME )) 
46                 return DECLINED;
47
48         mod_xmltools_config *cfg = ap_get_module_config(r->server->module_config, &mod_xmltools);
49         char* locale_dir = cfg->locale_dir;
50         char* default_locale = cfg->default_locale;
51
52         fprintf(stderr, "%s : %s\n", locale_dir, default_locale );
53         fflush(stderr);
54
55         /* we accept get/post requests */
56         r->allowed |= (AP_METHOD_BIT << M_GET);
57         r->allowed |= (AP_METHOD_BIT << M_POST);
58
59         ap_set_content_type(r, "text/html");
60
61         string_array* params = apacheParseParms(r);
62
63         char* file = r->filename;
64         char* dtdfile = get_dtd_lang_file(params, default_locale, locale_dir );
65
66         xmlDocPtr doc;
67
68         /* be explicit */
69         xmlSubstituteEntitiesDefault(0);
70
71         /* parse the doc */
72         if( (doc = xmlParseFile(file)) == NULL) {
73                 fprintf(stderr, "\n ^-- Error parsing XML file %s\n", file);
74                 fflush(stderr);
75                 return HTTP_INTERNAL_SERVER_ERROR;
76         }
77
78         /* process xincludes */
79         if( xmlXIncludeProcess(doc) < 0 ) {
80                 fprintf(stderr, "\n ^-- Error processing XIncludes for file %s\n", file);
81                 fflush(stderr);
82                 return HTTP_INTERNAL_SERVER_ERROR;
83         }
84
85
86         /* replace the DTD */
87         if(xmlReplaceDtd(doc, dtdfile) < 0) {
88                 fprintf(stderr, "Error replacing DTD file with file %s\n", dtdfile);
89                 fflush(stderr);
90                 return HTTP_INTERNAL_SERVER_ERROR;
91         }
92
93
94         /* force DTD entity replacement */
95         doc = xmlProcessDtdEntities(doc);
96
97         /* stringify */
98         char* xml = xmlDocToString(doc, 0);
99
100         /* print the doc */
101         ap_rputs(xml, r);
102
103         /* deallocate */
104         free(dtdfile);
105         free(xml);
106         xmlFreeDoc(doc);
107         xmlCleanupCharEncodingHandlers();
108         xmlCleanupParser();
109         
110         return OK;
111
112 }
113
114
115 /* register callbacks */
116 static void mod_xmltools_register_hooks (apr_pool_t *p) {
117         ap_hook_handler(mod_xmltools_handler, NULL, NULL, APR_HOOK_MIDDLE);
118         ap_hook_child_init(mod_xmltools_child_init,NULL,NULL,APR_HOOK_MIDDLE);
119 }
120
121
122 /* finally, flesh the module */
123 module AP_MODULE_DECLARE_DATA mod_xmltools = {
124         STANDARD20_MODULE_STUFF,
125         NULL,
126         NULL,
127         mod_xmltools_create_config,
128         NULL,
129         mod_xmltools_cmds,
130         mod_xmltools_register_hooks,
131 };
132
133
134
135 /* UTILITY FUNCTIONS ----------------------------------------------------- */
136 char* get_dtd_lang_file(string_array* params, char* default_locale, char* locale_dir) {
137
138         /* if no locale is provided via URL, we use the default */
139         char* locale = apacheGetFirstParamValue(params, PARAM_LOCALE);
140         if(!locale) locale = default_locale;
141         if(!locale) return NULL;
142
143         int len = strlen(LANG_DTD) + strlen(locale) + strlen(locale_dir) + 1;
144         char dtdfile[len];
145         bzero(dtdfile, len);
146
147         if(locale)
148                 sprintf(dtdfile, "%s/%s/%s",  locale_dir, locale, LANG_DTD );
149
150         return strdup(dtdfile);
151 }
152
153