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 Fetcher which automatically expands urls #558

Merged
merged 1 commit into from
Aug 19, 2024

Conversation

Sharktheone
Copy link
Member

The Fetcher automatically handles URLs expanding and requesting, so when it encounters a relative URL, it will make use of the base URL and "expands" the URL.

It also handles different schemes, like https, http and file we can add more later on.

For now, I only make use of it in the image requesting. I guess in the future, we want to build a fetcher, when we download the HTML and pass it to the different subcomponents, like drawing and so on.

The fetcher can then also make some caching, but only for the lifetime of the page, so if we have for example the same image twice on the page, it doesn't try to download it two times but only once.

@Sharktheone Sharktheone self-assigned this Aug 19, 2024
@Sharktheone Sharktheone force-pushed the net/fetcher branch 2 times, most recently from 22cab61 to b85a2b8 Compare August 19, 2024 11:31
@Sharktheone Sharktheone merged commit d472f0d into gosub-io:main Aug 19, 2024
10 checks passed
@Sharktheone Sharktheone deleted the net/fetcher branch August 19, 2024 11:41
}
}

pub fn set_str(&mut self, key: &str, value: &str) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Hello hello, once again a random comment after long silence 😉 I am actually very actively following the activity here on GitHub, but don't have a lot of time for communication. This has already been merged, but a little note for the future: Rust allows you to use impl Trait types as parameters, so you don't need separate methods set and set_str. That is widely used in the standard library too. The method would look like this:

pub fn set(&mut self, key: impl AsRef<str>, value: impl AsRef<str>) {
    self.headers.insert(key.as_ref().to_string(), value.as_ref().to_string());
}

When calling it with owned strings, the compiler will completely optimize this away into just self.headers.insert(key, value);. Because impl is just syntax sugar for bounded generics, the method actually gets monomorphized, resulting in the same 2 methods, but you are able to "generate" more by calling it with &str, String for example.

req.header.set("Content-Type", format!("text/{extension}")); // bad, don't do this, just an example

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, yeah. That looks interesting. I guess we could also just directly use impl Into<String>. I might change that when I'm adding more functionality to the fetcher. But thanks for the suggestion!

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

Successfully merging this pull request may close these issues.

3 participants