@@ -13,6 +13,7 @@ use crate::easy::handler::{Auth, NetRc, PostRedirections, ProxyType, SslOpt};
1313use crate :: easy:: handler:: { HttpVersion , IpResolve , SslVersion , TimeCondition } ;
1414use crate :: easy:: { Easy2 , Handler } ;
1515use crate :: easy:: { Form , List } ;
16+ use crate :: easy:: { WriteContext , WriteContext2 } ;
1617use crate :: Error ;
1718
1819/// Raw bindings to a libcurl "easy session".
@@ -105,6 +106,8 @@ unsafe impl Send for EasyData {}
105106#[ derive( Default ) ]
106107struct Callbacks < ' a > {
107108 write : Option < Box < dyn FnMut ( & [ u8 ] ) -> Result < usize , WriteError > + ' a > > ,
109+ write_context :
110+ Option < Box < dyn FnMut ( & [ u8 ] , & mut WriteContext ) -> Result < usize , WriteError > + ' a > > ,
108111 read : Option < Box < dyn FnMut ( & mut [ u8 ] ) -> Result < usize , ReadError > + ' a > > ,
109112 seek : Option < Box < dyn FnMut ( SeekFrom ) -> SeekResult + ' a > > ,
110113 debug : Option < Box < dyn FnMut ( InfoType , & [ u8 ] ) + ' a > > ,
@@ -251,6 +254,15 @@ impl Easy {
251254 Ok ( ( ) )
252255 }
253256
257+ /// Same as [`Easy::write_function`] but with access to the [`WriteContext`].
258+ pub fn write_function_with_context < F > ( & mut self , f : F ) -> Result < ( ) , Error >
259+ where
260+ F : FnMut ( & [ u8 ] , & mut WriteContext ) -> Result < usize , WriteError > + Send + ' static ,
261+ {
262+ self . inner . get_mut ( ) . owned . write_context = Some ( Box :: new ( f) ) ;
263+ Ok ( ( ) )
264+ }
265+
254266 /// Read callback for data uploads.
255267 ///
256268 /// This callback function gets called by libcurl as soon as it needs to
@@ -1515,6 +1527,22 @@ impl Handler for EasyData {
15151527 }
15161528 }
15171529
1530+ fn write_context (
1531+ & mut self ,
1532+ data : & [ u8 ] ,
1533+ ctx : & mut WriteContext2 < Self > ,
1534+ ) -> Result < usize , WriteError > {
1535+ unsafe {
1536+ match self . callback ( |s| & mut s. write_context ) {
1537+ Some ( write) => write ( data, WriteContext :: from_mut ( ctx) ) ,
1538+ None => match self . callback ( |s| & mut s. write ) {
1539+ Some ( write) => write ( data) ,
1540+ None => Ok ( data. len ( ) ) ,
1541+ } ,
1542+ }
1543+ }
1544+ }
1545+
15181546 fn read ( & mut self , data : & mut [ u8 ] ) -> Result < usize , ReadError > {
15191547 unsafe {
15201548 match self . callback ( |s| & mut s. read ) {
@@ -1587,6 +1615,16 @@ impl<'easy, 'data> Transfer<'easy, 'data> {
15871615 Ok ( ( ) )
15881616 }
15891617
1618+ /// Same as `Easy::write_context_function`, just takes a non `'static`
1619+ /// lifetime corresponding to the lifetime of this transfer.
1620+ pub fn write_context_function < F > ( & mut self , f : F ) -> Result < ( ) , Error >
1621+ where
1622+ F : FnMut ( & [ u8 ] , & mut WriteContext ) -> Result < usize , WriteError > + ' data ,
1623+ {
1624+ self . data . write_context = Some ( Box :: new ( f) ) ;
1625+ Ok ( ( ) )
1626+ }
1627+
15901628 /// Same as `Easy::read_function`, just takes a non `'static` lifetime
15911629 /// corresponding to the lifetime of this transfer.
15921630 pub fn read_function < F > ( & mut self , f : F ) -> Result < ( ) , Error >
0 commit comments