Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address argument encoding nits #2818

Closed
wants to merge 26 commits into from
Closed
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
71a2da7
FI develop into WNP_ContentBuilder_Prev1 (#2656)
loneursid Jun 23, 2022
9be0803
User/erlangl/fi develop into feature (#2660)
loneursid Jun 24, 2022
4355c4e
Merge branch 'develop' of https://github.com/microsoft/WindowsAppSDK …
kythant Jun 28, 2022
6149efb
FI develop into Content Builder feature branch (#2717)
loneursid Jul 12, 2022
d394165
FI develop into notification feature branch (#2733)
loneursid Jul 25, 2022
eca07a6
Merge branch 'develop' of https://github.com/microsoft/WindowsAppSDK …
kythant Jul 26, 2022
ad6a94c
AppNotificationBuilder features (#2786)
pmpurifoy Jul 29, 2022
2fd4a1f
Merge branch 'develop' into feature/WNP_ContentBuilder_Prev1
kythant Jul 29, 2022
a103cab
AppNotificationContent Builder 1.2 spec (#2648)
pmpurifoy Jul 30, 2022
7348ed6
Merge branch 'develop' into feature/WNP_ContentBuilder_Prev1
kythant Aug 1, 2022
6cb166b
AppNotificationBuilder - Code Improvements (#2801)
loneursid Aug 2, 2022
5c91499
AppNotificationContent Builder Core Features - part II (#2760)
loneursid Aug 3, 2022
3d9830d
Merge branch 'develop' into feature/WNP_ContentBuilder_Prev1
kythant Aug 3, 2022
5e4961a
AppNotificationBuilder - ProgressBar (#2780)
loneursid Aug 4, 2022
7410c86
Encoding/Decoding AppNotificationBuilder arguments (#2805)
pmpurifoy Aug 4, 2022
eaabfe9
Update AppNotificationActivatedEventArgs.cpp
pmpurifoy Aug 5, 2022
d683998
Fix c_str usages
pmpurifoy Aug 5, 2022
a15450e
Merge remote-tracking branch 'origin/develop' into user/purifoypaul/E…
pmpurifoy Aug 5, 2022
c9dc720
Merge branch 'develop' into user/purifoypaul/EncodingNits
loneursid Aug 5, 2022
8302357
Add sample
pmpurifoy Aug 5, 2022
cd6b346
Merge branch 'user/purifoypaul/EncodingNits' of https://github.com/mi…
pmpurifoy Aug 5, 2022
b7aae83
Fix encoding nits
pmpurifoy Aug 5, 2022
7fca1ed
Remove comment
pmpurifoy Aug 5, 2022
89099d7
Merge branch 'develop' into user/purifoypaul/EncodingNits
loneursid Aug 5, 2022
0ea16c8
Address nits
pmpurifoy Aug 5, 2022
8744826
Merge branch 'develop' into user/purifoypaul/EncodingNits
loneursid Aug 9, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions dev/AppNotifications/AppNotificationActivatedEventArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,35 @@

namespace winrt::Microsoft::Windows::AppNotifications::implementation
{
// Input without special chars: "key1=value1;key2=value2" -> output: { {key1, value1}, {key2, value2} }
// Input with special chars: "key%3B=value%3D;key%25=value%3D" -> output: { key;, value=}, {key%, value=} }
winrt::Windows::Foundation::Collections::IMap<hstring, hstring> AppNotificationActivatedEventArgs::DecodeArguments(std::wstring arguments)
{
auto result{ winrt::single_threaded_map<winrt::hstring, winrt::hstring>() };

std::vector<std::wstring> pairs{};
size_t pos{ 0 };

wchar_t delimiter{ L';'};
LPWSTR context{};
auto token{ wcstok_s(arguments.data(), &delimiter, &context) };
// Separate the key/value pairs by ';' as the delimiter
while ((pos = arguments.find(L';')) != std::wstring::npos)
while (token != nullptr)
pmpurifoy marked this conversation as resolved.
Show resolved Hide resolved
{
pairs.push_back(arguments.substr(0, pos));
arguments.erase(0, pos + 1);
pairs.push_back(token);
token = wcstok_s(nullptr, &delimiter, &context);
}

// Need to push back final string
pairs.push_back(arguments);

for (auto pair : pairs)
{
// Get the key/value individual values separated by '='
pos = pair.find(L'=');
size_t pos{ pair.find(L'=') };
if (pos == std::wstring::npos)
{
result.Insert(Decode(pair).c_str(), L"");
result.Insert(Decode(pair), L"");
}
else
{
result.Insert(Decode(pair.substr(0, pos)).c_str(), Decode(pair.substr(pos + 1)).c_str());
result.Insert(Decode(pair.substr(0, pos)), Decode(pair.substr(pos + 1)));
}
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace winrt::Microsoft::Windows::AppNotifications::Builder::implementation
{
THROW_HR_IF_MSG(E_INVALIDARG, key.empty(), "You must provide a key when adding an argument");

m_arguments.Insert(EncodeArgument(key.c_str()), EncodeArgument(value.c_str()));
m_arguments.Insert(EncodeArgument(key), EncodeArgument(value));
return *this;
}

Expand Down Expand Up @@ -67,7 +67,7 @@ namespace winrt::Microsoft::Windows::AppNotifications::Builder::implementation
{
THROW_HR_IF_MSG(E_INVALIDARG, m_textLines.size() >= c_maxTextElements, "Maximum number of text elements added");

m_textLines.push_back(wil::str_printf<std::wstring>(L"<text>%ls</text>", EncodeXml(text).c_str()).c_str());
m_textLines.push_back(wil::str_printf<std::wstring>(L"<text>%ls</text>", EncodeXml(text).c_str()));
return *this;
}

Expand All @@ -76,7 +76,7 @@ namespace winrt::Microsoft::Windows::AppNotifications::Builder::implementation
THROW_HR_IF_MSG(E_INVALIDARG, m_textLines.size() >= c_maxTextElements, "Maximum number of text elements added");

std::wstring props{ properties.as<winrt::Windows::Foundation::IStringable>().ToString() };
m_textLines.push_back(wil::str_printf<std::wstring>(L"%ls%ls</text>", props.c_str(), EncodeXml(text).c_str()).c_str());
m_textLines.push_back(wil::str_printf<std::wstring>(L"%ls%ls</text>", props.c_str(), EncodeXml(text).c_str()));

if (properties.IncomingCallAlignment())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ namespace winrt::Microsoft::Windows::AppNotifications::Builder::implementation
AppNotificationDuration m_duration{ AppNotificationDuration::Default };
AppNotificationScenario m_scenario{ AppNotificationScenario::Default };
bool m_useButtonStyle{};
std::vector<winrt::hstring> m_textLines{};
std::vector<std::wstring> m_textLines{};
winrt::hstring m_attributionText{};
winrt::hstring m_inlineImage{};
winrt::hstring m_appLogoOverride{};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,6 @@ namespace AppNotificationBuilder
using namespace winrt::Microsoft::Windows::AppNotifications::Builder;
}

inline std::unordered_map<wchar_t, std::wstring> GetXmlEscapeEncodings()
{
static std::unordered_map<wchar_t, std::wstring> encodings = { { L'&', L"&amp;"}, { L'\"', L"&quot;"}, {L'<', L"&lt;"}, {L'>', L"&gt;"}, {L'\'', L"&apos;"}};
return encodings;
}

inline std::unordered_map<wchar_t, std::wstring> GetPercentEncodings()
{
static std::unordered_map<wchar_t, std::wstring> encodings = {{ L'%', L"%25"}, {L';', L"%3B"}, {L'=', L"%3D"} };
return encodings;
}

inline std::unordered_map<std::wstring, wchar_t> GetPercentEncodingsReverse()
{
static std::unordered_map<std::wstring, wchar_t> encodings = { { L"%25", L'%' }, {L"%3B", L';' }, { L"%3D", L'=' } };
return encodings;
}

inline PCWSTR GetWinSoundEventString(AppNotificationBuilder::AppNotificationSoundEvent soundEvent)
{
switch (soundEvent)
Expand Down Expand Up @@ -96,26 +78,41 @@ inline PCWSTR GetWinSoundEventString(AppNotificationBuilder::AppNotificationSoun
}
}

inline std::wstring EncodeArgument(std::wstring const& value)
inline std::wstring GetArgumentEncoding(wchar_t const& ch)
pmpurifoy marked this conversation as resolved.
Show resolved Hide resolved
{
std::wstring encodedValue{};
static std::unordered_map<wchar_t, std::wstring> encodings = { { L'&', L"&amp;"}, { L'\"', L"&quot;"}, {L'<', L"&lt;"},
{L'>', L"&gt;"}, {L'\'', L"&apos;"}, { L'%', L"%25"},
{L';', L"%3B"}, {L'=', L"%3D"} };
if (encodings.find(ch) != encodings.end())
{
return encodings[ch];
}
else
{
return std::wstring(1, ch);
}
}

auto percentEncodings{ GetPercentEncodings() };
auto xmlEncodings{ GetXmlEscapeEncodings() };
inline std::wstring GetXmlEncoding(wchar_t const& ch)
pmpurifoy marked this conversation as resolved.
Show resolved Hide resolved
{
static std::unordered_map<wchar_t, std::wstring> encodings = { { L'&', L"&amp;"}, { L'\"', L"&quot;"}, {L'<', L"&lt;"},
pmpurifoy marked this conversation as resolved.
Show resolved Hide resolved
{L'>', L"&gt;"}, {L'\'', L"&apos;"} };
if (encodings.find(ch) != encodings.end())
{
return encodings[ch];
}
else
{
return std::wstring(1, ch);
}
}

inline std::wstring EncodeArgument(winrt::hstring const& value)
{
std::wstring encodedValue{};
pmpurifoy marked this conversation as resolved.
Show resolved Hide resolved
for (auto ch : value)
{
if (percentEncodings.find(ch) != percentEncodings.end())
{
encodedValue.append(percentEncodings[ch]);
}
else if (xmlEncodings.find(ch) != xmlEncodings.end())
{
encodedValue.append(xmlEncodings[ch]);
}
else
{
encodedValue.push_back(ch);
}
encodedValue.append(GetArgumentEncoding(ch));
}

return encodedValue;
Expand All @@ -124,18 +121,9 @@ inline std::wstring EncodeArgument(std::wstring const& value)
inline std::wstring EncodeXml(winrt::hstring const& value)
{
std::wstring encodedValue{};

auto xmlEncodings{ GetXmlEscapeEncodings() };
for (auto ch : value)
{
if (xmlEncodings.find(ch) != xmlEncodings.end())
{
encodedValue.append(xmlEncodings[ch]);
}
else
{
encodedValue.push_back(ch);
}
encodedValue.append(GetXmlEncoding(ch));
}

return encodedValue;
Expand All @@ -145,16 +133,16 @@ inline std::wstring EncodeXml(winrt::hstring const& value)
// https://github.com/CommunityToolkit/WindowsCommunityToolkit/blob/rel/7.1.0/Microsoft.Toolkit.Uwp.Notifications/Toasts/ToastArguments.cs#L389inline
inline std::wstring Decode(std::wstring const& value)
pmpurifoy marked this conversation as resolved.
Show resolved Hide resolved
{
std::wstring result{};
auto percentEncodings{ GetPercentEncodingsReverse() };
static std::unordered_map<std::wstring, wchar_t> encodings = { { L"%25", L'%' }, {L"%3B", L';' }, { L"%3D", L'=' } };

// Need to unescape special characters
std::wstring result{};
// Check every 3 chars for a percent encoding
for (size_t index = 0; index < value.size();)
{
std::wstring curr{ value.substr(index, c_maxEncodingSize) };
if (percentEncodings.find(curr) != percentEncodings.end())
if (encodings.find(curr) != encodings.end())
{
result.push_back(percentEncodings[curr]);
result.push_back(encodings[curr]);
index += c_maxEncodingSize;
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace winrt::Microsoft::Windows::AppNotifications::Builder::implementation
THROW_HR_IF_MSG(E_INVALIDARG, key.empty(), "You must provide a key when adding an argument");
THROW_HR_IF_MSG(E_INVALIDARG, m_protocolUri, "You cannot add an argument after calling SetInvokeUri");

m_arguments.Insert(EncodeArgument(key.c_str()), EncodeArgument(value.c_str()));
m_arguments.Insert(EncodeArgument(key), EncodeArgument(value));
return *this;
}

Expand All @@ -38,7 +38,7 @@ namespace winrt::Microsoft::Windows::AppNotifications::Builder::implementation

winrt::Microsoft::Windows::AppNotifications::Builder::AppNotificationButton AppNotificationButton::SetToolTip(winrt::hstring const& value)
{
m_toolTip = EncodeXml(value.c_str()).c_str();
m_toolTip = EncodeXml(value).c_str();
return *this;
}

Expand All @@ -56,7 +56,7 @@ namespace winrt::Microsoft::Windows::AppNotifications::Builder::implementation

winrt::Microsoft::Windows::AppNotifications::Builder::AppNotificationButton AppNotificationButton::SetInputId(winrt::hstring const& value)
{
m_inputId = EncodeXml(value.c_str()).c_str();
m_inputId = EncodeXml(value).c_str();
return *this;
}

Expand Down