Comments (4)
@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.
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 Url
s, 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 loopsRedirectPolicy::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.
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.
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)
- Unable to connect to IPv6 hosts when enabling trust-dns and binding to a local IPv6 address HOT 3
- Use web_sys::Url instead of url::Url on wasm32 targets HOT 4
- feature request: split native-tls/openssl and related flags from reqwest/native-tls flag HOT 1
- Using already existing external implementations for HTTP
- Replace serde_urlencoded with serde_qs HOT 2
- adding base url HOT 2
- Update http to 1.0 HOT 2
- unsuccessful tunnel problem HOT 3
- The http3 request failed and the request timed out. HOT 6
- tcp_nodelay on ClientBuilder does not actually set TCP_NODELAY
- When performing a streaming file upload, a Broken pipe error occurs HOT 5
- http 1.0 breaks test example for response HOT 1
- There are many errors present on macOS. HOT 2
- Redirect not supported in wasm client HOT 1
- WASM support extended HOT 2
- `no_gzip` & `gzip` not supported for wasm target HOT 10
- async client not reusable across tasks
- response err.is_timeout() can't satisfy reqwest::Error { kind: Body, source: TimedOut } case HOT 3
- Reading the body of a request HOT 2
- ConnectionRefused on windows11 HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from reqwest.