1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#![cfg_attr(feature = "docs", feature(doc_cfg))]
#![deny(
single_use_lifetimes,
missing_debug_implementations,
large_assignments,
exported_private_dependencies,
absolute_paths_not_starting_with_crate,
anonymous_parameters,
explicit_outlives_requirements,
keyword_idents,
macro_use_extern_crate,
meta_variable_misuse,
missing_docs,
non_ascii_idents,
indirect_structural_match,
trivial_casts,
trivial_numeric_casts,
unreachable_pub,
unsafe_code,
unused_crate_dependencies,
unused_extern_crates,
unused_import_braces,
unused_lifetimes,
unused_qualifications
)]
mod client;
mod extensions;
pub use client::Client;
pub use extensions::*;
pub use qiniu_http as http;
pub use ureq;
#[cfg(feature = "async")]
use std::{future::Future, pin::Pin};
#[cfg(feature = "async")]
type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + 'a + Send>>;
#[cfg(test)]
mod tests {
use super::*;
use bytes::Bytes;
use futures::channel::oneshot::channel;
use md5::{Digest, Md5};
use qiniu_http::HttpCaller;
use qiniu_http::{
header::{CONTENT_LENGTH, USER_AGENT},
Method, OnProgressCallback, SyncRequest, SyncRequestBody, TransferProgressInfo,
};
use rand::{thread_rng, RngCore};
use std::{
io::{copy as io_copy, Read},
sync::{
atomic::{AtomicU64, Ordering::Relaxed},
Arc,
},
};
use tokio::task::spawn_blocking;
use warp::{
filters::{body::bytes, method::post},
header::value as header_value,
http::header::HeaderValue,
path,
reply::Response,
Filter,
};
macro_rules! starts_with_server {
($addr:ident, $routes:ident, $code:block) => {{
let (tx, rx) = channel();
let ($addr, server) = warp::serve($routes).bind_with_graceful_shutdown(([127, 0, 0, 1], 0), async move {
rx.await.ok();
});
let handler = tokio::spawn(server);
$code?;
tx.send(()).ok();
handler.await.ok();
}};
}
const BUF_LEN: usize = 1 << 20;
const MD5_LEN: usize = 16;
#[tokio::test]
async fn sync_http_test() -> anyhow::Result<()> {
env_logger::builder().is_test(true).try_init().ok();
let routes = path!("dir1" / "dir2" / "file")
.and(post())
.and(header_value(USER_AGENT.as_str()))
.and(bytes())
.map(|user_agent: HeaderValue, req_body: Bytes| {
assert_eq!(req_body.len(), BUF_LEN + MD5_LEN);
{
let mut hasher = Md5::new();
hasher.update(&req_body[..BUF_LEN]);
assert_eq!(hasher.finalize().as_slice(), &req_body[BUF_LEN..]);
}
assert!(user_agent.to_str().unwrap().starts_with("QiniuRust/"));
assert!(user_agent.to_str().unwrap().ends_with("/qiniu-ureq"));
let mut resp_body = vec![0u8; BUF_LEN + MD5_LEN];
thread_rng().fill_bytes(&mut resp_body[..BUF_LEN]);
{
let mut hasher = Md5::new();
hasher.update(&resp_body[..BUF_LEN]);
resp_body[BUF_LEN..].copy_from_slice(hasher.finalize().as_slice());
}
Response::new(resp_body.into())
});
starts_with_server!(addr, routes, {
spawn_blocking(move || {
let mut request_body = vec![0u8; BUF_LEN + MD5_LEN];
thread_rng().fill_bytes(&mut request_body[..BUF_LEN]);
{
let mut hasher = Md5::new();
hasher.update(&request_body[..BUF_LEN]);
request_body[BUF_LEN..].copy_from_slice(hasher.finalize().as_slice());
}
let last_uploaded = Arc::new(AtomicU64::new(0));
let last_total = Arc::new(AtomicU64::new(0));
let mut response = {
let last_uploaded = last_uploaded.to_owned();
let last_total = last_total.to_owned();
let callback = move |info: TransferProgressInfo| {
last_uploaded.store(info.transferred_bytes(), Relaxed);
last_total.store(info.total_bytes(), Relaxed);
Ok(())
};
let mut request = SyncRequest::builder()
.method(Method::POST)
.url(format!("http://{addr}/dir1/dir2/file").parse().expect("invalid uri"))
.body(SyncRequestBody::from_referenced_bytes(&request_body))
.on_uploading_progress(OnProgressCallback::reference(&callback))
.build();
Client::default().call(&mut request)?
};
assert_eq!(
response.header(&CONTENT_LENGTH).map(|h| h.as_bytes()),
Some(format!("{}", BUF_LEN + MD5_LEN).as_bytes())
);
assert_eq!(last_uploaded.load(Relaxed), request_body.len() as u64);
assert_eq!(last_total.load(Relaxed), request_body.len() as u64);
{
let mut body_part = Vec::new();
let mut checksum_part = Vec::new();
assert_eq!(
io_copy(&mut response.body_mut().take(BUF_LEN as u64), &mut body_part)?,
BUF_LEN as u64
);
assert_eq!(
io_copy(&mut response.body_mut().take(MD5_LEN as u64), &mut checksum_part)?,
MD5_LEN as u64
);
let mut hasher = Md5::new();
hasher.update(&body_part);
assert_eq!(hasher.finalize().as_slice(), checksum_part.as_slice());
}
Ok::<_, anyhow::Error>(())
})
.await?
});
Ok(())
}
}