|
| 1 | +use std::fmt; |
| 2 | + |
| 3 | +use ::Url; |
| 4 | + |
| 5 | +/// A type that controls the policy on how to handle the following of redirects. |
| 6 | +/// |
| 7 | +/// The default value will catch redirect loops, and has a maximum of 10 |
| 8 | +/// redirects it will follow in a chain before returning an error. |
1 | 9 | #[derive(Debug)] |
2 | 10 | pub struct RedirectPolicy { |
3 | | - inner: () |
| 11 | + inner: Policy, |
4 | 12 | } |
5 | 13 |
|
6 | 14 | impl RedirectPolicy { |
7 | | - |
| 15 | + /// Create a RedirectPolicy with a maximum number of redirects. |
| 16 | + /// |
| 17 | + /// A `Error::TooManyRedirects` will be returned if the max is reached. |
| 18 | + pub fn limited(max: usize) -> RedirectPolicy { |
| 19 | + RedirectPolicy { |
| 20 | + inner: Policy::Limit(max), |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + /// Create a RedirectPolicy that does not follow any redirect. |
| 25 | + pub fn none() -> RedirectPolicy { |
| 26 | + RedirectPolicy { |
| 27 | + inner: Policy::None, |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + /// Create a custom RedirectPolicy using the passed function. |
| 32 | + /// |
| 33 | + /// # Note |
| 34 | + /// |
| 35 | + /// The default RedirectPolicy handles redirect loops and a maximum loop |
| 36 | + /// chain, but the custom variant does not do that for you automatically. |
| 37 | + /// The custom policy should hanve some way of handling those. |
| 38 | + /// |
| 39 | + /// There are variants on `::Error` for both cases that can be used as |
| 40 | + /// return values. |
| 41 | + /// |
| 42 | + /// # Example |
| 43 | + /// |
| 44 | + /// ```no_run |
| 45 | + /// # use reqwest::RedirectPolicy; |
| 46 | + /// # let mut client = reqwest::Client::new().unwrap(); |
| 47 | + /// client.redirect(RedirectPolicy::custom(|next, previous| { |
| 48 | + /// if previous.len() > 5 { |
| 49 | + /// Err(reqwest::Error::TooManyRedirects) |
| 50 | + /// } else if next.host_str() == Some("example.domain") { |
| 51 | + /// // prevent redirects to 'example.domain' |
| 52 | + /// Ok(false) |
| 53 | + /// } else { |
| 54 | + /// Ok(true) |
| 55 | + /// } |
| 56 | + /// })); |
| 57 | + /// ``` |
| 58 | + pub fn custom<T>(policy: T) -> RedirectPolicy |
| 59 | + where T: Fn(&Url, &[Url]) -> ::Result<bool> + Send + Sync + 'static { |
| 60 | + RedirectPolicy { |
| 61 | + inner: Policy::Custom(Box::new(policy)), |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + fn redirect(&self, next: &Url, previous: &[Url]) -> ::Result<bool> { |
| 66 | + match self.inner { |
| 67 | + Policy::Custom(ref custom) => custom(next, previous), |
| 68 | + Policy::Limit(max) => { |
| 69 | + if previous.len() == max { |
| 70 | + Err(::Error::TooManyRedirects) |
| 71 | + } else if previous.contains(next) { |
| 72 | + Err(::Error::RedirectLoop) |
| 73 | + } else { |
| 74 | + Ok(true) |
| 75 | + } |
| 76 | + }, |
| 77 | + Policy::None => Ok(false), |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +impl Default for RedirectPolicy { |
| 83 | + fn default() -> RedirectPolicy { |
| 84 | + RedirectPolicy::limited(10) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +enum Policy { |
| 89 | + Custom(Box<Fn(&Url, &[Url]) -> ::Result<bool> + Send + Sync + 'static>), |
| 90 | + Limit(usize), |
| 91 | + None, |
| 92 | +} |
| 93 | + |
| 94 | +impl fmt::Debug for Policy { |
| 95 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 96 | + match *self { |
| 97 | + Policy::Custom(..) => f.pad("Custom"), |
| 98 | + Policy::Limit(max) => f.debug_tuple("Limit").field(&max).finish(), |
| 99 | + Policy::None => f.pad("None"), |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +pub fn check_redirect(policy: &RedirectPolicy, next: &Url, previous: &[Url]) -> ::Result<bool> { |
| 105 | + policy.redirect(next, previous) |
| 106 | +} |
| 107 | + |
| 108 | +/* |
| 109 | +This was the desired way of doing it, but ran in to inference issues when |
| 110 | +using closures, since the arguments received are references (&Url and &[Url]), |
| 111 | +and the compiler could not infer the lifetimes of those references. That means |
| 112 | +people would need to annotate the closure's argument types, which is garbase. |
| 113 | +
|
| 114 | +pub trait Redirect { |
| 115 | + fn redirect(&self, next: &Url, previous: &[Url]) -> ::Result<bool>; |
| 116 | +} |
| 117 | +
|
| 118 | +impl<F> Redirect for F |
| 119 | +where F: Fn(&Url, &[Url]) -> ::Result<bool> { |
| 120 | + fn redirect(&self, next: &Url, previous: &[Url]) -> ::Result<bool> { |
| 121 | + self(next, previous) |
| 122 | + } |
| 123 | +} |
| 124 | +*/ |
| 125 | + |
| 126 | +#[test] |
| 127 | +fn test_redirect_policy_limit() { |
| 128 | + let policy = RedirectPolicy::default(); |
| 129 | + let next = Url::parse("http://x.y/z").unwrap(); |
| 130 | + let mut previous = (0..9) |
| 131 | + .map(|i| Url::parse(&format!("http://a.b/c/{}", i)).unwrap()) |
| 132 | + .collect::<Vec<_>>(); |
| 133 | + |
| 134 | + |
| 135 | + match policy.redirect(&next, &previous) { |
| 136 | + Ok(true) => {}, |
| 137 | + other => panic!("expected Ok(true), got: {:?}", other) |
| 138 | + } |
| 139 | + |
| 140 | + previous.push(Url::parse("http://a.b.d/e/33").unwrap()); |
| 141 | + |
| 142 | + match policy.redirect(&next, &previous) { |
| 143 | + Err(::Error::TooManyRedirects) => {}, |
| 144 | + other => panic!("expected TooManyRedirects, got: {:?}", other) |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +#[test] |
| 149 | +fn test_redirect_policy_custom() { |
| 150 | + let policy = RedirectPolicy::custom(|next, _previous| { |
| 151 | + if next.host_str() == Some("foo") { |
| 152 | + Ok(false) |
| 153 | + } else { |
| 154 | + Ok(true) |
| 155 | + } |
| 156 | + }); |
| 157 | + |
| 158 | + let next = Url::parse("http://bar/baz").unwrap(); |
| 159 | + assert_eq!(policy.redirect(&next, &[]).unwrap(), true); |
| 160 | + |
| 161 | + let next = Url::parse("http://foo/baz").unwrap(); |
| 162 | + assert_eq!(policy.redirect(&next, &[]).unwrap(), false); |
8 | 163 | } |
0 commit comments