Hi
On 10/16/26 14:53, Máté Kocsis wrote:
1. I don't think `QueryParamParsingOptions::$maxQueryStringLength` is
necessary or useful to have. Checking the input length is easy to do
manually (it's just a `strlen()` call) and I don't see a practical way
to abuse the API with large input lengths alone: The API will not
“blow
up” inputs into considerably larger data structures, the input strings
are mostly just “copied” over, not too different from e.g.
`str_replace()`.
Answered previously.
3. If `$maxQueryStringLength` goes away, the
`QueryParamParsingOptions`
would be a single property object. Maybe the param count limit should
become a regular parameter then, since I can't think of any other
useful
parse-time options.
Yes, without trying to future proof the API too much, I guess this is
the
only sensible
choice, unless someone finds any other option which would be needed.
I'm seeing that the RFC is now updated accordingly. Thank you.
4. For the `QueryParamBuildingOptions` I don't think they should be
passed as parameter to the parsing functions (and thus not be stored
as
an object property). Instead they should be passed to the `->to*()`
functions just when they are needed. That keeps the QueryParams class’
state focused on the actual parameters.
[…]
[…]
On the other hand, the query param representation of boolean or null
values
may vary
case by case, and as I've just realized it's not as simple as I
previously
thought:
Answered previously and I see that the RFC is now adjusted.
5. Is having a separate `parseRfc3986()` useful when RFC 3986 doesn't
specify the handling of query parameters and just delegates to RFC
1866
instead? A big benefit of the URI extension for me is that it clearly
references the relevant standards in its naming and strictly
implements
them, making it easy to discover and allowing the use of primary
references to understand what they are supposed to do. The
`parseRfc3986()` method would muddy this straight-forward standard’s
compliant implementation, particularly since it is defined to accept
malformed inputs such as `#foo=bar`, which I would find unexpected
from
a method referencing RFC 3986 in its name.
As far as I can see, supporting RFC 3986 parsing is necessary to keep
full
compatibility with the URI class so that the meaning of literal +
characters in
the query string wouldn't change during query parameter parsing.
Let's consider this code:
$uri = new Uri\Rfc3986\Uri("?q=C++");
$rfc1866Params = Uri\QueryParams::parseRfc1866($uri->getQuery());
$rfc3986Params = Uri\QueryParams::parseRfc3986($uri->getQuery());
var_dump($rfc1866Params->getFirst("q")); //
string(3)
"C"
var_dump($rfc3986Params->getFirst("q")); //
string(3)
"C++"
Unfortunately, WHATWG URL's URL and URLSearchParams classes have
different
query parameter parsing algorithms (see the "Percent-Encoding and
Decoding"
section),
so the same issue is already present there, and unavoidable. I'd like
not
to repeat it in
case of RFC 3986, that's why I think parseRfc3986() is needed. But let
me
know if
I miss something.
Given that - as far as I can tell - RFC 3986 does not specify any syntax
for parsing the query-string into smaller components, I don't think we
should have a `parseRfc3986` method. Looking at RFC 3986 I'm also not
seeing a reference to RFC 1866 (contrary to my previous claim that RFC
3986 delegates to 1866).
My suggestion would be adding a `bool $plusAsSpace = true` parameter on
`parseRfc1866()` and a corresponding `bool $spaceAsPlus = true` on
`toRfc1866String()` - and dropping the 3986 methods.
The `getQueryParams()` (and `withQueryParams()`) on `Uri\Rfc3986\Uri`
should then be removed as well to force the user to make an explicit
choice during decoding, i.e. opting into RFC 1866. Assuming that a `+`
is a `+` instead of a space does not hold given that
`application/x-www-form-urlencoded` (a.k.a. RFC 1866) is the standard
format for HTML forms (both for GET as well as POST submissions). Users
that naively use `$uri->getQueryParams()` would then get incorrect
results.
Regarding accepting malformed input: uriparser's own query parameter
handling
functions (uriDissectQueryMalloc*()) behave the same way as the RFC
currently
proposes.
Unfortunately, I could not really find any reasoning behind this choice
of
uriparser.
I don't think we should strictly tie us to choices made by the
underlying libraries we use and instead choose whatever we believe is
the best behavior for PHP users with our current knowledge and the state
of the industry. Given that “lax parsers” are a recurring cause of
security issues, I feel that being strict is the right choice.
To be clear, I don't insist on following uriparser's behavior for RFC
1866
and RFC 3986 (it's
not an option for WHATWG URL, since it specifies what needs to be done
in
every detail),
and probably strict parsing is what is more consistent with the Uri
class
indeed.
Yes, my suggestion would be to be strict, as per the above.
Then passing an invalid query string syntax would throw. However,
incorrect
UTF-8 sequences
would still be allowed, since neither RFC 1866, nor RFC 3986 require a
syntactically
correct query string to be valid UTF-8.
Yes, this is also in line with PHP strings being just bytestrings.
7. I cannot describe an actual issue, but I find accepting invalid
percent-encoding sequences to be dangerous. It would definitely be
unexpected that `%6f=x' would be `o=x`, but `%6g=y` would be `%6g=y`
(which would then become `%256g=y`).
Answered above. :)
My answer also above.
8. The naming of `hasValue()` sounds like it would check if there is a
`=` sign for the given key somewhere, not that it would check that
there
is a specific key-value pair. I'm also not sure if it is necessary.
Duplicated keys are comparatively rare and for the use cases where
this
is a thing, `in_array()` on the result of `getAll()` would just work
(or
`array_any()` for more complex checks).
This method is required by WHATWG URL:
https://url.spec.whatwg.org/#dom-urlsearchparams-has
It's specified as an overloaded method, so I had to introduce a
separate
signature for this use-case, as per the policy accepted in
https://wiki.php.net/rfc/deprecate_functions_with_overloaded_signatures#policy_about_new_functions#policy_about_new_functions
.
I see. However given that the QueryParams class is agnostic to either
standard, I don't think it is necessary or useful to import the behavior
wholesale when we don't consider it useful or even potentially
confusing. The relevant standards only come into play during parsing and
serialization. The remaining API should work for PHP. For the
Uri\WhatWg\Url class we are not strictly following the WHATWG API either
to keep it aligned with Uri\Rfc3986\Uri and PHP user’s expectations.
TBH, I find it useful (more convenient and more efficient than getAll()
+
in_array()).
The name itself may be slightly confusing at the first sight, but I
think
the
$value parameter which must be passed should clarify its purpose. But
in
any case,
I'm open to improving its name.
I'm not sure how `hasValue()` would be meaningfully more efficient than
`getAll()` + `in_array()`. Both should be a O(n) scan over the
parameters, no?
In practice for situations where a specific value is searched, it also
seems likely that one wants to match for several different alternatives.
In that case `array_intersect()` or `array_diff()` would allow to do the
entire matching in a single operation which would be more efficient than
repeated calls to `hasValue()`. Just “I want to know if the parameter
exists with a specific value and ignore all other values” does not seem
like a common use case to me.
9. I'm not sure I like the name `->list()`. Technically it's also
redundant with `iterator_to_array($queryParams)`, but having a
dedicated
method probably makes sense nevertheless.
Yes, I'm fine with renaming this method to toList(). It would go well
with
toArray()
IMO. It's more ergonomic and more efficient than the alternative, so I
agree
that it's useful.
Already answered and already changed in the RFC.
10. Would it make sense to rename `parseWhatWg()` to
`parseUrlSearchParams()` or similar to make it clear that this
constructor implements the URLSearchParams specification. I'm asking
due
to the section that mentioned how `$whatWgUrl->getQuery()` is not the
correct value to pass there (due to the questionmark handling).
Renaming
this method would also make sense when removing the RFC 3986 parser
(as
suggested in (5)).
I think it's a good idea. Maybe people using it would be less likely to
be
surprised by
its behavior (how it handles the leading ? character) if the name
referred
to
URLSearchParams. However, I think including WHATWG in the method name
would still have its purpose (people with less familiarity with WHATWG
URL
would know immediately that this method is related to the WHATWG URL
specification),
so my preferred name would be parseWhatWgUrlSearchParams(), even
if it's somewhat verbose.
That works for me (and I see it's already adjusted in the RFC).
11. Bullet point 8 also applies to `deleteValue()`. Is there an actual
use case for this method?
Yes, it has the same reason:
https://url.spec.whatwg.org/#dom-urlsearchparams-delete
Also, this function may come handy in case of filtering. For example,
let's
consider the
"tag=php&tag=web&sort=date" query string: deleteValue("tag", "php")
would
remove one
selected filter while keeping the other parameters intact.
See above for my opinion on `hasValue()`. The “filtering a specific
value” use case seems legitimate though. I wonder if we should make
`set()` a variadic function `set($name, ...$value)` (omitting types for
readability) and then calling:
->set('tag', 'php', 'web')
would result in `tag=php&tag=web`.
Then one could just do:
$q->set(
'tag',
...array_filter(
$q->getAll('tag'),
fn($tag) => $tag !== 'php',
),
);
to achieve this filtering. This would even make it easy to use a
regex-based filter or similar.
12. Is having the `withArray()` method useful? It does the same as
`QueryParams::fromArray()`, no? It feels confusing to have a “replace
everything” method when one could just create something entirely new.
Technically one could even call `$queryParams->fromArray()`, since
calling static methods on instances is legal.
Currently, the only description about this method is the following:
It replaces the whole query list with the input, but clones the current
instance rather than creating a completely new one.
So it's a more efficient form of instantiating a class based on an
array
input than fromArray().
I don't see how cloning is more efficient than creating a new object.
Additionally, it was designed to also keep the original building
options
(if they are going to be kept
in the final proposal).
$queryParams = Uri\QueryParams::parseRfc3986("foo=bar", 1000, new
Uri\QueryParamBuildingOptions("true", "false"));
$array = [...];
// Currently preferred solution:
$queryParams = $queryParams->withArray($array);
// The same solution using fromArray():
$queryParams = Uri\QueryParams::fromArray($array,
$queryParams->getBuildingOptions());
P.S. The getBuildingOptions() doesn't exist yet in the proposal, and it
should be added if building options remain in the RFC at last.
Given that the building options disappeared now, I would again suggest
to remove the `withArray()` method. The wholesale replacement is
confusing. Writing my reply to (11) I needed to think for a moment
whether or not `withArray()` would be useful for this kind of filtering
of a single parameter. The implementation could theoretically be
“replace all params that are specified and leave all others alone”. But
I think the variadic `set()` solves this more neatly.
13. “doesn't start with a “with” suffix” - that should read “prefix”.
Prefix is “start with”, suffix is “end with”.
Yeah, thanks for noticing it. It was an unfortunate typo, and I had
already fixed it in my draft version when you were reading it.
Perfect.
14. The “Backward Incompatible Changes” section should mention that
adding a new class is technically backwards incompatible, but an
allowed
break according to policy and that the Uri namespace is reserved for
ext/uri since 8.5.
Fixed.
Perfect.
One more thing which I'm probably going to change: it turned out that
WHATWG URL specifies
the sort() method
(https://url.spec.whatwg.org/#dom-urlsearchparams-sort)
which must use UTF-16 based
comparisons (using 2 bytes). It is really not obvious from the WHATWG
URL,
only the WHATWG Infra
specification defines it:
https://infra.spec.whatwg.org/#code-unit-less-than
In order to restore compatibility with this requirement, I'd define two
sorting algorithms:
- sortByBytes(): the currently specified one which would use bytes as
the
base of comparison
Sorting by bytes also works out as a sort by UTF-8 codepoints. As such I
believe it is the by far most common sorting operation. I suggest to
give it the default name of `sort()` to make it easy to use.
- sortByUtf16(): the one comparing UTF-16 sequences at once which would
be
compatible with WHATWG URL
And sorting by UTF-16 probably only is a thing because JavaScript is
UTF-16 (or rather UCS-2, I think). IMO it is not really necessary, but I
don't consider it to be as bad as `hasValue()`.
-------------
I'm going to give the RFC another fresh read later. But I wanted to work
through your response at least to clear of my stack. I tried to check if
my replies are still relevant with the current state of the RFC, but I
might've missed something. Please just let me know if something is
already resolved.
Best regards
Tim Düsterhus