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.

295 lines
9.3 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  3. Module Name:
  4. cachelogic.hxx
  5. Abstract:
  6. This file contains internal flags, structs, and function
  7. prototypes for the HTTP-URLCache interaction layer
  8. Author:
  9. Revision History:
  10. --*/
  11. #ifndef __CACHEINFO_HXX__
  12. #define __CACHEINFO_HXX__
  13. enum CACHE_STATE { CHECK_IF_WANT_CACHE,
  14. CHECK_IF_IN_CACHE,
  15. ADD_PARTIAL_CONTENT_UMS_AND_RANGE_HEADER,
  16. ADD_NORMAL_CONTENT_IMS_HEADER,
  17. PREPARE_DOWNLOAD_FROM_INET,
  18. CACHE_SEND_ERROR,
  19. PREPARE_READ_FROM_CACHE,
  20. PARTIAL_READ,
  21. PREPARE_READ_FROM_INET_AFTER_200_RESPONSE,
  22. BEGIN_DOWNLOAD_FROM_INET,
  23. END_READ_DATA,
  24. BEGIN_CACHE_READ,
  25. END_CACHE_READ,
  26. BEGIN_CACHE_WRITE,
  27. END_CACHE_WRITE,
  28. DOWNLOAD_FROM_INET_WITH_CACHE_WRITE,
  29. DOWNLOAD_FROM_INET_WITHOUT_CACHE_WRITE,
  30. WRITE_TO_CACHE_ENTRY,
  31. PREP_FOR_CACHE_WRITE,
  32. COMMIT_PARTIAL_CACHE_ENTRY,
  33. SEND_REQUEST
  34. };
  35. #define HTTP_LAST_MODIFIED_SZ "Last-Modified:"
  36. #define HTTP_EXPIRES_SZ "Expires:"
  37. #define HTTP_IF_MODIFIED_SINCE_SZ "If-Modified-Since:"
  38. #define HTTP_IF_MODIFIED_SINCE_LEN (sizeof(HTTP_IF_MODIFIED_SINCE_SZ) - 1)
  39. #define HTTP_UMS_SZ "Unless-Modified-Since"
  40. #define HTTP_UMS_LEN (sizeof(HTTP_UMS_SZ) - 1)
  41. #define HTTP_IF_RANGE_SZ "If-Range"
  42. #define HTTP_IF_RANGE_LEN (sizeof(HTTP_IF_RANGE_SZ) - 1)
  43. #define HTTP_RANGE_SZ "Range"
  44. #define HTTP_RANGE_LEN (sizeof(HTTP_RANGE_SZ) - 1)
  45. #define HTTP_IF_NONE_MATCH_SZ "If-None-Match"
  46. #define HTTP_IF_NONE_MATCH_LEN (sizeof(HTTP_IF_NONE_MATCH_SZ) - 1)
  47. #define NO_STORE_SZ "no-store"
  48. #define NO_STORE_LEN (sizeof(NO_STORE_SZ) -1)
  49. #define MUST_REVALIDATE_SZ "must-revalidate"
  50. #define MUST_REVALIDATE_LEN (sizeof(MUST_REVALIDATE_SZ) -1)
  51. #define MAX_AGE_SZ "max-age"
  52. #define MAX_AGE_LEN (sizeof(MAX_AGE_SZ) -1)
  53. #define PRIVATE_SZ "private"
  54. #define PRIVATE_LEN (sizeof(PRIVATE_SZ) - 1)
  55. #define POSTCHECK_SZ "post-check"
  56. #define POSTCHECK_LEN (sizeof(POSTCHECK_SZ) -1)
  57. #define PRECHECK_SZ "pre-check"
  58. #define PRECHECK_LEN (sizeof(PRECHECK_SZ) -1)
  59. #define FILENAME_SZ "filename"
  60. #define FILENAME_LEN (sizeof(FILENAME_SZ) - 1)
  61. #define HTTP1_1_SZ "HTTP/1.1"
  62. #define HTTP1_1_LEN (sizeof(HTTP1_1_SZ) - 1)
  63. #define NO_CACHE_SZ "no-cache"
  64. #define NO_CACHE_LEN (sizeof(NO_CACHE_SZ) -1)
  65. #define USER_AGENT_SZ "user-agent"
  66. #define USER_AGENT_LEN (sizeof(USER_AGENT_SZ) - 1)
  67. #define BYTES_SZ "bytes"
  68. // #define BYTES_LEN (sizeof(BYTES_SZ) - 1)
  69. //
  70. // An instance of the HTTPCACHE_REQUEST class only deals with the cache
  71. // interaction of one HTTP GET request
  72. //
  73. class HTTPCACHE_REQUEST
  74. {
  75. private:
  76. //////////////////////////////////////////////////////////////////////////
  77. //
  78. // Private members used in both cache read and cache write
  79. //
  80. CACHE_STATE _nextState; // initialized in the constructor
  81. CACHE_ENTRY_INFOEX * _pCacheEntryInfo; // initialized in OpenCacheReadStream()
  82. DWORD _RealCacheFileSize;
  83. DWORD _VirtualCacheFileSize;
  84. /////////////////////////////////////////////////////////////////////
  85. //
  86. // Cache-read related private variables and member functions (cacheread.cxx)
  87. //
  88. // The handle to the cache object. Used with the URLCACHE API
  89. HANDLE _hCacheReadStream; // initialized in OpenCacheReadStream()
  90. // This handle is used when for reading partial cache entry
  91. HANDLE _hSparseFileReadHandle;
  92. // Position of the current data stream
  93. DWORD _dwCurrentStreamPosition;
  94. // This variable indicates that an partial entry already exists in the
  95. // cache, and so after the user received the cached content in the
  96. // first ReadData() call, subsequent ReadData() calls should start
  97. // retrieving the data from the Internet.
  98. BOOL _fIsPartialCache;
  99. BOOL _fCacheReadInProgress;
  100. BOOL _fLazyUpdate;
  101. BOOL TransmitRequest(DWORD *);
  102. BOOL IsExpired();
  103. BOOL AddIfModifiedSinceHeaders();
  104. VOID CheckResponseAfterIMS(DWORD);
  105. VOID CalculateTimeStampsForCache();
  106. BOOL ReadDataFromCache(LPVOID, DWORD, LPDWORD);
  107. BOOL OpenCacheReadStream();
  108. BOOL CloseCacheReadStream();
  109. VOID ResetCacheReadVariables();
  110. BOOL FakeCacheResponseHeaders();
  111. BOOL AddTimeResponseHeader(
  112. IN FILETIME fTime,
  113. IN DWORD dwQueryIndex
  114. );
  115. /////////////////////////////////////////////////////////////////////
  116. //
  117. // Cache-write related private variables and member functions (cachewrite.cxx)
  118. //
  119. HANDLE _hCacheWriteFile;
  120. // If commiting the file fails, then we want to get rid of it by flushing
  121. // it out in the destructor
  122. BOOL _fDeleteWriteFile;
  123. // What the filename and extension should be when it's committed to the
  124. // cache. These variables DO NOT indicate the full-path at which the
  125. // cache content resides in the Temporary Internet Files folder
  126. LPSTR _lpszFileName;
  127. LPSTR _lpszFileExtension;
  128. // The full-path of the file that cache contents will be writing to
  129. LPSTR _lpszCacheWriteLocalFilename;
  130. BOOL _fCacheWriteInProgress;
  131. BOOL AppendWriteBuffer(LPCSTR lpBuffer, DWORD dwBufferLen);
  132. BOOL CommitCacheFileEntry(BOOL fNormal);
  133. VOID SetFilenameAndExtForCacheWrite();
  134. BOOL ExtractHeadersForCacheCommit(LPSTR, LPDWORD);
  135. BOOL FCanWriteHTTP1_1ResponseToCache(BOOL *);
  136. BOOL CreateCacheWriteFile();
  137. BOOL WriteToCacheFile(LPBYTE, DWORD, LPDWORD);
  138. BOOL IsStaticImage();
  139. ////////////////////////////////////////////////////////////////////
  140. //
  141. // Routines related to partial cache entries
  142. //
  143. BOOL IsPartialResponseCacheable(VOID);
  144. VOID DeletePartialCacheFile(VOID);
  145. BOOL FakePartialCacheResponseHeaders(VOID);
  146. BOOL IsPartialCacheEntry(VOID);
  147. VOID LockPartialCacheEntry(VOID);
  148. BOOL AddRangeRequestHeaders();
  149. /////////////////////////////////////////////////////////////////////
  150. //
  151. // Time stamps parsed from response
  152. //
  153. // These variables represent file times calculated from HTTP headers from
  154. // the HTTP response. They are calculated in the GetTimeStampsCall() call, and
  155. // are used in the CommitCacheEntry() call.
  156. FILETIME _ftLastModTime;
  157. FILETIME _ftExpiryTime;
  158. FILETIME _ftPostCheckTime;
  159. BOOL _fHasExpiry;
  160. BOOL _fHasLastModTime;
  161. BOOL _fHasPostCheck;
  162. BOOL _fMustRevalidate;
  163. /////////////////////////////////////////////////////////////////////
  164. //
  165. // Global flags.
  166. //
  167. // Global cache flags passed in from the constructor
  168. DWORD _dwCacheFlags;
  169. DWORD _dwRequestFlags;
  170. // Request handle for WinHTTP Request object routines
  171. HINTERNET _hRequest; // initialized in the contsructor
  172. // The URL that this object is dealing with
  173. TCHAR _szUrl[INTERNET_MAX_URL_LENGTH];
  174. public:
  175. /////////////////////////////////////////////////////////////////////
  176. //
  177. // Get operations to query for the object's properties
  178. //
  179. LPSTR GetUrl()
  180. {
  181. return _szUrl;
  182. }
  183. INTERNET_SCHEME GetScheme()
  184. {
  185. return (_dwRequestFlags & WINHTTP_FLAG_SECURE ? INTERNET_SCHEME_HTTPS: INTERNET_SCHEME_HTTP);
  186. }
  187. HINTERNET GetRequestHandle()
  188. {
  189. return _hRequest;
  190. }
  191. /////////////////////////////////////////////////////////////////////
  192. //
  193. // Public interface of the HTTPCACHE_REQUEST class
  194. //
  195. HTTPCACHE_REQUEST(HINTERNET);
  196. ~HTTPCACHE_REQUEST();
  197. BOOL SendRequest(
  198. IN LPCWSTR lpszHeaders,
  199. IN DWORD dwHeadersLength,
  200. IN LPVOID lpOptional,
  201. IN DWORD dwOptionalLength
  202. );
  203. BOOL ReceiveResponse(LPVOID);
  204. BOOL QueryDataAvailable(LPDWORD);
  205. BOOL ReadData(LPVOID, DWORD, LPDWORD);
  206. BOOL CloseRequestHandle();
  207. };
  208. /////////////////////////////////////////////////////////////////////
  209. //
  210. // Utility functions
  211. //
  212. /////////////////////////////////////////////////////////////////////
  213. char * StrTokExA (char ** pstring, const char * control);
  214. LPSTR
  215. GetFileExtensionFromUrl(
  216. IN LPSTR lpszUrl,
  217. IN OUT LPDWORD lpdwLength);
  218. BOOL
  219. GetFileExtensionFromMimeType(
  220. IN LPCSTR lpszMimeType,
  221. IN DWORD dwMimeLen,
  222. IN LPSTR lpszFileExtension,
  223. IN OUT LPDWORD lpdwLen
  224. );
  225. PRIVATE BOOL FExcludedMimeType(
  226. IN LPSTR lpszMimeType,
  227. IN DWORD dwMimeTypeSize
  228. );
  229. #endif // __CACHEINFO_HXX__