Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add an "append_path" function to url #934

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

cbeck88
Copy link

@cbeck88 cbeck88 commented Jun 2, 2024

This append_path function is an alternative to Url::join which addresses issues discussed in #333, mainly that Url::join is sensitive to trailing slashes is in the Url, and if the trailing slash is missing, may remove segments from the base url and replace them with segments from the joined Url.

There are good reasons for Url::join to behave that way, because that is was is specified in the Url standard. (mentioned here: #333 (comment))

However it's still inconvenient because it often leads to situations where, a service takes some base-url for some API as a config parameter, uses Url::join to append various routes to it and make requests, and if a trailing / is omitted in the config, you don't figure it out until deploying and looking at logs and seeing nonsense requests failing. In many situations in web development these trailing / are not significant so this is easy to forget and can become just an annoying papercut.

One suggestion in #333 was to add an alternative utility function that isn't sensitive to the trailing /'s in this way. This commit adds such a utility function with tests.

I've been copy-pasting this around in several projects, I figured I would PR it back and see if there was any interest since it was discussed in the github issue.

this function is an alternative to `Url::join` which addresses issues
discussed in servo#333, mainly that `Url::join` is sensitive to trailing
slashes is in the `Url`, and if the trailing slash is missing, may
remove segments from the base url and replace them with segments
from the joined `Url`.

There are good reasons for `Url::join` to behave that way, because
that is was is specified in the `Url` standard.

However it's still inconvenient because it often leads to situations
where, a service takes some base-url for some API as a config parameter,
uses `Url::join` to append various routes to it and make requests, and if
a trailing `/` is omitted in a config file, you don't figure it out until
deploying and looking at logs and seeing nonsense requests failing.
In many situations in web development these trailing `/` are not significant
so this is easy to forget and can become just an annoying papercut.

One suggestion in servo#333 was to add an alternative utility function that
isn't sensitive to the trailing `/`'s in this way.
This commit adds such a utility function with tests.
// Remove any leading `/` from the path we are appending, this makes our code tolerate leading `/`'s
let path = path.as_ref();
let path = path.strip_prefix('/').unwrap_or(path);
for segment in path.split('/') {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this splitting is based on similar code elsewhere in the library: https://docs.rs/url/2.5.0/src/url/lib.rs.html#1351

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant