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.

530 lines
11 KiB

  1. /*++
  2. Copyright (c) 1994 Microsoft Corporation
  3. Module Name:
  4. cacheapi.cxx
  5. Abstract:
  6. contains the URL cache mangemant APIs.
  7. Author:
  8. Madan Appiah (madana) 12-Dec-1994
  9. Environment:
  10. User Mode - Win32
  11. Revision History:
  12. Shishir Pardikar (shishirp) added: (as of 7/6/96)
  13. 1) Prefixed containers for supporting cookies and history
  14. 2) Default init, for distributing winint without setup
  15. 3) Crossprocess versionchecking scheme to allow all wininets
  16. to know about registry change
  17. --*/
  18. #include <wininetp.h>
  19. #include <cache.hxx>
  20. #include <time.h>
  21. // #include <resource.h>
  22. URLCACHEAPI
  23. BOOL
  24. WINAPI
  25. UnlockUrlCacheEntryStream(
  26. HANDLE hStream,
  27. IN DWORD dwReserved
  28. )
  29. /*++
  30. Routine Description:
  31. This API checks in the file that was check out as part of
  32. RetrieveUrlFile API.
  33. Arguments:
  34. hStreamHandle : stream handle returned by a RetrieveUrlCacheEntryStream call.
  35. dwReserved : reserved for future use.
  36. Return Value:
  37. Windows Error code.
  38. --*/
  39. {
  40. ENTER_CACHE_API ((DBG_API, Bool, "UnlockUrlCacheEntryStream",
  41. "%#x, %#x", hStream, dwReserved))
  42. DWORD Error;
  43. // Map and validate handle.
  44. CACHE_STREAM_CONTEXT_HANDLE *pStream;
  45. LOCK_CACHE();
  46. pStream = (CACHE_STREAM_CONTEXT_HANDLE *) HandleMgr.Map(hStream);
  47. UNLOCK_CACHE();
  48. if (!pStream)
  49. {
  50. Error = ERROR_INVALID_HANDLE;
  51. goto Cleanup;
  52. }
  53. TcpsvcsDbgAssert(pStream->SourceUrlName != NULL );
  54. //
  55. // close file handle.
  56. //
  57. CloseHandle( pStream->FileHandle );
  58. //
  59. // unlock cache file.
  60. //
  61. if(!UnlockUrlCacheEntryFile(pStream->SourceUrlName, dwReserved) )
  62. Error = GetLastError();
  63. else
  64. Error = ERROR_SUCCESS;
  65. //
  66. // freeup url name data buffer.
  67. //
  68. FREE_MEMORY (pStream->SourceUrlName);
  69. //
  70. // free up context structure.
  71. //
  72. LOCK_CACHE();
  73. HandleMgr.Free (hStream);
  74. UNLOCK_CACHE();
  75. LEAVE_CACHE_API();
  76. }
  77. URLCACHEAPI
  78. BOOL
  79. WINAPI
  80. ReadUrlCacheEntryStream(
  81. IN HANDLE hStream,
  82. IN DWORD dwLocation,
  83. IN OUT LPVOID lpBuffer,
  84. IN OUT LPDWORD lpdwLen,
  85. IN DWORD Reserved
  86. )
  87. /*++
  88. Routine Description:
  89. This API provides a method of reading the cached data from a stream
  90. which has been opened using the RetrieveUrlCacheEntryStream API.
  91. Arguments:
  92. hStream : Handle that was returned by the RetrieveCacheEntryStream API.
  93. dwLocation : file offset to read from.
  94. lpBuffer : Pointer to a buffer where the data is read.
  95. lpdwLen : Pointer to a DWORD location where the length of the above buffer passed in, on return it contains the actual length of the data read.
  96. dwReserved : For future use.
  97. Return Value:
  98. Windows Error code.
  99. --*/
  100. {
  101. ENTER_CACHE_API ((DBG_API, Bool, "ReadUrlCacheEntryStream",
  102. "%#x, %d, %#x, %#x, %#x", hStream, dwLocation, lpBuffer, lpdwLen, Reserved));
  103. DWORD Error;
  104. // Map and validate handle.
  105. CACHE_STREAM_CONTEXT_HANDLE* pStream;
  106. LOCK_CACHE();
  107. pStream = (CACHE_STREAM_CONTEXT_HANDLE*) HandleMgr.Map(hStream);
  108. UNLOCK_CACHE();
  109. if (!pStream)
  110. {
  111. Error = ERROR_INVALID_HANDLE;
  112. goto Cleanup;
  113. }
  114. TcpsvcsDbgAssert( pStream->SourceUrlName);
  115. // PERFPERF: should we remember position to avoid this call?
  116. if ((DWORD) -1L == SetFilePointer
  117. (pStream->FileHandle, dwLocation, NULL, FILE_BEGIN))
  118. {
  119. Error = GetLastError();
  120. goto Cleanup;
  121. }
  122. if( !ReadFile
  123. (pStream->FileHandle, lpBuffer, *lpdwLen, lpdwLen, NULL ) )
  124. {
  125. Error = GetLastError();
  126. goto Cleanup;
  127. }
  128. Error = ERROR_SUCCESS;
  129. LEAVE_CACHE_API();
  130. }
  131. URLCACHEAPI
  132. BOOL
  133. WINAPI
  134. FindCloseUrlCache(
  135. IN HANDLE hFind
  136. )
  137. /*++
  138. Routine Description:
  139. This member function returns the next entry in the cache.
  140. Arguments:
  141. hEnumHandle : Find First handle.
  142. Return Value:
  143. Returns the find first handle. If the returned handle is NULL,
  144. GetLastError() returns the extended error code. It returns
  145. ERROR_NO_MORE_ITEMS after it returns the last entry in the cache.
  146. --*/
  147. {
  148. ENTER_CACHE_API ((DBG_API, Bool, "FindCloseUrlCache",
  149. "%#x", hFind));
  150. DWORD Error;
  151. Error = GlobalUrlContainers->FreeFindHandle(hFind);
  152. if (Error != ERROR_SUCCESS)
  153. {
  154. SetLastError( Error );
  155. DEBUG_ERROR(INET, Error);
  156. }
  157. DEBUG_LEAVE_API (Error==ERROR_SUCCESS);
  158. return (Error==ERROR_SUCCESS);
  159. }
  160. BOOL
  161. GetCurrentSettingsVersion(LPDWORD lpdwVer) {
  162. // Initialize globals
  163. if (!InitGlobals())
  164. {
  165. SetLastError (ERROR_WINHTTP_INTERNAL_ERROR);
  166. return FALSE;
  167. }
  168. return GlobalUrlContainers->GetHeaderData(CACHE_HEADER_DATA_CURRENT_SETTINGS_VERSION,
  169. lpdwVer);
  170. }
  171. BOOL
  172. IncrementCurrentSettingsVersion(LPDWORD lpdwVer) {
  173. if (!InitGlobals())
  174. {
  175. SetLastError (ERROR_WINHTTP_INTERNAL_ERROR);
  176. return FALSE;
  177. }
  178. return GlobalUrlContainers->IncrementHeaderData(CACHE_HEADER_DATA_CURRENT_SETTINGS_VERSION,
  179. lpdwVer);
  180. }
  181. BOOL
  182. GetUrlCacheHeaderData(IN DWORD nIdx, OUT LPDWORD lpdwData)
  183. {
  184. if (!InitGlobals())
  185. {
  186. SetLastError (ERROR_WINHTTP_INTERNAL_ERROR);
  187. return FALSE;
  188. }
  189. return GlobalUrlContainers->GetHeaderData(nIdx, lpdwData);
  190. }
  191. BOOL
  192. SetUrlCacheHeaderData(IN DWORD nIdx, IN DWORD dwData)
  193. {
  194. if (!InitGlobals())
  195. {
  196. SetLastError (ERROR_WINHTTP_INTERNAL_ERROR);
  197. return FALSE;
  198. }
  199. return GlobalUrlContainers->SetHeaderData(nIdx, dwData);
  200. }
  201. BOOL
  202. IncrementUrlCacheHeaderData(IN DWORD nIdx, OUT LPDWORD lpdwData)
  203. {
  204. if (!InitGlobals())
  205. {
  206. SetLastError (ERROR_WINHTTP_INTERNAL_ERROR);
  207. return FALSE;
  208. }
  209. return GlobalUrlContainers->IncrementHeaderData(nIdx, lpdwData);
  210. }
  211. BOOL
  212. LoadUrlCacheContent(VOID)
  213. {
  214. DWORD dwError;
  215. if (!InitGlobals())
  216. {
  217. SetLastError(ERROR_WINHTTP_INTERNAL_ERROR);
  218. return FALSE;
  219. }
  220. dwError = GlobalUrlContainers->LoadContent();
  221. if (dwError == ERROR_SUCCESS)
  222. return TRUE;
  223. SetLastError(dwError);
  224. return FALSE;
  225. }
  226. BOOL
  227. GetUrlCacheContainerInfo(
  228. IN LPSTR lpszUrlName,
  229. OUT LPINTERNET_CACHE_CONTAINER_INFOA lpContainerInfo,
  230. IN OUT LPDWORD lpdwContainerInfoBufferSize,
  231. IN DWORD dwOptions
  232. )
  233. {
  234. DWORD dwError;
  235. // Initialize globals.
  236. if (!InitGlobals())
  237. {
  238. SetLastError(ERROR_WINHTTP_INTERNAL_ERROR);
  239. return FALSE;
  240. }
  241. dwError = GlobalUrlContainers->GetContainerInfo(lpszUrlName,
  242. lpContainerInfo, lpdwContainerInfoBufferSize);
  243. if (dwError == ERROR_SUCCESS)
  244. return TRUE;
  245. SetLastError(dwError);
  246. return FALSE;
  247. }
  248. /*
  249. BOOL
  250. UpdateUrlCacheContentPath(
  251. IN LPSTR lpszNewPath
  252. )
  253. {
  254. ENTER_CACHE_API ((DBG_API, Bool, "UpdateUrlCacheContentPath", "%q", lpszNewPath));
  255. INET_ASSERT(GlobalCacheInitialized);
  256. BOOL fResult = GlobalUrlContainers->SetContentPath(lpszNewPath);
  257. DEBUG_LEAVE_API(fResult);
  258. return fResult;
  259. }
  260. */
  261. INTERNETAPI
  262. GROUPID
  263. WINAPI
  264. CreateUrlCacheGroup(
  265. IN DWORD dwFlags,
  266. IN LPVOID lpReserved // must pass NULL
  267. )
  268. {
  269. ENTER_CACHE_API ((DBG_API, Bool, "CreateUrlCacheGroup", "%#x, %#x", dwFlags, lpReserved));
  270. GROUPID gid = 0;
  271. DWORD Error;
  272. // Initialize globals
  273. if (!InitGlobals())
  274. {
  275. Error = ERROR_WINHTTP_INTERNAL_ERROR;
  276. goto Cleanup;
  277. }
  278. Error = GlobalUrlContainers->CreateGroup(dwFlags, &gid);
  279. Cleanup:
  280. if( Error != ERROR_SUCCESS )
  281. {
  282. SetLastError(Error);
  283. DEBUG_ERROR(API, Error);
  284. }
  285. DEBUG_LEAVE_API(gid);
  286. return gid;
  287. }
  288. BOOLAPI
  289. DeleteUrlCacheGroup(
  290. IN GROUPID GroupId,
  291. IN DWORD dwFlags, // must pass 0
  292. IN LPVOID lpReserved // must pass NULL
  293. )
  294. {
  295. ENTER_CACHE_API ((DBG_API, Bool, "DeleteUrlCacheGroup", "%#x, %#x, %#x", GroupId, dwFlags, lpReserved));
  296. DWORD Error;
  297. // Initialize globals
  298. if (!InitGlobals())
  299. {
  300. Error = ERROR_WINHTTP_INTERNAL_ERROR;
  301. goto Cleanup;
  302. }
  303. Error = GlobalUrlContainers->DeleteGroup(GroupId, dwFlags);
  304. LEAVE_CACHE_API();
  305. }
  306. URLCACHEAPI
  307. HANDLE
  308. WINAPI
  309. FindFirstUrlCacheGroup(
  310. IN DWORD dwFlags,
  311. IN DWORD dwFilter,
  312. IN LPVOID lpSearchCondition,
  313. IN DWORD dwSearchCondition,
  314. OUT GROUPID* lpGroupId,
  315. IN OUT LPVOID lpReserved
  316. )
  317. {
  318. ENTER_CACHE_API ((DBG_API, Handle, "FindFirstUrlCacheGroup",
  319. "%d, %d, %#x, %d, %#x, %#x",
  320. dwFlags, dwFilter, lpSearchCondition,
  321. dwSearchCondition, lpGroupId, lpReserved ));
  322. DWORD Error;
  323. HANDLE hFind = 0;
  324. // Validate parameters.
  325. if( !lpGroupId )
  326. {
  327. Error = ERROR_INVALID_PARAMETER;
  328. goto Cleanup;
  329. }
  330. // Initialize globals.
  331. if (!InitGlobals())
  332. {
  333. Error = ERROR_WINHTTP_INTERNAL_ERROR;
  334. goto Cleanup;
  335. }
  336. // Get the first entry.
  337. Error = GlobalUrlContainers->FindNextGroup(&hFind, dwFlags, lpGroupId);
  338. Cleanup:
  339. if( Error != ERROR_SUCCESS )
  340. {
  341. GlobalUrlContainers->FreeFindHandle(hFind);
  342. SetLastError(Error);
  343. DEBUG_ERROR(API, Error);
  344. hFind = NULL;
  345. }
  346. INET_ASSERT (hFind);
  347. DEBUG_LEAVE_API (hFind);
  348. return hFind;
  349. }
  350. URLCACHEAPI
  351. BOOL
  352. WINAPI
  353. FindNextUrlCacheGroup(
  354. IN HANDLE hFind,
  355. OUT GROUPID* lpGroupId,
  356. IN OUT LPVOID lpReserved
  357. )
  358. {
  359. ENTER_CACHE_API ((DBG_API, Bool, "FindNextUrlCacheGroup",
  360. "%#x, %#x, %#x", hFind, lpGroupId, lpReserved ));
  361. DWORD Error;
  362. // Validate parameters.
  363. if( !lpGroupId )
  364. {
  365. Error = ERROR_INVALID_PARAMETER;
  366. goto Cleanup;
  367. }
  368. // Initialize globals.
  369. if (!InitGlobals())
  370. {
  371. Error = ERROR_WINHTTP_INTERNAL_ERROR;
  372. goto Cleanup;
  373. }
  374. // Get the next entry.
  375. Error = GlobalUrlContainers->FindNextGroup(&hFind, 0, lpGroupId);
  376. LEAVE_CACHE_API();
  377. }
  378. BOOL
  379. AnyFindsInProgress(DWORD ContainerID)
  380. {
  381. LOCK_CACHE();
  382. BOOL fInProgress = HandleMgr.InUse();
  383. UNLOCK_CACHE();
  384. return fInProgress;
  385. }
  386. BOOL
  387. RegisterUrlCacheNotification(
  388. HWND hWnd,
  389. UINT uMsg,
  390. GROUPID gid,
  391. DWORD dwFilter,
  392. DWORD dwReserve
  393. )
  394. {
  395. DWORD Error;
  396. ENTER_CACHE_API ((DBG_API, Bool, "RegisterUrlCacheNotification",
  397. "%#x,,%#x, %#x, %#x, %#x", hWnd, uMsg, gid, dwFilter, dwReserve));
  398. // Initialize globals.
  399. if (!InitGlobals())
  400. {
  401. Error = ERROR_WINHTTP_INTERNAL_ERROR;
  402. goto Cleanup;
  403. }
  404. Error = GlobalUrlContainers->RegisterCacheNotify(hWnd, uMsg, gid, dwFilter);
  405. LEAVE_CACHE_API();
  406. }