Enhanced File Streaming (Large 2GB) & MIME Type Management #1573
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
This PR introduces significant improvements to Pode's file handling capabilities and adds a comprehensive MIME type management system. The changes enable support for large file streaming (>2GB), implement HTTP range requests, and provide a robust MIME type registry with PowerShell cmdlets.
🔧 Major Changes
1. Enhanced PodeResponse.cs - Large File Streaming Support
Breaking the 2GB Barrier:
MAX_IN_MEMORY_FILE_SIZE
constant (64 MiB) to intelligently handle file bufferingWriteStreamAsync()
method for efficient streaming of large filesWriteLargeStream()
method with HTTP range request supportStreamSectionAsync()
for chunked streaming using ArrayPool for memory efficiencyNew File Handling Methods:
WriteFileAsync(string path, long[] ranges, PodeCompressionType compression)
- Async file streaming with range supportWriteFileAsync(FileSystemInfo file, long[] ranges, PodeCompressionType compression)
- Enhanced file streamingWriteByteAsync(byte[] bytes, long[] ranges, PodeCompressionType compression)
- Byte array streamingWriteStringAsync(string content, Encoding encoding, long[] ranges, PodeCompressionType compression)
- String content streamingHTTP Range Request Support:
Performance Improvements:
ArrayPool<byte>
for efficient memory management2. New PodeMimeTypes.cs - Comprehensive MIME Type Registry
Core Features:
Key Methods:
Get(string extension)
- Retrieve MIME type with fallback to "application/octet-stream"TryGet(string extension, out string contentType)
- Safe MIME type retrievalAdd(string extension, string contentType)
- Add new mappings (throws if exists)AddOrUpdate(string extension, string contentType)
- Upsert operationRemove(string extension)
- Remove mappingsContains(string extension)
- Check if mapping existsLoadFromFile(string path)
- Bulk load from Apache-style mime.types filesIsTextualMimeType(string type)
- Identify text-based content types3. PowerShell MIME Type Management (Mime.ps1)
User-Friendly Cmdlets:
🔄 Enhanced Route Pipeline Functions
Add-PodeRouteCache - Advanced HTTP Caching Configuration
This function provides comprehensive HTTP caching control for Pode routes, supporting modern caching strategies with ETag generation and fine-grained Cache-Control directives.
Key Parameters:
-Enable
: Activates caching for the route(s)-Disable
: Completely disables caching, overriding any existing settings-Visibility
: Controls cache visibility ('public'
,'private'
,'no-cache'
,'no-store'
)-MaxAge
: Setsmax-age
directive in seconds for client-side caching-SharedMaxAge
: Setss-maxage
directive for shared/proxy caches-MustRevalidate
: Forces revalidation when cache expires-Immutable
: Indicates resource will never change (useful for versioned assets)-ETagMode
: Controls ETag generation strategy:'None'
: Disables ETag generation'Auto'
: Intelligent selection ('mtime'
for static files,'hash'
for dynamic content)'Hash'
: Content-based hash ETags (CPU intensive but accurate)'Mtime'
: File modification time ETags (fast but less accurate)'Manual'
: Allows manual ETag setting via response cmdlets-WeakValidation
: Generates weak ETags (prefixed withW/
)-PassThru
: Returns modified routes for pipeline chainingPractical Examples:
Cache-Control Header Examples:
Add-PodeRouteCompression - Advanced Response Compression
This function configures response compression for Pode routes, supporting modern compression algorithms and directional control.
Key Parameters:
-Enable
: Activates compression for the route(s)-Disable
: Completely disables compression-Encoding
: Specifies compression algorithms ('gzip'
,'deflate'
,'br'
for Brotli)-Direction
: Controls compression direction:'Response'
(default): Compress outgoing responses'Request'
: Decompress incoming requests'Both'
: Compress responses and decompress requests-PassThru
: Returns modified routes for pipeline chainingPractical Examples:
Compression Priority & Browser Support:
Advanced Pipeline Combinations:
Performance Considerations:
💡 Technical Highlights
Memory Efficiency:
HTTP Compliance:
Backwards Compatibility:
🧪 Usage Examples
Large File Streaming:
MIME Type Management:
🔧 Files Changed
src/Listener/PodeResponse.cs
- Enhanced with large file streaming capabilitiessrc/Listener/PodeMimeTypes.cs
- New comprehensive MIME type registrysrc/Public/Mime.ps1
- New PowerShell cmdlets for MIME type managementsrc/Public/Routes.ps1
- Enhanced route pipeline functions🎯 Benefits
This enhancement positions Pode as a robust solution for serving large media files, supporting modern web applications, and providing enterprise-grade file streaming capabilities.