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.

188 lines
4.0 KiB

  1. // File: Utilities.cpp
  2. #include "stdafx.h"
  3. #include <tchar.h>
  4. #include <wininet.h>
  5. #include <windows.h>
  6. #include "Utilities.h"
  7. /*
  8. int
  9. UploadFile(
  10. )
  11. {
  12. DWORD ErrorCode = 0;
  13. HINTERNET hSession = NULL;
  14. HINTERNET hRequest = NULL;
  15. HINTERNET hConnect = NULL;
  16. INTERNET_BUFFERS BufferIn = {0};
  17. DWORD ResponseCode = 0;
  18. BOOL UploadSuccess = FALSE;
  19. DWORD NumRetries = 0;
  20. HANDLE hSourceFile = INVALID_HANDLE_VALUE;
  21. BYTE *pSourceBuffer = NULL;
  22. DWORD dwBytesRead = 0;
  23. DWORD dwBytesWritten = 0;
  24. BOOL bRet = FALSE;
  25. DWORD ResLength = 255;
  26. DWORD index = 0;
  27. static const TCHAR *pszAccept[] = {_T("*.*"), 0};
  28. if (pSourceBuffer = (BYTE *) malloc (10000))
  29. {
  30. if (! pSourceBuffer)
  31. {
  32. return GetLastError();
  33. }
  34. }
  35. // Open the internet session
  36. while ((NumRetries < MaxRetries) && (!UploadSuccess))
  37. {
  38. hSession = InternetOpen(_T("OCARPT Control"),
  39. INTERNET_OPEN_TYPE_PRECONFIG,
  40. NULL,
  41. NULL,
  42. 0);
  43. if (!hSession)
  44. {
  45. free (pSourceBuffer);
  46. ErrorCode = GetLastError();
  47. return ErrorCode;
  48. }
  49. hConnect = InternetConnect(hSession,
  50. Url,
  51. INTERNET_DEFAULT_HTTP_PORT,
  52. NULL,
  53. NULL,
  54. INTERNET_SERVICE_HTTP,
  55. 0,
  56. NULL);
  57. if (hConnect)
  58. {
  59. hRequest = HttpOpenRequest (hConnect,
  60. _T("PUT"),
  61. DestFileName,
  62. NULL,
  63. NULL,
  64. pszAccept,
  65. INTERNET_FLAG_NEED_FILE|INTERNET_FLAG_NO_CACHE_WRITE,
  66. 0);
  67. if (hRequest)
  68. {
  69. hSourceFile = CreateFile( SourceFileName,
  70. GENERIC_READ,
  71. FILE_SHARE_READ,
  72. NULL,
  73. OPEN_EXISTING,
  74. FILE_ATTRIBUTE_NORMAL,
  75. NULL);
  76. if (hSourceFile != INVALID_HANDLE_VALUE)
  77. {
  78. // Clear the buffer
  79. BufferIn.dwStructSize = sizeof( INTERNET_BUFFERSW );
  80. BufferIn.Next = NULL;
  81. BufferIn.lpcszHeader = NULL;
  82. BufferIn.dwHeadersLength = 0;
  83. BufferIn.dwHeadersTotal = 0;
  84. BufferIn.lpvBuffer = NULL;
  85. BufferIn.dwBufferLength = 0;
  86. BufferIn.dwOffsetLow = 0;
  87. BufferIn.dwOffsetHigh = 0;
  88. BufferIn.dwBufferTotal = GetFileSize (hSourceFile, NULL);
  89. ZeroMemory(pSourceBuffer, 10000); // Fill buffer with data
  90. if(HttpSendRequestEx( hRequest, &BufferIn, NULL, HSR_INITIATE, 0))
  91. {
  92. do
  93. {
  94. dwBytesRead = 0;
  95. if(! ReadFile(hSourceFile, pSourceBuffer, 10000, &dwBytesRead, NULL) )
  96. {
  97. ErrorCode = GetLastError();
  98. }
  99. else
  100. {
  101. bRet = InternetWriteFile(hRequest, pSourceBuffer, dwBytesRead, &dwBytesWritten);
  102. if ( (!bRet) || (dwBytesWritten==0) )
  103. {
  104. ErrorCode = GetLastError();
  105. }
  106. }
  107. } while ((dwBytesRead == 10000) && (!ErrorCode) );
  108. if (!ErrorCode)
  109. {
  110. bRet = HttpEndRequest(hRequest, NULL, 0, 0);
  111. if (!bRet)
  112. {
  113. ErrorCode = GetLastError();
  114. }
  115. else
  116. {
  117. ResponseCode = 0;
  118. HttpQueryInfo(hRequest,
  119. HTTP_QUERY_STATUS_CODE |HTTP_QUERY_FLAG_NUMBER ,
  120. &ResponseCode,
  121. &ResLength,
  122. &index);
  123. if ( (ResponseCode != 200) || (ResponseCode != 201))
  124. {
  125. ErrorCode= ResponseCode;
  126. }
  127. else
  128. {
  129. ErrorCode = 0;
  130. UploadSuccess = TRUE;
  131. }
  132. }
  133. }
  134. }
  135. }
  136. }
  137. }
  138. if (!UploadSuccess)
  139. {
  140. ++NumRetries;
  141. }
  142. if (hSourceFile != INVALID_HANDLE_VALUE)
  143. CloseHandle (hSourceFile);
  144. if (hRequest)
  145. InternetCloseHandle(hRequest);
  146. if (hConnect)
  147. InternetCloseHandle(hConnect);
  148. if (hSession)
  149. InternetCloseHandle(hSession);
  150. }
  151. if (pSourceBuffer)
  152. {
  153. free (pSourceBuffer);
  154. pSourceBuffer = NULL;
  155. }
  156. return 0;
  157. }
  158. */