Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
5.3 KiB

  1. /********************************************************************************
  2. / This is the base file to the Microsoft JScript Proxy Configuration
  3. / This file implements the code to provide the script site and the JSProxy psuedo
  4. / object for the script engine to call against.
  5. /
  6. / Created 11/27/96 larrysu
  7. /
  8. /
  9. /
  10. /
  11. /
  12. /
  13. /
  14. /
  15. /
  16. */
  17. #include "dllmain.h"
  18. CScriptSite *g_ScriptSite = NULL;
  19. BOOL fOleInited = FALSE;
  20. /*******************************************************************************
  21. * dll initialization and destruction
  22. ********************************************************************************/
  23. EXTERN_C
  24. BOOL APIENTRY DllMain(HMODULE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)
  25. {
  26. switch( ul_reason_for_call ) {
  27. case DLL_PROCESS_ATTACH:
  28. DisableThreadLibraryCalls(hModule);
  29. break;
  30. case DLL_PROCESS_DETACH:
  31. break;
  32. }
  33. return TRUE;
  34. }
  35. STDAPI_(BOOL) AUTOCONF_InternetInitializeAutoProxyDll(DWORD dwVersion,
  36. LPSTR lpszDownloadedTempFile,
  37. LPSTR lpszMime,
  38. AUTO_PROXY_HELPER_APIS *pAutoProxyCallbacks,
  39. LPAUTO_PROXY_EXTERN_STRUC lpExtraData)
  40. {
  41. HRESULT hr = E_FAIL;
  42. HANDLE hFile = 0;
  43. LPSTR szScript = NULL;
  44. DWORD dwFileSize = 0;
  45. DWORD dwBytesRead = 0;
  46. LPSTR result;
  47. LPSTR szAllocatedScript = NULL;
  48. if ( !fOleInited )
  49. {
  50. #ifndef unix
  51. CoInitializeEx(NULL, COINIT_MULTITHREADED);
  52. #else
  53. CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
  54. #endif /* unix */
  55. }
  56. // get the script text from the downloaded file!
  57. // open the file
  58. if ( lpExtraData == NULL ||
  59. lpExtraData->dwStructSize != sizeof(AUTO_PROXY_EXTERN_STRUC) ||
  60. lpExtraData->lpszScriptBuffer == NULL )
  61. {
  62. if (!lpszDownloadedTempFile)
  63. return FALSE;
  64. hFile = CreateFile((LPCSTR)lpszDownloadedTempFile,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
  65. if (hFile == INVALID_HANDLE_VALUE)
  66. return FALSE;
  67. // Get the size
  68. dwFileSize = GetFileSize(hFile,NULL);
  69. // allocate the buffer to hold the data.
  70. szScript = (LPSTR) GlobalAlloc(GMEM_FIXED|GMEM_ZEROINIT,dwFileSize+1);
  71. szAllocatedScript = szScript;
  72. BOOL f = TRUE;
  73. // if the memory was allocated
  74. if (szScript)
  75. {
  76. // read the data
  77. f = ReadFile(hFile,(LPVOID) szScript,dwFileSize,&dwBytesRead,NULL);
  78. }
  79. CloseHandle(hFile);
  80. if (!f)
  81. goto Cleanup;
  82. }
  83. else
  84. {
  85. szScript = (LPSTR) lpExtraData->lpszScriptBuffer;
  86. }
  87. // Create a new CScriptSite object and initiate it with the autoconfig script.
  88. g_ScriptSite = new CScriptSite;
  89. if (g_ScriptSite)
  90. hr = g_ScriptSite->Init(pAutoProxyCallbacks, szScript);
  91. else
  92. hr = E_OUTOFMEMORY;
  93. Cleanup:
  94. if ( szAllocatedScript )
  95. {
  96. // Free the script text
  97. GlobalFree(szAllocatedScript);
  98. szAllocatedScript = NULL;
  99. }
  100. if (SUCCEEDED(hr))
  101. return TRUE;
  102. else
  103. return FALSE;
  104. }
  105. // This function frees the script engine and destroys the script site.
  106. EXTERN_C BOOL CALLBACK AUTOCONF_InternetDeInitializeAutoProxyDll(LPSTR lpszMime, DWORD dwReserved)
  107. {
  108. // Release and destroy the CScriptSite object and initiate it with the autoconfig script.
  109. // DeInit the script site.
  110. if (g_ScriptSite)
  111. {
  112. g_ScriptSite->DeInit();
  113. g_ScriptSite->Release();
  114. g_ScriptSite = NULL;
  115. }
  116. if ( fOleInited )
  117. {
  118. CoUninitialize();
  119. }
  120. return TRUE;
  121. }
  122. // This function is called when the host wants to run the script.
  123. EXTERN_C BOOL CALLBACK InternetGetProxyInfo(LPCSTR lpszUrl,
  124. DWORD dwUrlLength,
  125. LPSTR lpszUrlHostName,
  126. DWORD dwUrlHostNameLength,
  127. LPSTR *lplpszProxyHostName,
  128. LPDWORD lpdwProxyHostNameLength)
  129. {
  130. HRESULT hr = S_OK;
  131. LPSTR szHost;
  132. // The host passed in may be too big. Copy it an make the
  133. // HostLength + 1 position will be slammed with \0.
  134. szHost = (LPSTR) GlobalAlloc(GMEM_FIXED|GMEM_ZEROINIT,dwUrlHostNameLength+1);
  135. if (!szHost)
  136. return FALSE;
  137. if(lpszUrlHostName && !lstrcpyn(szHost,lpszUrlHostName,dwUrlHostNameLength+1))
  138. {
  139. GlobalFree(szHost);
  140. return FALSE;
  141. }
  142. // construct a jscript call with the passed in url and host.
  143. if (g_ScriptSite)
  144. // hr = g_ScriptSite->RunScript(lpszUrl,lpszUrlHostName,lplpszProxyHostName);
  145. hr = g_ScriptSite->RunScript(lpszUrl,szHost,lplpszProxyHostName);
  146. GlobalFree(szHost);
  147. if (SUCCEEDED(hr))
  148. {
  149. *lpdwProxyHostNameLength = lstrlen(*lplpszProxyHostName) +1;
  150. return TRUE;
  151. }
  152. else
  153. return FALSE;
  154. }