-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttp_local_chunk_transfer.rs
More file actions
299 lines (284 loc) · 9.07 KB
/
http_local_chunk_transfer.rs
File metadata and controls
299 lines (284 loc) · 9.07 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
mod support;
use std::sync::Arc;
use async_trait::async_trait;
use reqwest::header::{HeaderMap, HeaderValue};
use reqwest::Method;
use rusty_cat::api::{
DownloadPounceBuilder, MeowClient, UploadChunkCtx, UploadPounceBuilder, UploadPrepareCtx,
UploadResumeInfo,
};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::{TcpListener, TcpStream};
use tokio::sync::{oneshot, Mutex};
use support::{
config, make_file, remove_if_exists, run_task, temp_path, AnyResult, FIVE_MB, ONE_MB,
};
#[derive(Clone)]
struct BinaryUpload {
url: String,
}
#[async_trait]
impl rusty_cat::api::BreakpointUpload for BinaryUpload {
async fn prepare(
&self,
_ctx: UploadPrepareCtx<'_>,
) -> Result<UploadResumeInfo, rusty_cat::api::MeowError> {
Ok(UploadResumeInfo {
completed_file_id: None,
next_byte: Some(0),
})
}
async fn upload_chunk(
&self,
ctx: UploadChunkCtx<'_>,
) -> Result<UploadResumeInfo, rusty_cat::api::MeowError> {
let mut headers = HeaderMap::new();
headers.insert(
"x-offset",
HeaderValue::from_str(&ctx.offset.to_string()).unwrap(),
);
let resp = ctx
.client
.put(&self.url)
.headers(headers)
.body(reqwest::Body::from(ctx.chunk.clone()))
.send()
.await
.map_err(|e| {
rusty_cat::api::MeowError::from_source(
rusty_cat::api::InnerErrorCode::HttpError,
e.to_string(),
e,
)
})?;
if !resp.status().is_success() {
return Err(rusty_cat::api::MeowError::from_code(
rusty_cat::api::InnerErrorCode::ResponseStatusError,
format!("upload status {}", resp.status()),
));
}
Ok(UploadResumeInfo {
completed_file_id: None,
next_byte: Some(ctx.offset + ctx.chunk.len() as u64),
})
}
}
struct LocalServer {
addr: String,
shutdown: Option<oneshot::Sender<()>>,
}
impl Drop for LocalServer {
fn drop(&mut self) {
if let Some(tx) = self.shutdown.take() {
let _ = tx.send(());
}
}
}
async fn start_server(data: Vec<u8>) -> AnyResult<LocalServer> {
let listener = TcpListener::bind("127.0.0.1:0").await?;
let addr = listener.local_addr()?;
let download = Arc::new(data);
let upload = Arc::new(Mutex::new(vec![0_u8; FIVE_MB]));
let (tx, mut rx) = oneshot::channel::<()>();
tokio::spawn(async move {
loop {
tokio::select! {
_ = &mut rx => break,
accepted = listener.accept() => {
if let Ok((stream, _)) = accepted {
let download = Arc::clone(&download);
let upload = Arc::clone(&upload);
tokio::spawn(async move {
let _ = handle_connection(stream, download, upload).await;
});
}
}
}
}
});
Ok(LocalServer {
addr: format!("http://{}", addr),
shutdown: Some(tx),
})
}
async fn handle_connection(
mut stream: TcpStream,
download: Arc<Vec<u8>>,
upload: Arc<Mutex<Vec<u8>>>,
) -> AnyResult<()> {
let mut buf = Vec::new();
let mut tmp = [0_u8; 4096];
let header_end = loop {
let n = stream.read(&mut tmp).await?;
if n == 0 {
return Ok(());
}
buf.extend_from_slice(&tmp[..n]);
if let Some(pos) = find_header_end(&buf) {
break pos;
}
};
let header = String::from_utf8_lossy(&buf[..header_end]).to_string();
let mut lines = header.lines();
let request = lines.next().unwrap_or_default().to_string();
let mut headers = Vec::new();
for line in lines {
if let Some((k, v)) = line.split_once(':') {
headers.push((k.trim().to_ascii_lowercase(), v.trim().to_string()));
}
}
let content_len = header_value(&headers, "content-length")
.and_then(|v| v.parse::<usize>().ok())
.unwrap_or(0);
let mut body = buf[header_end + 4..].to_vec();
while body.len() < content_len {
let n = stream.read(&mut tmp).await?;
if n == 0 {
break;
}
body.extend_from_slice(&tmp[..n]);
}
let parts: Vec<&str> = request.split_whitespace().collect();
let method = parts.first().copied().unwrap_or_default();
let path = parts.get(1).copied().unwrap_or_default();
match (method, path) {
("HEAD", "/file") => {
write_response(
&mut stream,
"200 OK",
&[("Content-Length", download.len().to_string())],
&[],
)
.await?;
}
("GET", "/file") => {
let range = header_value(&headers, "range").unwrap_or_else(|| "bytes=0-".to_string());
let (start, end) = parse_range(&range, download.len() as u64);
let bytes = &download[start as usize..=end as usize];
write_response(
&mut stream,
"206 Partial Content",
&[
("Content-Length", bytes.len().to_string()),
(
"Content-Range",
format!("bytes {}-{}/{}", start, end, download.len()),
),
],
bytes,
)
.await?;
}
("PUT", "/upload") => {
let offset = header_value(&headers, "x-offset")
.and_then(|v| v.parse::<usize>().ok())
.unwrap_or(0);
let mut target = upload.lock().await;
let end = offset + body.len();
if end <= target.len() {
target[offset..end].copy_from_slice(&body);
write_response(
&mut stream,
"200 OK",
&[("Content-Length", "0".to_string())],
&[],
)
.await?;
} else {
write_response(
&mut stream,
"416 Range Not Satisfiable",
&[("Content-Length", "0".to_string())],
&[],
)
.await?;
}
}
_ => {
write_response(
&mut stream,
"404 Not Found",
&[("Content-Length", "0".to_string())],
&[],
)
.await?;
}
}
Ok(())
}
fn find_header_end(buf: &[u8]) -> Option<usize> {
buf.windows(4).position(|w| w == b"\r\n\r\n")
}
fn header_value(headers: &[(String, String)], name: &str) -> Option<String> {
headers
.iter()
.find(|(k, _)| k == name)
.map(|(_, v)| v.clone())
}
fn parse_range(value: &str, total: u64) -> (u64, u64) {
let spec = value.strip_prefix("bytes=").unwrap_or("0-");
let (start, end) = spec.split_once('-').unwrap_or(("0", ""));
let start = start
.parse::<u64>()
.unwrap_or(0)
.min(total.saturating_sub(1));
let end = if end.is_empty() {
total.saturating_sub(1)
} else {
end.parse::<u64>().unwrap_or(total.saturating_sub(1))
}
.min(total.saturating_sub(1));
(start, end)
}
async fn write_response(
stream: &mut TcpStream,
status: &str,
headers: &[(&str, String)],
body: &[u8],
) -> AnyResult<()> {
let mut response = format!("HTTP/1.1 {status}\r\nConnection: close\r\n");
for (k, v) in headers {
response.push_str(k);
response.push_str(": ");
response.push_str(v);
response.push_str("\r\n");
}
response.push_str("\r\n");
stream.write_all(response.as_bytes()).await?;
if !body.is_empty() {
stream.write_all(body).await?;
}
Ok(())
}
#[tokio::main]
async fn main() -> AnyResult<()> {
let upload_path = temp_path("rusty_cat_http_upload_5mb.bin");
let download_path = temp_path("rusty_cat_http_download_5mb.bin");
remove_if_exists(&upload_path)?;
remove_if_exists(&download_path)?;
make_file(&upload_path, FIVE_MB)?;
let server = start_server(std::fs::read(&upload_path)?).await?;
let client = MeowClient::new(config()?);
let upload_protocol = BinaryUpload {
url: format!("{}/upload", server.addr),
};
let upload_task = UploadPounceBuilder::new("http-upload.bin", &upload_path, ONE_MB)
.with_url(format!("{}/upload", server.addr))
.with_method(Method::PUT)
.with_breakpoint_upload(Arc::new(upload_protocol))
.build()?;
run_task(&client, upload_task, "http upload").await?;
let download_task = DownloadPounceBuilder::new(
"http-download.bin",
&download_path,
ONE_MB,
format!("{}/file", server.addr),
)
.build();
run_task(&client, download_task, "http download").await?;
client.close().await?;
drop(server);
remove_if_exists(&upload_path)?;
remove_if_exists(&download_path)?;
Ok(())
}