Source code of Windows XP (NT5)
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.

187 lines
6.4 KiB

  1. // Copyright (c) 1998-1999 Microsoft Corporation. All Rights Reserved.
  2. #ifndef _INC_CWUDownload
  3. #include <wininet.h>
  4. #include <wustl.h>
  5. //
  6. // NOTE: IWUProgress not an OLE interface. We will report progress
  7. // using this interface. Currently CWUProgress implmenents
  8. // this interface and displays a progress dialog box
  9. class IWUProgress
  10. {
  11. public:
  12. virtual void SetDownloadTotal(DWORD dwTotal) = 0;
  13. virtual void SetDownload(DWORD dwDone = 0xffffffff) = 0;
  14. virtual void SetDownloadAdd(DWORD dwAddSize, DWORD dwTime = 0) = 0;
  15. virtual void SetInstallTotal(DWORD dwTotal) = 0;
  16. virtual void SetInstall(DWORD dwDone = 0xffffffff) = 0;
  17. virtual void SetInstallAdd(DWORD dwAdd) = 0;
  18. virtual void SetStatusText(LPCTSTR pszStatus) = 0;
  19. virtual HANDLE GetCancelEvent() = 0;
  20. };
  21. #define NO_DOWNLOAD_FLAGS 0x00
  22. #define CACHE_FILE_LOCALLY 0x01
  23. #define DOWNLOAD_NEWER 0x02
  24. #define EXACT_FILENAME 0x04
  25. const int MAX_TIMESTRING_LENGTH = 100;
  26. // session ID initialization constants
  27. const int SID_INITIALIZED = 1;
  28. const int SID_NOT_INITIALIZED = 0;
  29. class CWUDownload
  30. {
  31. public:
  32. static BOOL s_fOffline;
  33. static TCHAR m_szWUSessionID[MAX_TIMESTRING_LENGTH]; // unique session ID (SID)
  34. static int m_fIsSIDInitialized; // flag - has the static SID beein initialized?
  35. CWUDownload(
  36. IN LPCTSTR pszURL,
  37. IN OPTIONAL int iBufferSize = 1024
  38. );
  39. ~CWUDownload();
  40. /*
  41. * BOOL Copy(IN LPCTSTR szSourceFile, IN OPTIONAL LPCTSTR szDestFile, IN OPTIONAL PBYTE *ppData, IN OPTIONAL ULONG *piDataLen, IN OPTIONAL int (*CallBack)(int iPercent))
  42. *
  43. * PURPOSE: This method reads a file from an internet server.
  44. *
  45. * ARGUMENTS:
  46. *
  47. * IN LPSTR szSourceFile name of file to download. This is relative to the
  48. * server root so that http://www.server.com/myfile.txt
  49. * would be specified as: myfile.txt.
  50. * IN OPTIONAL LPSTR szDestFile local destination this is NULL if no local file required
  51. * OUT OPTIONAL PBYTE *ppData returned pointer to memory block that will contain the
  52. * downloaded file on a successfull return. This parameter
  53. * can be NULL if this functionality is not required.
  54. * IN OPTIONAL ULONG *piDataLen pointer to returned downloaded data file length. This
  55. * parameter can be NULL if the downloaded file size is
  56. * not required. The caller is responsible for freeing this
  57. * pointer if a non NULL pointer is returned to the caller.
  58. * IN OPTIONAL BOOL dwFlags Flags that control the specific download behavior. This
  59. * parameter can be 0 which is the default or one or more of
  60. * of the following:
  61. * NO_DOWNLOAD_FLAGS no download modifiers, this is the same as 0.
  62. * CACHE_FILE_LOCALLY cache the file in the Windows Update directory.
  63. * DOWNLOAD_NEWER download the file only if it's date and time is
  64. * newer than the file with the same name in the
  65. * Local machines Windows Update cache.
  66. * use local file if it is the same or newer.
  67. * EXACT_FILENAME If this flag is specified then the file
  68. * created in the cache will be exactly the
  69. * same as the file on the server. If this
  70. * flags is not specified then the cache file
  71. * name will be constructed of the entire
  72. * directory path name.
  73. * IN OPTIONAL IWUProgress* pProgress
  74. * A pointer to a IWUProgress interface. This paramter
  75. * can be NULL if progress is not required.
  76. *
  77. *
  78. * COMMENTS:
  79. *
  80. * If a callback function is specified as long as FALSE is returned the download continued. If
  81. * the callback function returns TRUE then the download is aborted and LastError is set to
  82. * ERROR_OPERATION_ABORTED. This allows the application to control the download process.
  83. */
  84. BOOL Copy(
  85. IN LPCTSTR szSourceFile,
  86. IN OPTIONAL LPCTSTR szDestFile = NULL,
  87. OUT OPTIONAL PVOID *ppData = NULL,
  88. OUT OPTIONAL ULONG *piDataLen = NULL,
  89. IN OPTIONAL BOOL dwFlags = NO_DOWNLOAD_FLAGS,
  90. IN OPTIONAL IWUProgress* pProgress = NULL
  91. );
  92. /*
  93. * BOOL QCopy(IN LPSTR szSourceFile, IN PBYTE *ppData, IN OPTIONAL ULONG *piDataLen)
  94. *
  95. * PURPOSE: This method reads a file from an internet server into memory. This function
  96. * is used when the file being read is < 1024 bytes in length so can be read
  97. * quickly.
  98. *
  99. * ARGUMENTS:
  100. *
  101. * IN LPSTR szSourceFile name of file to download. This is relative to the
  102. * server root so that http://www.server.com/myfile.txt
  103. * would be specified as: myfile.txt.
  104. * OUT PBYTE *ppData returned pointer to memory block that will contain the
  105. * downloaded file on a successfull return. This parameter
  106. * can be NULL if this functionality is not required.
  107. * IN OPTIONAL ULONG *piDataLen pointer to returned downloaded data file length. This
  108. * parameter can be NULL if the downloaded file size is
  109. * not required. The caller is responsible for freeing this
  110. * pointer if a non NULL pointer is returned to the caller.
  111. *
  112. * COMMENTS:
  113. *
  114. */
  115. BOOL QCopy(
  116. IN LPCTSTR szSourceFile,
  117. OUT OPTIONAL PVOID *ppData,
  118. OUT OPTIONAL ULONG *piDataLen
  119. );
  120. BOOL MemCopy(
  121. IN LPCTSTR szSourceFile,
  122. OUT byte_buffer& bufDest
  123. );
  124. // posts formdata and downloads the response to szDestFile
  125. BOOL PostCopy(LPCTSTR pszFormData, LPCTSTR szDestFile, IWUProgress* pProgress);
  126. LPTSTR GetServerPath()
  127. {
  128. return m_szServerPath;
  129. }
  130. BOOL CacheUsed()
  131. {
  132. return m_bCacheUsed;
  133. }
  134. // return time taken for last download operation in Millisecons, 0 if cache used
  135. DWORD GetCopyTime()
  136. {
  137. return m_dwCopyTime;
  138. }
  139. // return size in bytes of the last file downloaded
  140. DWORD GetCopySize()
  141. {
  142. return m_dwCopySize;
  143. }
  144. private:
  145. int m_iBufferSize; //internal copy buffer size.
  146. PSTR m_szBuffer; //internal copy buffer of m_iBufferSize bytes.
  147. HINTERNET m_hSession; //handle to wininet library
  148. HINTERNET m_hConnection; //handle to server connection.
  149. HANDLE m_exitEvent; //request thread handler to exit
  150. HANDLE m_readEvent; //request internet block read
  151. HANDLE m_finished; //requested event (read or exit) is finished
  152. TCHAR *m_szServerPath;
  153. TCHAR *m_szRelURL;
  154. BOOL m_bCacheUsed;
  155. DWORD m_dwCopyTime;
  156. DWORD m_dwCopySize;
  157. };
  158. #define _INC_CWUDownload
  159. #endif