cpp-netlib/uri

Building a URI with query is awkward and dangerous

Closed this issue · 1 comments

The uri_builder class has two query methods, one that appends but incorrectly encodes and another that correctly encodes but replaces instead of appending.

First method

auto uri = network::uri_builder()
    .scheme("http")
    .host("example.com")
    .query("q1=foo bar")
    .query("q2=biz baz")
    .uri();
std::cout << uri << '\n';

What I expect: http://example.com?q1=foo%20bar&q2=biz%20baz
What I get: http://example.com?q2=biz%20baz

The actual output is correctly encoded, but the second query call replaced result of the first query call.

Second method

auto uri = network::uri_builder()
    .scheme("http")
    .host("example.com")
    .query("q1", "foo bar")
    .query("q2", "biz baz")
    .uri();
std::cout << uri << '\n';

What I expect: http://example.com?q1=foo%20bar&q2=biz%20baz
What I get: http://example.com?q1=foo bar&q2=biz baz

The actual output is incorrectly encoded, though at least the second query call appended to the result of the first query call.

Commentary

The inconsistency in the API is asking to be misused by Murphy. At the least, the two query methods should behave consistently with regards to each other.

Even better, both query methods should always percent-encode and always append. I struggle to think of a use case where I would want an incorrectly encoded URI, and I struggle to think of a use case where, when using uri_builder, I would want to undo a previous call to query by replacing its result with a subsequent call to query.

Addressed this in #82 by renaming the two overloads.