morpheus_network/state/http.rs
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 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
//! HTTP download state machine.
//!
//! Non-blocking HTTP GET with streaming support for ISO downloads.
//!
//! # States
//! ```text
//! Init → Resolving → Connecting → SendingRequest → ReceivingHeaders → ReceivingBody → Done
//! ↓ ↓ ↓ ↓ ↓ ↓
//! Failed Failed Failed Failed Failed Failed
//! ```
//!
//! # Architecture
//!
//! This state machine composes DNS and TCP state machines:
//! - `DnsResolveState` for hostname resolution
//! - `TcpConnState` for connection establishment
//!
//! The HTTP layer handles request/response framing on top of TCP.
//!
//! # Streaming Mode
//!
//! For large downloads (ISOs), data is passed to a callback function
//! as it arrives, rather than buffering the entire response.
//!
//! # Reference
//! NETWORK_IMPL_GUIDE.md §5.5
use alloc::string::{String, ToString};
use alloc::vec;
use alloc::vec::Vec;
use core::net::Ipv4Addr;
use super::dns::{resolve_without_dns, DnsError, DnsResolveState};
use super::tcp::{TcpConnState, TcpError, TcpSocketState};
use super::{StateError, StepResult, TscTimestamp};
use crate::http::Headers;
use crate::url::Url;
// ═══════════════════════════════════════════════════════════════════════════
// HTTP ERROR
// ═══════════════════════════════════════════════════════════════════════════
/// HTTP-specific errors.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum HttpError {
/// Invalid URL
InvalidUrl,
/// DNS resolution failed
DnsError(DnsError),
/// TCP connection failed
TcpError(TcpError),
/// Send timeout
SendTimeout,
/// Receive timeout
ReceiveTimeout,
/// HTTP status error (non-2xx)
HttpStatus { code: u16, reason: String },
/// Invalid HTTP response
InvalidResponse,
/// Response too large
ResponseTooLarge,
/// Connection closed unexpectedly
ConnectionClosed,
/// HTTPS not supported
HttpsNotSupported,
}
impl From<DnsError> for HttpError {
fn from(e: DnsError) -> Self {
HttpError::DnsError(e)
}
}
impl From<TcpError> for HttpError {
fn from(e: TcpError) -> Self {
HttpError::TcpError(e)
}
}
impl From<HttpError> for StateError {
fn from(e: HttpError) -> Self {
match e {
HttpError::DnsError(_) => StateError::DnsError,
HttpError::TcpError(TcpError::ConnectTimeout) => StateError::Timeout,
HttpError::TcpError(TcpError::ConnectionRefused) => StateError::ConnectionRefused,
HttpError::TcpError(TcpError::ConnectionReset) => StateError::ConnectionReset,
HttpError::TcpError(_) => StateError::ConnectionFailed,
HttpError::SendTimeout | HttpError::ReceiveTimeout => StateError::Timeout,
HttpError::HttpStatus { code, .. } => StateError::HttpStatus(code),
_ => StateError::HttpError,
}
}
}
// ═══════════════════════════════════════════════════════════════════════════
// HTTP RESPONSE INFO
// ═══════════════════════════════════════════════════════════════════════════
/// Information extracted from HTTP response headers.
#[derive(Debug, Clone)]
pub struct HttpResponseInfo {
/// HTTP status code (e.g., 200, 404)
pub status_code: u16,
/// Reason phrase (e.g., "OK", "Not Found")
pub reason: String,
/// Content-Length if provided
pub content_length: Option<usize>,
/// Content-Type if provided
pub content_type: Option<String>,
/// Whether response uses chunked transfer encoding
pub chunked: bool,
/// Full headers
pub headers: Headers,
}
impl HttpResponseInfo {
/// Check if response indicates success (2xx).
pub fn is_success(&self) -> bool {
(200..300).contains(&self.status_code)
}
/// Check if response indicates redirect (3xx).
pub fn is_redirect(&self) -> bool {
(300..400).contains(&self.status_code)
}
}
// ═══════════════════════════════════════════════════════════════════════════
// HTTP DOWNLOAD PROGRESS
// ═══════════════════════════════════════════════════════════════════════════
/// Download progress information.
#[derive(Debug, Clone, Copy)]
pub struct HttpProgress {
/// Bytes received so far
pub received: usize,
/// Total expected bytes (if Content-Length known)
pub total: Option<usize>,
}
impl HttpProgress {
/// Calculate percentage complete (0-100).
pub fn percent(&self) -> Option<u8> {
self.total.map(|t| {
if t == 0 {
100
} else {
((self.received as u64 * 100) / t as u64) as u8
}
})
}
}
// ═══════════════════════════════════════════════════════════════════════════
// HEADER PARSING STATE
// ═══════════════════════════════════════════════════════════════════════════
/// Internal state for accumulating headers.
#[derive(Debug)]
pub struct HeaderAccumulator {
/// Raw header data
buffer: Vec<u8>,
/// Maximum header size (prevent DoS)
max_size: usize,
}
impl HeaderAccumulator {
const DEFAULT_MAX_SIZE: usize = 16 * 1024; // 16KB max headers
fn new() -> Self {
Self {
buffer: Vec::with_capacity(4096),
max_size: Self::DEFAULT_MAX_SIZE,
}
}
/// Append data to buffer.
/// Returns true if headers complete (\r\n\r\n found).
fn append(&mut self, data: &[u8]) -> Result<bool, HttpError> {
if self.buffer.len() + data.len() > self.max_size {
return Err(HttpError::ResponseTooLarge);
}
self.buffer.extend_from_slice(data);
// Check for end of headers: \r\n\r\n
Ok(self.find_header_end().is_some())
}
/// Find position of header/body separator.
fn find_header_end(&self) -> Option<usize> {
self.buffer.windows(4).position(|w| w == b"\r\n\r\n")
}
/// Parse headers and return body data.
fn parse(&self) -> Result<(HttpResponseInfo, &[u8]), HttpError> {
let sep_pos = self.find_header_end().ok_or(HttpError::InvalidResponse)?;
let header_bytes = &self.buffer[..sep_pos];
let body_bytes = &self.buffer[sep_pos + 4..];
let info = Self::parse_headers(header_bytes)?;
Ok((info, body_bytes))
}
/// Parse HTTP response headers.
fn parse_headers(data: &[u8]) -> Result<HttpResponseInfo, HttpError> {
let header_str = core::str::from_utf8(data).map_err(|_| HttpError::InvalidResponse)?;
let mut lines = header_str.lines();
// Parse status line: "HTTP/1.1 200 OK"
let status_line = lines.next().ok_or(HttpError::InvalidResponse)?;
let (status_code, reason) = Self::parse_status_line(status_line)?;
// Parse headers
let mut headers = Headers::new();
for line in lines {
if line.is_empty() {
break;
}
if let Some((name, value)) = line.split_once(':') {
headers.set(name.trim(), value.trim());
}
}
// Extract key header values
let content_length = headers.get("Content-Length").and_then(|v| v.parse().ok());
let content_type = headers.get("Content-Type").map(|v| v.to_string());
let chunked = headers
.get("Transfer-Encoding")
.map(|v| v.eq_ignore_ascii_case("chunked"))
.unwrap_or(false);
Ok(HttpResponseInfo {
status_code,
reason,
content_length,
content_type,
chunked,
headers,
})
}
/// Parse HTTP status line.
fn parse_status_line(line: &str) -> Result<(u16, String), HttpError> {
// "HTTP/1.1 200 OK"
let mut parts = line.split_whitespace();
// HTTP version
let _version = parts.next().ok_or(HttpError::InvalidResponse)?;
// Status code
let code_str = parts.next().ok_or(HttpError::InvalidResponse)?;
let code = code_str.parse().map_err(|_| HttpError::InvalidResponse)?;
// Reason phrase (rest of line)
let reason: String = parts.collect::<Vec<_>>().join(" ");
let reason = if reason.is_empty() {
Self::default_reason(code).to_string()
} else {
reason
};
Ok((code, reason))
}
/// Default reason phrase for status code.
fn default_reason(code: u16) -> &'static str {
match code {
200 => "OK",
201 => "Created",
204 => "No Content",
206 => "Partial Content",
301 => "Moved Permanently",
302 => "Found",
304 => "Not Modified",
400 => "Bad Request",
401 => "Unauthorized",
403 => "Forbidden",
404 => "Not Found",
500 => "Internal Server Error",
502 => "Bad Gateway",
503 => "Service Unavailable",
_ => "Unknown",
}
}
}
// ═══════════════════════════════════════════════════════════════════════════
// HTTP DOWNLOAD STATE MACHINE
// ═══════════════════════════════════════════════════════════════════════════
/// HTTP download state machine.
///
/// Orchestrates DNS resolution → TCP connection → HTTP request/response.
/// Supports streaming for large downloads.
#[derive(Debug)]
pub enum HttpDownloadState {
/// Initial state with target URL.
Init { url: Url },
/// Resolving hostname to IP address.
Resolving {
/// DNS state machine
dns: DnsResolveState,
/// Target host
host: String,
/// Target port
port: u16,
/// Request path
path: String,
/// Query string (if any)
query: Option<String>,
},
/// Connecting to server.
Connecting {
/// TCP state machine
tcp: TcpConnState,
/// Resolved IP
ip: Ipv4Addr,
/// Target port
port: u16,
/// Host header value
host_header: String,
/// Request path + query
request_uri: String,
},
/// Sending HTTP request.
SendingRequest {
/// Socket handle
socket_handle: usize,
/// Serialized HTTP request
request: Vec<u8>,
/// Bytes sent so far
sent: usize,
/// When send started
start_tsc: TscTimestamp,
},
/// Receiving HTTP response headers.
ReceivingHeaders {
/// Socket handle
socket_handle: usize,
/// Header accumulator
accumulator: HeaderAccumulator,
/// When receive started
start_tsc: TscTimestamp,
},
/// Receiving HTTP response body.
ReceivingBody {
/// Socket handle
socket_handle: usize,
/// Response info from headers
response_info: HttpResponseInfo,
/// Bytes received so far
received: usize,
/// When body receive started
start_tsc: TscTimestamp,
/// Last activity time (for idle timeout)
last_activity_tsc: TscTimestamp,
},
/// Download complete.
Done {
/// Response info
response_info: HttpResponseInfo,
/// Total bytes received (body only)
total_bytes: usize,
},
/// Download failed.
Failed {
/// Error details
error: HttpError,
},
}
impl HttpDownloadState {
/// Create new HTTP download state machine.
pub fn new(url: Url) -> Result<Self, HttpError> {
// HTTPS not supported in this bare-metal implementation
if url.is_https() {
return Err(HttpError::HttpsNotSupported);
}
Ok(HttpDownloadState::Init { url })
}
/// Start the download.
///
/// Transitions from Init to Resolving (or Connecting if IP known).
pub fn start(&mut self, _now_tsc: u64) {
if let HttpDownloadState::Init { url } = self {
let host = url.host.clone();
let port = url.port_or_default();
let path = url.path.clone();
let query = url.query.clone();
let host_header = url.host_header();
let request_uri = url.request_uri();
// Try to resolve without DNS first (IP or hardcoded)
if let Some(ip) = resolve_without_dns(&host) {
// Skip DNS, go directly to connecting
let tcp = TcpConnState::new();
// Note: actual connection initiation happens in step()
*self = HttpDownloadState::Connecting {
tcp,
ip,
port,
host_header,
request_uri,
};
} else {
// Need DNS resolution
let dns = DnsResolveState::new();
// Note: actual DNS query initiation happens in step()
*self = HttpDownloadState::Resolving {
dns,
host,
port,
path,
query,
};
}
}
}
/// Step the state machine.
///
/// # Arguments
/// - `dns_result`: Result from smoltcp DNS query (if resolving)
/// - `tcp_state`: Current TCP socket state (if connected)
/// - `recv_data`: Data received from socket (if any)
/// - `can_send`: Whether socket can accept more data
/// - `now_tsc`: Current TSC value
/// - `timeouts`: Timeout configuration (DNS, TCP, HTTP timeouts)
///
/// # Returns
/// - `Pending`: Still in progress
/// - `Done`: Download complete, call `result()` for info
/// - `Timeout`: Operation timed out
/// - `Failed`: Operation failed, call `error()` for details
///
/// # Callback
///
/// When `recv_data` contains body data during ReceivingBody state,
/// the caller should pass that data to the storage layer.
pub fn step(
&mut self,
dns_result: Result<Option<Ipv4Addr>, ()>,
tcp_state: TcpSocketState,
recv_data: Option<&[u8]>,
_can_send: bool,
now_tsc: u64,
dns_timeout: u64,
tcp_timeout: u64,
http_send_timeout: u64,
http_recv_timeout: u64,
) -> StepResult {
// Take ownership for state transitions
let current = core::mem::replace(
self,
HttpDownloadState::Init {
url: Url {
scheme: crate::url::parser::Scheme::Http,
host: String::new(),
port: None,
path: String::new(),
query: None,
},
},
);
let (new_state, result) = self.step_inner(
current,
dns_result,
tcp_state,
recv_data,
_can_send,
now_tsc,
dns_timeout,
tcp_timeout,
http_send_timeout,
http_recv_timeout,
);
*self = new_state;
result
}
/// Internal step implementation.
fn step_inner(
&self,
current: HttpDownloadState,
dns_result: Result<Option<Ipv4Addr>, ()>,
tcp_state: TcpSocketState,
recv_data: Option<&[u8]>,
_can_send: bool,
now_tsc: u64,
dns_timeout: u64,
tcp_timeout: u64,
http_send_timeout: u64,
http_recv_timeout: u64,
) -> (HttpDownloadState, StepResult) {
match current {
HttpDownloadState::Init { url } => {
// Not started yet
(HttpDownloadState::Init { url }, StepResult::Pending)
}
HttpDownloadState::Resolving {
mut dns,
host,
port,
path,
query,
} => {
// Step DNS state machine
let result = dns.step(dns_result, now_tsc, dns_timeout);
match result {
StepResult::Done => {
// DNS resolved, start TCP connection
let ip = dns.ip().unwrap();
let host_header = if port != 80 {
alloc::format!("{}:{}", host, port)
} else {
host.clone()
};
let request_uri = match &query {
Some(q) => alloc::format!("{}?{}", path, q),
None => path.clone(),
};
(
HttpDownloadState::Connecting {
tcp: TcpConnState::new(),
ip,
port,
host_header,
request_uri,
},
StepResult::Pending,
)
}
StepResult::Pending => (
HttpDownloadState::Resolving {
dns,
host,
port,
path,
query,
},
StepResult::Pending,
),
StepResult::Timeout => (
HttpDownloadState::Failed {
error: HttpError::DnsError(DnsError::Timeout),
},
StepResult::Timeout,
),
StepResult::Failed => {
let error = dns.error().unwrap_or(DnsError::QueryFailed);
(
HttpDownloadState::Failed {
error: HttpError::DnsError(error),
},
StepResult::Failed,
)
}
}
}
HttpDownloadState::Connecting {
mut tcp,
ip,
port,
host_header,
request_uri,
} => {
// Step TCP state machine
let result = tcp.step(tcp_state, now_tsc, tcp_timeout);
match result {
StepResult::Done => {
// Connected! Build HTTP request
let socket_handle = tcp.socket_handle().unwrap();
let request = Self::build_request(&host_header, &request_uri);
(
HttpDownloadState::SendingRequest {
socket_handle,
request,
sent: 0,
start_tsc: TscTimestamp::new(now_tsc),
},
StepResult::Pending,
)
}
StepResult::Pending => (
HttpDownloadState::Connecting {
tcp,
ip,
port,
host_header,
request_uri,
},
StepResult::Pending,
),
StepResult::Timeout => (
HttpDownloadState::Failed {
error: HttpError::TcpError(TcpError::ConnectTimeout),
},
StepResult::Timeout,
),
StepResult::Failed => {
let error = tcp.error().unwrap_or(TcpError::ConnectionRefused);
(
HttpDownloadState::Failed {
error: HttpError::TcpError(error),
},
StepResult::Failed,
)
}
}
}
HttpDownloadState::SendingRequest {
socket_handle,
request,
sent,
start_tsc,
} => {
// Check timeout
if start_tsc.is_expired(now_tsc, http_send_timeout) {
return (
HttpDownloadState::Failed {
error: HttpError::SendTimeout,
},
StepResult::Timeout,
);
}
// Check connection status
if tcp_state == TcpSocketState::Closed {
return (
HttpDownloadState::Failed {
error: HttpError::ConnectionClosed,
},
StepResult::Failed,
);
}
// Sending is tracked externally, we just track progress
if sent >= request.len() {
// Request fully sent, start receiving
(
HttpDownloadState::ReceivingHeaders {
socket_handle,
accumulator: HeaderAccumulator::new(),
start_tsc: TscTimestamp::new(now_tsc),
},
StepResult::Pending,
)
} else {
// Still sending
(
HttpDownloadState::SendingRequest {
socket_handle,
request,
sent,
start_tsc,
},
StepResult::Pending,
)
}
}
HttpDownloadState::ReceivingHeaders {
socket_handle,
mut accumulator,
start_tsc,
} => {
// Check timeout
if start_tsc.is_expired(now_tsc, http_recv_timeout) {
return (
HttpDownloadState::Failed {
error: HttpError::ReceiveTimeout,
},
StepResult::Timeout,
);
}
// Check connection status
if tcp_state == TcpSocketState::Closed {
return (
HttpDownloadState::Failed {
error: HttpError::ConnectionClosed,
},
StepResult::Failed,
);
}
// Process received data
if let Some(data) = recv_data {
match accumulator.append(data) {
Ok(true) => {
// Headers complete, parse them
match accumulator.parse() {
Ok((response_info, body_data)) => {
// Check for HTTP error status
if !response_info.is_success() && !response_info.is_redirect() {
return (
HttpDownloadState::Failed {
error: HttpError::HttpStatus {
code: response_info.status_code,
reason: response_info.reason.clone(),
},
},
StepResult::Failed,
);
}
// Start body reception
// Note: body_data may contain initial body bytes
let initial_received = body_data.len();
(
HttpDownloadState::ReceivingBody {
socket_handle,
response_info,
received: initial_received,
start_tsc: TscTimestamp::new(now_tsc),
last_activity_tsc: TscTimestamp::new(now_tsc),
},
StepResult::Pending,
)
}
Err(e) => {
(HttpDownloadState::Failed { error: e }, StepResult::Failed)
}
}
}
Ok(false) => {
// Headers not complete yet
(
HttpDownloadState::ReceivingHeaders {
socket_handle,
accumulator,
start_tsc,
},
StepResult::Pending,
)
}
Err(e) => (HttpDownloadState::Failed { error: e }, StepResult::Failed),
}
} else {
// No data received yet
(
HttpDownloadState::ReceivingHeaders {
socket_handle,
accumulator,
start_tsc,
},
StepResult::Pending,
)
}
}
HttpDownloadState::ReceivingBody {
socket_handle,
response_info,
received,
start_tsc,
last_activity_tsc,
} => {
// Check for idle timeout (no data for too long)
if last_activity_tsc.is_expired(now_tsc, http_recv_timeout) {
return (
HttpDownloadState::Failed {
error: HttpError::ReceiveTimeout,
},
StepResult::Timeout,
);
}
// Check if complete based on Content-Length
if let Some(content_length) = response_info.content_length {
if received >= content_length {
return (
HttpDownloadState::Done {
response_info,
total_bytes: received,
},
StepResult::Done,
);
}
}
// Check connection status
if tcp_state == TcpSocketState::Closed {
// Connection closed - check if we're done
if response_info.content_length.is_none() {
// No Content-Length, connection close means done
return (
HttpDownloadState::Done {
response_info,
total_bytes: received,
},
StepResult::Done,
);
} else {
// Had Content-Length but didn't receive it all
return (
HttpDownloadState::Failed {
error: HttpError::ConnectionClosed,
},
StepResult::Failed,
);
}
}
// Process received data
let new_received = if let Some(data) = recv_data {
received + data.len()
} else {
received
};
let new_last_activity = if recv_data.is_some() {
TscTimestamp::new(now_tsc)
} else {
last_activity_tsc
};
(
HttpDownloadState::ReceivingBody {
socket_handle,
response_info,
received: new_received,
start_tsc,
last_activity_tsc: new_last_activity,
},
StepResult::Pending,
)
}
HttpDownloadState::Done {
response_info,
total_bytes,
} => (
HttpDownloadState::Done {
response_info,
total_bytes,
},
StepResult::Done,
),
HttpDownloadState::Failed { error } => {
let result = match &error {
HttpError::SendTimeout
| HttpError::ReceiveTimeout
| HttpError::DnsError(DnsError::Timeout)
| HttpError::TcpError(TcpError::ConnectTimeout)
| HttpError::TcpError(TcpError::CloseTimeout) => StepResult::Timeout,
_ => StepResult::Failed,
};
(HttpDownloadState::Failed { error }, result)
}
}
}
/// Build HTTP GET request.
fn build_request(host: &str, request_uri: &str) -> Vec<u8> {
// Build HTTP/1.1 GET request
let request = alloc::format!(
"GET {} HTTP/1.1\r\n\
Host: {}\r\n\
User-Agent: MorpheusX/1.0\r\n\
Accept: */*\r\n\
Connection: close\r\n\
\r\n",
request_uri,
host
);
request.into_bytes()
}
/// Get the request bytes to send (for SendingRequest state).
pub fn request_bytes(&self) -> Option<(&[u8], usize)> {
if let HttpDownloadState::SendingRequest { request, sent, .. } = self {
Some((&request[*sent..], *sent))
} else {
None
}
}
/// Update bytes sent (for SendingRequest state).
pub fn mark_sent(&mut self, additional: usize) {
if let HttpDownloadState::SendingRequest { sent, .. } = self {
*sent += additional;
}
}
/// Get socket handle (if connected).
pub fn socket_handle(&self) -> Option<usize> {
match self {
HttpDownloadState::SendingRequest { socket_handle, .. }
| HttpDownloadState::ReceivingHeaders { socket_handle, .. }
| HttpDownloadState::ReceivingBody { socket_handle, .. } => Some(*socket_handle),
HttpDownloadState::Connecting { tcp, .. } => tcp.socket_handle(),
_ => None,
}
}
/// Get download progress.
pub fn progress(&self) -> Option<HttpProgress> {
if let HttpDownloadState::ReceivingBody {
response_info,
received,
..
} = self
{
Some(HttpProgress {
received: *received,
total: response_info.content_length,
})
} else {
None
}
}
/// Get response info (if headers received).
pub fn response_info(&self) -> Option<&HttpResponseInfo> {
match self {
HttpDownloadState::ReceivingBody { response_info, .. }
| HttpDownloadState::Done { response_info, .. } => Some(response_info),
_ => None,
}
}
/// Get result (if complete).
pub fn result(&self) -> Option<(&HttpResponseInfo, usize)> {
if let HttpDownloadState::Done {
response_info,
total_bytes,
} = self
{
Some((response_info, *total_bytes))
} else {
None
}
}
/// Get error (if failed).
pub fn error(&self) -> Option<&HttpError> {
if let HttpDownloadState::Failed { error } = self {
Some(error)
} else {
None
}
}
/// Check if download is complete (success or failure).
pub fn is_terminal(&self) -> bool {
matches!(
self,
HttpDownloadState::Done { .. } | HttpDownloadState::Failed { .. }
)
}
/// Check if download is in progress.
pub fn is_active(&self) -> bool {
!matches!(
self,
HttpDownloadState::Init { .. }
| HttpDownloadState::Done { .. }
| HttpDownloadState::Failed { .. }
)
}
}
impl Default for HttpDownloadState {
fn default() -> Self {
HttpDownloadState::Init {
url: Url {
scheme: crate::url::parser::Scheme::Http,
host: String::new(),
port: None,
path: String::new(),
query: None,
},
}
}
}