Skip to content

Commit

Permalink
Check SCRIPT_URI env-var for ows_onlineresource.
Browse files Browse the repository at this point in the history
Apache HTTPD's mod_rewrite sets the SCRIPT_URI environment variable
which contains the "logical" URI used to access the resource while
the standard CGI parameter SCRIPT_NAME (and SERVER_NAME, SERVER_PORT)
contain the "physical" path to the resource.

When using MapServer as a WxS server it currently defaults
ows_onlineresource parameter to:
  http[s]://SERVER_NAME:SERVER_PORT/SCRIPT_NAME?[map=...]

This is less than ideal when using a rewrite rule to hide the path of
the mapserver binary and present the illusion to the users that the
mapfile is the URI endpoint (much like how .pl or .php files are
usually presented.) This allows access to be controlled to each
mapfile to be controlled using standard Apache configuration.  E.g.:

RewriteRule ^/datasets/(.*\.map) /cgi-bin/mapserv [PT,env=MS_MAPFILE:/srv/datasets/$1]

However, in this case SCRIPT_URI contains the correct (i.e. what the
client sees) base URI.  So, ows_onlineresource should default to:
  SCRIPT_URI?[map=...]

This patch implements this if the SCRIPT_URI environment variable is
present.  Otherwise, the behavior is the same as before.
  • Loading branch information
klassenjs committed Sep 22, 2017
1 parent 2c9fe26 commit 2d69a4b
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions maputil.c
Original file line number Diff line number Diff line change
Expand Up @@ -2372,10 +2372,11 @@ void *msSmallCalloc( size_t nCount, size_t nSize )
char *msBuildOnlineResource(mapObj *map, cgiRequestObj *req)
{
char *online_resource = NULL;
const char *value, *hostname, *port, *script, *protocol="http", *mapparam=NULL;
const char *value, *script_uri, *hostname, *port, *script, *protocol="http", *mapparam=NULL;
char **hostname_array = NULL;
int mapparam_len = 0, hostname_array_len = 0;

script_uri = getenv("SCRIPT_URI"); /* Set to logical URI if using Apache mod_rewrite */
hostname = getenv("HTTP_X_FORWARDED_HOST");
if(!hostname)
hostname = getenv("SERVER_NAME");
Expand Down Expand Up @@ -2415,7 +2416,17 @@ char *msBuildOnlineResource(mapObj *map, cgiRequestObj *req)
}
}

if (hostname && port && script) {
if ( script_uri ) { /* If called via Apache mod_rewrite */
size_t buffer_size;
buffer_size = strlen(script_uri)+mapparam_len+2; /* 2 comes from [script_uri]?[map]\0, i.e. "?\0" */
online_resource = (char*)msSmallMalloc(buffer_size);
snprintf(online_resource, buffer_size, "%s?", script_uri);
if(mapparam) {
int baselen;
baselen = strlen(online_resource);
snprintf(online_resource+baselen, buffer_size-baselen, "map=%s&", mapparam);
}
} else if (hostname && port && script) {
size_t buffer_size;
buffer_size = strlen(hostname)+strlen(port)+strlen(script)+mapparam_len+11; /* 11 comes from https://[host]:[port][scriptname]?[map]\0, i.e. "https://:?\0" */
online_resource = (char*)msSmallMalloc(buffer_size);
Expand Down

0 comments on commit 2d69a4b

Please sign in to comment.