You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
EDIT: for anyone seeking a quick fix: boost::urls::encode("aA1 +?", boost::urls::unreserved_chars + "whatever your unreserved chars")
With the default option encoding_opts::space_as_plus = true, url::params() decodes plus sign into space, but never encodes it into %2B, leading to surprising (*url.params().find(key)).value != value.
This looks correct to me. params() uses default encoding opts, append assumes it's receiving input to be encoded, and then it encodes + as whitespace, as defined in the default encoding opts.
Although I agree, it's surprising that an element of params looks different when appending and retrieving. I'm not sure it's possible to make them always look the same because these URL transformations are not one-to-one or reversible.
The problem is, params() decodes + into whitespace, which is desirable, but somehow leaves + unencoded. If encoded + means decoded whitespace, shouldn't decoded + be mapped to something else?
I've been experimenting with this for a while now. The root of the problem is that this library is based on RFC 3986, but also supports this extra encoding option that's not only not part of the RFC (spaces as plus is an HTTP thing) but ambiguous with its rules. Although the problem is easy to explain ("what represents a literal plus when plus means space?"), it is quite complex to solve for two reasons:
1) The normalization problem
The intuition for an initial solution is simple: if + and represent whitespaces (it's even quite common for applications to directly encode "+" as "%20" when it's in the query), then something has to represent a literal + and the obvious choice is %2B. However, when we normalize a URL, its meaning shouldn't change, and normalizing "%2B" in a query gives us "+" because "+" is an unreserved query char and all unreserved chars should be decoded during normalization. But now "%2B" became "+", changing its meaning from "+" to " ".
For this problem, the obvious intuition would be to just implement an extension to RFC 3986 where normalization would take this new rule into account and not decode "%2B" into "+" when we're encoding space as plus. However, this creates a design problem, because URLs or URL views in the library don't carry information about which encoding options are used by each component. When calling a function such as params(), the library assumes encoding options are just options regarding how the query parameters should be interpreted locally. The library doesn't assume these encoding options are a property of the URL.
One solution would be to also extent other functions, such as url_base::normalize() to also accept encoding options and then the user would be responsible for ensuring the same encoding options are being used whenever they call a functions that accepts encoding options. This is unsafe and semantically wrong.
A second solution would be to give URLs this property so each URL knows which encoding options it assumes. This opens up a new set of design problems. How would every other function that parses and creates URL need to change? How are URLs with different encoding options compared? Should URLs be enforcing rules that are specific to HTTP URLs? Which encoding options should be the default (the default for params() is space_as_plus as true, which is specific to HTTP)? What happens to existing functions that currently accept encoding options? What happens to existing functions that currently do to accept encoding options? This would be breaking change almost for sure.
A third solution would be a breaking change where params() would have space_as_plus = false by default. That means URLs don't use HTTP specific rules that cause ambiguity by default. The normalization problem still remains if the user explicitly decides to interpret the URL in a way that goes beyond the RFC 3986 rules.
A fourth option is to only change this behavior for params_ref when you call append because now we're talking about query parameters. So appending "500+500" would append an encoded "500%2B500" and this would be decoded as "500+500" instead of "500 500". In this case, the user would be responsible for not normalizing the query string because normalization would use RFC 3986 rules that would make this ambiguous. This is not so simple to implement because append and many other functions currently use a private URL function edit_params that interacts with other functionality but the behavior is easy to describe. The problem here is we're leaving many problems unsolved and might even be making behavior more complex.
2) The standardization problem
Although this might be a smaller problem, whatever behavior we implement needs to be formalized because we don't want to follow our intuition to later find out some other standard actually describes how this should work. As I understand it, this "space-as-plus rule" is mentioned sparsely in the WHATWG standard, which we don't track with this library because it's a "live" standard.
Activity
alandefreitas commentedon Apr 11, 2025
This looks correct to me.
params()uses default encoding opts,appendassumes it's receiving input to be encoded, and then it encodes+as whitespace, as defined in the default encoding opts.Aren't you expecting
params/params_ref/params_viewto behave likeencoded_params/params_encoded_ref/params_encoded_view? You probably wantencoded_params()so the behavior is not surprising/encoded.Although I agree, it's surprising that an element of
paramslooks different when appending and retrieving. I'm not sure it's possible to make them always look the same because these URL transformations are not one-to-one or reversible.yangfl commentedon Apr 12, 2025
It does not encode
+into whitespace; actually it encodes into%20.url.params().append({"k", "a+b c"}); std::cout << url << std::endl; // https://a/a?k=a+b%20cThe problem is,
params()decodes+into whitespace, which is desirable, but somehow leaves+unencoded. If encoded+means decoded whitespace, shouldn't decoded+be mapped to something else?alandefreitas commentedon Apr 17, 2025
Yes. That makes sense. I'll have a look at this.
alandefreitas commentedon May 8, 2025
I've been experimenting with this for a while now. The root of the problem is that this library is based on RFC 3986, but also supports this extra encoding option that's not only not part of the RFC (spaces as plus is an HTTP thing) but ambiguous with its rules. Although the problem is easy to explain ("what represents a literal plus when plus means space?"), it is quite complex to solve for two reasons:
1) The normalization problem
The intuition for an initial solution is simple: if
+andrepresent whitespaces (it's even quite common for applications to directly encode "+" as "%20" when it's in the query), then something has to represent a literal+and the obvious choice is%2B. However, when we normalize a URL, its meaning shouldn't change, and normalizing "%2B" in a query gives us "+" because "+" is an unreserved query char and all unreserved chars should be decoded during normalization. But now "%2B" became "+", changing its meaning from "+" to " ".For this problem, the obvious intuition would be to just implement an extension to RFC 3986 where normalization would take this new rule into account and not decode "%2B" into "+" when we're encoding space as plus. However, this creates a design problem, because URLs or URL views in the library don't carry information about which encoding options are used by each component. When calling a function such as
params(), the library assumes encoding options are just options regarding how the query parameters should be interpreted locally. The library doesn't assume these encoding options are a property of the URL.One solution would be to also extent other functions, such as
url_base::normalize()to also accept encoding options and then the user would be responsible for ensuring the same encoding options are being used whenever they call a functions that accepts encoding options. This is unsafe and semantically wrong.A second solution would be to give URLs this property so each URL knows which encoding options it assumes. This opens up a new set of design problems. How would every other function that parses and creates URL need to change? How are URLs with different encoding options compared? Should URLs be enforcing rules that are specific to HTTP URLs? Which encoding options should be the default (the default for
params()isspace_as_plusastrue, which is specific to HTTP)? What happens to existing functions that currently accept encoding options? What happens to existing functions that currently do to accept encoding options? This would be breaking change almost for sure.A third solution would be a breaking change where
params()would havespace_as_plus = falseby default. That means URLs don't use HTTP specific rules that cause ambiguity by default. The normalization problem still remains if the user explicitly decides to interpret the URL in a way that goes beyond the RFC 3986 rules.A fourth option is to only change this behavior for
params_refwhen you callappendbecause now we're talking about query parameters. So appending "500+500" would append an encoded "500%2B500" and this would be decoded as "500+500" instead of "500 500". In this case, the user would be responsible for not normalizing the query string because normalization would use RFC 3986 rules that would make this ambiguous. This is not so simple to implement becauseappendand many other functions currently use a private URL functionedit_paramsthat interacts with other functionality but the behavior is easy to describe. The problem here is we're leaving many problems unsolved and might even be making behavior more complex.2) The standardization problem
Although this might be a smaller problem, whatever behavior we implement needs to be formalized because we don't want to follow our intuition to later find out some other standard actually describes how this should work. As I understand it, this "space-as-plus rule" is mentioned sparsely in the WHATWG standard, which we don't track with this library because it's a "live" standard.
alandefreitas commentedon May 9, 2025
Here's the spec: https://datatracker.ietf.org/doc/html/rfc1866#section-8.2.1 / https://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.1
Unfortunately still ambiguous:
but "URL" says we must not encode “+”, so “+” could represent both ” ” and “+”.
My partial conclusion is
encoding_optsin Boost.URL shouldn’t exist at all. Now that it exists, we need a breaking change:encoding_optsshould always be the one that follows the RFC 1866 rules (no space-as-plus by default).paramswould decode+aswhen space-as-plus was explicitly asked forparamswould encode+as%2Bwhen space-as-plus was explicitly asked for+as%2Bwould make it ambiguous.yangfl commentedon May 9, 2025
Agree. But maybe space_as_plus
should be clarified as "spaces encode from plus signs only" for now.
fix: consistent behavior for space-as-plus option
fix: consistent behavior for space-as-plus option
fix: consistent behavior for space-as-plus option
fix: consistent behavior for space-as-plus option
fix: consistent behavior for space-as-plus option
fix: consistent behavior for space-as-plus option