@@ -70,20 +70,26 @@ pub trait HttpClient: Debug + Unpin + Send + Sync + Clone + 'static {
70
70
/// like `Vec<u8>` or `String`, using the `From` trait.
71
71
pub struct Body {
72
72
reader : Box < dyn AsyncRead + Unpin + Send + ' static > ,
73
+ /// Intentionally use `u64` over `usize` here.
74
+ /// `usize` won't work if you try to send 10GB file from 32bit host.
75
+ #[ allow( dead_code) ] // not all backends make use of this
76
+ len : Option < u64 > ,
73
77
}
74
78
75
79
impl Body {
76
80
/// Create a new empty body.
77
81
pub fn empty ( ) -> Self {
78
82
Self {
79
83
reader : Box :: new ( futures:: io:: empty ( ) ) ,
84
+ len : Some ( 0 ) ,
80
85
}
81
86
}
82
87
83
88
/// Create a new instance from a reader.
84
89
pub fn from_reader ( reader : impl AsyncRead + Unpin + Send + ' static ) -> Self {
85
90
Self {
86
91
reader : Box :: new ( reader) ,
92
+ len : None ,
87
93
}
88
94
}
89
95
}
@@ -110,8 +116,10 @@ impl From<Vec<u8>> for Body {
110
116
#[ allow( missing_doc_code_examples) ]
111
117
#[ inline]
112
118
fn from ( vec : Vec < u8 > ) -> Body {
119
+ let len = vec. len ( ) as u64 ;
113
120
Self {
114
121
reader : Box :: new ( Cursor :: new ( vec) ) ,
122
+ len : Some ( len) ,
115
123
}
116
124
}
117
125
}
@@ -120,6 +128,6 @@ impl<R: AsyncRead + Unpin + Send + 'static> From<Box<R>> for Body {
120
128
/// Converts an `AsyncRead` into a Body.
121
129
#[ allow( missing_doc_code_examples) ]
122
130
fn from ( reader : Box < R > ) -> Self {
123
- Self { reader }
131
+ Self { reader, len : None }
124
132
}
125
133
}
0 commit comments