refactored xmlconf

This commit is contained in:
roberto@precise64
2012-02-18 08:07:48 +01:00
parent bfcd9dad25
commit 355e989eea
4 changed files with 4 additions and 276 deletions
-51
View File
@@ -1159,56 +1159,6 @@ void uwsgi_python_init_thread(int core_id) {
}
int uwsgi_python_xml(char *node, char *content) {
PyThreadState *interpreter = NULL;
if (uwsgi.single_interpreter) {
interpreter = up.main_thread;
}
if (!strcmp("script", node)) {
return init_uwsgi_app(LOADER_UWSGI, content, uwsgi.wsgi_req, interpreter, PYTHON_APP_TYPE_WSGI);
}
else if (!strcmp("file", node)) {
return init_uwsgi_app(LOADER_FILE, content, uwsgi.wsgi_req, interpreter, PYTHON_APP_TYPE_WSGI);
}
else if (!strcmp("eval", node)) {
return init_uwsgi_app(LOADER_EVAL, content, uwsgi.wsgi_req, interpreter, PYTHON_APP_TYPE_WSGI);
}
else if (!strcmp("wsgi", node)) {
return init_uwsgi_app(LOADER_EVAL, content, uwsgi.wsgi_req, interpreter, PYTHON_APP_TYPE_WSGI);
}
else if (!strcmp("module", node)) {
uwsgi.wsgi_req->module = content;
uwsgi.wsgi_req->module_len = strlen(content);
uwsgi.wsgi_req->callable = strchr(uwsgi.wsgi_req->module, ':');
if (uwsgi.wsgi_req->callable) {
uwsgi.wsgi_req->callable[0] = 0;
uwsgi.wsgi_req->callable++;
uwsgi.wsgi_req->callable_len = strlen(uwsgi.wsgi_req->callable);
uwsgi.wsgi_req->module_len = strlen(uwsgi.wsgi_req->module);
return init_uwsgi_app(LOADER_DYN, uwsgi.wsgi_req, uwsgi.wsgi_req, interpreter, PYTHON_APP_TYPE_WSGI);
}
else {
return init_uwsgi_app(LOADER_UWSGI, content, uwsgi.wsgi_req, interpreter, PYTHON_APP_TYPE_WSGI);
}
return 1;
}
else if (!strcmp("pyhome", node)) {
uwsgi.wsgi_req->pyhome = content;
uwsgi.wsgi_req->pyhome_len = strlen(content);
return 1;
}
else if (!strcmp("callable", node)) {
uwsgi.wsgi_req->callable = content;
uwsgi.wsgi_req->callable_len = strlen(content);
return init_uwsgi_app(LOADER_DYN, uwsgi.wsgi_req, uwsgi.wsgi_req, interpreter, PYTHON_APP_TYPE_WSGI);
}
return 0;
}
#ifndef UWSGI_PYPY
void uwsgi_python_suspend(struct wsgi_request *wsgi_req) {
@@ -1558,7 +1508,6 @@ struct uwsgi_plugin python_plugin = {
.enable_threads = uwsgi_python_enable_threads,
.init_thread = uwsgi_python_init_thread,
.manage_xml = uwsgi_python_xml,
.magic = uwsgi_python_magic,
+1 -8
View File
@@ -3201,13 +3201,6 @@ void uwsgi_init_all_apps() {
}
}
/*parse xml for <app> tags */
#ifdef UWSGI_XML
if (uwsgi.xml_round2 && uwsgi.xml_config != NULL) {
uwsgi_xml_config(uwsgi.xml_config, uwsgi.wsgi_req, 1, NULL);
}
#endif
for (i = 0; i < uwsgi.mounts_cnt; i++) {
char *what = strchr(uwsgi.mounts[i], '=');
if (what) {
@@ -3673,7 +3666,7 @@ void uwsgi_opt_load_ini(char *opt, char *filename, void *none) {
#ifdef UWSGI_XML
void uwsgi_opt_load_xml(char *opt, char *filename, void *none) {
config_magic_table_fill(filename, uwsgi.magic_table);
uwsgi_xml_config(filename, uwsgi.wsgi_req, 0, uwsgi.magic_table);
uwsgi_xml_config(filename, uwsgi.wsgi_req, uwsgi.magic_table);
}
#endif
+1 -7
View File
@@ -574,7 +574,6 @@ struct uwsgi_plugin {
void (*master_cycle) (void);
int (*mount_app) (char *, char *, int);
int (*manage_udp) (char *, int, char *, int);
int (*manage_xml) (char *, char *);
void (*suspend) (struct wsgi_request *);
void (*resume) (struct wsgi_request *);
@@ -1356,13 +1355,8 @@ struct uwsgi_server {
mode_t chmod_logfile_value;
int listen_queue;
#ifdef UWSGI_XML
char *xml_config;
#endif
char *file_config;
#ifdef UWSGI_ROUTING
struct uwsgi_router *routers;
struct uwsgi_route *routes;
@@ -1837,7 +1831,7 @@ void harakiri(void);
void stats(int);
#ifdef UWSGI_XML
void uwsgi_xml_config(char *, struct wsgi_request *, int, char *[]);
void uwsgi_xml_config(char *, struct wsgi_request *, char *[]);
#endif
void internal_server_error(struct wsgi_request *, char *);
+2 -210
View File
@@ -9,19 +9,15 @@ extern struct uwsgi_server uwsgi;
#include <libxml/parser.h>
#include <libxml/tree.h>
void uwsgi_xml_config(char *filename, struct wsgi_request *wsgi_req, int app_tag, char *magic_table[]) {
void uwsgi_xml_config(char *filename, struct wsgi_request *wsgi_req, char *magic_table[]) {
xmlDoc *doc = NULL;
xmlNode *element = NULL;
xmlNode *node = NULL;
xmlNode *node2 = NULL;
xmlChar *xml_uwsgi_mountpoint = NULL;
xmlChar *xml_uwsgi_domain = NULL;
xmlChar *node_mode;
char *colon;
int i;
char *xml_id;
char *xml_content;
int xml_size = 0;
@@ -51,9 +47,7 @@ void uwsgi_xml_config(char *filename, struct wsgi_request *wsgi_req, int app_tag
exit(1);
}
if (!app_tag) {
uwsgi_log( "[uWSGI] parsing config file %s\n", filename);
}
uwsgi_log( "[uWSGI] parsing config file %s\n", filename);
element = xmlDocGetRootElement(doc);
if (element == NULL) {
@@ -85,7 +79,6 @@ void uwsgi_xml_config(char *filename, struct wsgi_request *wsgi_req, int app_tag
}
if (!app_tag) {
// first check for options
for (node = element->children; node; node = node->next) {
if (node->type == XML_CDATA_SECTION_NODE) {
@@ -95,11 +88,6 @@ void uwsgi_xml_config(char *filename, struct wsgi_request *wsgi_req, int app_tag
}
else if (node->type == XML_ELEMENT_NODE) {
if (!strcmp((char *) node->name, "app")) {
uwsgi.xml_round2 = 1;
continue;
}
node_mode = xmlGetProp(node, (const xmlChar *) "mode");
if (uwsgi.mode && node_mode) {
if (strcmp(uwsgi.mode, (char *) node_mode)) {
@@ -123,54 +111,8 @@ void uwsgi_xml_config(char *filename, struct wsgi_request *wsgi_req, int app_tag
}
}
}
else {
// ... then for apps and routing
for (node = element->children; node; node = node->next) {
if (node->type == XML_ELEMENT_NODE) {
if (!strcmp((char *) node->name, "app")) {
wsgi_req->appid_len = 0;
wsgi_req->host_len = 0;
xml_uwsgi_mountpoint = xmlGetProp(node, (const xmlChar *) "mountpoint");
if (xml_uwsgi_mountpoint) {
wsgi_req->appid = (char *) xml_uwsgi_mountpoint;
wsgi_req->appid_len = strlen(wsgi_req->appid);
}
xml_uwsgi_domain = xmlGetProp(node, (const xmlChar *) "domain");
if (xml_uwsgi_domain) {
wsgi_req->host = (char *) xml_uwsgi_domain;
wsgi_req->host_len = strlen(wsgi_req->host);
}
for (node2 = node->children; node2; node2 = node2->next) {
if (node2->type == XML_ELEMENT_NODE) {
//we have a mountpoint now pass every node to the xml handler of each plugin
if (node2->children) {
if (node2->children->content) {
for(i=0;i<0xFF;i++) {
if (uwsgi.p[i]->manage_xml) {
if (uwsgi.p[i]->manage_xml( (char *)node2->name, (char *) node2->children->content)) break;
}
}
}
}
}
}
}
}
}
}
/* We cannot free xml resources on the first round (and with routing enabled) as the string pointer must be valid for all the server lifecycle */
if (app_tag) {
xmlFreeDoc (doc);
xmlCleanupParser ();
}
}
#endif
@@ -178,156 +120,6 @@ void uwsgi_xml_config(char *filename, struct wsgi_request *wsgi_req, int app_tag
#include <expat.h>
int current_xmlnode;
int current_xmlnode_has_arg;
char *current_xmlnode_text;
int current_xmlnode_text_len;
void uwsgi_endElement(void *userData, const char *name) {
if (current_xmlnode && !current_xmlnode_has_arg) {
manage_opt(current_xmlnode, NULL);
}
else if (current_xmlnode_has_arg) {
if (!current_xmlnode_text_len) {
uwsgi_log("option %s requires an argument\n", name);
exit(1);
}
// HACK: use the first char of closing tag for nulling string
current_xmlnode_text[current_xmlnode_text_len] = 0;
manage_opt(current_xmlnode, current_xmlnode_text);
}
current_xmlnode = 0;
current_xmlnode_has_arg = 0;
current_xmlnode_text = NULL;
current_xmlnode_text_len = 0;
}
void uwsgi_endApp(void *userData, const char *name) {}
void uwsgi_textHandler(void *userData, const char *s, int len) {
if (current_xmlnode && current_xmlnode_has_arg) {
current_xmlnode_text = (char *) s;
current_xmlnode_text_len = len;
}
}
void uwsgi_textApp(void *userData, const char *s, int len) {
struct wsgi_request *wsgi_req = (struct wsgi_request *) userData;
if (current_xmlnode) {
wsgi_req->wsgi_script = (char *) s;
wsgi_req->wsgi_script_len = len;
//init_uwsgi_app(&uwsgi, NULL);
current_xmlnode = 0;
}
};
void uwsgi_startApp(void *userData, const char *name, const char **attrs) {
struct wsgi_request *wsgi_req = (struct wsgi_request *) userData;
if (!strcmp(name, "app")) {
current_xmlnode = 0;
uwsgi_log("%s = %s\n", attrs[0], attrs[1]);
if (strcmp(attrs[0], "mountpoint")) {
uwsgi_log("invalid attribute for app tag. must be 'mountpoint'\n");
exit(1);
}
if (attrs[1]) {
wsgi_req->script_name = (char *) attrs[1];
wsgi_req->script_name_len = strlen(attrs[1]);
}
else {
wsgi_req->script_name = "";
wsgi_req->script_name_len = 0;
}
}
else if (!strcmp(name, "script")) {
current_xmlnode = 1;
}
}
void uwsgi_startElement(void *userData, const char *name, const char **attrs) {
struct option *long_options = (struct option *) userData;
struct option *lopt, *aopt;
lopt = uwsgi.long_options;
while ((aopt = lopt)) {
if (!aopt->name)
break;
if (!strcmp(name, aopt->name)) {
if (aopt->flag) {
*aopt->flag = aopt->val;
break;
}
else {
current_xmlnode = aopt->val;
current_xmlnode_has_arg = aopt->has_arg;
break;
}
}
lopt++;
}
}
void uwsgi_xml_config(struct wsgi_request *wsgi_req, struct option *long_options) {
int xmlfd;
size_t rlen;
struct stat stat_buf;
char *xmlbuf;
XML_Parser parser = XML_ParserCreate(NULL);
xmlfd = open(uwsgi.xml_config, O_RDONLY);
if (xmlfd < 0) {
uwsgi_error_open(uwsgi.xml_config);
exit(1);
}
if (fstat(xmlfd, &stat_buf)) {
uwsgi_error("fstat()");
exit(1);
}
xmlbuf = malloc(stat_buf.st_size);
if (!xmlbuf) {
uwsgi_error("malloc()");
exit(1);
}
rlen = read(xmlfd, xmlbuf, stat_buf.st_size);
if (rlen != stat_buf.st_size) {
uwsgi_error("read()");
exit(1);
}
close(xmlfd);
if (long_options) {
XML_SetUserData(parser, long_options);
XML_SetElementHandler(parser, uwsgi_startElement, uwsgi_endElement);
XML_SetCharacterDataHandler(parser, uwsgi_textHandler);
}
else {
XML_SetUserData(parser, wsgi_req);
XML_SetElementHandler(parser, uwsgi_startApp, uwsgi_endApp);
XML_SetCharacterDataHandler(parser, uwsgi_textApp);
}
if (!XML_Parse(parser, xmlbuf, stat_buf.st_size, 1)) {
uwsgi_log( "%s at line %d\n", XML_ErrorString(XML_GetErrorCode(parser)), (int) XML_GetCurrentLineNumber(parser));
exit(1);
}
if (!long_options) {
XML_ParserFree(parser);
free(xmlbuf);
}
}
#endif