yushang
yushang

Reputation: 133

Why Uri can not handle www escape character?

I'm trying to parse a url which has www escape character in its query part. The code is as follow

int main()
{
  init_apartment();
  Uri uri{L"http://fake.com/path?key1=val1&key2=val2"}; // A
  auto parsed = uri.QueryParsed();
  for(uint32_t idx = 0; idx<parsed.Size(); idx++)
  {
    auto ent = parsed.GetAt(idx);
    printf("%ws : %ws\n",ent.Name().data(),ent.Value().data());
  }
}

at A &#38; is escape character for &, it seems Uri can't handle with this situation. The expected output is

key1 : val1
key2 : val2

but the atcually output is

key1 : val1
 :

What should I do ? Should I pre-process the url myself before passing it to Uri ? Many thanks!

Upvotes: 0

Views: 256

Answers (1)

IInspectable
IInspectable

Reputation: 51489

There are 2 issues with the code:

Addressing the former is simple: Just replace the part that reads &#38; with %26 to produce the following output:

key1 : val1&key2=val2

This is the correct (expected) result. The API will not (must not, even) unescape the URI during parsing. Otherwise it would be impossible to ever have a value containing a reserved character. Do note that Value() does return an unescaped string.

If you need to manually unescape the URI, use the UnescapeComponent static class member:

int main()
{
    init_apartment();

    auto const unescaped{
        Uri::UnescapeComponent(L"http://fake.com/path?key1=val1%26key2=val2")
    };
    Uri uri{ unescaped };

    auto const parsed{ uri.QueryParsed() };
    for (auto const& arg : parsed) {
        wprintf(L"%s : %s\n", arg.Name().c_str(), arg.Value().c_str());
    }
}

This produces the following output:

key1 : val1
key2 : val2

Upvotes: 1

Related Questions