Skip to content

fix: send Content-Length on upload requests instead of chunked - #143

Merged
maddie merged 1 commit into
masterfrom
fix/upload-content-length
Aug 17, 2026
Merged

maddie merged 1 commit into
masterfrom
fix/upload-content-length

Conversation

@maddie

@maddie maddie commented Aug 17, 2026

Copy link
Copy Markdown
Collaborator

Problem

Upload wraps the payload in an io.TeeReader for counting, which hides its length from http.NewRequest, so Go sends Transfer-Encoding: chunked. Servers with hand-written HTTP parsers that never chunk-decode — librespeed/speedtest-rust — read the raw stream in fixed-size blocks instead. Once the finite payload is sent the stream does not end (the client keeps the connection open), the block fill stalls, and the server never answers. The request never completes, no replacement request is spawned, and the upload stops after a single payload: 1 MiB / 15 s default duration = 0.55 Mbps, exactly the value in #122.

Reproduced locally against a server replicating the librespeed-rs body handling: current master reports Upload rate: 0.55 Mbps; the server log shows CHUNKED: read 1048837 raw bytes, then EOF/blocked — 1024×1024 payload + 261 bytes of chunk framing, not a multiple of the 1024-byte read block, so the final block never fills.

Fix

Set uploadReq.ContentLength = int64(uploadSize) for the fixed-size payload (preallocation mode). Content-Length is the standard, universally supported form:

client encoding librespeed-rs semantics server standard Go server (~PHP/official)
chunked (before) 0.55 Mbps, stalled ~15000 Mbps
Content-Length (after) ~22000 Mbps ~23000 Mbps

--no-preallocate keeps its crypto/rand stream and stays chunked, which is safe: an endless stream keeps a raw-block reader making progress (the pre-v1.0.13 behaviour).

Verification

  • New regression test TestUploadSendsContentLength: runs Upload against a raw TCP server replicating librespeed-rs body semantics, asserts the request carries Content-Length and more than one payload is consumed. Fails without the fix (server saw Content-Length 0).
  • go build / go vet / go test ./... / go test -race ./... all pass.

Closes #122 (pending confirmation from the reporter that the rust backend now behaves).

The upload body goes through an io.TeeReader, which hides its length, so Go
switched to Transfer-Encoding: chunked. Servers with hand-written HTTP
parsers that never chunk-decode (librespeed-rs) read the raw stream in
fixed-size blocks; once the finite payload is sent the stream does not end
(the client keeps the connection open) and the block fill stalls, so the
server never answers. The request then never completes and the upload stops
after a single payload: 1 MiB / 15 s default duration = 0.55 Mbps
(#122).

Set an explicit Content-Length for the fixed-size payload, which every
server handles. The --no-preallocate stream from crypto/rand stays chunked,
which is safe: it never ends, so a raw-block reader keeps making progress.

Regression test runs Upload against a raw TCP server replicating the
librespeed-rs body handling and asserts the request carries Content-Length
and the upload consumes more than one payload. It fails without the fix
(server sees Content-Length 0).
@maddie
maddie merged commit 51fd860 into master Aug 17, 2026
@maddie
maddie deleted the fix/upload-content-length branch August 17, 2026 05:13
maddie added a commit that referenced this pull request Aug 17, 2026
#144)

#143 set Content-Length to the raw --upload-size option value, but
SetUploadSize scales it by 1024, so a default 1024 KiB payload went out as
ContentLength=1024 with a 1 MiB body. The transport rejects the mismatch
("ContentLength=1024 with Body length 1048576"), the request fails, and
the upload stalls at one payload again (1 MiB / 5 s = 1.61 Mbps).

Use the generated payload's actual length instead. The regression test now
uses the CLI's real unit semantics (1024 KiB -> 1 MiB payload) and asserts
the server sees Content-Length 1048576; the previous test passed by
accident because it fed an already-scaled uploadSize in.
@maddie maddie mentioned this pull request Aug 17, 2026
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.

新版上传有bug

1 participant