Skip to content

Commit 386e2c4

Browse files
committed
Expose the POST redirection setting (#286)
Change-Id: Ib094f5480f7571255c50f9a7d05fa3102df74e27
1 parent 51a0865 commit 386e2c4

File tree

3 files changed

+82
-3
lines changed

3 files changed

+82
-3
lines changed

Diff for: src/easy/handle.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use curl_sys;
1010
use libc::c_void;
1111

1212
use crate::easy::handler::{self, InfoType, ReadError, SeekResult, WriteError};
13-
use crate::easy::handler::{Auth, NetRc, ProxyType, SslOpt};
13+
use crate::easy::handler::{Auth, NetRc, PostRedirections, ProxyType, SslOpt};
1414
use crate::easy::handler::{HttpVersion, IpResolve, SslVersion, TimeCondition};
1515
use crate::easy::{Easy2, Handler};
1616
use crate::easy::{Form, List};
@@ -802,6 +802,11 @@ impl Easy {
802802
self.inner.max_redirections(max)
803803
}
804804

805+
/// Same as [`Easy2::post_redirections`](struct.Easy2.html#method.post_redirections)
806+
pub fn post_redirections(&mut self, redirects: &PostRedirections) -> Result<(), Error> {
807+
self.inner.post_redirections(redirects)
808+
}
809+
805810
/// Same as [`Easy2::put`](struct.Easy2.html#method.put)
806811
pub fn put(&mut self, enable: bool) -> Result<(), Error> {
807812
self.inner.put(enable)

Diff for: src/easy/handler.rs

+75-1
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,10 @@ pub struct Auth {
571571
pub struct SslOpt {
572572
bits: c_long,
573573
}
574+
/// Structure which stores possible post redirection options to pass to `post_redirections`.
575+
pub struct PostRedirections {
576+
bits: c_ulong,
577+
}
574578

575579
impl<H: Handler> Easy2<H> {
576580
/// Creates a new "easy" handle which is the core of almost all operations
@@ -1474,7 +1478,14 @@ impl<H> Easy2<H> {
14741478
self.setopt_long(curl_sys::CURLOPT_MAXREDIRS, max as c_long)
14751479
}
14761480

1477-
// TODO: post_redirections
1481+
/// Set the policy for handling redirects to POST requests.
1482+
///
1483+
/// By default a POST is changed to a GET when following a redirect. Setting any
1484+
/// of the `PostRedirections` flags will preserve the POST method for the
1485+
/// selected response codes.
1486+
pub fn post_redirections(&mut self, redirects: &PostRedirections) -> Result<(), Error> {
1487+
self.setopt_long(curl_sys::CURLOPT_POSTREDIR, redirects.bits as c_long)
1488+
}
14781489

14791490
/// Make an HTTP PUT request.
14801491
///
@@ -3911,3 +3922,66 @@ impl fmt::Debug for SslOpt {
39113922
.finish()
39123923
}
39133924
}
3925+
3926+
impl PostRedirections {
3927+
/// Create an empty PostRedirection setting with no flags set.
3928+
pub fn new() -> PostRedirections {
3929+
PostRedirections { bits: 0 }
3930+
}
3931+
3932+
/// Configure POST method behaviour on a 301 redirect. Setting the value
3933+
/// to true will preserve the method when following the redirect, else
3934+
/// the method is changed to GET.
3935+
pub fn redirect_301(&mut self, on: bool) -> &mut PostRedirections {
3936+
self.flag(curl_sys::CURL_REDIR_POST_301, on)
3937+
}
3938+
3939+
/// Configure POST method behaviour on a 302 redirect. Setting the value
3940+
/// to true will preserve the method when following the redirect, else
3941+
/// the method is changed to GET.
3942+
pub fn redirect_302(&mut self, on: bool) -> &mut PostRedirections {
3943+
self.flag(curl_sys::CURL_REDIR_POST_302, on)
3944+
}
3945+
3946+
/// Configure POST method behaviour on a 303 redirect. Setting the value
3947+
/// to true will preserve the method when following the redirect, else
3948+
/// the method is changed to GET.
3949+
pub fn redirect_303(&mut self, on: bool) -> &mut PostRedirections {
3950+
self.flag(curl_sys::CURL_REDIR_POST_303, on)
3951+
}
3952+
3953+
/// Configure POST method behaviour for all redirects. Setting the value
3954+
/// to true will preserve the method when following the redirect, else
3955+
/// the method is changed to GET.
3956+
pub fn redirect_all(&mut self, on: bool) -> &mut PostRedirections {
3957+
self.flag(curl_sys::CURL_REDIR_POST_ALL, on)
3958+
}
3959+
3960+
fn flag(&mut self, bit: c_ulong, on: bool) -> &mut PostRedirections {
3961+
if on {
3962+
self.bits |= bit;
3963+
} else {
3964+
self.bits &= !bit;
3965+
}
3966+
self
3967+
}
3968+
}
3969+
3970+
impl fmt::Debug for PostRedirections {
3971+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3972+
f.debug_struct("PostRedirections")
3973+
.field(
3974+
"redirect_301",
3975+
&(self.bits & curl_sys::CURL_REDIR_POST_301 != 0),
3976+
)
3977+
.field(
3978+
"redirect_302",
3979+
&(self.bits & curl_sys::CURL_REDIR_POST_302 != 0),
3980+
)
3981+
.field(
3982+
"redirect_303",
3983+
&(self.bits & curl_sys::CURL_REDIR_POST_303 != 0),
3984+
)
3985+
.finish()
3986+
}
3987+
}

Diff for: src/easy/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ mod windows;
1515

1616
pub use self::form::{Form, Part};
1717
pub use self::handle::{Easy, Transfer};
18-
pub use self::handler::{Auth, NetRc, ProxyType, SslOpt};
18+
pub use self::handler::{Auth, NetRc, PostRedirections, ProxyType, SslOpt};
1919
pub use self::handler::{Easy2, Handler};
2020
pub use self::handler::{HttpVersion, IpResolve, SslVersion, TimeCondition};
2121
pub use self::handler::{InfoType, ReadError, SeekResult, WriteError};

0 commit comments

Comments
 (0)