Skip to content

Conversation

jollySleeper
Copy link

This change fixes HTTP range request handling issues in the piped-proxy, ensuring proper partial content responses and Content-Range headers are returned when clients request specific byte ranges from video streams. This will fix playback issues on LibreTube (libre-tube/LibreTube#7463)

Problem Solved

The proxy was not properly handling HTTP range requests, causing several issues:

  1. Incorrect Status Codes: Range requests were returning 200 OK instead of the correct 206 Partial Content status
  2. Missing Content-Range Headers: Responses lacked proper Content-Range headers required for partial content
  3. Malformed Range Headers: UMP streams had incorrect Content-Range header formatting
  4. Range Truncation Issues: YouTube sometimes truncates ranges (start > end), which wasn't handled properly

Changes Made

New Range Request Handling Infrastructure

Added new structs and functions in src/main.rs:

struct RangeRequest {
    start: u64,
    end: u64,
    total_size: u64,
}
  • parse_range(): Parses range strings (e.g., "0-1023") into structured RangeRequest objects with proper bounds checking
  • handle_range_response_correction(): Main correction logic that detects range requests and converts 200 OK responses to 206 Partial Content
  • apply_range_headers(): Applies proper HTTP headers for partial content responses

Core Fixes

  1. Range Response Correction: Added call to handle_range_response_correction() after response building to fix status codes and headers
  2. Content-Range Header Fix: Improved UMP stream handling with properly formatted Content-Range headers
  3. Range Truncation Handling: Special logic for when YouTube truncates ranges (start > end positions)
  4. Bounds Validation: Ensures range requests don't exceed content length

Technical Details

  • Status Code Conversion: Automatically converts 200 OK to 206 Partial Content for range requests
  • Header Formatting: Generates correct Content-Range: bytes start-end/total headers
  • Content-Length Calculation: Properly calculates actual content length for partial responses
  • Error Handling: Graceful fallback when range parsing fails or headers are malformed

Benefits

  • HTTP Compliance: Proper adherence to HTTP range request specifications
  • Client Compatibility: Better support for video players that rely on range requests
  • Bandwidth Efficiency: Enables proper resumable downloads and seeking
  • UMP Stream Support: Correct partial content handling for YouTube's UMP streams
  • Robust Parsing: Handles edge cases like truncated ranges and malformed inputs

Testing

The changes include comprehensive error handling and validation:

  • Invalid range formats are rejected gracefully
  • Missing content-length headers are handled
  • Range bounds are validated against content size
  • YouTube-specific range truncation scenarios are covered

This fix ensures the proxy correctly handles partial content requests, improving compatibility with media players and enabling features like video seeking and resumable downloads.


LLM's help were taken to solve this issue 😅

@jollySleeper jollySleeper changed the title fix: add proper partial content response handling Add proper partial content response handling Sep 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant