File: xmlconf.c

package info (click to toggle)
uwsgi 2.0.29-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,684 kB
  • sloc: ansic: 87,027; python: 7,001; cpp: 1,131; java: 708; perl: 678; sh: 585; ruby: 555; makefile: 148; xml: 130; cs: 121; objc: 37; php: 28; erlang: 20; javascript: 11
file content (284 lines) | stat: -rw-r--r-- 6,513 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#ifdef UWSGI_XML

#include "uwsgi.h"

extern struct uwsgi_server uwsgi;

#ifdef UWSGI_XML_LIBXML2

#include <libxml/parser.h>
#include <libxml/tree.h>

void uwsgi_xml_config(char *filename, struct wsgi_request *wsgi_req, char *magic_table[]) {
	xmlDoc *doc = NULL;
	xmlNode *element = NULL;
	xmlNode *node = NULL;

	xmlChar *node_mode;

	char *colon;

	char *xml_id;
	char *xml_content;
	size_t xml_size = 0;

	LIBXML_TEST_VERSION

	if (uwsgi_check_scheme(filename)) {
		colon = uwsgi_get_last_char(filename, '/');
		colon = uwsgi_get_last_char(colon, ':');
	}
	else {
		colon = uwsgi_get_last_char(filename, ':');
	}
	if (colon) {
		colon[0] = 0;
		colon++;
		if (*colon == 0) {
			uwsgi_log("invalid xml id\n");
			exit(1);
		}
		uwsgi_log("[uWSGI] using xml uwsgi id: %s\n", colon);
	}

	xml_content = uwsgi_open_and_read(filename, &xml_size, 0, magic_table);

	doc = xmlReadMemory(xml_content, xml_size, filename, NULL, 0);
	if (doc == NULL) {
		uwsgi_log("[uWSGI] could not parse file %s.\n", filename);
		exit(1);
	}

	uwsgi_log_initial("[uWSGI] parsing config file %s\n", filename);

	element = xmlDocGetRootElement(doc);
	if (element == NULL) {
		uwsgi_log("[uWSGI] invalid xml config file.\n");
		exit(1);
	}
	if (strcmp((char *) element->name, "uwsgi")) {
		for (node = element->children; node; node = node->next) {
			element = NULL;
			if (node->type == XML_ELEMENT_NODE) {
				if (!strcmp((char *) node->name, "uwsgi")) {
					xml_id = (char *) xmlGetProp(node, (const xmlChar *) "id");

					if (colon && xml_id) {
						if (strcmp(colon, xml_id)) {
							continue;
						}
					}
					element = node;
					break;
				}
			}
		}

		if (!element) {
			uwsgi_log("[uWSGI] invalid xml root element, <uwsgi> expected.\n");
			exit(1);
		}
	}


	// first check for options
	for (node = element->children; node; node = node->next) {

		node_mode = xmlGetProp(node, (const xmlChar *) "mode");
		if (uwsgi.mode && node_mode) {
			if (strcmp(uwsgi.mode, (char *) node_mode)) {
				continue;
			}
		}

		xml_id = (char *) xmlGetProp(node, (const xmlChar *) "id");
		if (colon && xml_id) {
			if (strcmp(colon, xml_id)) {
				continue;
			}
		}

		if (node->type == XML_CDATA_SECTION_NODE) {
			if (node->content) {
				add_exported_option((char *) "eval", strdup((char *) node->content), 0);
			}
		}
		else if (node->type == XML_ELEMENT_NODE) {

			if (!strcmp((char *) node->name, (char *) "app")) {

				char *mountpoint = (char *) xmlGetProp(node, (const xmlChar *) "mountpoint");
				char *domain = (char *) xmlGetProp(node, (const xmlChar *) "domain");

				if (!node->children) {
					add_exported_option("app", strdup(""), 0);
				}
				else if (node->children && node->children->content && !node->children->next) {
					char *opt_value = strdup((char *) node->children->content);

					if (mountpoint) {
						opt_value = uwsgi_concat3(mountpoint, "=", opt_value);
					}
					else if (domain) {
						opt_value = uwsgi_concat3(domain, "|=", opt_value);
					}
					add_exported_option("mount", opt_value, 0);
					add_exported_option("app", strdup(""), 0);
				}
				else if (node->children && node->children->next && node->children->next->children && node->children->next->children->content) {

					char *opt_value = strdup((char *) node->children->next->children->content);

					if (mountpoint) {
						opt_value = uwsgi_concat3(mountpoint, "=", opt_value);
					}
					else if (domain) {
						opt_value = uwsgi_concat3(domain, "|=", opt_value);
					}

					add_exported_option("mount", opt_value, 0);
					add_exported_option("app", strdup(""), 0);
				}
			}
			else {

				if (node->children) {
					add_exported_option(strdup((char *) node->name), strdup((char *) node->children->content), 0);
				}
				else {
					add_exported_option(strdup((char *) node->name), strdup("1"), 0);
				}
			}
		}
	}
	/* We can safely free resources */

	if (colon) colon[0] = ':';

	xmlFreeDoc(doc);
	xmlCleanupParser();

}


