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.

267 lines
7.7 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997.
  5. //
  6. // File: N C I N E T . C P P
  7. //
  8. // Contents: Wrappers for the WinInet APIs so they return HRESULTs
  9. //
  10. // Notes:
  11. //
  12. // Author: danielwe 11 Oct 1999
  13. //
  14. //----------------------------------------------------------------------------
  15. #include <pch.h>
  16. #pragma hdrstop
  17. #include "ncinet.h"
  18. #include "ncbase.h"
  19. #include "ncdebug.h"
  20. HINTERNET g_hInetSess = NULL;
  21. BOOL g_fInited = FALSE;
  22. HRESULT HrInternetSetOptionA(
  23. IN HINTERNET hInternet OPTIONAL,
  24. IN DWORD dwOption,
  25. IN LPVOID lpBuffer,
  26. IN DWORD dwBufferLength
  27. )
  28. {
  29. HRESULT hr = S_OK;
  30. if (!InternetSetOptionA(hInternet, dwOption, lpBuffer, dwBufferLength))
  31. {
  32. hr = HrFromLastWin32Error();
  33. }
  34. TraceErrorSkip1("HrInternetSetOptionA", hr,
  35. HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER));
  36. return hr;
  37. }
  38. HRESULT HrInternetQueryOptionA(
  39. IN HINTERNET hInternet OPTIONAL,
  40. IN DWORD dwOption,
  41. OUT LPVOID lpBuffer OPTIONAL,
  42. IN OUT LPDWORD lpdwBufferLength
  43. )
  44. {
  45. HRESULT hr = S_OK;
  46. if (!InternetQueryOptionA(hInternet, dwOption, lpBuffer,lpdwBufferLength))
  47. {
  48. hr = HrFromLastWin32Error();
  49. }
  50. TraceErrorSkip1("HrInternetQueryOption", hr,
  51. HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER));
  52. return hr;
  53. }
  54. INTERNET_STATUS_CALLBACK HinInternetSetStatusCallback(IN HINTERNET hInternet,
  55. IN INTERNET_STATUS_CALLBACK lpfnInternetCallback)
  56. {
  57. HRESULT hr = S_OK;
  58. return InternetSetStatusCallback(hInternet, lpfnInternetCallback);
  59. }
  60. HINTERNET HinInternetOpenA(IN LPCSTR lpszAgent, IN DWORD dwAccessType,
  61. IN LPCSTR lpszProxyName, IN LPCSTR lpszProxyBypass,
  62. IN DWORD dwFlags)
  63. {
  64. HRESULT hr = S_OK;
  65. DWORD dwTimeout = 5000; // 5 second
  66. HINTERNET hInet = InternetOpenA(lpszAgent, dwAccessType, lpszProxyName,
  67. lpszProxyBypass, dwFlags);
  68. if (hInet)
  69. {
  70. hr = HrInternetSetOptionA( hInet,
  71. INTERNET_OPTION_CONNECT_TIMEOUT,
  72. (LPVOID)&dwTimeout,
  73. sizeof(dwTimeout));
  74. if (SUCCEEDED(hr))
  75. {
  76. hr = HrInternetSetOptionA( hInet,
  77. INTERNET_OPTION_RECEIVE_TIMEOUT,
  78. (LPVOID)&dwTimeout,
  79. sizeof(dwTimeout));
  80. }
  81. }
  82. TraceError("HinInternetOpenA", hr);
  83. return hInet;
  84. }
  85. HRESULT HrInternetCloseHandle(HINTERNET hInternet)
  86. {
  87. AssertSz(g_hInetSess, "Must have internet session already!");
  88. if (!InternetCloseHandle(hInternet))
  89. {
  90. HRESULT hr = HrFromLastWin32Error();
  91. TraceError("HrInternetCloseHandle", hr);
  92. return hr;
  93. }
  94. return S_OK;
  95. }
  96. HRESULT HrInternetCrackUrlA(IN LPCSTR lpszUrl, IN DWORD dwUrlLength,
  97. IN DWORD dwFlags,
  98. IN OUT LPURL_COMPONENTSA lpUrlComponents)
  99. {
  100. HRESULT hr = S_OK;
  101. if (!InternetCrackUrlA(lpszUrl, dwUrlLength, dwUrlLength, lpUrlComponents))
  102. {
  103. hr = HrFromLastWin32Error();
  104. }
  105. TraceError("HrInternetCrackUrl", hr);
  106. return hr;
  107. }
  108. HINTERNET HinInternetConnectA(IN HINTERNET hInternet, IN LPCSTR lpszServerName,
  109. IN INTERNET_PORT nServerPort,
  110. IN LPCSTR lpszUserName OPTIONAL,
  111. IN LPCSTR lpszPassword OPTIONAL,
  112. IN DWORD dwService, IN DWORD dwFlags,
  113. IN DWORD_PTR dwContext)
  114. {
  115. HRESULT hr = S_OK;
  116. return InternetConnectA(hInternet, lpszServerName, nServerPort,
  117. lpszUserName, lpszPassword, dwService,
  118. dwFlags, dwContext);
  119. }
  120. HINTERNET HinHttpOpenRequestA(IN HINTERNET hConnect, IN LPCSTR lpszVerb,
  121. IN LPCSTR lpszObjectName, IN LPCSTR lpszVersion,
  122. IN LPCSTR lpszReferrer OPTIONAL,
  123. IN LPCSTR FAR * lplpszAcceptTypes OPTIONAL,
  124. IN DWORD dwFlags, IN DWORD_PTR dwContext)
  125. {
  126. HRESULT hr = S_OK;
  127. return HttpOpenRequestA(hConnect, lpszVerb, lpszObjectName, lpszVersion,
  128. lpszReferrer, lplpszAcceptTypes, dwFlags,
  129. dwContext);
  130. }
  131. HRESULT HrHttpAddRequestHeadersA(IN HINTERNET hRequest, IN LPCSTR lpszHeaders,
  132. IN DWORD dwHeadersLength, IN DWORD dwModifiers)
  133. {
  134. HRESULT hr = S_OK;
  135. if (!HttpAddRequestHeadersA(hRequest, lpszHeaders, dwHeadersLength,
  136. dwModifiers))
  137. {
  138. hr = HrFromLastWin32Error();
  139. }
  140. TraceError("HrHttpAddRequestHeaders", hr);
  141. return hr;
  142. }
  143. HRESULT HrHttpSendRequestExA(IN HINTERNET hRequest,
  144. IN LPINTERNET_BUFFERSA lpBuffersIn OPTIONAL,
  145. OUT LPINTERNET_BUFFERSA lpBuffersOut OPTIONAL,
  146. IN DWORD dwFlags, IN DWORD_PTR dwContext)
  147. {
  148. HRESULT hr = S_OK;
  149. if (!HttpSendRequestExA(hRequest, lpBuffersIn, lpBuffersOut,
  150. dwFlags, dwContext))
  151. {
  152. hr = HrFromLastWin32Error();
  153. }
  154. TraceError("HrHttpSendRequestEx", hr);
  155. return hr;
  156. }
  157. HRESULT HrHttpSendRequestA(IN HINTERNET hRequest, IN LPCSTR lpszHeaders OPTIONAL,
  158. IN DWORD dwHeadersLength, IN LPVOID lpOptional OPTIONAL,
  159. IN DWORD dwOptionalLength)
  160. {
  161. HRESULT hr = S_OK;
  162. if (!HttpSendRequestA(hRequest, lpszHeaders, dwHeadersLength, lpOptional,
  163. dwOptionalLength))
  164. {
  165. hr = HrFromLastWin32Error();
  166. }
  167. TraceError("HrHttpSendRequest", hr);
  168. return hr;
  169. }
  170. HRESULT HrInternetWriteFile(IN HINTERNET hFile, IN LPCVOID lpBuffer,
  171. IN DWORD dwNumberOfBytesToWrite,
  172. OUT LPDWORD lpdwNumberOfBytesWritten)
  173. {
  174. HRESULT hr = S_OK;
  175. if (!InternetWriteFile(hFile, lpBuffer, dwNumberOfBytesToWrite,
  176. lpdwNumberOfBytesWritten))
  177. {
  178. hr = HrFromLastWin32Error();
  179. }
  180. TraceError("HrInternetWriteFile", hr);
  181. return hr;
  182. }
  183. HRESULT HrHttpEndRequest(IN HINTERNET hRequest,
  184. OUT LPINTERNET_BUFFERSA lpBuffersOut OPTIONAL,
  185. IN DWORD dwFlags, IN DWORD_PTR dwContext)
  186. {
  187. HRESULT hr = S_OK;
  188. if (!HttpEndRequestA(hRequest, lpBuffersOut, dwFlags, dwContext))
  189. {
  190. hr = HrFromLastWin32Error();
  191. }
  192. TraceError("HrHttpEndRequest", hr);
  193. return hr;
  194. }
  195. HRESULT HrHttpQueryInfo(IN HINTERNET hRequest, IN DWORD dwInfoLevel,
  196. IN OUT LPVOID lpBuffer OPTIONAL,
  197. IN OUT LPDWORD lpdwBufferLength,
  198. IN OUT LPDWORD lpdwIndex OPTIONAL)
  199. {
  200. HRESULT hr = S_OK;
  201. if (!HttpQueryInfoA(hRequest, dwInfoLevel, lpBuffer,
  202. lpdwBufferLength, lpdwIndex))
  203. {
  204. hr = HrFromLastWin32Error();
  205. }
  206. TraceErrorSkip1("HrHttpQueryInfo", hr,
  207. HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER));
  208. return hr;
  209. }
  210. HRESULT HrGetRequestUriA(LPCSTR szUrl, DWORD cbUri, LPSTR szUri)
  211. {
  212. URL_COMPONENTSA urlComp = {0};
  213. HRESULT hr = S_OK;
  214. // Parse the server name out of the URL
  215. //
  216. urlComp.dwStructSize = sizeof(URL_COMPONENTS);
  217. urlComp.lpszUrlPath = szUri;
  218. urlComp.dwUrlPathLength = cbUri;
  219. hr = HrInternetCrackUrlA(szUrl, 0, 0, &urlComp);
  220. TraceError("HrGetRequestUri", hr);
  221. return hr;
  222. }