diff --git a/include/mapcache.h b/include/mapcache.h index 5d3bdedd..a9d68aa9 100644 --- a/include/mapcache.h +++ b/include/mapcache.h @@ -965,6 +965,11 @@ struct mapcache_cfg { */ mapcache_service * services[MAPCACHE_SERVICES_COUNT]; + /** + * default service which will be used if no service type was specified in the url + */ + mapcache_service *default_service; + /** * hashtable containing configured mapcache_source%s */ diff --git a/lib/configuration.c b/lib/configuration.c index 30301b27..68f3da50 100644 --- a/lib/configuration.c +++ b/lib/configuration.c @@ -260,6 +260,8 @@ mapcache_cfg* mapcache_configuration_create(apr_pool_t *pool) cfg->loglevel = MAPCACHE_WARN; cfg->autoreload = 0; + cfg->default_service = NULL; + return cfg; } diff --git a/lib/configuration_xml.c b/lib/configuration_xml.c index 2b728ac0..facf9a31 100644 --- a/lib/configuration_xml.c +++ b/lib/configuration_xml.c @@ -981,60 +981,50 @@ void mapcache_configuration_parse_xml(mapcache_context *ctx, const char *filenam for(service_node = node; service_node; service_node = service_node->next) { char *enabled = (char*)ezxml_attr(service_node,"enabled"); char *type = (char*)ezxml_attr(service_node,"type"); - if(!strcasecmp(enabled,"true")) { - if (!strcasecmp(type,"wms")) { - mapcache_service *new_service = mapcache_service_wms_create(ctx); - if(new_service->configuration_parse_xml) { - new_service->configuration_parse_xml(ctx,service_node,new_service,config); - } - config->services[MAPCACHE_SERVICE_WMS] = new_service; - } else if (!strcasecmp(type,"tms")) { - mapcache_service *new_service = mapcache_service_tms_create(ctx); - if(new_service->configuration_parse_xml) { - new_service->configuration_parse_xml(ctx,service_node,new_service,config); - } - config->services[MAPCACHE_SERVICE_TMS] = new_service; - } else if (!strcasecmp(type,"wmts")) { - mapcache_service *new_service = mapcache_service_wmts_create(ctx); - if(new_service->configuration_parse_xml) { - new_service->configuration_parse_xml(ctx,service_node,new_service,config); - } - config->services[MAPCACHE_SERVICE_WMTS] = new_service; - } else if (!strcasecmp(type,"kml")) { - mapcache_service *new_service = mapcache_service_kml_create(ctx); - if(new_service->configuration_parse_xml) { - new_service->configuration_parse_xml(ctx,service_node,new_service,config); - } - config->services[MAPCACHE_SERVICE_KML] = new_service; - } else if (!strcasecmp(type,"gmaps")) { - mapcache_service *new_service = mapcache_service_gmaps_create(ctx); - if(new_service->configuration_parse_xml) { - new_service->configuration_parse_xml(ctx,service_node,new_service,config); - } - config->services[MAPCACHE_SERVICE_GMAPS] = new_service; - } else if (!strcasecmp(type,"mapguide")) { - mapcache_service *new_service = mapcache_service_mapguide_create(ctx); - if(new_service->configuration_parse_xml) { - new_service->configuration_parse_xml(ctx,service_node,new_service,config); - } - config->services[MAPCACHE_SERVICE_MAPGUIDE] = new_service; - } else if (!strcasecmp(type,"ve")) { - mapcache_service *new_service = mapcache_service_ve_create(ctx); - if(new_service->configuration_parse_xml) { - new_service->configuration_parse_xml(ctx,service_node,new_service,config); - } - config->services[MAPCACHE_SERVICE_VE] = new_service; - } else if (!strcasecmp(type,"demo")) { - mapcache_service *new_service = mapcache_service_demo_create(ctx); - if(new_service->configuration_parse_xml) { - new_service->configuration_parse_xml(ctx,service_node,new_service,config); - } - config->services[MAPCACHE_SERVICE_DEMO] = new_service; - } else { - ctx->set_error(ctx,400,"unknown type %s",type); - } - if(GC_HAS_ERROR(ctx)) goto cleanup; + char *default_service = (char*)ezxml_attr(service_node,"default"); + int service_type; + mapcache_service *new_service; + + if(strcasecmp(enabled,"true")) + continue; + + if (!strcasecmp(type,"wms")) { + new_service = mapcache_service_wms_create(ctx); + service_type = MAPCACHE_SERVICE_WMS; + } else if (!strcasecmp(type,"tms")) { + new_service = mapcache_service_tms_create(ctx); + service_type = MAPCACHE_SERVICE_TMS; + } else if (!strcasecmp(type,"wmts")) { + new_service = mapcache_service_wmts_create(ctx); + service_type = MAPCACHE_SERVICE_WMTS; + } else if (!strcasecmp(type,"kml")) { + new_service = mapcache_service_kml_create(ctx); + service_type = MAPCACHE_SERVICE_KML; + } else if (!strcasecmp(type,"gmaps")) { + new_service = mapcache_service_gmaps_create(ctx); + service_type = MAPCACHE_SERVICE_GMAPS; + } else if (!strcasecmp(type,"mapguide")) { + new_service = mapcache_service_mapguide_create(ctx); + service_type = MAPCACHE_SERVICE_MAPGUIDE; + } else if (!strcasecmp(type,"ve")) { + new_service = mapcache_service_ve_create(ctx); + service_type = MAPCACHE_SERVICE_VE; + } else if (!strcasecmp(type,"demo")) { + new_service = mapcache_service_demo_create(ctx); + service_type = MAPCACHE_SERVICE_DEMO; + } else { + ctx->set_error(ctx,400,"unknown type %s",type); } + + if(GC_HAS_ERROR(ctx)) goto cleanup; + + if(new_service->configuration_parse_xml) + new_service->configuration_parse_xml(ctx,service_node,new_service,config); + + if(default_service && !strcasecmp(default_service,"true")) + config->default_service = new_service; + + config->services[service_type] = new_service; } } else if ((node = ezxml_child(doc,"services")) != NULL) { ctx->log(ctx,MAPCACHE_WARN," tag is deprecated, use "); diff --git a/lib/services.c b/lib/services.c index 53e524b8..2dd3b61f 100644 --- a/lib/services.c +++ b/lib/services.c @@ -38,9 +38,16 @@ void mapcache_service_dispatch_request(mapcache_context *ctx, mapcache_request **request, char *pathinfo, apr_table_t *params, mapcache_cfg *config) { int i; + mapcache_service *service = NULL; - /* skip empty pathinfo */ - if(!pathinfo) { + /* skip leading /'s */ + while(pathinfo && (*pathinfo) == '/') + ++pathinfo; + + /* set default url prefix or skip empty pathinfo */ + if((!pathinfo || strlen(pathinfo) == 0) && config->default_service) { + pathinfo = apr_pstrdup(ctx->pool,config->default_service->url_prefix); + } else if(!pathinfo) { ctx->set_error(ctx,404,"missing a service"); return; } @@ -52,7 +59,6 @@ void mapcache_service_dispatch_request(mapcache_context *ctx, mapcache_request * for(i=0; iservices[i]; if(!service) continue; /* skip an unconfigured service */ prefixlen = strlen(service->url_prefix); @@ -67,6 +73,20 @@ void mapcache_service_dispatch_request(mapcache_context *ctx, mapcache_request * /* stop looping on services */ return; } + + if (config->default_service) { + /* no matching url prefix of any service: + assume request should go to the default service */ + service = config->default_service; + ctx->service = service; + ctx->service->parse_request(ctx,service,request,pathinfo,params,config); + + if(*request) + (*request)->service = service; + + return; + } + ctx->set_error(ctx,404,"unknown service %s",pathinfo); }