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

RelativeUrlWithoutBase error while parsing successful response url #2489

Open
ivoleitao opened this issue Dec 1, 2024 · 2 comments
Open
Labels
A-wasm Area: WASM. E-pr-welcome The feature is welcome to be added, instruction should be found in the issue.

Comments

@ivoleitao
Copy link

ivoleitao commented Dec 1, 2024

Hi ! I'm using trunk to develop an app that makes http calls to the shadertoy api. My code is roughly the following:

        let shader_ids = ShaderRequest {
            shaders: vec![shader_id],
        };

        params.insert("s", &s);
        params.insert("nt", "1");
        params.insert("nl", "1");

        let url = context.shadertoy_url.as_str();
        // url=https://www.shadertoy.com/shadertoy
        let browse_url = context.browse_url.as_str();
        // browse_url=https://www.shadertoy.com/browse
        let shaders = client
            .post(url)
            .fetch_mode_no_cors()
            .header(REFERER, browse_url)
            .form(&params)
            .send()
            .await?
            .json::<Vec<Shader>>()
            .await?;

Eveything works fine in non wasm environment, but on wasm I always get RelativeUrlWithoutBase error. This is happening not at request time but at response time, as I traced it back to this call on the url parse of the response (see
here) snippet bellow:

let url = Url::parse(&js_resp.url()).expect_throw("url parse");

I checked all the issues that reference this problem but all seem to talk about request time url parsing not reponse time. I have nop way (I don't know how) to obtain the value that is being returned and triggering this problem on rust-url (see here):

    /// https://url.spec.whatwg.org/#concept-basic-url-parser
    pub fn parse_url(mut self, input: &str) -> ParseResult<Url> {
        let input = Input::new_trim_c0_control_and_space(input, self.violation_fn);
        if let Ok(remaining) = self.parse_scheme(input.clone()) {
            return self.parse_with_scheme(remaining);
        }

        // No-scheme state
        if let Some(base_url) = self.base_url {
            if input.starts_with('#') {
                self.fragment_only(base_url, input)
            } else if base_url.cannot_be_a_base() {
                Err(ParseError::RelativeUrlWithCannotBeABaseBase)
            } else {
                let scheme_type = SchemeType::from(base_url.scheme());
                if scheme_type.is_file() {
                    self.parse_file(input, scheme_type, Some(base_url))
                } else {
                    self.parse_relative(input, scheme_type, base_url)
                }
            }
        } else {
            Err(ParseError::RelativeUrlWithoutBase)
        }
    }

I tried almost everything inclusive changing trunk configurations but to no avail. Any idea how can I solve this and what is causing it ? Any workarounds ?

@seanmonstar
Copy link
Owner

I'm not sure why the fetch response wouldn't have a full a URL, but we could add some code to try to handle it. The purpose of that code in reqwest is so you have the URL after any redirects. But we do have the original URL that was passed to fetch. So, I suppose one solution could be that if parsing response.url is an error, then we could just use the request url.

Would you like to submit a PR doing so?

@seanmonstar seanmonstar added E-pr-welcome The feature is welcome to be added, instruction should be found in the issue. A-wasm Area: WASM. labels Dec 2, 2024
@ivoleitao
Copy link
Author

ivoleitao commented Dec 2, 2024

Hi, I managed to use the C/C++ DevTools Support (DWARF) on chrome and the response url is empty on this case so perhaps we just need to check for an empty string and fallback to the request url without even trying to parse it like bellow:

let url = Url::parse(&js_resp.url()).expect_throw("url parse");

I need to make sure it works thus I will connect my source code to a forked version of reqwest with this change. If all works out I will provide a PR if you agree with the above.

To be honest I'm a bit confused because it seems that no response url is working on wasm on my scenario. Saying that because I tried to do the following as well:

        let res = reqwest::Client::new()
            .get("https://postman-echo.com/get")
            .fetch_mode_no_cors()
            .send()
            .await?;

        let text = res.text().await?;
        println!("{}", text);

Same error exactly. The only thing here is the no cors mode as without it I cannot make a call I always get an error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-wasm Area: WASM. E-pr-welcome The feature is welcome to be added, instruction should be found in the issue.
Projects
None yet
Development

No branches or pull requests

2 participants