Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle default service type w/o pathinfo #46

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions include/mapcache.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
2 changes: 2 additions & 0 deletions lib/configuration.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
96 changes: 43 additions & 53 deletions lib/configuration_xml.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <service> 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 <service> 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,"<services> tag is deprecated, use <service type=\"wms\" enabled=\"true|false\">");
Expand Down
26 changes: 23 additions & 3 deletions lib/services.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -52,7 +59,6 @@ void mapcache_service_dispatch_request(mapcache_context *ctx, mapcache_request *
for(i=0; i<MAPCACHE_SERVICES_COUNT; i++) {
/* loop through the services that have been configured */
int prefixlen;
mapcache_service *service = NULL;
service = config->services[i];
if(!service) continue; /* skip an unconfigured service */
prefixlen = strlen(service->url_prefix);
Expand All @@ -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);
}

Expand Down