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.

391 lines
9.8 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  3. Module Name:
  4. httpcache.cxx
  5. Abstract:
  6. Contains API implementation of the WinHTTP-UrlCache interaction layer
  7. Environment:
  8. Win32 user-level
  9. Revision History:
  10. --*/
  11. /*++
  12. - TODO: All functions assume that parameter validation has been performed already in the layer above it. Make
  13. sure all parameters that gets passed in (in test programs etc...) are validated and correct.
  14. - As it currently stands, the HTTP-Cache API functions are exposed outside via DLL exports. This is NOT what's supposed
  15. to happen. This layer is supposed to be an internal layer. Eliminate the DLL exports as soon as the API hooks are
  16. completed and extensive testing has been done to make sure that the component is really working as expected.
  17. - FindUrlCacheEntry trys to find the address specified by "http://"+_szServername+_szLastVerb
  18. -- */
  19. #include <wininetp.h>
  20. #define __CACHE_INCLUDE__
  21. #include "..\urlcache\cache.hxx"
  22. #include "cachelogic.hxx"
  23. #include "cachehndl.hxx"
  24. ////////////////////////////////////////////////////////////////////////////////////////////
  25. // Global Variables
  26. //
  27. CACHE_HANDLE_MANAGER * CacheHndlMgr;
  28. ////////////////////////////////////////////////////////////////////////////////////////////
  29. //
  30. // API implementation
  31. //
  32. HINTERNET
  33. WINAPI
  34. WinHttpCacheOpen(
  35. IN LPCWSTR pszAgentW,
  36. IN DWORD dwAccessType,
  37. IN LPCWSTR pszProxyW OPTIONAL,
  38. IN LPCWSTR pszProxyBypassW OPTIONAL,
  39. IN DWORD dwFlags
  40. )
  41. {
  42. DEBUG_ENTER((DBG_CACHE,
  43. Handle,
  44. "WinHttpCacheOpen",
  45. "%wq, %s (%d), %wq, %wq, %#x",
  46. pszAgentW,
  47. InternetMapOpenType(dwAccessType),
  48. dwAccessType,
  49. pszProxyW,
  50. pszProxyBypassW,
  51. dwFlags
  52. ));
  53. DWORD dwErr;
  54. HINTERNET hInternet = NULL;
  55. if ((dwFlags & WINHTTP_FLAG_ASYNC) &&
  56. (dwFlags & WINHTTP_CACHE_FLAGS_MASK))
  57. {
  58. dwErr = ERROR_INVALID_PARAMETER;
  59. goto cleanup;
  60. }
  61. hInternet = WinHttpOpen(
  62. pszAgentW,
  63. dwAccessType,
  64. pszProxyW,
  65. pszProxyBypassW,
  66. dwFlags);
  67. cleanup:
  68. if (dwErr!=ERROR_SUCCESS) {
  69. SetLastError(dwErr);
  70. DEBUG_ERROR(API, dwErr);
  71. }
  72. DEBUG_LEAVE_API(hInternet);
  73. return hInternet;
  74. }
  75. HINTERNET
  76. WINAPI
  77. WinHttpCacheOpenRequest(
  78. IN HINTERNET hConnect,
  79. IN LPCWSTR lpszVerb,
  80. IN LPCWSTR lpszObjectName,
  81. IN LPCWSTR lpszVersion,
  82. IN LPCWSTR lpszReferrer OPTIONAL,
  83. IN LPCWSTR FAR * lplpszAcceptTypes OPTIONAL,
  84. IN DWORD dwFlags
  85. )
  86. {
  87. DEBUG_ENTER_API((DBG_CACHE,
  88. Handle,
  89. "WinHttpCacheOpenRequest",
  90. "%#x, %.80wq, %.80wq, %.80wq, %.80wq, %#x, %#08x",
  91. hConnect,
  92. lpszVerb,
  93. lpszObjectName,
  94. lpszVersion,
  95. lpszReferrer,
  96. lplpszAcceptTypes,
  97. dwFlags
  98. ));
  99. HINTERNET hRequest;
  100. hRequest = WinHttpOpenRequest(
  101. hConnect,
  102. lpszVerb,
  103. lpszObjectName,
  104. lpszVersion,
  105. lpszReferrer,
  106. lplpszAcceptTypes,
  107. dwFlags);
  108. if (hRequest != NULL)
  109. {
  110. // The caching layer only works with GET requests
  111. if(wcscmp(L"GET", lpszVerb) == 0)
  112. {
  113. if (CacheHndlMgr == NULL)
  114. {
  115. CacheHndlMgr = new CACHE_HANDLE_MANAGER;
  116. if (CacheHndlMgr == NULL)
  117. {
  118. DEBUG_PRINT(CACHE, ERROR, ("Not enough memory to initialize CACHE_HANDLE_MANAGER"));
  119. goto quit;
  120. }
  121. }
  122. CacheHndlMgr->AddCacheRequestObject(hRequest);
  123. }
  124. }
  125. quit:
  126. DEBUG_LEAVE(hRequest);
  127. return hRequest;
  128. }
  129. BOOL
  130. WINAPI
  131. WinHttpCacheSendRequest(
  132. IN HINTERNET hRequest,
  133. IN LPCWSTR lpszHeaders,
  134. IN DWORD dwHeadersLength,
  135. IN LPVOID lpOptional,
  136. IN DWORD dwOptionalLength,
  137. IN DWORD dwTotalLength,
  138. IN DWORD_PTR dwContext
  139. )
  140. {
  141. DEBUG_ENTER((DBG_CACHE,
  142. Bool,
  143. "WinHttpCacheSendRequest",
  144. "%#x, %.80wq, %d, %#x, %d, %d, %x",
  145. hRequest,
  146. lpszHeaders,
  147. dwHeadersLength,
  148. lpOptional,
  149. dwOptionalLength,
  150. dwTotalLength,
  151. dwContext
  152. ));
  153. BOOL fResult = FALSE;
  154. if (CacheHndlMgr != NULL)
  155. {
  156. HTTPCACHE_REQUEST * HTTPCacheRequest;
  157. if ((HTTPCacheRequest =
  158. CacheHndlMgr->GetCacheRequestObject(hRequest)) != NULL)
  159. {
  160. fResult = HTTPCacheRequest->SendRequest(
  161. lpszHeaders,
  162. dwHeadersLength,
  163. lpOptional,
  164. dwOptionalLength
  165. );
  166. goto quit;
  167. }
  168. }
  169. fResult = WinHttpSendRequest(hRequest,
  170. lpszHeaders,
  171. dwHeadersLength,
  172. lpOptional,
  173. dwOptionalLength,
  174. dwTotalLength,
  175. dwContext);
  176. quit:
  177. DEBUG_LEAVE(fResult);
  178. return fResult;
  179. }
  180. BOOL
  181. WINAPI
  182. WinHttpCacheReceiveResponse(
  183. IN HINTERNET hRequest,
  184. IN LPVOID lpBuffersOut
  185. )
  186. {
  187. DEBUG_ENTER((DBG_CACHE,
  188. Bool,
  189. "WinHttpCacheReceiveResponse",
  190. "%#x, %#x",
  191. hRequest,
  192. lpBuffersOut
  193. ));
  194. BOOL fResult;
  195. if (CacheHndlMgr != NULL)
  196. {
  197. HTTPCACHE_REQUEST * HTTPCacheRequest;
  198. if ((HTTPCacheRequest =
  199. CacheHndlMgr->GetCacheRequestObject(hRequest)) != NULL)
  200. {
  201. fResult = HTTPCacheRequest->ReceiveResponse(lpBuffersOut);
  202. goto quit;
  203. }
  204. }
  205. fResult = WinHttpReceiveResponse(hRequest,
  206. lpBuffersOut);
  207. quit:
  208. DEBUG_LEAVE(fResult);
  209. return fResult;
  210. }
  211. BOOL
  212. WINAPI
  213. WinHttpCacheQueryDataAvailable(
  214. IN HINTERNET hRequest,
  215. OUT LPDWORD lpdwNumberOfBytesAvailable
  216. )
  217. {
  218. DEBUG_ENTER((DBG_CACHE,
  219. Bool,
  220. "WinHttpCacheQueryDataAvailable",
  221. "%#x, %#x, %#x",
  222. hRequest,
  223. lpdwNumberOfBytesAvailable
  224. ));
  225. BOOL fResult;
  226. if (CacheHndlMgr != NULL)
  227. {
  228. HTTPCACHE_REQUEST * HTTPCacheRequest;
  229. if ((HTTPCacheRequest =
  230. CacheHndlMgr->GetCacheRequestObject(hRequest)) != NULL)
  231. {
  232. fResult = HTTPCacheRequest->QueryDataAvailable(lpdwNumberOfBytesAvailable);
  233. goto quit;
  234. }
  235. }
  236. fResult = WinHttpQueryDataAvailable(hRequest,
  237. lpdwNumberOfBytesAvailable);
  238. quit:
  239. DEBUG_LEAVE(fResult);
  240. return fResult;
  241. }
  242. BOOL
  243. WINAPI
  244. WinHttpCacheReadData(
  245. IN HINTERNET hRequest,
  246. IN LPVOID lpBuffer,
  247. IN DWORD dwNumberOfBytesToRead,
  248. OUT LPDWORD lpdwNumberOfBytesRead
  249. )
  250. {
  251. DEBUG_ENTER((DBG_CACHE,
  252. Bool,
  253. "WinHttpCacheReadData",
  254. "%#x, %#x, %d, %#x",
  255. hRequest,
  256. lpBuffer,
  257. dwNumberOfBytesToRead,
  258. lpdwNumberOfBytesRead
  259. ));
  260. BOOL fResult;
  261. if (CacheHndlMgr != NULL)
  262. {
  263. HTTPCACHE_REQUEST * HTTPCacheRequest;
  264. if ((HTTPCacheRequest =
  265. CacheHndlMgr->GetCacheRequestObject(hRequest)) != NULL)
  266. {
  267. fResult = HTTPCacheRequest->ReadData(lpBuffer,
  268. dwNumberOfBytesToRead,
  269. lpdwNumberOfBytesRead);
  270. goto quit;
  271. }
  272. }
  273. fResult = WinHttpReadData(hRequest,
  274. lpBuffer,
  275. dwNumberOfBytesToRead,
  276. lpdwNumberOfBytesRead);
  277. quit:
  278. DEBUG_LEAVE(fResult);
  279. return fResult;
  280. }
  281. BOOL
  282. WINAPI
  283. WinHttpCacheCloseHandle(
  284. IN HINTERNET hInternet
  285. )
  286. {
  287. DEBUG_ENTER((DBG_CACHE,
  288. Bool,
  289. "WinHTTPCacheCloseRequestHandle",
  290. "%#x",
  291. hInternet
  292. ));
  293. DWORD dwHandleType;
  294. DWORD dwSize = sizeof(DWORD);
  295. DWORD fResult = FALSE;
  296. if (hInternet == NULL)
  297. goto quit;
  298. WinHttpQueryOption(hInternet, WINHTTP_OPTION_HANDLE_TYPE, &dwHandleType, &dwSize);
  299. if (dwHandleType == WINHTTP_HANDLE_TYPE_REQUEST)
  300. {
  301. if (CacheHndlMgr != NULL)
  302. {
  303. HTTPCACHE_REQUEST * HTTPCacheRequest;
  304. if ((HTTPCacheRequest =
  305. CacheHndlMgr->GetCacheRequestObject(hInternet)) != NULL)
  306. {
  307. fResult = HTTPCacheRequest->CloseRequestHandle();
  308. CacheHndlMgr->RemoveCacheRequestObject(hInternet);
  309. if (CacheHndlMgr->RefCount() == 0)
  310. {
  311. delete CacheHndlMgr;
  312. CacheHndlMgr = NULL;
  313. }
  314. goto quit;
  315. }
  316. }
  317. }
  318. fResult = WinHttpCloseHandle(hInternet);
  319. quit:
  320. DEBUG_LEAVE(fResult);
  321. return fResult;
  322. }