WinStd
Windows Win32 API using Standard C++
Loading...
Searching...
No Matches
WinHTTP.h
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 1991-2024 Amebis
4 Copyright © 2016 GÉANT
5*/
6
8
9#pragma once
10
11#include "Common.h"
12#include <winhttp.h>
13#include <string>
14
17
23inline _Success_(return) BOOL WinHttpQueryHeaders(_In_ HINTERNET hRequest, _In_ DWORD dwInfoLevel, _Out_ DWORD& dwData)
24{
25 DWORD dwSize = sizeof(dwData);
26 if (WinHttpQueryHeaders(hRequest, dwInfoLevel | WINHTTP_QUERY_FLAG_NUMBER, WINHTTP_HEADER_NAME_BY_INDEX, &dwData, &dwSize, WINHTTP_NO_HEADER_INDEX)) {
27 assert(dwSize == sizeof(dwData));
28 return TRUE;
29 }
30 return FALSE;
31}
32
38inline _Success_(return) BOOL WinHttpQueryHeaders(_In_ HINTERNET hRequest, _In_ DWORD dwInfoLevel, _Inout_ std::wstring& sData)
39{
40 DWORD dwSize = 0x100;
41 for (;;) {
42 sData.resize(dwSize - 1);
43 dwSize *= sizeof(WCHAR);
44 if (WinHttpQueryHeaders(hRequest, dwInfoLevel, WINHTTP_HEADER_NAME_BY_INDEX, sData.data(), &dwSize, WINHTTP_NO_HEADER_INDEX)) {
45 dwSize /= sizeof(WCHAR);
46 sData.resize(dwSize);
47 return TRUE;
48 }
49 DWORD result = GetLastError();
50 if (result == ERROR_NOT_ENOUGH_MEMORY) {
51 dwSize /= sizeof(WCHAR);
52 dwSize *= 2;
53 }
54 else
55 return FALSE;
56 }
57}
58
60
61namespace winstd
62{
65
71 class http : public handle<HINTERNET, NULL>
72 {
73 WINSTD_HANDLE_IMPL(http, HINTERNET, NULL)
74
75 public:
81 virtual ~http()
82 {
83 if (m_h != invalid)
85 }
86
87 protected:
93 void free_internal() noexcept override
94 {
95 WinHttpCloseHandle(m_h);
96 }
97 };
98
100}
Base abstract template class to support generic object handle keeping.
Definition Common.h:1024
handle_type m_h
Definition Common.h:1276
HTTP handle wrapper class.
Definition WinHTTP.h:72
virtual ~http()
Closes a handle to the HTTP.
Definition WinHTTP.h:81
void free_internal() noexcept override
Closes a handle to the HTTP.
Definition WinHTTP.h:93
#define WINSTD_HANDLE_IMPL(C, T, INVAL)
Implements default constructors and operators to prevent their auto-generation by compiler.
Definition Common.h:164
static const HINTERNET invalid
Definition Common.h:1034
BOOL WinHttpQueryHeaders(HINTERNET hRequest, DWORD dwInfoLevel, DWORD &dwData)
Retrieves header information associated with an HTTP request.
Definition WinHTTP.h:23