Skip to main content

check_if_range

Function check_if_range 

Source
pub fn check_if_range(
    if_range: &str,
    etag: Option<&str>,
    last_modified: Option<&str>,
) -> IfRangeResult
Expand description

Check an If-Range precondition header against a validator.

The If-Range header contains either:

  • An ETag value (e.g., "abc123")
  • A Last-Modified date (e.g., Wed, 21 Oct 2015 07:28:00 GMT)

If the If-Range value matches the current resource, the Range request should be honored (return 206 Partial Content). Otherwise, the full resource should be returned (ignore the Range header, return 200 OK).

§Arguments

  • if_range - The If-Range header value from the request
  • etag - The current ETag of the resource (if available)
  • last_modified - The current Last-Modified of the resource (if available)

§Returns

  • IfRangeResult::ServePartial if the condition is satisfied
  • IfRangeResult::ServeFull if the condition fails or no validators are available

§Example

use fastapi_http::range::{check_if_range, IfRangeResult};

// ETag match
let result = check_if_range(
    "\"abc123\"",
    Some("\"abc123\""),
    None,
);
assert_eq!(result, IfRangeResult::ServePartial);

// ETag mismatch
let result = check_if_range(
    "\"abc123\"",
    Some("\"def456\""),
    None,
);
assert_eq!(result, IfRangeResult::ServeFull);