Permalink
Browse files

fix #220: support for environment variable based configuration

  • Loading branch information...
1 parent a45411a commit fc9a51f4b739251aa8fc6590874a8bfab66fcc68 @tjanczuk tjanczuk committed Feb 12, 2013
Showing with 94 additions and 18 deletions.
  1. +92 −18 src/iisnode/cmoduleconfiguration.cpp
  2. +1 −0 src/iisnode/cmoduleconfiguration.h
  3. +1 −0 src/iisnode/errors.h
@@ -403,20 +403,58 @@ HRESULT CModuleConfiguration::GetConfigSection(IHttpContext* context, IAppHostEl
return hr;
}
+HRESULT CModuleConfiguration::GetEnvVariable(LPCWSTR propertyName, LPWSTR buffer, DWORD bufferSize, LPWSTR* result)
+{
+ HRESULT hr;
+ WCHAR variableName[124];
+
+ CheckNull(result);
+ ErrorIf(124 < (wcslen(propertyName) + 9), IISNODE_ERROR_UNABLE_TO_READ_CONFIGURATION_FROM_ENVIRONMENT);
+ wcscpy(variableName, L"IISNODE_");
+ wcscat(variableName, propertyName);
+ DWORD size = GetEnvironmentVariableW(variableName, buffer, bufferSize);
+ ErrorIf(size > bufferSize, IISNODE_ERROR_UNABLE_TO_READ_CONFIGURATION_FROM_ENVIRONMENT);
+ if (size == 0)
+ {
+ *result = NULL;
+ ErrorIf(ERROR_ENVVAR_NOT_FOUND != (hr = GetLastError()), hr);
+ }
+ else
+ {
+ *result = buffer;
+ }
+
+ return S_OK;
+Error:
+ return hr;
+}
+
HRESULT CModuleConfiguration::GetString(IAppHostElement* section, LPCWSTR propertyName, LPWSTR* value)
{
HRESULT hr = S_OK;
BSTR sysPropertyName = NULL;
BSTR sysPropertyValue = NULL;
IAppHostProperty* prop = NULL;
+ WCHAR variableValueBuffer[1024];
+ WCHAR* variableValue = NULL;
CheckNull(value);
*value = NULL;
- ErrorIf(NULL == (sysPropertyName = SysAllocString(propertyName)), ERROR_NOT_ENOUGH_MEMORY);
- CheckError(section->GetPropertyByName(sysPropertyName, &prop));
- CheckError(prop->get_StringValue(&sysPropertyValue));
- ErrorIf(NULL == (*value = new WCHAR[wcslen(sysPropertyValue) + 1]), ERROR_NOT_ENOUGH_MEMORY);
- wcscpy(*value, sysPropertyValue);
+
+ CheckError(CModuleConfiguration::GetEnvVariable(propertyName, variableValueBuffer, 1024, &variableValue));
+ if (variableValue)
+ {
+ ErrorIf(NULL == (*value = new WCHAR[wcslen(variableValue) + 1]), ERROR_NOT_ENOUGH_MEMORY);
+ wcscpy(*value, variableValue);
+ }
+ else
+ {
+ ErrorIf(NULL == (sysPropertyName = SysAllocString(propertyName)), ERROR_NOT_ENOUGH_MEMORY);
+ CheckError(section->GetPropertyByName(sysPropertyName, &prop));
+ CheckError(prop->get_StringValue(&sysPropertyValue));
+ ErrorIf(NULL == (*value = new WCHAR[wcslen(sysPropertyValue) + 1]), ERROR_NOT_ENOUGH_MEMORY);
+ wcscpy(*value, sysPropertyValue);
+ }
Error:
@@ -447,20 +485,42 @@ HRESULT CModuleConfiguration::GetBOOL(IAppHostElement* section, LPCWSTR property
BSTR sysPropertyName = NULL;
IAppHostProperty* prop = NULL;
VARIANT var;
+ WCHAR variableValueBuffer[8];
+ WCHAR* variableValue = NULL;
CheckNull(value);
*value = FALSE;
VariantInit(&var);
- ErrorIf(NULL == (sysPropertyName = SysAllocString(propertyName)), ERROR_NOT_ENOUGH_MEMORY);
- if (S_OK != section->GetPropertyByName(sysPropertyName, &prop))
+
+ CheckError(CModuleConfiguration::GetEnvVariable(propertyName, variableValueBuffer, 8, &variableValue));
+ if (variableValue)
{
- *value = defaultValue;
+ if (0 == _wcsicmp(variableValue, L"true") || 0 == _wcsicmp(variableValue, L"1"))
+ {
+ *value = TRUE;
+ }
+ else if (0 == _wcsicmp(variableValue, L"false") || 0 == _wcsicmp(variableValue, L"0"))
+ {
+ *value = FALSE;
+ }
+ else
+ {
+ CheckError(IISNODE_ERROR_UNABLE_TO_READ_CONFIGURATION_FROM_ENVIRONMENT);
+ }
}
- else
+ else
{
- CheckError(prop->get_Value(&var));
- CheckError(VariantChangeType(&var, &var, 0, VT_BOOL));
- *value = (V_BOOL(&var) == VARIANT_TRUE);
+ ErrorIf(NULL == (sysPropertyName = SysAllocString(propertyName)), ERROR_NOT_ENOUGH_MEMORY);
+ if (S_OK != section->GetPropertyByName(sysPropertyName, &prop))
+ {
+ *value = defaultValue;
+ }
+ else
+ {
+ CheckError(prop->get_Value(&var));
+ CheckError(VariantChangeType(&var, &var, 0, VT_BOOL));
+ *value = (V_BOOL(&var) == VARIANT_TRUE);
+ }
}
Error:
@@ -488,15 +548,29 @@ HRESULT CModuleConfiguration::GetDWORD(IAppHostElement* section, LPCWSTR propert
BSTR sysPropertyName = NULL;
IAppHostProperty* prop = NULL;
VARIANT var;
+ WCHAR variableValueBuffer[64];
+ WCHAR* variableValue = NULL;
+ WCHAR* endValue = NULL;
CheckNull(value);
- *value = 0;
+ *value = NULL;
VariantInit(&var);
- ErrorIf(NULL == (sysPropertyName = SysAllocString(propertyName)), ERROR_NOT_ENOUGH_MEMORY);
- CheckError(section->GetPropertyByName(sysPropertyName, &prop));
- CheckError(prop->get_Value(&var));
- CheckError(VariantChangeType(&var, &var, 0, VT_UI4));
- *value = var.ulVal;
+
+ CheckError(CModuleConfiguration::GetEnvVariable(propertyName, variableValueBuffer, 64, &variableValue));
+ if (variableValue)
+ {
+ long parsed = wcstol(variableValue, &endValue, 10);
+ ErrorIf(ERANGE == errno || parsed < 0 || variableValue == endValue, IISNODE_ERROR_UNABLE_TO_READ_CONFIGURATION_FROM_ENVIRONMENT);
+ *value = parsed;
+ }
+ else
+ {
+ ErrorIf(NULL == (sysPropertyName = SysAllocString(propertyName)), ERROR_NOT_ENOUGH_MEMORY);
+ CheckError(section->GetPropertyByName(sysPropertyName, &prop));
+ CheckError(prop->get_Value(&var));
+ CheckError(VariantChangeType(&var, &var, 0, VT_UI4));
+ *value = var.ulVal;
+ }
Error:
@@ -59,6 +59,7 @@ class CModuleConfiguration : public IHttpStoredContext
static HRESULT TokenizePromoteServerVars(CModuleConfiguration* c);
static HRESULT ApplyDefaults(CModuleConfiguration* c);
static HRESULT EnsureCurrent(IHttpContext* context, CModuleConfiguration* config);
+ static HRESULT GetEnvVariable(LPCWSTR propertyName, LPWSTR buffer, DWORD bufferSize, LPWSTR* result);
CModuleConfiguration();
~CModuleConfiguration();
@@ -9,5 +9,6 @@
#define IISNODE_ERROR_UNABLE_TO_CREATE_DEBUGGER_FILES 1030L
#define IISNODE_ERROR_UNABLE_TO_READ_CONFIGURATION 1031L
#define IISNODE_ERROR_UNABLE_TO_READ_CONFIGURATION_OVERRIDE 1032L
+#define IISNODE_ERROR_UNABLE_TO_READ_CONFIGURATION_FROM_ENVIRONMENT 1033L
#endif

0 comments on commit fc9a51f

Please sign in to comment.