Git Product home page Git Product logo

Comments (4)

Arnavion avatar Arnavion commented on August 21, 2024

@Arnavion mind adding exactly what you'd need to the issue?

Well, currently reqwest effectively has a redirect policy of FollowAll because it sets the underlying request policy to FollowNone and has a loop to always follow redirects. I want to be able to control which redirects get followed and which don't, which hyper does by letting me set a FollowIf(fn(&Url) -> bool) policy.

My crate interfaces with a web API that sometimes sends a redirect to a login page instead of a 401/403 response, so by preventing that redirect I'm able to report an equivalent error to the caller of my function instead of trying to deserialize an HTML page as JSON.

I also plan in the future to prevent redirects from taking me to hostnames outside of a whitelist to prevent abuse.

from reqwest.

seanmonstar avatar seanmonstar commented on August 21, 2024

I've felt that the RedirectPolicy in hyper was too limited, and wanted to use this opportunity to make a better one. To start, the struct won't have it's variants exposed, so new ones can be added. Also, other problems with RedirectPolicy::FollowIf are:

  • It must be a standard function, so setting configuration variables at startup and using them is not possible
  • It is not possible to check for infinite redirects
  • It is not possible to set a number limit

Looking at other implementations for inspiration, there is this in golang's http package:

CheckRedirect func(req *Request, via []*Request) error

This allows a user to a) count the number of previous requests, b) check for duplicate URLs, among other things. If I understand Go enough, I believe that also means the CheckRedirect function could even modify the *Request, but I don't know if that's something desirable or not.


So, the design I've been thinking of looks something like this:

trait Redirect {
    fn redirect(&self, next: &Url, previous: &[Url]) -> bool;
}

impl Client {
    pub fn set_redirect_policy<T: Redirect>(&mut self, policy: T) {
        self.redirect_policy = Box::new(policy);
    }
}

The Redirect trait could perhaps receive Request instead of Urls, if there is use in inspecting the Headers also.

There could be a couple built-ins, such as:

  • RedirectPolicy::default() - max redirects 10, look for loops
  • RedirectPolicy::none() - always return false

It would also allow custom policies like this:

let max_redirects = config.load('max_redirects').unwrap_or(5);
client.set_redirect_policy(move |_next, previous| {
    previous.len() >= max_redirects    
})

from reqwest.

Arnavion avatar Arnavion commented on August 21, 2024

That sounds good. Maybe you can also add operators to combine redirect policies but that can be done in user code as well.

from reqwest.

seanmonstar avatar seanmonstar commented on August 21, 2024

I have an implementation in #29.

I ended up not having a trait Redirect, because the compiler had issues infer lifetimes of the &Url and &[Url] arguments passed to closures. Otherwise, it does what was suggested in my previous comment.

from reqwest.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.