|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the MIT License. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +#include "ModuleEnvironment.h" |
| 5 | +#include <string> |
| 6 | +#include <sstream> |
| 7 | + |
| 8 | +// Set in the RegisterModule call IIS uses to initiate the module |
| 9 | +extern DWORD g_dwIISServerVersion; |
| 10 | + |
| 11 | +static std::wstring GetIISVersion() { |
| 12 | + int major = (int)(g_dwIISServerVersion >> 16); |
| 13 | + int minor = (int)(g_dwIISServerVersion & 0xffff); |
| 14 | + |
| 15 | + std::wstringstream version; |
| 16 | + version << major << "." << minor; |
| 17 | + |
| 18 | + return version.str(); |
| 19 | +} |
| 20 | + |
| 21 | +static std::wstring ToVirtualPath(const std::wstring& configurationPath) { |
| 22 | + int segments = 0; |
| 23 | + size_t position = configurationPath.find('/'); |
| 24 | + |
| 25 | + // Skip first 4 segments of config path |
| 26 | + while (segments != 3 && position != std::wstring::npos) |
| 27 | + { |
| 28 | + segments++; |
| 29 | + position = configurationPath.find('/', position + 1); |
| 30 | + } |
| 31 | + |
| 32 | + if (position != std::wstring::npos) |
| 33 | + { |
| 34 | + return configurationPath.substr(position); |
| 35 | + } |
| 36 | + |
| 37 | + return L"/"; |
| 38 | +} |
| 39 | + |
| 40 | +void SetApplicationEnvironmentVariables(_In_ IHttpServer &server, _In_ IHttpContext &pHttpContext) { |
| 41 | + SetEnvironmentVariable(L"ASPNETCORE_IIS_VERSION", GetIISVersion().c_str()); |
| 42 | + |
| 43 | + SetEnvironmentVariable(L"ASPNETCORE_IIS_APP_POOL_ID", server.GetAppPoolName()); |
| 44 | + |
| 45 | + IHttpServer2* server2; |
| 46 | + if (SUCCEEDED(HttpGetExtendedInterface(&server, &server, &server2))) { |
| 47 | + SetEnvironmentVariable(L"ASPNETCORE_IIS_APP_POOL_CONFIG_FILE", server2->GetAppPoolConfigFile()); |
| 48 | + } |
| 49 | + |
| 50 | + IHttpSite* site = pHttpContext.GetSite(); |
| 51 | + SetEnvironmentVariable(L"ASPNETCORE_IIS_SITE_NAME", site->GetSiteName()); |
| 52 | + SetEnvironmentVariable(L"ASPNETCORE_IIS_SITE_ID", std::to_wstring(site->GetSiteId()).c_str()); |
| 53 | + |
| 54 | + IHttpApplication* app = pHttpContext.GetApplication(); |
| 55 | + SetEnvironmentVariable(L"ASPNETCORE_IIS_APP_CONFIG_PATH", app->GetAppConfigPath()); |
| 56 | + SetEnvironmentVariable(L"ASPNETCORE_IIS_APPLICATION_ID", app->GetApplicationId()); |
| 57 | + SetEnvironmentVariable(L"ASPNETCORE_IIS_APPLICATION_VIRTUAL_PATH", ToVirtualPath(app->GetAppConfigPath()).c_str()); |
| 58 | +} |
0 commit comments