diff options
Diffstat (limited to 'src/libs/dutil/WixToolset.DUtil/inetutil.cpp')
| -rw-r--r-- | src/libs/dutil/WixToolset.DUtil/inetutil.cpp | 155 |
1 files changed, 155 insertions, 0 deletions
diff --git a/src/libs/dutil/WixToolset.DUtil/inetutil.cpp b/src/libs/dutil/WixToolset.DUtil/inetutil.cpp new file mode 100644 index 00000000..8dace55f --- /dev/null +++ b/src/libs/dutil/WixToolset.DUtil/inetutil.cpp | |||
| @@ -0,0 +1,155 @@ | |||
| 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. | ||
| 2 | |||
| 3 | #include "precomp.h" | ||
| 4 | |||
| 5 | |||
| 6 | // Exit macros | ||
| 7 | #define InetExitOnLastError(x, s, ...) ExitOnLastErrorSource(DUTIL_SOURCE_INETUTIL, x, s, __VA_ARGS__) | ||
| 8 | #define InetExitOnLastErrorDebugTrace(x, s, ...) ExitOnLastErrorDebugTraceSource(DUTIL_SOURCE_INETUTIL, x, s, __VA_ARGS__) | ||
| 9 | #define InetExitWithLastError(x, s, ...) ExitWithLastErrorSource(DUTIL_SOURCE_INETUTIL, x, s, __VA_ARGS__) | ||
| 10 | #define InetExitOnFailure(x, s, ...) ExitOnFailureSource(DUTIL_SOURCE_INETUTIL, x, s, __VA_ARGS__) | ||
| 11 | #define InetExitOnRootFailure(x, s, ...) ExitOnRootFailureSource(DUTIL_SOURCE_INETUTIL, x, s, __VA_ARGS__) | ||
| 12 | #define InetExitOnFailureDebugTrace(x, s, ...) ExitOnFailureDebugTraceSource(DUTIL_SOURCE_INETUTIL, x, s, __VA_ARGS__) | ||
| 13 | #define InetExitOnNull(p, x, e, s, ...) ExitOnNullSource(DUTIL_SOURCE_INETUTIL, p, x, e, s, __VA_ARGS__) | ||
| 14 | #define InetExitOnNullWithLastError(p, x, s, ...) ExitOnNullWithLastErrorSource(DUTIL_SOURCE_INETUTIL, p, x, s, __VA_ARGS__) | ||
| 15 | #define InetExitOnNullDebugTrace(p, x, e, s, ...) ExitOnNullDebugTraceSource(DUTIL_SOURCE_INETUTIL, p, x, e, s, __VA_ARGS__) | ||
| 16 | #define InetExitOnInvalidHandleWithLastError(p, x, s, ...) ExitOnInvalidHandleWithLastErrorSource(DUTIL_SOURCE_INETUTIL, p, x, s, __VA_ARGS__) | ||
| 17 | #define InetExitOnWin32Error(e, x, s, ...) ExitOnWin32ErrorSource(DUTIL_SOURCE_INETUTIL, e, x, s, __VA_ARGS__) | ||
| 18 | #define InetExitOnGdipFailure(g, x, s, ...) ExitOnGdipFailureSource(DUTIL_SOURCE_INETUTIL, g, x, s, __VA_ARGS__) | ||
| 19 | |||
| 20 | |||
| 21 | /******************************************************************* | ||
| 22 | InternetGetSizeByHandle - returns size of file by url handle | ||
| 23 | |||
| 24 | *******************************************************************/ | ||
| 25 | extern "C" HRESULT DAPI InternetGetSizeByHandle( | ||
| 26 | __in HINTERNET hiFile, | ||
| 27 | __out LONGLONG* pllSize | ||
| 28 | ) | ||
| 29 | { | ||
| 30 | Assert(pllSize); | ||
| 31 | |||
| 32 | HRESULT hr = S_OK; | ||
| 33 | DWORD dwSize = 0; | ||
| 34 | DWORD cb = 0; | ||
| 35 | |||
| 36 | cb = sizeof(dwSize); | ||
| 37 | if (!::HttpQueryInfoW(hiFile, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, reinterpret_cast<LPVOID>(&dwSize), &cb, NULL)) | ||
| 38 | { | ||
| 39 | InetExitOnLastError(hr, "Failed to get size for internet file handle"); | ||
| 40 | } | ||
| 41 | |||
| 42 | *pllSize = dwSize; | ||
| 43 | LExit: | ||
| 44 | return hr; | ||
| 45 | } | ||
| 46 | |||
| 47 | |||
| 48 | /******************************************************************* | ||
| 49 | InetGetCreateTimeByHandle - returns url creation time | ||
| 50 | |||
| 51 | ******************************************************************/ | ||
| 52 | extern "C" HRESULT DAPI InternetGetCreateTimeByHandle( | ||
| 53 | __in HINTERNET hiFile, | ||
| 54 | __out LPFILETIME pft | ||
| 55 | ) | ||
| 56 | { | ||
| 57 | Assert(pft); | ||
| 58 | |||
| 59 | HRESULT hr = S_OK; | ||
| 60 | SYSTEMTIME st = {0 }; | ||
| 61 | DWORD cb = sizeof(SYSTEMTIME); | ||
| 62 | |||
| 63 | if (!::HttpQueryInfoW(hiFile, HTTP_QUERY_LAST_MODIFIED | HTTP_QUERY_FLAG_SYSTEMTIME, reinterpret_cast<LPVOID>(&st), &cb, NULL)) | ||
| 64 | { | ||
| 65 | InetExitWithLastError(hr, "failed to get create time for internet file handle"); | ||
| 66 | } | ||
| 67 | |||
| 68 | if (!::SystemTimeToFileTime(&st, pft)) | ||
| 69 | { | ||
| 70 | InetExitWithLastError(hr, "failed to convert system time to file time"); | ||
| 71 | } | ||
| 72 | |||
| 73 | LExit: | ||
| 74 | return hr; | ||
| 75 | } | ||
| 76 | |||
| 77 | |||
| 78 | /******************************************************************* | ||
| 79 | InternetQueryInfoString - query info string | ||
| 80 | |||
| 81 | *******************************************************************/ | ||
| 82 | extern "C" HRESULT DAPI InternetQueryInfoString( | ||
| 83 | __in HINTERNET hRequest, | ||
| 84 | __in DWORD dwInfo, | ||
| 85 | __deref_out_z LPWSTR* psczValue | ||
| 86 | ) | ||
| 87 | { | ||
| 88 | HRESULT hr = S_OK; | ||
| 89 | SIZE_T cbOriginal = 0; | ||
| 90 | DWORD cbValue = 0; | ||
| 91 | DWORD dwIndex = 0; | ||
| 92 | |||
| 93 | // If nothing was provided start off with some arbitrary size. | ||
| 94 | if (!*psczValue) | ||
| 95 | { | ||
| 96 | hr = StrAlloc(psczValue, 64); | ||
| 97 | InetExitOnFailure(hr, "Failed to allocate memory for value."); | ||
| 98 | } | ||
| 99 | |||
| 100 | hr = StrSize(*psczValue, &cbOriginal); | ||
| 101 | InetExitOnFailure(hr, "Failed to get size of value."); | ||
| 102 | |||
| 103 | cbValue = (DWORD)min(DWORD_MAX, cbOriginal); | ||
| 104 | |||
| 105 | if (!::HttpQueryInfoW(hRequest, dwInfo, static_cast<void*>(*psczValue), &cbValue, &dwIndex)) | ||
| 106 | { | ||
| 107 | DWORD er = ::GetLastError(); | ||
| 108 | if (ERROR_INSUFFICIENT_BUFFER == er) | ||
| 109 | { | ||
| 110 | cbValue += sizeof(WCHAR); // add one character for the null terminator. | ||
| 111 | |||
| 112 | hr = StrAlloc(psczValue, cbValue / sizeof(WCHAR)); | ||
| 113 | InetExitOnFailure(hr, "Failed to allocate value."); | ||
| 114 | |||
| 115 | if (!::HttpQueryInfoW(hRequest, dwInfo, static_cast<void*>(*psczValue), &cbValue, &dwIndex)) | ||
| 116 | { | ||
| 117 | er = ::GetLastError(); | ||
| 118 | } | ||
| 119 | else | ||
| 120 | { | ||
| 121 | er = ERROR_SUCCESS; | ||
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 | hr = HRESULT_FROM_WIN32(er); | ||
| 126 | InetExitOnRootFailure(hr, "Failed to get query information."); | ||
| 127 | } | ||
| 128 | |||
| 129 | LExit: | ||
| 130 | return hr; | ||
| 131 | } | ||
| 132 | |||
| 133 | |||
| 134 | /******************************************************************* | ||
| 135 | InternetQueryInfoNumber - query info number | ||
| 136 | |||
| 137 | *******************************************************************/ | ||
| 138 | extern "C" HRESULT DAPI InternetQueryInfoNumber( | ||
| 139 | __in HINTERNET hRequest, | ||
| 140 | __in DWORD dwInfo, | ||
| 141 | __inout LONG* plInfo | ||
| 142 | ) | ||
| 143 | { | ||
| 144 | HRESULT hr = S_OK; | ||
| 145 | DWORD cbCode = sizeof(LONG); | ||
| 146 | DWORD dwIndex = 0; | ||
| 147 | |||
| 148 | if (!::HttpQueryInfoW(hRequest, dwInfo | HTTP_QUERY_FLAG_NUMBER, static_cast<void*>(plInfo), &cbCode, &dwIndex)) | ||
| 149 | { | ||
| 150 | InetExitWithLastError(hr, "Failed to get query information."); | ||
| 151 | } | ||
| 152 | |||
| 153 | LExit: | ||
| 154 | return hr; | ||
| 155 | } | ||
