aboutsummaryrefslogtreecommitdiff
path: root/src/dutil/inetutil.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/dutil/inetutil.cpp')
-rw-r--r--src/dutil/inetutil.cpp137
1 files changed, 137 insertions, 0 deletions
diff --git a/src/dutil/inetutil.cpp b/src/dutil/inetutil.cpp
new file mode 100644
index 00000000..69a0176a
--- /dev/null
+++ b/src/dutil/inetutil.cpp
@@ -0,0 +1,137 @@
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/*******************************************************************
7 InternetGetSizeByHandle - returns size of file by url handle
8
9*******************************************************************/
10extern "C" HRESULT DAPI InternetGetSizeByHandle(
11 __in HINTERNET hiFile,
12 __out LONGLONG* pllSize
13 )
14{
15 Assert(pllSize);
16
17 HRESULT hr = S_OK;
18 DWORD dwSize;
19 DWORD cb;
20
21 cb = sizeof(dwSize);
22 if (!::HttpQueryInfoW(hiFile, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, reinterpret_cast<LPVOID>(&dwSize), &cb, NULL))
23 {
24 ExitOnLastError(hr, "Failed to get size for internet file handle");
25 }
26
27 *pllSize = dwSize;
28LExit:
29 return hr;
30}
31
32
33/*******************************************************************
34 InetGetCreateTimeByHandle - returns url creation time
35
36******************************************************************/
37extern "C" HRESULT DAPI InternetGetCreateTimeByHandle(
38 __in HINTERNET hiFile,
39 __out LPFILETIME pft
40 )
41{
42 Assert(pft);
43
44 HRESULT hr = S_OK;
45 SYSTEMTIME st = {0 };
46 DWORD cb = sizeof(SYSTEMTIME);
47
48 if (!::HttpQueryInfoW(hiFile, HTTP_QUERY_LAST_MODIFIED | HTTP_QUERY_FLAG_SYSTEMTIME, reinterpret_cast<LPVOID>(&st), &cb, NULL))
49 {
50 ExitWithLastError(hr, "failed to get create time for internet file handle");
51 }
52
53 if (!::SystemTimeToFileTime(&st, pft))
54 {
55 ExitWithLastError(hr, "failed to convert system time to file time");
56 }
57
58LExit:
59 return hr;
60}
61
62
63/*******************************************************************
64 InternetQueryInfoString - query info string
65
66*******************************************************************/
67extern "C" HRESULT DAPI InternetQueryInfoString(
68 __in HINTERNET hRequest,
69 __in DWORD dwInfo,
70 __deref_out_z LPWSTR* psczValue
71 )
72{
73 HRESULT hr = S_OK;
74 DWORD_PTR cbValue = 0;
75 DWORD dwIndex = 0;
76
77 // If nothing was provided start off with some arbitrary size.
78 if (!*psczValue)
79 {
80 hr = StrAlloc(psczValue, 64);
81 ExitOnFailure(hr, "Failed to allocate memory for value.");
82 }
83
84 hr = StrSize(*psczValue, &cbValue);
85 ExitOnFailure(hr, "Failed to get size of value.");
86
87 if (!::HttpQueryInfoW(hRequest, dwInfo, static_cast<void*>(*psczValue), reinterpret_cast<DWORD*>(&cbValue), &dwIndex))
88 {
89 DWORD er = ::GetLastError();
90 if (ERROR_INSUFFICIENT_BUFFER == er)
91 {
92 cbValue += sizeof(WCHAR); // add one character for the null terminator.
93
94 hr = StrAlloc(psczValue, cbValue / sizeof(WCHAR));
95 ExitOnFailure(hr, "Failed to allocate value.");
96
97 if (!::HttpQueryInfoW(hRequest, dwInfo, static_cast<void*>(*psczValue), reinterpret_cast<DWORD*>(&cbValue), &dwIndex))
98 {
99 er = ::GetLastError();
100 }
101 else
102 {
103 er = ERROR_SUCCESS;
104 }
105 }
106
107 hr = HRESULT_FROM_WIN32(er);
108 ExitOnRootFailure(hr, "Failed to get query information.");
109 }
110
111LExit:
112 return hr;
113}
114
115
116/*******************************************************************
117 InternetQueryInfoNumber - query info number
118
119*******************************************************************/
120extern "C" HRESULT DAPI InternetQueryInfoNumber(
121 __in HINTERNET hRequest,
122 __in DWORD dwInfo,
123 __out LONG* plInfo
124 )
125{
126 HRESULT hr = S_OK;
127 DWORD cbCode = sizeof(LONG);
128 DWORD dwIndex = 0;
129
130 if (!::HttpQueryInfoW(hRequest, dwInfo | HTTP_QUERY_FLAG_NUMBER, static_cast<void*>(plInfo), &cbCode, &dwIndex))
131 {
132 ExitWithLastError(hr, "Failed to get query information.");
133 }
134
135LExit:
136 return hr;
137}