Skip to content

fix: correct Content-Range end byte in chunked upload (RFC 7233) - #212

Closed
chrisdebian wants to merge 2 commits into
mapilio:mainfrom
chrisdebian:fix/upload-content-range-header
Closed

chrisdebian wants to merge 2 commits into
mapilio:mainfrom
chrisdebian:fix/upload-content-range-header

Conversation

@chrisdebian

Copy link
Copy Markdown
Contributor

Summary

  • Fixes MapSyncer issue Update README.md #87 — Invalid "Content-Range" provided error when uploading large tracks (>64 MB, i.e. more than one chunk).
  • The upload loop in upload_manager.py was sending bytes={offset}-{entity_size}/{entity_size} for every chunk. RFC 7233 requires the end value to be the last byte of the current chunk (0-indexed, inclusive), not the total file size. For multi-chunk uploads this meant every intermediate chunk carried an invalid range, causing the server to reject it.
  • Adds two unit tests: single-chunk and multi-chunk Content-Range header construction.

Root cause

# Before (wrong — end is total size, not chunk end):
"content-range": f"bytes={offset}-{self.entity_size}/{self.entity_size}"

# After (correct — end is last byte of this chunk):
chunk_end = (offset + len(chunk) - 1) if chunk else self.entity_size
"content-range": f"bytes={offset}-{chunk_end}/{self.entity_size}"

Related issues

Test plan

  • pytest tests/test_upload_manager.py — 2 new tests pass
  • Full suite: pytest — 97 tests pass
  • On-device: upload a sequence larger than 64 MB and confirm no Invalid Content-Range error

🤖 Generated with Claude Code

chrisdebian and others added 2 commits May 17, 2026 15:09
Hard-coded 4096x1344 values only work for one specific GoPro MAX
recording mode. Use exiftool computed-value syntax so CroppedArea
and FullPano tags are derived from actual ImageWidth/ImageHeight,
making the command work for any output resolution (e.g. 4096x2048).

CroppedAreaTopPixels = (ImageWidth/2 - ImageHeight) / 2

Closes mapilio#200
The upload loop was sending `bytes={offset}-{entity_size}/{entity_size}`
for every chunk. RFC 7233 requires the end value to be the last byte of
the *current chunk* (0-indexed, inclusive), not the total entity size.
For multi-chunk uploads this caused the server to reject intermediate
chunks with "Invalid Content-Range provided" (MapSyncer issue mapilio#87).

Fix: compute `chunk_end = offset + len(chunk) - 1` for non-empty chunks
and keep the existing sentinel value for the empty finalisation request.

Adds two unit tests covering single-chunk and multi-chunk Content-Range
header construction.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@chrisdebian

Copy link
Copy Markdown
Contributor Author

Hi,

Just flagging that this is the root cause behind the MapSyncer upload failures reported in MapSyncer #87 and #89 — users hitting 'Invalid Content-Range' on any track over 64 MB. The fix and unit tests are ready whenever you have a moment to review.

Thanks, Chris

@ozcan-durak ozcan-durak left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, Chris. I tested this against the repository image-server handler. The current client passes all six transfer cases; this patch fails the one-byte file, final remainder and resumed-final-byte cases. We need to align the client and server contract before merging. Please also separate the GoPro change underneath this commit.

headers = {
'Connection': "keep-alive",
"content-range": f"bytes={offset}-{self.entity_size}/{self.entity_size}",
"content-range": f"bytes={offset}-{chunk_end}/{self.entity_size}",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The server rejects rangeStart >= rangeEnd. For a 101-byte file with 100-byte chunks, this emits bytes=100-100/101 for the final byte and gets HTTP 400. A one-byte file and a resume with one byte remaining fail too. Please add actual transfer tests for these boundaries; the current equals-sign protocol has a separate completion request, so changing only the end offset is incompatible.

@chrisdebian

Copy link
Copy Markdown
Contributor Author

Thanks for testing this so thoroughly, Ozcan. You're right: I'd treated the header as the standard HTTP range format, but it's the upload server's own contract (the end value is the total size, and an empty final chunk completes the upload). The current client already matches it, so I'm closing this PR rather than changing both sides.

The GoPro change is still in #211 on its own, so closing this one doesn't affect it.

If it would help, I could open a small PR with tests for your six transfer cases (including the one-byte file, the final remainder and resuming with one byte left), plus a short comment in upload_manager.py explaining the header. That way nobody "fixes" it the way I did. Happy to leave it if you'd rather not.

Chris

@ozcan-durak

Copy link
Copy Markdown
Contributor

Thanks, Chris. Yes, please send the six transfer cases as a test-only PR, with the short comment explaining the server's range format. Keeping that contract covered should help avoid the same confusion later. The GoPro change can stay separate in #211.

@chrisdebian
chrisdebian deleted the fix/upload-content-range-header branch November 10, 2026 07:59
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.

2 participants