| 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 | LPWSTR sczValue = NULL; |
| 34 | |
| 35 | hr = InternetQueryInfoString(hiFile, HTTP_QUERY_CONTENT_LENGTH, &sczValue); |
| 36 | InetExitOnLastError(hr, "Failed to get content length string for internet file handle"); |
| 37 | |
| 38 | hr = StrStringToInt64(sczValue, 0, pllSize); |
| 39 | InetExitOnLastError(hr, "Failed to parse size for internet file handle: %ls", sczValue); |
| 40 | |
| 41 | LExit: |
| 42 | return hr; |
| 43 | } |
| 44 | |
| 45 | |
| 46 | /******************************************************************* |
| 47 | InetGetCreateTimeByHandle - returns url creation time |
| 48 | |
| 49 | ******************************************************************/ |
| 50 | extern "C" HRESULT DAPI InternetGetCreateTimeByHandle( |
| 51 | __in HINTERNET hiFile, |
| 52 | __out LPFILETIME pft |
| 53 | ) |
| 54 | { |
| 55 | Assert(pft); |
| 56 | |
| 57 | HRESULT hr = S_OK; |
| 58 | SYSTEMTIME st = {0 }; |
| 59 | DWORD cb = sizeof(SYSTEMTIME); |
| 60 | |
| 61 | if (!::HttpQueryInfoW(hiFile, HTTP_QUERY_LAST_MODIFIED | HTTP_QUERY_FLAG_SYSTEMTIME, reinterpret_cast<LPVOID>(&st), &cb, NULL)) |
| 62 | { |
| 63 | InetExitWithLastError(hr, "failed to get create time for internet file handle"); |
| 64 | } |
| 65 | |
| 66 | if (!::SystemTimeToFileTime(&st, pft)) |
| 67 | { |
| 68 | InetExitWithLastError(hr, "failed to convert system time to file time"); |
| 69 | } |
| 70 | |
| 71 | LExit: |
| 72 | return hr; |
| 73 | } |
| 74 | |
| 75 | |
| 76 | /******************************************************************* |
| 77 | InternetQueryInfoString - query info string |
| 78 | |
| 79 | *******************************************************************/ |
| 80 | extern "C" HRESULT DAPI InternetQueryInfoString( |
| 81 | __in HINTERNET hRequest, |
| 82 | __in DWORD dwInfo, |
| 83 | __deref_out_z LPWSTR* psczValue |
| 84 | ) |
| 85 | { |
| 86 | HRESULT hr = S_OK; |
| 87 | SIZE_T cbOriginal = 0; |
| 88 | DWORD cbValue = 0; |
| 89 | DWORD dwIndex = 0; |
| 90 | |
| 91 | // If nothing was provided start off with some arbitrary size. |
| 92 | if (!*psczValue) |
| 93 | { |
| 94 | hr = StrAlloc(psczValue, 64); |
| 95 | InetExitOnFailure(hr, "Failed to allocate memory for value."); |
| 96 | } |
| 97 | |
| 98 | hr = StrSize(*psczValue, &cbOriginal); |
| 99 | InetExitOnFailure(hr, "Failed to get size of value."); |
| 100 | |
| 101 | cbValue = (DWORD)min(DWORD_MAX, cbOriginal); |
| 102 | |
| 103 | if (!::HttpQueryInfoW(hRequest, dwInfo, static_cast<void*>(*psczValue), &cbValue, &dwIndex)) |
| 104 | { |
| 105 | DWORD er = ::GetLastError(); |
| 106 | if (ERROR_INSUFFICIENT_BUFFER == er) |
| 107 | { |
| 108 | cbValue += sizeof(WCHAR); // add one character for the null terminator. |
| 109 | |
| 110 | hr = StrAlloc(psczValue, cbValue / sizeof(WCHAR)); |
| 111 | InetExitOnFailure(hr, "Failed to allocate value."); |
| 112 | |
| 113 | if (!::HttpQueryInfoW(hRequest, dwInfo, static_cast<void*>(*psczValue), &cbValue, &dwIndex)) |
| 114 | { |
| 115 | er = ::GetLastError(); |
| 116 | } |
| 117 | else |
| 118 | { |
| 119 | er = ERROR_SUCCESS; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | hr = HRESULT_FROM_WIN32(er); |
| 124 | InetExitOnRootFailure(hr, "Failed to get query information."); |
| 125 | } |
| 126 | |
| 127 | LExit: |
| 128 | return hr; |
| 129 | } |
| 130 | |
| 131 | |
| 132 | /******************************************************************* |
| 133 | InternetQueryInfoNumber - query info number |
| 134 | |
| 135 | *******************************************************************/ |
| 136 | extern "C" HRESULT DAPI InternetQueryInfoNumber( |
| 137 | __in HINTERNET hRequest, |
| 138 | __in DWORD dwInfo, |
| 139 | __inout LONG* plInfo |
| 140 | ) |
| 141 | { |
| 142 | HRESULT hr = S_OK; |
| 143 | DWORD cbCode = sizeof(LONG); |
| 144 | DWORD dwIndex = 0; |
| 145 | |
| 146 | if (!::HttpQueryInfoW(hRequest, dwInfo | HTTP_QUERY_FLAG_NUMBER, static_cast<void*>(plInfo), &cbCode, &dwIndex)) |
| 147 | { |
| 148 | InetExitWithLastError(hr, "Failed to get query information."); |
| 149 | } |
| 150 | |
| 151 | LExit: |
| 152 | return hr; |
| 153 | } |