fix: resolve binary file upload errors in non-blocking I/O#55
Merged
jos-felipe merged 1 commit intomainfrom Aug 23, 2025
Merged
fix: resolve binary file upload errors in non-blocking I/O#55jos-felipe merged 1 commit intomainfrom
jos-felipe merged 1 commit intomainfrom
Conversation
## Problem Binary file uploads were failing with "Empty reply from server" error, particularly for files larger than 4KB. The issue affected: - Large binary files (images, executables, archives) - Files sent in multiple TCP packets - Non-text files with binary content ## Root Cause The server incorrectly handled non-blocking I/O when HTTP request data arrived in multiple packets: 1. Headers received in first packet → parsed successfully 2. Body data arrives in subsequent packets 3. recv() returns 0 (no data available yet) 4. Server incorrectly treats this as "connection closed" 5. Connection terminated prematurely → upload fails ## Solution **Enhanced Connection State Management:** - Added `_connectionError` flag to track real connection issues - Added `hasConnectionError()` method to distinguish between: - Actual connection closure - Temporary data unavailability **Improved Non-blocking I/O Handling:** - Smart logic for recv() == 0 based on parsing state - Keep connections open when expecting more body data - Only close on real errors or at request boundaries **Additional Improvements:** - Enhanced multipart boundary parsing robustness - Better filename extraction with security validation - Upload directory existence validation - Consistent redirect to success page ## Testing ✅ Small binary files (< 4KB): Work correctly ✅ Large binary files (10KB+): Now handle multi-packet uploads ✅ PNG/image files: Upload with perfect integrity ✅ Text files: Maintain backward compatibility ✅ MD5 verification: All files preserve binary integrity
Collaborator
|
Accepted. Great work on verifying if the connection is complete! |
Adedayo-Sanni
approved these changes
Aug 23, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Problem Description
Binary file uploads were failing, particularly for files larger than 4KB that get sent in multiple TCP packets. The server was incorrectly treating temporary data unavailability as connection closure, terminating uploads prematurely.
Affected scenarios:
Root Cause Analysis
The issue occurred in the non-blocking I/O handling:
recv()returns 0 (no data available yet)Technical Solution
Enhanced Connection State Management
_connectionErrorflag toHttpRequestclasshasConnectionError()method to distinguish between:Improved Non-blocking I/O Logic
recv() == 0based on current parsing stateAdditional Improvements
Files Changed
include/HttpRequest.hpp: Added connection error trackingsrc/http/HttpRequest.cpp: Enhanced I/O handling + upload improvementssrc/server/Server.cpp: Improved connection management logicTesting Results
All test scenarios now pass with perfect binary integrity:
✅ Small binary files (< 4KB): Work correctly
✅ Large binary files (10KB+): Handle multi-packet uploads successfully
✅ PNG/image files: Upload with perfect integrity (MD5 verified)
✅ Text files: Maintain backward compatibility
✅ Security: Path traversal protection and directory validation
Test Plan
Impact
This fix resolves a fundamental issue with binary file uploads while enhancing overall upload security and robustness.