X-Git-Url: http://jsfdemo.indexdata.com/?a=blobdiff_plain;f=src%2Fpazpar2_config.c;h=5c808b8a612a34388d7770a4a9150a4973935a6b;hb=5b4ea0cf66dd82c871ed7d69a5801d78789087b2;hp=e796e00f4be66a68341a846fbb77dfa9d469fbcc;hpb=a4d692f24164e5e8271599c2fcfd9cc5acc25c4b;p=pazpar2-moved-to-github.git diff --git a/src/pazpar2_config.c b/src/pazpar2_config.c index e796e00..5c808b8 100644 --- a/src/pazpar2_config.c +++ b/src/pazpar2_config.c @@ -34,14 +34,24 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include #include -#define CONFIG_NOEXTERNS #include "pazpar2_config.h" +#include "settings.h" +#include "eventl.h" +#include "http.h" +struct conf_config +{ + NMEM nmem; /* for conf_config and servers memory */ + struct conf_server *servers; + WRBUF confdir; +}; -static char confdir[256] = "."; -struct conf_config *config = 0; +static char *parse_settings(struct conf_config *config, + NMEM nmem, xmlNode *node); +static struct conf_targetprofiles *parse_targetprofiles(NMEM nmem, + xmlNode *node); static struct conf_metadata * conf_metadata_assign(NMEM nmem, @@ -95,8 +105,9 @@ struct conf_sortkey * conf_sortkey_assign(NMEM nmem, } -struct conf_service * conf_service_create(int num_metadata, int num_sortkeys, - const char *service_id) +struct conf_service * conf_service_create(struct conf_config *config, + int num_metadata, int num_sortkeys, + const char *service_id) { struct conf_service * service = 0; NMEM nmem = nmem_create(); @@ -106,6 +117,10 @@ struct conf_service * conf_service_create(int num_metadata, int num_sortkeys, service = nmem_malloc(nmem, sizeof(struct conf_service)); service->nmem = nmem; service->next = 0; + service->settings = 0; + service->databases = 0; + service->targetprofiles = 0; + service->config = config; service->id = service_id ? nmem_strdup(nmem, service_id) : 0; service->num_metadata = num_metadata; @@ -208,7 +223,8 @@ int conf_service_sortkey_field_id(struct conf_service *service, /* Code to parse configuration file */ /* ==================================================== */ -static struct conf_service *parse_service(xmlNode *node, const char *service_id) +static struct conf_service *parse_service(struct conf_config *config, + xmlNode *node, const char *service_id) { xmlNode *n; int md_node = 0; @@ -230,13 +246,36 @@ static struct conf_service *parse_service(xmlNode *node, const char *service_id) xmlFree(sortkey); } - service = conf_service_create(num_metadata, num_sortkeys, service_id); + service = conf_service_create(config, + num_metadata, num_sortkeys, service_id); for (n = node->children; n; n = n->next) { if (n->type != XML_ELEMENT_NODE) continue; - if (!strcmp((const char *) n->name, (const char *) "metadata")) + if (!strcmp((const char *) n->name, "settings")) + { + if (service->settings) + { + yaz_log(YLOG_FATAL, "Can't repeat 'settings'"); + return 0; + } + service->settings = parse_settings(config, service->nmem, n); + if (!service->settings) + return 0; + } + else if (!strcmp((const char *) n->name, (const char *) "targetprofiles")) + { + if (service->targetprofiles) + { + yaz_log(YLOG_FATAL, "Can't repeat targetprofiles"); + return 0; + } + if (!(service->targetprofiles = + parse_targetprofiles(service->nmem, n))) + return 0; + } + else if (!strcmp((const char *) n->name, (const char *) "metadata")) { xmlChar *xml_name = xmlGetProp(n, (xmlChar *) "name"); xmlChar *xml_brief = xmlGetProp(n, (xmlChar *) "brief"); @@ -373,8 +412,7 @@ static struct conf_service *parse_service(xmlNode *node, const char *service_id) } sortkey_offset = sk_node; - conf_service_add_sortkey( -service, sk_node, + conf_service_add_sortkey(service, sk_node, (const char *) xml_name, sk_type); sk_node++; @@ -414,20 +452,22 @@ service, sk_node, return service; } -static char *parse_settings(NMEM nmem, xmlNode *node) +static char *parse_settings(struct conf_config *config, + NMEM nmem, xmlNode *node) { xmlChar *src = xmlGetProp(node, (xmlChar *) "src"); char *r; if (src) { - if (yaz_is_abspath((const char *) src)) + if (yaz_is_abspath((const char *) src) || !wrbuf_len(config->confdir)) r = nmem_strdup(nmem, (const char *) src); else { r = nmem_malloc(nmem, - strlen(confdir) + strlen((const char *) src) + 2); - sprintf(r, "%s/%s", confdir, src); + wrbuf_len(config->confdir) + + strlen((const char *) src) + 2); + sprintf(r, "%s/%s", wrbuf_cstr(config->confdir), src); } } else @@ -439,7 +479,8 @@ static char *parse_settings(NMEM nmem, xmlNode *node) return r; } -static struct conf_server *parse_server(NMEM nmem, xmlNode *node) +static struct conf_server *parse_server(struct conf_config *config, + NMEM nmem, xmlNode *node) { xmlNode *n; struct conf_server *server = nmem_malloc(nmem, sizeof(struct conf_server)); @@ -449,9 +490,10 @@ static struct conf_server *parse_server(NMEM nmem, xmlNode *node) server->proxy_host = 0; server->proxy_port = 0; server->myurl = 0; + server->proxy_addr = 0; server->service = 0; server->next = 0; - server->settings = 0; + server->server_settings = 0; server->relevance_pct = 0; server->sort_pct = 0; server->mergekey_pct = 0; @@ -488,12 +530,12 @@ static struct conf_server *parse_server(NMEM nmem, xmlNode *node) } else if (!strcmp((const char *) n->name, "settings")) { - if (server->settings) + if (server->server_settings) { yaz_log(YLOG_FATAL, "Can't repeat 'settings'"); return 0; } - if (!(server->settings = parse_settings(nmem, n))) + if (!(server->server_settings = parse_settings(config, nmem, n))) return 0; } else if (!strcmp((const char *) n->name, "relevance")) @@ -516,7 +558,7 @@ static struct conf_server *parse_server(NMEM nmem, xmlNode *node) } else if (!strcmp((const char *) n->name, "service")) { - const char *service_id = (const char *) + char *service_id = (char *) xmlGetProp(n, (xmlChar *) "id"); struct conf_service **sp = &server->service; @@ -537,10 +579,19 @@ static struct conf_server *parse_server(NMEM nmem, xmlNode *node) return 0; else { - struct conf_service *s = parse_service(n, service_id); + struct conf_service *s = parse_service(config, n, service_id); if (s) + { + s->relevance_pct = server->relevance_pct ? + server->relevance_pct : pp2_charset_create(0); + s->sort_pct = server->sort_pct ? + server->sort_pct : pp2_charset_create(0); + s->mergekey_pct = server->mergekey_pct ? + server->mergekey_pct : pp2_charset_create(0); *sp = s; + } } + xmlFree(service_id); } else { @@ -548,22 +599,18 @@ static struct conf_server *parse_server(NMEM nmem, xmlNode *node) return 0; } } - if (!server->relevance_pct) - server->relevance_pct = pp2_charset_create(0); - if (!server->sort_pct) - server->sort_pct = pp2_charset_create(0); - if (!server->mergekey_pct) - server->mergekey_pct = pp2_charset_create(0); return server; } -xsltStylesheet *conf_load_stylesheet(const char *fname) +xsltStylesheet *conf_load_stylesheet(struct conf_config *config, + const char *fname) { char path[256]; - if (yaz_is_abspath(fname)) + if (yaz_is_abspath(fname) || !config || !wrbuf_len(config->confdir)) yaz_snprintf(path, sizeof(path), fname); else - yaz_snprintf(path, sizeof(path), "%s/%s", confdir, fname); + yaz_snprintf(path, sizeof(path), "%s/%s", + wrbuf_cstr(config->confdir), fname); return xsltParseStylesheetFile((xmlChar *) path); } @@ -604,9 +651,10 @@ static struct conf_targetprofiles *parse_targetprofiles(NMEM nmem, return r; } -struct conf_service *locate_service(const char *service_id) +struct conf_service *locate_service(struct conf_server *server, + const char *service_id) { - struct conf_service *s = config->servers->service; + struct conf_service *s = server->service; for (; s; s = s->next) if (s->id && service_id && 0 == strcmp(s->id, service_id)) return s; @@ -616,15 +664,9 @@ struct conf_service *locate_service(const char *service_id) } -static struct conf_config *parse_config(xmlNode *root) +static int parse_config(struct conf_config *config, xmlNode *root) { - NMEM nmem = nmem_create(); xmlNode *n; - struct conf_config *r = nmem_malloc(nmem, sizeof(struct conf_config)); - - r->nmem = nmem; - r->servers = 0; - r->targetprofiles = 0; for (n = root->children; n; n = n->next) { @@ -632,36 +674,68 @@ static struct conf_config *parse_config(xmlNode *root) continue; if (!strcmp((const char *) n->name, "server")) { - struct conf_server *tmp = parse_server(nmem, n); + struct conf_server *tmp = parse_server(config, config->nmem, n); if (!tmp) - return 0; - tmp->next = r->servers; - r->servers = tmp; + return -1; + tmp->next = config->servers; + config->servers = tmp; } else if (!strcmp((const char *) n->name, "targetprofiles")) { - // It would be fun to be able to fix this sometime - if (r->targetprofiles) - { - yaz_log(YLOG_FATAL, "Can't repeat targetprofiles"); - return 0; - } - if (!(r->targetprofiles = parse_targetprofiles(nmem, n))) - return 0; + yaz_log(YLOG_FATAL, "targetprofiles unsupported here. Must be part of service"); + return -1; + } else { yaz_log(YLOG_FATAL, "Bad element: %s", n->name); - return 0; + return -1; } } - return r; + return 0; +} + +static void config_include_src(struct conf_config *config, xmlNode *n, + const char *src) +{ + xmlDoc *doc = xmlParseFile(src); + yaz_log(YLOG_LOG, "processing incldue src=%s", src); + if (doc) + { + xmlNodePtr t = xmlDocGetRootElement(doc); + xmlReplaceNode(n, xmlCopyNode(t, 1)); + xmlFreeDoc(doc); + } } -int read_config(const char *fname) +static void process_config_includes(struct conf_config *config, xmlNode *n) +{ + for (; n; n = n->next) + { + if (n->type == XML_ELEMENT_NODE) + { + if (!strcmp((const char *) n->name, "include")) + { + xmlChar *src = xmlGetProp(n, (xmlChar *) "src"); + if (src) + { + config_include_src(config, n, (const char *) src); + xmlFree(src); + } + } + } + process_config_includes(config, n->children); + } +} + +struct conf_config *read_config(const char *fname) { xmlDoc *doc = xmlParseFile(fname); + xmlNode *n; const char *p; + int r; + NMEM nmem = nmem_create(); + struct conf_config *config = nmem_malloc(nmem, sizeof(struct conf_config)); xmlSubstituteEntitiesDefault(1); xmlLoadExtDtdDefaultValue = 1; @@ -670,6 +744,11 @@ int read_config(const char *fname) yaz_log(YLOG_FATAL, "Failed to read %s", fname); exit(1); } + + config->nmem = nmem; + config->servers = 0; + + config->confdir = wrbuf_alloc(); if ((p = strrchr(fname, #ifdef WIN32 '\\' @@ -679,20 +758,96 @@ int read_config(const char *fname) ))) { int len = p - fname; - if (len >= sizeof(confdir)) - len = sizeof(confdir)-1; - strncpy(confdir, fname, len); - confdir[len] = '\0'; + wrbuf_write(config->confdir, fname, len); } - config = parse_config(xmlDocGetRootElement(doc)); + wrbuf_puts(config->confdir, ""); + + n = xmlDocGetRootElement(doc); + process_config_includes(config, n); + xmlDocFormatDump(stdout, doc, 0); + r = parse_config(config, n); xmlFreeDoc(doc); - if (config) - return 1; - else + if (r) return 0; + return config; +} + +void config_read_settings(struct conf_config *config, + const char *path_override) +{ + struct conf_service *s = config->servers->service; + for (;s ; s = s->next) + { + init_settings(s); + if (path_override) + settings_read(s, path_override); + else if (s->settings) + settings_read(s, s->settings); + else if (config->servers->server_settings) + settings_read(s, config->servers->server_settings); + else + yaz_log(YLOG_WARN, "No settings for service"); + } } +void config_stop_listeners(struct conf_config *conf) +{ + struct conf_server *ser; + for (ser = conf->servers; ser; ser = ser->next) + http_close_server(ser); +} + +int config_start_listeners(struct conf_config *conf, + const char *listener_override, + const char *proxy_override) +{ + struct conf_server *ser; + for (ser = conf->servers; ser; ser = ser->next) + { + WRBUF w = wrbuf_alloc(); + int r; + if (listener_override) + { + wrbuf_puts(w, listener_override); + listener_override = 0; /* only first server is overriden */ + } + else + { + if (ser->host) + wrbuf_puts(w, ser->host); + if (ser->port) + { + if (wrbuf_len(w)) + wrbuf_puts(w, ":"); + wrbuf_printf(w, "%d", ser->port); + } + } + r = http_init(wrbuf_cstr(w), ser); + wrbuf_destroy(w); + if (r) + return -1; + + w = wrbuf_alloc(); + if (proxy_override) + wrbuf_puts(w, proxy_override); + else if (ser->proxy_host || ser->proxy_port) + { + if (ser->proxy_host) + wrbuf_puts(w, ser->proxy_host); + if (ser->proxy_port) + { + if (wrbuf_len(w)) + wrbuf_puts(w, ":"); + wrbuf_printf(w, "%d", ser->proxy_port); + } + } + if (wrbuf_len(w)) + http_set_proxyaddr(wrbuf_cstr(w), ser); + wrbuf_destroy(w); + } + return 0; +} /* * Local variables: