# WinStd: include/WinStd/WinHTTP.h Source File

Loading...
Searching...
No Matches
WinHTTP.h
/*
SPDX-License-Identifier: MIT
Copyright © 1991-2024 Amebis
Copyright © 2016 GÉANT
*/
#pragma once
#include "Common.h"
#include <winhttp.h>
#include <string>
inline _Success_(return) BOOL WinHttpQueryHeaders(_In_ HINTERNET hRequest, _In_ DWORD dwInfoLevel, _Out_ DWORD& dwData)
{
DWORD dwSize = sizeof(dwData);
if (WinHttpQueryHeaders(hRequest, dwInfoLevel | WINHTTP_QUERY_FLAG_NUMBER, WINHTTP_HEADER_NAME_BY_INDEX, &dwData, &dwSize, WINHTTP_NO_HEADER_INDEX)) {
assert(dwSize == sizeof(dwData));
return TRUE;
}
return FALSE;
}
inline _Success_(return) BOOL WinHttpQueryHeaders(_In_ HINTERNET hRequest, _In_ DWORD dwInfoLevel, _Inout_ std::wstring& sData)
{
DWORD dwSize = 0x100;
for (;;) {
sData.resize(dwSize - 1);
dwSize *= sizeof(WCHAR);
if (WinHttpQueryHeaders(hRequest, dwInfoLevel, WINHTTP_HEADER_NAME_BY_INDEX, sData.data(), &dwSize, WINHTTP_NO_HEADER_INDEX)) {
dwSize /= sizeof(WCHAR);
sData.resize(dwSize);
return TRUE;
}
DWORD result = GetLastError();
if (result == ERROR_NOT_ENOUGH_MEMORY) {
dwSize /= sizeof(WCHAR);
dwSize *= 2;
}
else
return FALSE;
}
}
namespace winstd
{
class http : public handle<HINTERNET, NULL>
{
WINSTD_HANDLE_IMPL(http, HINTERNET, NULL)
public:
virtual ~http()
{
if (m_h != invalid)
free_internal();
}
protected:
void free_internal() noexcept override
{
WinHttpCloseHandle(m_h);
}
};
}
winstd::handle
Base abstract template class to support generic object handle keeping.
Definition Common.h:874
winstd::handle< HINTERNET, NULL >::m_h
handle_type m_h
Definition Common.h:1126
winstd::http
HTTP handle wrapper class.
Definition WinHTTP.h:72
winstd::http::~http
virtual ~http()
Closes a handle to the HTTP.
Definition WinHTTP.h:81
winstd::http::free_internal
void free_internal() noexcept override
Closes a handle to the HTTP.
Definition WinHTTP.h:93
WINSTD_HANDLE_IMPL
#define WINSTD_HANDLE_IMPL(C, T, INVAL)
Implements default constructors and operators to prevent their auto-generation by compiler.
Definition Common.h:164
winstd::handle< HINTERNET, NULL >::invalid
static const HINTERNET invalid
Definition Common.h:884
WinHttpQueryHeaders
BOOL WinHttpQueryHeaders(HINTERNET hRequest, DWORD dwInfoLevel, DWORD &dwData)
Retrieves header information associated with an HTTP request.
Definition WinHTTP.h:23