]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/xinclude/mod_xinclude.c
022a149439278a28b2461df366846484c76fddfa
[OpenSRF.git] / src / xinclude / mod_xinclude.c
1 #include "httpd.h"
2 #include "http_config.h"
3 #include "http_core.h"
4 #include "http_protocol.h"
5 #include "apr_compat.h"
6 #include "apr_strings.h"
7
8 #include <libxml/parser.h>
9 #include <libxml/xinclude.h>
10
11 #define MODULE_NAME "xinclude_module"
12
13 static int mod_xinclude_handler (request_rec *r) {
14
15         /* make sure we're needed first thing*/
16         if (strcmp(r->handler, MODULE_NAME )) 
17                 return DECLINED;
18
19         /* set content type */
20         ap_set_content_type(r, "text/html");
21
22
23         /* which file are we parsing */
24         char* file = r->filename;
25
26         if(!file) { 
27                 fprintf(stderr, "No XML file to parse");
28                 return HTTP_INTERNAL_SERVER_ERROR;
29         }
30
31         /* parse the doc */
32         xmlDocPtr doc = xmlParseFile(file);
33
34         if(!doc) {
35                 fprintf(stderr, "Error parsing XML file %s\n", file);
36                 return HTTP_INTERNAL_SERVER_ERROR;
37         }
38
39         /* process the xincludes */
40         int status = xmlXIncludeProcess(doc);
41         
42         if(status < 0) {
43                 fprintf(stderr, "Error processing XIncludes in  XML file %s\n", file);
44                 return HTTP_INTERNAL_SERVER_ERROR;
45         }
46
47         xmlBufferPtr xmlbuf = xmlBufferCreate();
48         xmlNodeDump( xmlbuf, doc, xmlDocGetRootElement(doc), 0, 0);
49         char* xml = (char*) (xmlBufferContent(xmlbuf));
50
51         ap_rputs(xml,r);
52
53         xmlBufferFree(xmlbuf);
54         xmlFreeDoc(doc);
55
56         return OK;
57 }
58
59
60 static void mod_xinclude_register_hooks (apr_pool_t *p) {
61         ap_hook_handler(mod_xinclude_handler, NULL, NULL, APR_HOOK_MIDDLE);
62 }
63
64 module AP_MODULE_DECLARE_DATA xinclude_module = {
65         STANDARD20_MODULE_STUFF,
66         NULL,
67         NULL,
68         NULL,
69         NULL,
70         NULL,
71         mod_xinclude_register_hooks,
72 };
73