From 3c3c9b3934f42808c15957f9378dec904203228d Mon Sep 17 00:00:00 2001 From: Seth G Date: Fri, 23 Apr 2021 10:20:22 +0200 Subject: [PATCH] Use CPLSetConfigOption/CPLGetConfigOption for some CGI/FastCGI-related env vars (#6305) Push a few high-value env vars into CPL config and then reference that instead of the env (mostly for IIS/FastCGI). --- maphttp.c | 4 ++-- mapserv.c | 11 +++++++++++ mapserv.h | 1 + mapservutil.c | 13 +++++++++---- 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/maphttp.c b/maphttp.c index 98329d4d23..249cfb7ddc 100644 --- a/maphttp.c +++ b/maphttp.c @@ -39,7 +39,7 @@ #include "mapthread.h" #include "mapows.h" - +#include "cpl_conv.h" #include #ifndef _WIN32 @@ -471,7 +471,7 @@ int msHTTPExecuteRequests(httpRequestObj *pasReqInfo, int numRequests, * If set then the value is the full path to the ca-bundle.crt file * e.g. CURL_CA_BUNDLE=/usr/local/share/curl/curl-ca-bundle.crt */ - pszCurlCABundle = getenv("CURL_CA_BUNDLE"); + pszCurlCABundle = CPLGetConfigOption("CURL_CA_BUNDLE", NULL); if (debug) { msDebug("HTTP: Starting to prepare HTTP requests.\n"); diff --git a/mapserv.c b/mapserv.c index 5924571d2e..5f76308889 100644 --- a/mapserv.c +++ b/mapserv.c @@ -36,6 +36,8 @@ #include "mapio.h" #include "maptime.h" +#include "cpl_conv.h" + #ifndef WIN32 #include #endif @@ -156,6 +158,15 @@ int main(int argc, char *argv[]) if(msGetGlobalDebugLevel() >= MS_DEBUGLEVEL_TUNING) msGettimeofday(&execstarttime, NULL); + /* push high-value ENV vars into the CPL global config - primarily for IIS/FastCGI */ + const char* const apszEnvVars[] = { + "CURL_CA_BUNDLE", "MS_MAPFILE", "MS_MAP_NO_PATH", "MS_MAP_PATTERN", + NULL /* guard */ }; + for( int i = 0; apszEnvVars[i] != NULL; ++i ) { + const char* value = getenv(apszEnvVars[i]); + if(value) CPLSetConfigOption(apszEnvVars[i], value); + } + /* -------------------------------------------------------------------- */ /* Process arguments. In normal use as a cgi-bin there are no */ /* commandline switches, but we provide a few for test/debug */ diff --git a/mapserv.h b/mapserv.h index 845a544dac..27042a04e3 100644 --- a/mapserv.h +++ b/mapserv.h @@ -41,6 +41,7 @@ #include "maptile.h" #include "cgiutil.h" + /* ** Defines */ diff --git a/mapservutil.c b/mapservutil.c index 6b21676769..9a4fe6197b 100644 --- a/mapservutil.c +++ b/mapservutil.c @@ -33,6 +33,8 @@ #include "maptime.h" #include "mapows.h" +#include "cpl_conv.h" + /* ** Enumerated types, keep the query modes in sequence and at the end of the enumeration (mode enumeration is in maptemplate.h). */ @@ -199,12 +201,15 @@ mapObj *msCGILoadMap(mapservObj *mapserv) int i, j; mapObj *map = NULL; + const char *ms_mapfile = CPLGetConfigOption("MS_MAPFILE", NULL); + const char *ms_map_no_path = CPLGetConfigOption("MS_MAP_NO_PATH", NULL); + const char *ms_map_pattern = CPLGetConfigOption("MS_MAP_PATTERN", NULL); + for(i=0; irequest->NumParams; i++) /* find the mapfile parameter first */ if(strcasecmp(mapserv->request->ParamNames[i], "map") == 0) break; if(i == mapserv->request->NumParams) { - char *ms_mapfile = getenv("MS_MAPFILE"); - if(ms_mapfile) { + if(ms_mapfile != NULL) { map = msLoadMap(ms_mapfile,NULL); } else { msSetError(MS_WEBERR, "CGI variable \"map\" is not set.", "msCGILoadMap()"); /* no default, outta here */ @@ -215,12 +220,12 @@ mapObj *msCGILoadMap(mapservObj *mapserv) map = msLoadMap(getenv(mapserv->request->ParamValues[i]), NULL); else { /* by here we know the request isn't for something in an environment variable */ - if(getenv("MS_MAP_NO_PATH")) { + if(ms_map_no_path != NULL) { msSetError(MS_WEBERR, "Mapfile not found in environment variables and this server is not configured for full paths.", "msCGILoadMap()"); return NULL; } - if(getenv("MS_MAP_PATTERN") && msEvalRegex(getenv("MS_MAP_PATTERN"), mapserv->request->ParamValues[i]) != MS_TRUE) { + if(ms_map_pattern != NULL && msEvalRegex(ms_map_pattern, mapserv->request->ParamValues[i]) != MS_TRUE) { msSetError(MS_WEBERR, "Parameter 'map' value fails to validate.", "msCGILoadMap()"); return NULL; }