1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_WINHTTP_SCOPED_WINTTP_PROXY_INFO_H_
#define COMPONENTS_WINHTTP_SCOPED_WINTTP_PROXY_INFO_H_
#include <windows.h>
#include <winhttp.h>
#include <string>
#include "base/logging.h"
namespace winhttp {
// Wrapper class for the WINHTTP_PROXY_INFO structure.
// Note that certain Win32 APIs expected the strings to be allocated with
// with GlobalAlloc.
class ScopedWinHttpProxyInfo {
public:
ScopedWinHttpProxyInfo() {}
ScopedWinHttpProxyInfo(const ScopedWinHttpProxyInfo& other) = delete;
ScopedWinHttpProxyInfo& operator=(const ScopedWinHttpProxyInfo& other) =
delete;
ScopedWinHttpProxyInfo(ScopedWinHttpProxyInfo&& other) {
proxy_info_.lpszProxy = other.proxy_info_.lpszProxy;
other.proxy_info_.lpszProxy = nullptr;
proxy_info_.lpszProxyBypass = other.proxy_info_.lpszProxyBypass;
other.proxy_info_.lpszProxyBypass = nullptr;
}
ScopedWinHttpProxyInfo& operator=(ScopedWinHttpProxyInfo&& other) {
proxy_info_.lpszProxy = other.proxy_info_.lpszProxy;
other.proxy_info_.lpszProxy = nullptr;
proxy_info_.lpszProxyBypass = other.proxy_info_.lpszProxyBypass;
other.proxy_info_.lpszProxyBypass = nullptr;
return *this;
}
~ScopedWinHttpProxyInfo() {
if (proxy_info_.lpszProxy)
::GlobalFree(proxy_info_.lpszProxy);
if (proxy_info_.lpszProxyBypass)
::GlobalFree(proxy_info_.lpszProxyBypass);
}
bool IsValid() const { return proxy_info_.lpszProxy; }
void set_access_type(DWORD access_type) {
proxy_info_.dwAccessType = access_type;
}
wchar_t* proxy() const { return proxy_info_.lpszProxy; }
void set_proxy(const std::wstring& proxy) {
if (proxy.empty())
return;
proxy_info_.lpszProxy = GlobalAlloc(proxy);
}
void set_proxy_bypass(const std::wstring& proxy_bypass) {
if (proxy_bypass.empty())
return;
proxy_info_.lpszProxyBypass = GlobalAlloc(proxy_bypass);
}
// Return the raw pointer since WinHttpSetOption requires a non const pointer.
const WINHTTP_PROXY_INFO* get() const { return &proxy_info_; }
WINHTTP_PROXY_INFO* receive() { return &proxy_info_; }
private:
wchar_t* GlobalAlloc(const std::wstring& str) {
const size_t size_in_bytes = (str.length() + 1) * sizeof(wchar_t);
wchar_t* string_mem =
reinterpret_cast<wchar_t*>(::GlobalAlloc(GPTR, size_in_bytes));
if (!string_mem) {
PLOG(ERROR) << "GlobalAlloc failed to allocate " << size_in_bytes
<< " bytes";
return nullptr;
}
memcpy(string_mem, str.data(), size_in_bytes);
return string_mem;
}
WINHTTP_PROXY_INFO proxy_info_ = {};
};
} // namespace winhttp
#endif // COMPONENTS_WINHTTP_SCOPED_WINTTP_PROXY_INFO_H_
|