Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Use CPLSetConfigOption/CPLGetConfigOption for some CGI/FastCGI-relate…
…d 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).
  • Loading branch information
geographika authored and sdlime committed Apr 30, 2021
1 parent 7e36981 commit 3c3c9b3
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
4 changes: 2 additions & 2 deletions maphttp.c
Expand Up @@ -39,7 +39,7 @@
#include "mapthread.h"
#include "mapows.h"


#include "cpl_conv.h"

#include <time.h>
#ifndef _WIN32
Expand Down Expand Up @@ -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");
Expand Down
11 changes: 11 additions & 0 deletions mapserv.c
Expand Up @@ -36,6 +36,8 @@
#include "mapio.h"
#include "maptime.h"

#include "cpl_conv.h"

#ifndef WIN32
#include <signal.h>
#endif
Expand Down Expand Up @@ -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 */
Expand Down
1 change: 1 addition & 0 deletions mapserv.h
Expand Up @@ -41,6 +41,7 @@
#include "maptile.h"

#include "cgiutil.h"

/*
** Defines
*/
Expand Down
13 changes: 9 additions & 4 deletions mapservutil.c
Expand Up @@ -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).
*/
Expand Down Expand Up @@ -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; i<mapserv->request->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 */
Expand All @@ -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;
}
Expand Down

0 comments on commit 3c3c9b3

Please sign in to comment.