ada-url / ada

WHATWG-compliant and fast URL parser written in modern C++
https://ada-url.com
Apache License 2.0
1.26k stars 77 forks source link

WHATWG URL Pattern: support for route matching #679

Open meftunca opened 1 month ago

meftunca commented 1 month ago

What is the problem this feature will solve?

With this support we can increase the spread of ada-url. The library is really fast, but by adding a route matching feature for url, it can be a really inclusive library.

What is the feature you are proposing to solve the problem?

Here is my proposed solution: /users/:id/friends with “:/” separated by the character “:/” to enable route matching. Of course, there may be more specific examples like “{id}”,“[a-zAz]”

What alternatives have you considered?

Since this suggestion is usually found in router libraries, it is difficult to give a specific reference, but I have listed an example of its use below

lemire commented 1 month ago

Can you be more precise as to what you mean by 'route matching'. Do you refer to a specification?

meftunca commented 1 month ago

@lemire ,I actually mean the user's route tokens, which is the main task of a router. as an example the following list may help

defined route incoming route by request
/api/v1/users/:userId /api/v1/users/35
/api/v1/products/:productId /api/v1/products/217

Of course, there may be specifications you want to stick to. But in general, I think it is very important to provide this structure that every http router basically needs. NodeJS uses your library if I'm not mistaken. Most routers under nodejs (expressjs, fastify ...) really waste time in the matching part. This feature can significantly improve the performance of such libraries

lemire commented 1 month ago

@meftunca My question stands: what is the specification you are are referencing? To be clearer, you propose that routing be implemented in ada, but a pre-requisite is that one has a formal definition of what routing means.

The one thing that comes to mind is the WHATWG URL Pattern specification. Is that what you mean, or do you mean something else?

meftunca commented 1 month ago

Hello,

The feature I propose is to add URL routing, a common mechanism in web applications, to the ada-url library. This feature would allow incoming HTTP requests to be routed to the appropriate handler functions by matching their URLs against specific patterns.

This feature would expand the scope of ada-url, making it easier to develop more comprehensive web applications. For example, this feature would be very useful when building a REST API or serving different content to different URLs.

My proposed solution is to use placeholder values like /users/:id/friends in URL patterns. These placeholders would be matched with the actual values in the request's URL and passed as parameters to the handler function. Additionally, more flexible matching rules could be defined with dynamic segments like {id} or regex expressions like [a-zA-Z].

As ada-url already has the ability to parse URL parts, adding this feature could be relatively easy. This would allow application developers to perform URL routing using only the ada-url library.

This feature would make ada-url a more powerful and versatile tool, making life easier for many developers like me. The routing feature I propose is based on the WHATWG URL Pattern specification. This standard defines a URL matching mechanism that is widely used in modern web applications and ensures consistency across different platforms. Adding this feature to the ada-url library would make ada-url a more powerful and versatile tool, making it easier for developers to build more complex web applications. Reference link: https://urlpattern.spec.whatwg.org/#parsing Thank you.

lemire commented 1 month ago

Ok. So we agree that the issue about WHATWG URL Pattern. I have retitled it.