WinStd/include/WinStd/WinHTTP.h
Simon Rozman d1bfd1abed WinHTTP: Add http_error
WinHTTP errors don't get resolved by FormatMessage by default. We need
to format them using WINHTTP.DLL resources.

Signed-off-by: Simon Rozman <simon@rozman.si>
2025-02-20 14:12:14 +01:00

228 lines
7.2 KiB
C++

/*
SPDX-License-Identifier: MIT
Copyright © 1991-2025 Amebis
Copyright © 2016 GÉANT
*/
/// \defgroup WinStdWinHTTP Windows HTTP Client
#pragma once
#include "Common.h"
#include <winhttp.h>
#include <string>
/// \addtogroup WinStdWinHTTP
/// @{
///
/// Retrieves header information associated with an HTTP request.
///
/// \sa [WinHttpQueryHeaders function](https://learn.microsoft.com/en-us/windows/win32/api/winhttp/nf-winhttp-winhttpqueryheaders)
///
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;
}
///
/// Retrieves header information associated with an HTTP request.
///
/// \sa [WinHttpQueryHeaders function](https://learn.microsoft.com/en-us/windows/win32/api/winhttp/nf-winhttp-winhttpqueryheaders)
///
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
{
/// \addtogroup WinStdWinHTTP
/// @{
///
/// HTTP handle wrapper class
///
/// \sa [WinHttpOpen function](https://learn.microsoft.com/en-us/windows/win32/api/winhttp/nf-winhttp-winhttpopen)
///
class http : public handle<HINTERNET, NULL>
{
WINSTD_HANDLE_IMPL(http, HINTERNET, NULL)
public:
///
/// Closes a handle to the HTTP.
///
/// \sa [WinHttpCloseHandle function](https://learn.microsoft.com/en-us/windows/win32/api/winhttp/nf-winhttp-winhttpclosehandle)
///
virtual ~http()
{
if (m_h != invalid)
free_internal();
}
protected:
///
/// Closes a handle to the HTTP.
///
/// \sa [WinHttpCloseHandle function](https://learn.microsoft.com/en-us/windows/win32/api/winhttp/nf-winhttp-winhttpclosehandle)
///
void free_internal() noexcept override
{
WinHttpCloseHandle(m_h);
}
};
///
/// WINHTTP_PROXY_INFO wrapper class
///
/// \sa [WinHttpGetProxyForUrl function](https://learn.microsoft.com/en-us/windows/win32/api/winhttp/nf-winhttp-winhttpgetproxyforurl)
///
struct http_proxy_info : public WINHTTP_PROXY_INFO
{
http_proxy_info()
{
dwAccessType = 0;
lpszProxy = NULL;
lpszProxyBypass = NULL;
}
http_proxy_info(_Inout_ WINHTTP_PROXY_INFO&& other)
{
dwAccessType = other.dwAccessType;
lpszProxy = other.lpszProxy;
other.lpszProxy = NULL;
lpszProxyBypass = other.lpszProxyBypass;
other.lpszProxyBypass = NULL;
}
http_proxy_info& operator=(_Inout_ WINHTTP_PROXY_INFO&& other)
{
if (this != std::addressof(other)) {
dwAccessType = other.dwAccessType;
if (lpszProxy) GlobalFree(lpszProxy);
lpszProxy = other.lpszProxy;
other.lpszProxy = NULL;
if (lpszProxyBypass) GlobalFree(lpszProxyBypass);
lpszProxyBypass = other.lpszProxyBypass;
other.lpszProxyBypass = NULL;
}
return *this;
}
~http_proxy_info()
{
if (lpszProxy) GlobalFree(lpszProxy);
if (lpszProxyBypass) GlobalFree(lpszProxyBypass);
}
};
/// @}
/// \addtogroup WinStdExceptions
/// @{
///
/// WinHTTP error
///
class http_error : public num_runtime_error<DWORD>
{
public:
///
/// Constructs an exception
///
/// \param[in] num WinHTTP error code
///
http_error(_In_ error_type num) : num_runtime_error<DWORD>(num, message(num))
{}
///
/// Constructs an exception
///
/// \param[in] num WinHTTP error code
/// \param[in] msg Error message
///
http_error(_In_ error_type num, _In_ const std::string& msg) : num_runtime_error<DWORD>(num, msg + ": " + message(num))
{}
///
/// Constructs an exception
///
/// \param[in] num WinHTTP error code
/// \param[in] msg Error message
///
http_error(_In_ error_type num, _In_z_ const char *msg) : num_runtime_error<DWORD>(num, std::string(msg) + ": " + message(num))
{}
///
/// Constructs an exception using `GetLastError()`
///
http_error() : num_runtime_error<DWORD>(GetLastError(), message(GetLastError()))
{}
///
/// Constructs an exception using `GetLastError()`
///
/// \param[in] msg Error message
///
http_error(_In_ const std::string& msg) : num_runtime_error<DWORD>(GetLastError(), msg + ": " + message(GetLastError()))
{}
///
/// Constructs an exception using `GetLastError()`
///
/// \param[in] msg Error message
///
http_error(_In_z_ const char *msg) : num_runtime_error<DWORD>(GetLastError(), std::string(msg) + ": " + message(GetLastError()))
{}
protected:
///
/// Returns a user-readable WinHTTP error message.
/// As std::exception messages may only be char*, we use UTF-8 by convention.
///
/// \sa [FormatMessage function](https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-formatmessage)
///
static std::string message(_In_ error_type num, _In_opt_ DWORD dwLanguageId = 0)
{
last_error_saver last_error_save;
std::wstring wstr;
winstd::library winhttp(LoadLibraryW(L"WINHTTP.DLL"));
if (WINHTTP_ERROR_BASE < num && num <= WINHTTP_ERROR_LAST && winhttp && FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_IGNORE_INSERTS, (HMODULE)winhttp, num, dwLanguageId, wstr, NULL) ||
FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, num, dwLanguageId, wstr, NULL)) {
// Stock Windows error messages contain CRLF. Well... Trim all the trailing white space.
wstr.erase(wstr.find_last_not_of(L" \t\n\r\f\v") + 1);
} else
sprintf(wstr, num >= 0x10000 ? L"Error 0x%X" : L"Error %u", num);
std::string str;
WideCharToMultiByte(CP_UTF8, 0, wstr, str, NULL, NULL);
return str;
}
};
/// @}
}