#endif

#ifdef UWSGI_XML_EXPAT

#include <expat.h>

int uwsgi_xml_found_stanza = 0;
char *uwsgi_xml_found_opt_key = NULL;

static void startElement(void *xml_id, const XML_Char * name, const XML_Char ** attrs) {


	if (!uwsgi_xml_found_stanza) {
		if (xml_id) {
			if (!attrs[0])
				return;
			if (!attrs[1])
				return;
			if (strcmp("id", attrs[0]))
				return;
			if (strcmp((char *) xml_id, attrs[1]))
				return;
		}
		if (!strcmp("uwsgi", name))
			uwsgi_xml_found_stanza = 1;
	}
	else {
		uwsgi_xml_found_opt_key = (char *) name;
	}
}


static void textElement(void *xml_id, const char *s, int len) {

	if (!uwsgi_xml_found_stanza)
		return;
	if (uwsgi_xml_found_opt_key) {
		add_exported_option(strdup(uwsgi_xml_found_opt_key), uwsgi_concat2n((char *) s, len, (char *) "", 0), 0);
		uwsgi_xml_found_opt_key = NULL;
	}
}

static void endElement(void *xml_id, const XML_Char * name) {

	if (!uwsgi_xml_found_stanza)
		return;

	if (!strcmp(name, "uwsgi")) {
		uwsgi_xml_found_stanza = 0;
		return;
	}

	if (!uwsgi_xml_found_opt_key)
		return;

	add_exported_option(strdup(uwsgi_xml_found_opt_key), strdup("1"), 0);
	uwsgi_xml_found_opt_key = NULL;
}

void uwsgi_xml_config(char *filename, struct wsgi_request *wsgi_req, char *magic_table[]) {

	char *colon;

	char *xml_content;
	size_t xml_size = 0;
	int done = 0;

	if (uwsgi_check_scheme(filename)) {
		colon = uwsgi_get_last_char(filename, '/');
		colon = uwsgi_get_last_char(colon, ':');
	}
	else {
		colon = uwsgi_get_last_char(filename, ':');
	}
	if (colon) {
		colon[0] = 0;
		colon++;
		if (*colon == 0) {
			uwsgi_log("invalid xml id\n");
			exit(1);
		}
		uwsgi_log("[uWSGI] using xml uwsgi id: %s\n", colon);
	}

	xml_content = uwsgi_open_and_read(filename, &xml_size, 0, magic_table);

	uwsgi_log("[uWSGI] parsing config file %s\n", filename);

	XML_Parser parser = XML_ParserCreate(NULL);
	XML_SetUserData(parser, NULL);
	if (colon) {
		XML_SetUserData(parser, colon);
	}

	XML_SetElementHandler(parser, startElement, endElement);
	XML_SetCharacterDataHandler(parser, textElement);

	do {
		if (!XML_Parse(parser, xml_content, xml_size, done)) {
			if (XML_GetErrorCode(parser) != XML_ERROR_JUNK_AFTER_DOC_ELEMENT) {
				uwsgi_log("unable to parse xml file: %s (line %d)\n", XML_ErrorString(XML_GetErrorCode(parser)), XML_GetCurrentLineNumber(parser));
				exit(1);
			}
			else {
				break;
			}
		}

	} while (!done);

	if (colon) colon[0] = ':';

	// we can safely free, as we have a copy of datas
	XML_ParserFree(parser);
}

#endif

#else
#warning "*** XML configuration support is disabled ***"
#endif