(
state: Arc<AppState>,
url: Url,
metadata: bool,
range: Option<(u64, Option<u64>)>,
client_ip_addr: IpAddr,
)
| 475 | |
| 476 | #[instrument(skip_all)] |
| 477 | async fn stream( |
| 478 | state: Arc<AppState>, |
| 479 | url: Url, |
| 480 | metadata: bool, |
| 481 | range: Option<(u64, Option<u64>)>, |
| 482 | client_ip_addr: IpAddr, |
| 483 | ) -> Response { |
| 484 | let send_headers = send_headers(range); |
| 485 | |
| 486 | let client_response = if metadata { |
| 487 | info!("🍍 start stream -> HEAD {}", url.as_str()); |
| 488 | state.client.head(url).headers(send_headers).send().await |
| 489 | } else { |
| 490 | info!("🍍 start stream -> GET {}", url.as_str()); |
| 491 | state.client.get(url).headers(send_headers).send().await |
| 492 | }; |
| 493 | |
| 494 | // Handle this error properly. Shortcut return a 500? |
| 495 | let client_response = match client_response { |
| 496 | Ok(cr) => cr, |
| 497 | Err(e) => { |
| 498 | error!(?e, "Error handling client response"); |
| 499 | return (StatusCode::INTERNAL_SERVER_ERROR).into_response(); |
| 500 | } |
| 501 | }; |
| 502 | |
| 503 | let headers = filter_headers(client_response.headers(), metadata); |
| 504 | |
| 505 | // Filter the headers |
| 506 | let status = client_response.status(); |
| 507 | |
| 508 | if metadata { |
| 509 | (status, headers).into_response() |
| 510 | } else { |
| 511 | // When streaming, even if a range was specified, the content-length |
| 512 | // correctly represents the amount of content we are about to recieve. |
| 513 | let cnt_amt = headers |
| 514 | .get("content-length") |
| 515 | .and_then(|hk| hk.to_str().ok().and_then(|i| usize::from_str(i).ok())) |
| 516 | .unwrap_or(0); |
| 517 | |
| 518 | trace_client_req_size(state.daily_quota.as_ref(), client_ip_addr, cnt_amt).await; |
| 519 | |
| 520 | // let stream = client_response.bytes_stream(); |
| 521 | let buffered_client_stream = BufferedStream::new(client_response.bytes_stream()); |
| 522 | let body = Body::from_stream(buffered_client_stream); |
| 523 | |
| 524 | (status, headers, body).into_response() |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | #[instrument(skip_all)] |
| 529 | async fn miss( |
no test coverage detected