Request query parameter storage #2

Closed
opened 2024-10-27 15:45:11 +00:00 by 0xEAB · 3 comments

Query parameters are currently stored in a const(StringMultiValueMap).

Abstraction

This implies that a server has to parse the query parameters for any given input URL in advance to populate the structure the way a user would expect. I wonder whether it would be better to separate the actual parsing from the request abstraction and make it opt-in.

oceandrift-http does this at the moment:
https://github.com/oceandrift/http/blob/v0.24.312/microframework/oceandrift/http/microframework/form.d#L124
https://github.com/oceandrift/http/blob/v0.24.312/microframework/oceandrift/http/microframework/uri.d#L50

PSR-7 leaves it up to the implementation how it is handled (by defining only the abstract interfaces):
https://www.php-fig.org/psr/psr-7/

Original order is lost

Another thing is:

A multi-valued mapping, where a key is mapped to one or more values. The map
is sorted by keys […]

adr has pointed out the following issue on Discord:

The query params are put into one of those, and the problem is that is violating the HTTP spec.
The order of params is actually important. Good that it allows multiple values per key – that's something a lot of people leave out – but leaving out the original ordering is another similar mistake.

Query parameters are currently stored in a [`const(StringMultiValueMap)`](https://git.andrewlalis.com/Handy-Http/primitives/src/commit/23c74f82bedf662d3d6f8f2aed159a270f5a782d/source/handy_http_primitives/request.d#L23). ## Abstraction This implies that a server has to parse the query parameters for any given input URL in advance to populate the structure the way a user would expect. I wonder whether it would be better to separate the actual parsing from the request abstraction and make it opt-in. oceandrift-http does this at the moment: <https://github.com/oceandrift/http/blob/v0.24.312/microframework/oceandrift/http/microframework/form.d#L124> <https://github.com/oceandrift/http/blob/v0.24.312/microframework/oceandrift/http/microframework/uri.d#L50> PSR-7 leaves it up to the implementation how it is handled (by defining only the abstract interfaces): <https://www.php-fig.org/psr/psr-7/> ## Original order is lost Another thing is: > A multi-valued mapping, where a key is mapped to one or more values. The map > **is sorted** by keys […] adr has pointed out the following issue on Discord: > The query params are put into one of those, and the problem is that is violating the HTTP spec. > The order of params is actually important. Good that it allows multiple values per key – that's something a lot of people leave out – but leaving out the original ordering is another similar mistake.
Owner

So you're suggesting that the ServerHttpRequest simply provides the full raw URL, and that users would be free to parse it however they like? And in that case, I'd provide convenience functions to parse the query parameters, path parameters, and other things which only some implementations may care about, right?

So you're suggesting that the `ServerHttpRequest` simply provides the full raw URL, and that users would be free to parse it however they like? And in that case, I'd provide convenience functions to parse the query parameters, path parameters, and other things which only some implementations may care about, right?
Owner

I've pushed a commit which adds a parseQueryParameters function that takes a string and returns a list of QueryParameter structs, which is more appropriate. I've removed the query params from the request struct, and a user can now parse the query params if and when they need them.

I've pushed a commit which adds a `parseQueryParameters` function that takes a string and returns a list of `QueryParameter` structs, which is more appropriate. I've removed the query params from the request struct, and a user can now parse the query params if and when they need them.
Author

So you're suggesting that the ServerHttpRequest simply provides the full raw URL, and that users would be free to parse it however they like?

Yeah. Sounds like the most flexible approach to me.

> So you're suggesting that the ServerHttpRequest simply provides the full raw URL, and that users would be free to parse it however they like? Yeah. Sounds like the most flexible approach to me.
Sign in to join this conversation.
No Label
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: Handy-Http/primitives#2
No description provided.