Skip to content

Fix and refactor HTTP encoding and retry tests - #6207

Merged
chrisd8088 merged 20 commits into
git-lfs:mainfrom
chrisd8088:fix-http-encoding-range-tests
Apr 1, 2026
Merged

chrisd8088 merged 20 commits into
git-lfs:mainfrom
chrisd8088:fix-http-encoding-range-tests

Conversation

@chrisd8088

@chrisd8088 chrisd8088 commented Feb 26, 2026 •

Copy link
Copy Markdown
Member

This PR corrects, updates, and reorganizes our shell tests of Batch API and object transfer HTTP request retries and our shell tests of gzip-encoding during object transfer HTTP download requests.

These changes should make the relationships between these tests more apparent, and should provide a framework in which to add similar tests in the future, for example in PR #6196.

This PR will be most easily reviewed on a commit-by-commit basis.

Test Reorganization

We move the storage upload with compression test from the t/t-push.sh script into a new t/t-batch-storage-encoding.sh script, and rename the test to batch storage HTTP download with gzip encoding since it only tests the use of gzip compression during HTTP downloads, not uploads.

Next, we move the tests from the t/t-batch-retries.sh script and the t/t-resume-http-range.sh script into a single new t/t-batch-storage-retries.sh script, and rename these tests and the tests in both the t/t-batch-retries-ratelimit.sh script and the t/t-batch-storage-retries-ratelimit.sh script to all follow a common pattern.

Finally, we move the tests from the t/t-resume-tus.sh script to a new t/t-batch-storage-upload-tus.sh script, and rename the tests to align with the test names we've introduced in our other changes.

Test Fixes and Updates

We correct the Content-Range headers returned to several tests by our lfstest-gitserver utility so they read, for instance, bytes 10-27/28 instead of bytes 10-28/-18, as they do now.

We revise the lfstest-gitserver utility so that when it returns truncated object data to simulate an interrupted HTTP object download, it explicitly sets the Content-Length header to the full object's length. Otherwise, as occurs now, the net/http package from the Go standard library automatically inserts the Content-Length header and uses the length of the truncated data, which fails to provide a true simulation of an interrupted response. The result is that the Git LFS client understands the transfer to be complete, but assumes that the object must be corrupted on the server because the SHA-256 hash of the downloaded data does not match the OID. For this reason, several of our tests expect the Git LFS client to report an error due to an interrupted download, when in fact the client should just retry the request immediately with a Range header.

At present, the resume-http-range-retry test (which we also move and rename, per the notes above) passes trivially in all cases. The test creates a file with a .tmp extension in the .git/lfs/incomplete directory to try to simulate an interrupted object transfer download. However, since PR #3826 the files the client creates in that directory have the extension .part, not .tmp. As a consequence, the client ignores the file with the .tmp extension and simply makes a fresh, complete download of the object. We therefore update the file extension of the temporary file, and also expand the test so it will guard against similar regressions in the future.

We remove a misleading comment from the tus-upload-interrupted-resume test (which we also move and rename, per the notes above) and simplify the string used to create an object since the test does not actually depend on the object's contents at all.

We refactor the two blocks in the lfstest-gitserver utility where we parse Range request headers into a common handleRangeRequest() function, which we be able to reuse in subsequent PRs when we need to add new tests that require the utility to validate Range headers.

In addition, we try to refine many of the tests we are otherwise moving, renaming, or correcting in this PR. For instance, we revise the internal code comments in a number of the tests. We also expand the sets of messages and HTTP headers the tests look for in Git LFS command trace logs, add checks of commands' exit codes where appropriate, eliminate unnecessary log files, and in general try to make the tests as consistent with each other as possible.

@chrisd8088
chrisd8088 marked this pull request as ready for review February 26, 2026 17:42
@chrisd8088
chrisd8088 requested a review from a team as a code owner February 26, 2026 17:42
In our "lfstest-gitserver" test utility program, the storageHandler()
function is used to generate responses to HTTP requests which upload
or download individual Git LFS objects.  For both types of requests,
if the content of an object matches a specific "magic" string value
then the function simulates a set of conditions relevant for the
current shell test by returning a particular status code or checking
for a particular request header.

When handling a PUT object upload request, the object's SHA-256 hash
value as provided by the Git LFS client in the URL path is used to
determine whether the object content matches a given special or "magic"
key value.  For these types of requests, the storageHandler() function
first looks for an entry in the global oidHandlers map using the provided
OID as the key.  If an entry exists, its value is then checked against a
series of keys, and if a match is found, the PUT upload handler simulates
the relevant test conditions.

By contrast, when handling a GET object download request, the same
storageHandler() function does not refer to the oidHandlers map.
Instead, if it finds a stored object matching the OID provided by the
client, it then tries to match the object's content against each of a
series of special key strings.  Each match check is made by comparing
the length of the string and the length of the byte array containing
the object content, and then if those are equal, comparing the contents
of the string and the byte array.

This technique was first introduced to the object download handling
logic in commit c9c457c of PR git-lfs#1297
and then copied every time another special object content key was added
in later commits and PRs.

While functional, the discrepancy between the object upload and download
handling portions of the storageHandler() function somewhat obscures the
similarities between the two code segments, and has also led to some
confusion as to which special object content keys need to be defined in
the global contentHandlers array, from which the oidHandlers map is
populated.

For instance, in commit 864beb8 of
PR git-lfs#1585 the "storage-download-retry" string was added to the
contentHandlers array, but the corresponding entry in the oidHandlers
array is never utilized.

As we expect to update and correct some of the specialized handling
of object download test conditions in subsequent commits in this PR,
we first revise the storageHandler() function so that it mirrors the
design of the object upload handling section.  When responding to a
GET request, the function now checks for an entry in the oidHandlers
map, and if an entry is found, then uses its value in a "switch"
statement to search for a match against a specific string.

To make this change successful, we also need to register several
additional special object content keys in the contentHandlers array,
specifically the "status-batch-retry", "storage-download-retry-later",
and "storage-download-retry-later-no-header" strings.
In commit 44fae30 of PR git-lfs#4345 we added
the "storage upload with compression" test to our "t/t-push.sh" shell
test script in order to verify that when fetching Git LFS objects from
an HTTP service, we support gzip-encoded responses.

While this test is effective, its purpose differs from all the other
tests in the "t/t-push.sh" test script, since those are intended to
validate the action of the "git lfs push" command under various
circumstances.

The "storage upload with compression" test, by contrast, merely runs
the "git lfs push" command to establish its initial context.  It then
uses the "git lfs pull" command to verify the Git LFS client's handling
of a gzip-encoded object download.

As this test does not specifically check any aspect of the "git lfs push"
command's actions, we now extract it from the "t/t-push.sh" script into
a new test script named "t/t-batch-storage-encoding.sh".

This change provides a convenient location for any new tests we may
add in the future, should we introduce support for object downloads
encoded in other formats besides the gzip format.
In a prior commit in this PR we extracted the "storage upload with
compression" test from our "t/t-push.sh" shell test script into a
new "t/t-batch-storage-encoding.sh" script.

The original version of this test was first added in commit
44fae30 of PR git-lfs#4345, in order to
verify that the Git LFS client supports gzip-encoded responses to its
object download requests if they include an "Accept-Encoding: gzip"
header.

The test creates a single Git LFS object whose content is a special
string recognized by our "lfstest-gitserver" test utility, which then
performs distinct actions when responding to upload and download
requests for the object.

At present, this special key is the string "storage-compress", and when
an object with this value is uploaded or downloaded the test utility
checks that an "Accept-Encoding: gzip" header is present, and responds
with a 500 status code if not.  As well, when handling a download request
for an object with this value, the utility gzip-encodes the body of
the HTTP response and includes a "Content-Encoding: gzip" header.

While this specialized download request handling logic is necessary for
the "storage upload with compression" test to succeed, the upload request
handling logic is not, despite the name of the test.

In fact, the check for an "Accept-Encoding: gzip" request header in this
case serves no useful purpose, since the header is merely a notice to the
HTTP server that it may, if it chooses, send a gzip-encoded response body.
In particular, such a request header does not indicate that the request
body is encoded, so the "not encoded" error message our check would return
if the header was missing or lacked the value "gzip" is simply incorrect.

Further, by default the "net/http" package in the Go standard library
adds an "Accept-Encoding: gzip" header to all HTTP requests, so there
is little value to checking for the presence of that header in more than
one instance, and we already do so when handling the object download
made by the test.

We therefore remove the check for an "Accept-Encoding: gzip" header from
our object upload handling logic, which in turn allows us to rename the
special object content key from the generic term "storage-compress" to
the more specific term "storage-download-encoding-gzip".

As well, we rename the test from "storage upload with compression" to
"batch storage HTTP download with gzip encoding", which better reflects
its intent, and update the name of the repository created by the test
to match.
In prior commits in this PR we extracted the "storage upload with
compression" test from our "t/t-push.sh" shell test script into a
new "t/t-batch-storage-encoding.sh" script and then renamed the test
to "batch storage HTTP download with gzip encoding" to better reflect
its purpose.  (The original version of this test was first added in
commit 44fae30 of PR git-lfs#4345.)

We now update the test in several minor respects so it can serve as
a template for similar tests we may add in the future, should we
introduce support for object downloads encoded in other formats
besides the gzip format.

First, we reorganize and simplify the test's preliminary setup actions
to match our general idiom for tests of this type.  For instance,
since the test uses the "git lfs push" command simply to establish
its initial context, we can rely on that command to report any
unexpected failures and do not need to either capture its output into
a log file or call the assert_server_object() assertion function.
(If a regression was introduced to the "git lfs push" command, other
tests in our suite would detect this problem.)

Next, we simplify the test by replacing the "git clone" command
with a "git lfs pull" command.  Following a pattern we use in many
other tests, we delete the local Git LFS storage directories before
running the "git lfs pull" command so it will download the missing
object from our test server utility.

We then check the trace log output of the "git lfs pull" command
for the "decompressed gzipped response" message, which should only
appear if a gzip-encoded object download transfer took place.  This
is the test's key verification step, and was included with the original
version of the test in commit 44fae30,
at which time the trace log message was also introduced.

However, we would like this test to serve as a template for similar tests
we may add in the future, if we implement support for object downloads
encoded in other formats besides the gzip format.  With this goal in mind,
we expand the checks performed by the test in several ways.

As well as checking for the "decompressed gzipped response" trace log
message, we now also confirm that "Accept-Encoding: gzip" request headers
are captured in the log file.  In addition, we check that these headers
appear twice, once from the Git LFS Batch API request and once from the
subsequent object download request, and we check that the "decompressed
gzipped response" message appears only once, from the object download
request.

Finally, we add a number of comments to the test to help explicate
its function and the reasons why we expect two "Accept-Encoding: gzip"
request headers but only one "decompressed gzipped response" message
in the "git lfs pull" command's trace log output.
In PR git-lfs#1585 we added tests to our shell test suite to verify that the
Git LFS client will retry object upload and download transfer requests
when one or more initial requests fail due to HTTP errors.  We placed
these tests in a shell script named "t/t-batch-retries.sh".

Then in PR git-lfs#3449 we updated the Git LFS client to respect HTTP responses
with 429 status codes and optional Retry-After headers by delaying its
subsequent retries, but we only implemented this change for individual
object upload and download requests and not for requests to the Batch API
endpoint itself.  This PR included several new shell tests, which we
placed in a script named "t/t-batch-retries-ratelimit.sh".

Later, in PR git-lfs#4573, we revised the client to also respect Batch API
responses with 429 status codes and optional Retry-After headers, and
added an appropriate set of tests to the "t/t-batch-retries-ratelimit.sh"
script, but only after moving the existing tests in that script into
a new file named "t/t-batch-storage-retries-ratelimit.sh".  As we
noted in commit 32f571d when we
made these changes:

  This commit ... moves the existing storage API endpoint tests ...
  for the same scenario to a more logical
  `t-batch-storage-retries-ratelimit.sh` file to not cause confusion
  with the actual batch API endpoint tests.

Following the same rationale, we now rename the "t/t-batch-retries.sh"
script to "t/t-batch-storage-retries.sh", since the tests it contains
exercise the client's retry logic for object upload and download
HTTP requests and not the retry logic for Batch API requests.
In PR git-lfs#1297 we updated the Git LFS client so it attempts to resume
interrupted object download transfer requests by retrying the request
and adding a Range header.  The original version of this PR implemented
support for the resumption of HTTP object download requests in a
dedicated transfer adapter named "http-range".  This transfer adapter
was not included in the final version of the PR, though, because we
simply enhanced our basic transfer adapter so it will retry an
interrupted download request using a Range header to retrieve the
missing portion of the object.

References to the prototype "http-range" transfer adapter still remain,
however, in the name of the "t/t-resume-http-range.sh" shell script
added PR git-lfs#1297, and in the names of the tests it contains.  The first
two tests in this script were introduced in that PR, and they verify
that after an interrupted object download request, the client makes
another request with a Range header.  The first confirms that the client
correctly handles a successful response with a 216 Partial Content status
code, while the second test ensures that if the server replies with a
416 Range Not Satisfiable status code, the client then retries the full
original request without a Range header.

We further updated our basic transfer adapter in PR git-lfs#3518 so that it
would avoid making requests with a Range header unless the bytes indexes
it would provide in the header had sensible values.  In particular,
if the client has previously downloaded sufficient data to meet or
exceed the expected size of the object, but another attempt has been
enqueued for the same object due to an HTTP error, the next request
will simply retry the full original request.

To verify this updated behaviour, PR git-lfs#3518 added a third test named
"resume-http-range-retry" to the "t/t-resume-http-range.sh" script.
This test is intended to check that the Git LFS client retries an
object download request without using a Range header if the object
data already downloaded has the same length as the object.

At present, all of these tests succeed, but they also all fail to
properly simulate the conditions they intend to test, and in the case
of the "resume-http-range-retry" test the consequence is that it now
just passes trivially regardless of the test conditions.

We will address these issues in other commits in this PR, but first
we move the tests from the "t/t-resume-http-range.sh" script into the
"t/t-batch-storage-retries.sh" script (which we renamed from the
"t/t-batch-retries.sh" script in a prior commit in this PR).

By doing so, we consolidate all of the tests related to the client's
handling of failed or interrupted object transfer HTTP requests, except
for those which verify our support of the non-standard "tus" protocol.
(Note that in a subsequent commit in this PR we expect to also rename
the "t/t-resume-tus.sh" script that contains our tests of the "tus"
protocol.)
In prior commits in this PR we renamed our "t/t-batch-retries.sh"
shell test script to "t/t-batch-storage-retries.sh", and then moved
all of the tests from our "t/t-resume-http-range.sh" script into
the new "t/t-batch-storage-retries.sh" script.

To further clarify the relationship between these tests and their
individual purposes, we now rename the tests to describe them with
greater specificity.  As well, we rename the tests in the
"t/t-batch-storage-retries-ratelimit.sh" script to align with the
new names of the tests in the "t/t-batch-storage-retries.sh" script.

In the case of the three tests which we migrated from the
"t/t-resume-http-range.sh" script, we also update the names of the
repositories they create so that these names follow the same format
used in the other, related tests.

Finally, we revise the special object content keys those three tests
use as the values of the Git LFS objects they create, and update our
"lfstest-gitserver" test utility to recognize the new keys.  All of
the keys used by the tests in the "t/t-batch-storage-retries.sh"
script now being with the same "storage-download-" prefix.
In prior commits in this PR we renamed our "t/t-batch-retries.sh"
shell test script to "t/t-batch-storage-retries.sh", and then moved
all of the tests from our "t/t-resume-http-range.sh" script into
the new "t/t-batch-storage-retries.sh" script.  We then renamed the
tests in these scripts to follow a consistent pattern and more fully
explicate each test's unique purpose.

Meanwhile, our "t/t-batch-retries-ratelimit.sh" script contains another
set of tests which verify the behaviour of the Git LFS client when its
HTTP request to a Batch API endpoint receives a response with a 429
status code and, possibly, a Retry-After header.

To further clarify the relationship between the tests in these
various scripts, we now rename some of the tests in the
"t/t-batch-retries-ratelimit.sh" script so they follow the same
pattern we established in previous commits in this PR and better
reflect the intent of each individual test.
In our "lfstest-gitserver" test utility program, the storageHandler()
function is used to generate responses to HTTP requests which upload
or download individual Git LFS objects.  For both types of requests,
if the content of an object matches a specific "magic" string value
then the function simulates a set of conditions relevant for the
current shell test, such by returning a particular status code or
checking for a particular request header.

In a previous commit in this PR we renamed several of these special
object content keys so all the strings recognized when the
storageHandler() function responds to a GET request have the same
"storage-download-" prefix.

We now reorder the "case" clauses in the "switch" statement the
storageHandler() function executes when responding to a GET request
so that all the special object content keys which begin with the
"storage-download-retry-" prefix are grouped together.

This change helps distinguish the sole "case" clause for the object
content key string "storage-download-encoding-gzip" from the other
clauses.  If in the future we introduce support for object downloads
encoded in other formats besides the gzip format, we can then add
appropriate "case" clauses adjacent to this clause without further
separating the clauses which all handle the object content keys with
the same "storage-download-retry-" prefix.
Since commit f082660 of PR git-lfs#1297 our
"lfstest-gitserver" test utility program has responded to certain
HTTP requests with Range headers by returning a 206 Partial Content
status code and the specified portion of a stored Git LFS object in
the body of the HTTP response.  These responses also include a
Content-Range header whose purpose is to identify the exact portion
of the content that was returned in the response body.

However, at present our test utility returns invalid data in these
Content-Range headers.  If the Git LFS client makes a request with a
Range header which specifies the byte range "10-27" of a 28-byte object,
the Content-Range header we generate will appear as "bytes 10-28/-18"
instead of the correct value, which should be "bytes 10-27/28".
For reference regarding the official specification of this header, see:

  https://www.rfc-editor.org/rfc/rfc9110.html#field.content-range

The first error in our Content-Range headers occurs because we use the
size of the object as the end position byte index, but both indexes are
zero-based and so should never exceed one less than the full size of the
object.

The second error occurs because we do not simply use the full size of
the object as the value of the third, denominator-like field, but
instead subtract the size of the object from the start position byte
index, which yields a negative integer.

To resolve this issue, we adjust the two instances in our test utility
where we format a Content-Range header so that both cases we use the
correct values in all three of the header's numeric fields.

Note that we copied the original, incorrect code from PR git-lfs#1297 in commit
73e8a71 of PR git-lfs#3518 when we added
support in the "lfstest-gitserver" utility for what was at the time named
the "resume-http-range-retry" test in the "t/t-resume-http-range.sh"
script.  In prior commits in this PR we have now moved this test to the
"t/t-batch-storage-retries.sh" script and renamed it as the "batch
storage HTTP download retries without invalid Range header" test.
In commits c9c457c and
c8817ff of PR git-lfs#1297 we implemented
a pair of tests to verify that the Git LFS client attempts to resume
interrupted object download transfer requests by retrying the request
and adding a Range header.  The first confirms that the client
correctly handles a successful response with a 216 Partial Content status
code, while the second test ensures that if the server replies with a
416 Range Not Satisfiable status code, the client then retries the full
original request without a Range header.

(Note that in prior commits in this PR we moved these tests from their
previous location in the "t/t-resume-http-range.sh" shell script to a
new script file named "t/t-batch-storage-retries.sh", and we also
renamed the tests to better reflect their purpose.)

To simulate an HTTP response being interrupted, the same commits in
PR git-lfs#1297 updated the storageHandler() function in our "lfstest-gitserver"
test utility program to truncate the data it returned when an object's
content matched specific "magic" strings.  The tests added in the PR
create objects with those special keys as their content, and expect
that downloads of those objects may be interrupted, given certain other
conditions.

When these conditions apply, the storageHandler() function truncates the
data it sends in its responses to GET requests by returning only a slice
from the byte array that contains the object data.

However, the function relies on the "net/http" package from the Go
standard library to generate a Content-Length response header, which
it does based on the length of the data it receives.  As a result,
the Content-Length header values returned to the client with truncated
object data reflect the length of that shortened data and not the
actual Git LFS object size, as they would if a normal object download
request experienced a connection failure.

When the client receives the response with the truncated object data,
the "net/http" package uses the value from the Content-Length header
to determine whether or not the response body has been fully received.
Since the length of the data matches the header's value, the "net/http"
packages terminates the data with a regular EOF marker, and so the
Git LFS client assumes the object has been fully downloaded.  It then
checks the SHA-256 hash value it has computed from the data against the
object's ID, and when these do not match, reports an error and does
not retry the object download request.  The incomplete object data is
retained, however, in a file in the ".git/lfs/incomplete" directory.

If another Git LFS command is run which also attempts to download
the same object, it will locate the temporary file left by the previous
attempt and then make another download request, this time using a
Range header to request the missing object data.

Both of the tests added in PR git-lfs#1297 therefore execute an initial
"git lfs fetch" command, which stops after downloading the first
portion of a test object, and then execute a second "git lfs fetch"
command and check its trace log output to confirm that an HTTP request
with a Range header was sent.  As a consequence, these tests do not
actually verify whether a single Git LFS command will attempt to
resume an interrupted object download request.

We now revise the "lfstest-gitserver" utility to correctly simulate
an interrupted HTTP response when responding to the initial requests
from these tests, which in turn allows us to simplify the tests because
they each need to run just a single "git lfs fetch" command.

The only change we need to make to the "lfstest-gitserver" utility is
in the section of the storageHandler() function which responds to GET
requests.  When one of the two tests' special object content keys is
detected and the other relevant conditions are met, the function
indicates that the response data should be truncated by setting the
"byteLimit" variable to a positive integer.  We therefore insert a
conditional block prior to the output of the response data which checks
for a "byteLimit" value greater than zero, and in that case explicitly
sets the Content-Length header to the full object's size.

We then alter both the "batch storage HTTP download retries with Range
header" and "batch storage HTTP download retries after Range header
rejected" tests in the "t/t-batch-storage-retries.sh" script so they
each only perform a single "git lfs fetch" command and then check
that the command has successfully downloaded a Git LFS object.

In addition to the existing calls to the assert_local_object() test
assertion function we add several grep(1) commands to check for a
number of trace log messages and HTTP headers that we expect the
"git lfs fetch" commands to output.  To capture the HTTP headers we
set the GIT_CURL_VERBOSE environment variable to a value considered
"true" before executing the "git lfs fetch" commands.

After each "git lfs fetch" command runs, we now also verify that the
command succeeded by checking that it returned a zero exit code.
Following an idiom we use in many of our shell tests, we check the first
element of the PIPESTATUS array variable to determine whether the
"git lfs fetch" command succeeded.

Previously the first command in each test would return a non-zero
exit code, but this is no longer the case.  (Note that this did not
cause the tests to fail because the shell's "errexit" attribute ignores
the exit codes of all commands in a pipeline other than the final one,
which is why we need to check the PIPESTATUS array variable.)

As we are simplifying the operation of the tests, we also rewrite their
internal code comments to more accurately explain how the tests operate.
In particular, we correct the comments which stated that the initial
"git lfs fetch" commands reported an error because their object download
requests were interrupted, since that is not technically accurate.
In fact, the commands formerly reported errors because the hashes of
the object data they downloaded did not match the corresponding OIDs,
while the responses to all their HTTP requests were considered complete
and successful due to the values provided in the Content-Length headers.

We also take the opportunity to adjust the log files captured by the
tests to match our standard practice in other similar tests.  We now
use the simple file name "fetch.log" for the log files in which we
capture the "git lfs fetch" commands' output.  We also remove the
redirections and pipelines which generated a log file when running
"git commit" commands, as we expect those commands to succeed and the
tests did not check the "commit.log" files at all.
In commit 73e8a71 of PR git-lfs#3518 we
updated the Git LFS client so that when it attempts to download an
object over HTTP after one or more previous attempts have failed,
if the object data accumulated from the earlier HTTP responses meets
or exceeds the object's expected size and yet is not considered valid
based on its SHA-256 hash, the client will make a request for the
full object and not use a Range header to request only a portion of
the object's data.

To verify the client behaves as expected under these conditions, the
same commit also included a new test which creates a file in the
".git/lfs/incomplete" directory before running a "git lfs fetch"
command.  The file the test creates is designed to match the size
and OID of the Git LFS object the test creates, but contain invalid
content, thus forcing the command to request the entire object rather
than only a portion of it.

(Note that in prior commits in this PR we moved this test from its
previous location in the "t/t-resume-http-range.sh" shell script to a
new script file named "t/t-batch-storage-retries.sh", and we also
changed the name of the test from "resume-http-range-retry" to
"batch storage HTTP download retries without invalid Range header".)

At the time the test was first written, it worked as intended.  However,
in commit 1edb976 of PR git-lfs#3826 we
altered the name of the temporary files created by the Git LFS client
in the ".git/lfs/incomplete" directory, as part of a set of revisions
to ensure multiple worker routines do not generate conflicting
operations on these files.

Since PR git-lfs#3826 the files the client stores in the ".git/lfs/incomplete"
directory now have a file extension of ".part", whereas they had the
extension ".tmp" when PR git-lfs#3518 was merged.  Because the test was not
updated in PR git-lfs#3826, it still uses the ".tmp" extension on the file
it creates in the ".git/lfs/incomplete" directory.

The consequence is that when the test added by PR git-lfs#3518 runs, the
client just ignores the file with the ".tmp" extension and so the
"git lfs fetch" command executed by the test downloads the given Git LFS
object as if no previous downloads had been attempted.  The test performs
no additional checks other than confirming that the object has been
downloaded, and therefore passes trivially even though it fails to
simulate the conditions it was designed to assess.

We resolve this problem simply by updating the test to use the ".part"
file extension when creating files in the ".git/lfs/incomplete"
directory.

However, we would like to guard against future regressions which might
cause this test to again become ineffective.  To do this, we introduce
an initial check which also creates a file in the ".git/lfs/incomplete"
directory and then runs a "git lfs fetch" command.  In this instance,
though, the file we create contains only a valid but truncated portion
of the Git LFS object's data.  We then verify after the "git lfs fetch"
command is executed that it detected the presence of the incomplete
data file and so issues an HTTP request with a Range header to download
just the missing portion of the object's data.

With this additional check in place, the test should now fail if in the
future we alter how the Git LFS client manages its temporary storage of
data during object downloads, but forget to alter this test as well.
The initial "git lfs fetch" command would likely still download the
object successfully, but do so without using a Range header in its
request, and our new check will fail when that header is not found
among the command's trace log output.

Note, though, that this extra check specifically does not suffice to
verify the changes made to the client in PR git-lfs#3518.  As described above,
the key behaviour the test needs to confirm is that the client retries
a full object download when it finds a temporary file left by a previous
download attempt but the file is not smaller than the object to be
retrieved.

At present, however, the test does not actually verify this behaviour on
the part of the client, other than to check that it ultimately succeeds
in downloading the full object.  To make sure the test validates that
the object is retrieved without using a request with a Range header,
we now capture trace log output from the "git lfs fetch" command and
check that it does not include any indication that a request with a
Range header had been performed.

The original version of the test actually ran the "git lfs fetch" command
twice, although not for the reason that we now run the command an extra
time, since we have added a preliminary instance of the command solely to
confirm that the client will find the temporary object data files the
test creates.

According to the comments in the test, the reason it runs the "git lfs
fetch" command twice is because the first instance of the command may
experience an error.  This behaviour was likely observed when the test
was first written because the test was designed to replicate the problem
reported in git-lfs#3494, before PR git-lfs#3518 was developed to resolve that issue.

Without the changes introduced in PR git-lfs#3518 (or PR git-lfs#3826), the first time
the "git lfs fetch" command was run it would have made a request with
a Range header whose contents were invalid, such as "bytes=18-17" for
an 18-byte object.  The client would then receive a response from the
server with a 400 Bad Request status code, remove the invalid temporary
object data file, and report an error.  When the command was run a
second time, though, no temporary file would be found and the client
would make a request for the entire object, which would succeed.

Since the modifications from PR git-lfs#3518 were merged, though, the test has
likely never required the second instance of the "git lfs fetch" command,
even before the test was inadvertently made ineffective by the changes
from PR git-lfs#3826.  We can therefore just remove the extra invocation of the
"git lfs fetch" command (but note that as described above, we have added
one initial invocation of the command which runs under different
conditions, in order to guard regressions in the test's design).

In addition to the existing call to the assert_local_object() assertion
function at the end of the test, we add several grep(1) commands to
check for a number of trace log messages and HTTP headers that we expect
the final "git lfs fetch" command to output.  As with our new, initial
invocation of that command, we set the GIT_CURL_VERBOSE environment
variable to a value considered "true" so we can capture all the headers
from the HTTP requests made by the client.

After each "git lfs fetch" command runs, we now also verify that the
command succeeded by checking that it returned a zero exit code.
Following an idiom we use in many of our shell tests, we check the first
element of the PIPESTATUS array variable to determine whether the
"git lfs fetch" command succeeded.

Previously at least the first command run by the test would return a
non-zero exit code, but this did not cause the tests to fail because the
shell's "errexit" attribute ignores the exit codes of all commands in
a pipeline other than the final one (which is why we need to check the
PIPESTATUS array variable).

We also adjust the log files captured by the test to match our standard
practice in other similar tests.  We now use the simple file name
"fetch.log" for the log files in which we capture the "git lfs fetch"
commands' output.  We also remove the redirection and pipeline which
generated a log file when running the "git commit" command, as we expect
this command to succeed and the test did not check the "commit.log"
file at all.

Finally, to help clarify the purpose of the test's various steps, we
rewrite and expand its internal code comments to more accurately explain
how it operates.  As well, we add a comment to the portion of the
"lfstest-gitserver" test utility's storageHandler() function that is
exercised by this test to indicate that the program should never
receive a Range header from the client with an invalid set of start
and end position byte indexes.  The utility's check that the provided
byte range is sensible was added while investigating and reproducing
the issue reported in git-lfs#3494, but this condition should never occur
due to the changes made to the client in PR git-lfs#3518.
In both PR git-lfs#1297 and PR git-lfs#3518 we introduced tests which exercise
the Git LFS client in conditions where it attempts to resume an
interrupted object download transfer request by retrying the request
and, in some cases, adding a Range header.

(Note that in prior commits in this PR we moved these tests from their
previous location in the "t/t-resume-http-range.sh" shell script to a
new script file named "t/t-batch-storage-retries.sh", and we also
renamed the tests to better reflect their purpose.)

These tests require our "lfstest-gitserver" test utility program to
parse any Range headers sent by the Git LFS client, and so PRs git-lfs#1297
and git-lfs#3518 each added logic to the utility for this purpose, in two
separate blocks.  In both blocks, the program first checks if a Range
header was provided in the current HTTP request to download an object,
and if a Range header is found, then the program applies a regular
expression to try to extract start and end position byte indexes from
the header's value.

As we may in the future want to add other tests that require the
"lfstest-gitserver" utility to parse Range headers, we refactor the
program now to consolidate its parsing logic for these headers into
a single common function named handleRangeRequest().

We also take the opportunity to make the utility's parsing of Range
header values more strict.  For instance, the program will now return
an HTTP response with a 400 Bad Request status code if the Range
header's value does not match against the regular expression we use
parse it.

Likewise, the program now returns a response with a 400 Bad Request
status code if the client sends both a Range header and an
Accept-Encoding header.  While the HTTP specification allows both
headers to appear in a request, if the content is encoded by the server
then the start and end position indexes in the Range header should be
interpreted as offsets into the encoded content:

  https://www.rfc-editor.org/rfc/rfc9110.html#section-14.1.2-3

Since the Git LFS client calculates byte range indexes it sends with
Range headers as offsets into the unencoded object data, the client
should never sent an Accept-Encoding header with a Range header as
the server might then choose to encode the object data and then apply
the requested byte range to the encoded data.  For the same reason,
the "net/http" package in the Go standard library does not automatically
insert an "Accept-Encoding: gzip" header into a request unless no Range
header is present:

  https://github.com/golang/go/blob/go1.25.7/src/net/http/transport.go#L2768
  https://github.com/golang/go/blob/go1.25.7/src/net/http/transport.go#L2779-L2781

We slightly refine the regular expression used to parse the value of
a Range header, and add a comment which notes that the expression would
not match a valid header value that followed the "suffix-range" format
(e.g., "bytes=-100") but that we expect the Git LFS client to never
send a header with such a value.

If an end position byte index is provided, we now always check that it
is at least equal to the start position index, and if it is not, we
return a 400 Bad Request status code.  For greater clarity, we now also
use the aliases from the "net/http" package in place of literal HTTP
status codes like 400.

Finally, our changes allow us to remove the "resumeAt" variable from the
storageHandlers() function, so we do that as well.
In PR git-lfs#1303 we updated the Git LFS client to support version 1.0 of
the "tus" protocol, which allows for HTTP upload requests to be resumed
if they are interrupted:

  https://tus.io/protocols/resumable-upload/1-0-x

In the same PR, we also added tests to our shell test suite to verify
that the when the "lfs.tusTransfers" configuration option is set to
a value considered "true", the Git LFS client will use the "tus"
protocol when uploading individual objects over HTTP, and that the
client will retry an interrupted request and send the additional data
necessary to fully upload an object.

These tests are located in the "t/t-resume-tus.sh" script, whose name
follows the pattern established in PR git-lfs#1297 when we created the
"t/t-resume-http-range.sh" shell script.  However, only one of the
two tests in the "t/t-resume-tus.sh" script actually checks whether
an interrupted object upload request can be resumed with the "tus"
protocol.

In a previous commit in this PR we moved the tests from the
"t/t-resume-http-range.sh" script to a new shell script named
"t/t-batch-storage-retries.sh", in order to consolidate all of the
tests related to the client's handling of failed or interrupted object
transfer HTTP requests, except for those which verify our support of
the "tus" protocol.

To keep the names of our test scripts as consistent as possible
we now rename the "t/t-resume-tus.sh" script to
"t/t-batch-storage-upload-tus.sh", which also better reflects the
fact that both tests in the "t/t-resume-tus.sh" script validate the
client's use of the "tus" protocol for object uploads but only one
actually causes a request to be resumed.
In a prior commit in this PR we renamed our "t/t-resume-tus.sh" shell
test script to "t/t-batch-storage-upload-tus.sh", following earlier
commits also in this PR where we moved and renamed a number of other
tests and test scripts related to Git LFS client's handling of failed
or interrupted object transfer HTTP requests.

The tests in the "t/t-batch-storage-upload-tus.sh" script are related
to these other tests in that they verify that the Git LFS client can
use the "tus" protocol and that in particular it can use the protocol
to resume an object upload after a request is interrupted (assuming
the appropriate configuration option is enabled).

To further this relationship between the tests, we now rename the
tests in the "t/t-batch-storage-upload-tus.sh" script to align with
the new names of the tests in the "t-batch-storage-retries-ratelimit.sh",
"t/t-batch-storage-retries.sh", and "t-batch-retries-ratelimit.sh"
scripts.  Our revised names for the tests of the "tus" protocol should
also now describe their purpose with somewhat greater specificity.
In PR git-lfs#1303 we updated the Git LFS client to support version 1.0 of
the "tus" protocol, which allows for HTTP upload requests to be resumed
if they are interrupted:

  https://tus.io/protocols/resumable-upload/1-0-x

In the same PR, we also added tests to our shell test suite to verify
that the when the "lfs.tusTransfers" configuration option is set to
a value considered "true", the Git LFS client will use the "tus"
protocol when uploading individual objects over HTTP, and that the
client will retry an interrupted request and send the additional data
necessary to fully upload an object.

(Note that in prior commits in this PR we moved these tests from
their previous location in the "t/t-resume-tus.sh" shell script to a
new script file named "t/t-batch-storage-upload-tus.sh", and we also
renamed the tests to better reflect their purpose.)

We now update these tests in several minor respects, and in particular
we remove a misleading comment and the related Git LFS object data
from the "batch storage HTTP upload retries with tus protocol" test.

In that test, which was originally named "tus-upload-interrupted-resume",
one comment states that the specific content of a test Git LFS object
"announces to server that we want it to abort the download part way,
but reject the Range: header and fall back on re-downloading instead".

However, the test does not download any objects at all, because the
purpose of the test is solely to validate the Git LFS client's support
for the "tus" protocol when uploading objects.  The comment appears
to have been inadvertently copied from a test in another script, likely
what is now the "batch storage HTTP download retries after Range header
rejected" test in the "t/t-batch-storage-retries.sh" script.  (In prior
commits in this PR we renamed the test from "resume-http-range-fallback",
and moved it from the "t/t-resume-http-range.sh" script to its current
location.)

In the "batch storage HTTP upload retries with tus protocol" test, the
comment regarding downloads and Range headers precedes the creation
of a Git LFS object with a long, random string of characters as its
content.  This adds to the potential confusion which may arise from the
comment, since it appears this string may have some special purpose
or value.  In practice, though, we can replace it with a simple string
such as the word "test" without any impact on the test at all, so we
do that now.

Both tests in the "t/t-batch-storage-upload-tus.sh" script also create
objects with the string "send-verify-action" as their content.  Unlike
the object described above, this string does have a special purpose, in
that it causes our "lfstest-gitserver" test utility program to include
a verification URL for the object in its JSON response to a Batch API
upload request.  The Git LFS client will then make a request to this
URL after uploading the object, which allows the server to verify that
the object has been received and is complete.

While our test suite does need to ascertain that the client makes
verification URL requests after uploading objects, if a URL is provided
for them in a Batch API response, we already do this in the tests in
our "t/t-verify.sh" shell script.  Therefore we do not actually need
to create objects with the "send-verify-action" special content key
in the tests of the client's support for the "tus" protocol.

Nevertheless, we leave these objects with the "send-verify-action" special
content key in place, since the additional checks are still worthwhile.
They demonstrate that after uploading an object using the "tus" protocol,
the client will contact verification URLs if the server requests them.
We do, though, add comments to both tests to clarify the purpose of the
special content key and that the additional object verification step is
not crucial for the overall correctness of the tests.

Next, we reorganize and simplify the tests' other setup actions to
match our general idiom for tests of this type.  In doing so we update
the first test so it creates two objects with the same content as the
objects created by the second test.  This change simply aligns the
actions of tests and makes comparison between them more straightforward.

We also expand the comments in both tests regarding the special names
of their respective test repositories, since these are recognized by
the "lfstest-gitserver" program and cause it to behave in the
appropriate manner for each test.

In addition to the existing calls to the assert_local_object() assertion
function at the end of the tests, we add several grep(1) commands to
check for a number of trace log messages and HTTP headers that we expect
the final "git lfs push" command to output.  To capture the HTTP headers we
set the GIT_CURL_VERBOSE environment variable to a value considered
"true" before executing the "git lfs push" commands.

Note that when accepting object uploads from the "batch storage HTTP
upload retries with tus protocol" test, the "lfstest-gitserver" utility
will only accept the first third of the object data when the data begins
with the first byte of an object.  The Git LFS client should then attempt
a second pair of HEAD and PATCH requests to upload the remaining data,
as per the "tus" protocol.

To confirm these subsequent requests are made as we expect, we check for
specific trace log messages and HTTP headers with byte offset values
calculated by dividing the objects' lengths by three.  Those offsets
should match the points at which the "lfstest-gitserver" program
interrupted the initial upload requests.

In both tests, after the "git lfs push" command runs we now also verify
that the command succeeded by checking that it returned a zero exit code.
Following an idiom we use in many of our shell tests, we check the first
element of the PIPESTATUS array variable to determine whether the
"git lfs push" command succeeded.  (Note that we need to check the
PIPESTATUS array variable because the shell's "errexit" attribute ignores
the exit codes of all commands in a pipeline other than the final one.)

We also adjust the log files captured by the tests to match our standard
practice in other similar tests.  We now use the simple file name
"push.log" for the log files in which we capture the "git lfs push"
commands' output.  We also remove the redirections and pipelines which
generated a log file when running "git commit" and "git lfs track"
commands, as we expect these commands to succeed and the test did not
check the "commit.log" file at all.
@chrisd8088
chrisd8088 force-pushed the fix-http-encoding-range-tests branch from da5fd7c to ac27ecf Compare March 1, 2026 22:55
chrisd8088 added a commit to chrisd8088/git-lfs that referenced this pull request Mar 2, 2026
In prior commits in this PR we introduced a new configuration option
named "lfs.httpDownloadEncoding", which may be set to either of the
values "gzip" or "zstd", with a default value of "gzip".

When our new option is not defined or is set to "gzip", the Git LFS
client simply follows its legacy behaviour when making object transfer
download requests over HTTP.  Specifically, the client should allow the
"net/http" package in the Go standard library to insert an
"Accept-Encoding: gzip" header into the requests, and if the server
chooses to respond with gzip-encoded data, the client should expect
the "net/http" package to automatically decode the body of the response.

A test of this legacy behaviour was first introduced in PR git-lfs#4345.
More recently, in PR git-lfs#6207, we created the "t/t-batch-storage-encoding.sh"
shell script and moved the test into that script, while also renaming
the test to "batch storage HTTP download with gzip encoding".

We now expand the test to verify that the client continues to behave
as it has previously when our new configuration option is explicitly
set to "gzip", either universally or for a specific URL.
chrisd8088 added a commit to chrisd8088/git-lfs that referenced this pull request Mar 2, 2026
In prior commits in this PR we introduced a new configuration option
named "lfs.httpDownloadEncoding", which may be set to either of the
values "gzip" or "zstd", with a default value of "gzip".  We also
added a set of tests to confirm that the Git LFS client sends an
Accept-Encoding header with the selected encoding type when making
object transfer download HTTP requests, and that the client is able
to decode the response data if the server chooses to encode it.

We now move those tests into the "t/t-batch-storage-encoding.sh"
script we created recently in PR git-lfs#6207, and rename and refactor them
to follow the design of the existing test in that script.  In doing
so, we combine the "lfs.httpDownloadEncoding zstd" test and the
"lfs.<url>.httpDownloadEncoding zstd" test into a single test
named "batch storage HTTP download with zstd encoding".

We also elide the "lfs.httpDownloadEncoding gzip does not set header"
test, as the existing "batch storage HTTP download with gzip encoding"
test should be sufficient to demonstrate that when our new configuration
option is explicitly set to "gzip", all the Accept-Encoding headers
we expect the client to send have the value "gzip".

In our "batch storage HTTP download with zstd encoding" test we check
that the client sends an "Accept-Encoding: zstd" header when downloading
individual objects, but still sends an "Accept-Encoding: gzip" header
in its Batch API requests, since we only support zstd encoding for
object transfer downloads.  We use the -c option to the grep(1) command
to verify that we see the expected number of Accept-Encoding headers
with each value.

As well as checking the Accept-Encoding headers, we also update the
test and our "lfstest-gitserver" utility program so that we can confirm
that the Git LFS client properly decodes the object data it receives
from a server, if a "Content-Encoding: zstd" response header indicates
that the data is zstd-encoded.

First, we enhance the "lfstest-gitserver" utility so that when handling
a GET request for an object, if the object's content is the special key
string "storage-download-encoding-zstd" and if an "Accept-Encoding: zstd"
header was sent, then the program returns a response with a
"Content-Encoding: zstd" header and a zstd-encoded body.

Next, in our "batch storage HTTP download with zstd encoding" test we
check that a single "Content-Encoding: zstd" header was received by each
"git lfs pull" command, and we use our assert_local_object() assertion
to verify that a local copy of the object has been created.

In a previous commit in this PR we updated the "batch storage HTTP
download with gzip encoding" test so that it repeats its checks
of the client's default behaviour with the "lfs.httpDownloadEncoding"
option explicitly set to the value "gzip", and then repeats the checks
again with a URL-specific version of the option set to the value "gzip".

We follow this pattern in our "batch storage HTTP download with zstd
encoding" test, except that the test does not exercise the client's
default behaviour, since the client sends an "Accept-Encoding: gzip"
header when performing object downloads in that case.

In both tests, when we set the URL-specific configuration option, we
use the value of the GITSERVER shell variable for the URL, since it is
conveniently available and generic across all our tests' repositories.

We also ascertain that each "git lfs pull" command succeeds by checking
the first element of the PIPESTATUS array variable.  This is necessary
because the shell's "errexit" attribute ignores the exit codes of all
commands in a pipeline other than the final one, and we pipe the output
of our "git lfs pull" commands into the tee(1) command to capture a
log file.  As in many of the other tests in our suite, we use the name
"pull.log" for this file.
chrisd8088 added a commit to chrisd8088/git-lfs that referenced this pull request Mar 2, 2026
In prior commits in this PR we introduced a new configuration option
named "lfs.transfer.httpDownloadEncoding", which may be set to either
of the values "gzip" or "zstd", with a default value of "gzip".

When our new option is not defined or is set to "gzip", the Git LFS
client simply follows its legacy behaviour when making object transfer
download requests over HTTP.  Specifically, the client should allow the
"net/http" package in the Go standard library to insert an
"Accept-Encoding: gzip" header into the requests, and if the server
chooses to respond with gzip-encoded data, the client should expect
the "net/http" package to automatically decode the body of the response.

A test of this legacy behaviour was first introduced in PR git-lfs#4345.
More recently, in PR git-lfs#6207, we created the "t/t-batch-storage-encoding.sh"
shell script and moved the test into that script, while also renaming
the test to "batch storage HTTP download with gzip encoding".

In order for our test to detect that an object transfer download request
was gzip-encoded, in PR git-lfs#4345 we also updated the DoWithRedirect()
method of the Client structure in our "lfshttp" package.  The method
checks whether the "net/http" package has set the Uncompressed field
in the Response structure it returns, which would indicate that the
response body was gzip-encoded and that it has been decoded for us.

If the Uncompressed field is set, our DoWithRedirect() method outputs
a trace log message.  The test is then able to use the presence of
this message in the logs it captures from each "git lfs pull" command
to determine that the client received a gzip-encoded response to an
object transfer download request and handled it appropriately.

We would now like to include a similar check in the "batch storage
HTTP download with zstd encoding" test that we added to our
"t/t-batch-storage-encoding.sh" shell script in previous commits
in this PR.

This test verifies that the changes we have made in this PR work as
expected to implement support for zstd-encoded object HTTP downloads.
Specifically, when our new configuration option is set to the value
"zstd", the Git LFS client should send an "Accept-Encoding: zstd"
header with each object download request, and should decode the
response body appropriately if the server chooses to encode it using
the zstd algorithm.

We therefore update the download() method of the basicDownloadAdapter
structure in our "tq" package to output a trace log message when
processing a zstd-encoded response to an object transfer download
request.  We then update our "batch storage HTTP download with zstd
encoding" test to check for this message in the logs it captures from
each "git lfs pull" command.
chrisd8088 added a commit to chrisd8088/git-lfs that referenced this pull request Mar 2, 2026
In prior commits in this PR we introduced a new configuration option
named "lfs.transfer.httpDownloadEncoding", which may be set to either
of the values "gzip" or "zstd", with a default value of "gzip".

When the option is set to the value "zstd", the Git LFS client will
send an "Accept-Encoding: zstd" header when it makes an object transfer
download HTTP request.  If the server chooses, it may then reply with
a response that includes a "Content-Encoding: zstd" header and whose
body is zstd-encoded.  When this occurs, the client should recognize
the Content-Encoding header and attempt to decode the response body
using the zstd algorithm.

If the response to an object transfer download request is interrupted,
the client may only receive a portion of the object data, and when the
data is zstd-encoded, the client may only be able to decode an even
smaller amount of the data than was received, or none at all.

During an object transfer download, the Git LFS client writes the data
it receives into a temporary file in the ".git/lfs/incomplete"
directory.  If the download is interrupted but some data has been
received and written to the temporary file, the client renames the
file with the extension ".part" and leaves the file in place so it is
available if the client tries to download the object again.

While executing a single command, the client will retry a failed
object transfer download request up to the limit defined by the
"lfs.transfer.maxRetries" configuration option.  Each time the client
attempts to download the object again, if partial data is available in
a file with the ".part" extension in the ".git/lfs/incomplete" directory,
the client will make an HTTP request with a Range header that specifies
only the outstanding portion of the object data.

Further, if a command fails to completely download an object, but
another Git LFS command is run later to download the same object, it
will also make a request with a Range header if an incomplete portion
of the object's data exists in a file with the ".part" extension.

When a Range header is set for an HTTP request, the "net/http" package
in the Go standard library will not automatically insert an
"Accept-Encoding: gzip" header:

  https://github.com/golang/go/blob/go1.25.7/src/net/http/transport.go#L2768
  https://github.com/golang/go/blob/go1.25.7/src/net/http/transport.go#L2779-L2781

While the HTTP specification allows both Range and Accept-Encoding
headers to appear in a request, if the content is encoded by the server
then the start and end position indexes in the Range header should be
interpreted as offsets into the encoded content:

  https://www.rfc-editor.org/rfc/rfc9110.html#section-14.1.2-3

Since the "net/http" package will decode a gzip-encoded response body
if the package added an "Accept-Encoding: gzip" header to the request,
the caller may never be aware that the server supports encoding.  Hence
any start and end positions provided in a Range header are likely to be
offsets into the full, uncompressed source data, and so the "net/http"
package does not insert an Accept-Encoding header when a Range header
is defined.

In the case where our Git LFS client sends a request with an
"Accept-Encoding: zstd" header, we are responsible for decoding the
response body, if the server chooses to compress the source data with
the zstd algorithm.  Should a zstd-encoded response be interrupted,
we may have written some of the decoded data to a temporary file,
so when we retry the request, the byte indexes we use in the Range
header will be offsets into the original object data and not offsets
into its encoded version.

Therefore, we avoid adding an "Accept-Encoding: zstd" header when we
retry a request, unless no temporary file exists for the given object,
or a temporary file exists but is empty.

To implement this change we simply modify the download() method of the
basicDownloadAdapter structure in our "tq" package so that it only
sends an "Accept-Encoding: zstd" header when the "fromByte" variable
has a zero value.  This variable will be set to the size of any
temporary file with previously-downloaded data for the current object,
and so a Range header will be included in the next request with the
"fromByte" variable's value used as the start position byte index.

(In theory, we could try to determine where the encoded data stream
from the response was interrupted, if we read from the stream before
we passed it the Decoder structure from the "zstd" package of the
"github.com/klauspost/compress" module.  Our intermediate reader
would then be able to detect when an unexpected EOF appeared in the
encoded data stream.  However, this would also entail significant
other changes, as we would need to retain the offset into the
encoded data stream between retry attempts, and potentially even
between attempts made by separate Git LFS commands.)

To verify that our changes are effective, we introduce a new "batch
storage HTTP download with zstd encoding retries with Range header
and without encoding" test to our "t/t-batch-storage-encoding.sh" shell
script.  This test creates an object whose content is the special key
string "storage-download-encoding-zstd-retry-range", which our
"lfstest-gitserver" test utility will now recognize when responding
to a GET request for an object.

We update our "lfstest-gitserver" utility so that when this special
content key is detected, the program calls the handleRangeRequest()
function we recently developed in PR git-lfs#6207.  This function handles
requests that are expected to have a Range header, and will return a
400 Bad Request status code to the client if it also sent an
Accept-Encoding header.

However, when the "git lfs pull" command run by our new test makes
its first object transfer download request, it will not send a Range
header but will send an "Accept-Encoding: zstd" header.  To simulate
an interrupted response to this first request, we further update
the "lfstest-gitserver" utility so that it returns only one frame
of zstd-encoded data, representing half of the complete object, plus
some but not all of the remaining encoded data.

By encoding the object's data in two separate frames, and sending at
least the initial frame, we guarantee that the client will be able to
fully decode the first frame, and so some data will be written to a
temporary file when the request is interrupted.  Otherwise the client
will leave an empty temporary file and so will keep retrying the request
with an "Accept-Encoding: zstd" header and without a Range header.

After the "git lfs pull" command completes, our new test then checks
the messages in the trace log it captured of the command's output.
The test confirms that only one request with an "Accept-Encoding: zstd"
header was made, and that the client resumed by making another request
with a Range header, to which the server responded with a 206 Partial
Content status code and a Content-Range header with the expected
byte range indexes for the missing portion of the object's data.
Comment thread t/t-batch-storage-retries.sh Resolved
Comment thread t/t-batch-storage-retries.sh Resolved
Comment thread t/cmd/lfstest-gitserver.go Resolved
Comment thread t/t-batch-storage-retries.sh Resolved
In prior commits in this PR we moved all of the tests from our
"t/t-resume-http-range.sh" shell script into a new script named
"t/t-batch-storage-retries.sh", and also renamed the tests to
follow a consistent pattern and better reflect their purpose.

We likewise renamed the "t/t-resume-tus.sh" shell script as
"t/t-batch-storage-upload-tus.sh" and renamed the tests in that
script as well.

All these tests begin in part by invoking our clone_repo() test helper
function and passing the "reponame" shell variable as both the function's
two parameters.  However, some of the tests quote the "reponame" variable
inconsistently in these calls, a discrepancy which was first introduced
with the original versions of the tests in commits
c9c457c and
c8817ff of PR git-lfs#1297, and in
commit 44cf589 of PR git-lfs#1303.

As suggested by larsxschneider on PR review, we now add double quotation
marks around the "reponame" variable in all of the clone_repo() calls
in these functions.

Note that there are other instances of this specific inconsistency in
other scripts in our test suite, but we leave those to be addressed in
a future PR, in order to keep the scope of this PR somewhat constrained.

We also take the opportunity to align the indentation of one comment
in the "batch storage HTTP download retries with Range header" test to
match our standard formatting.
In prior commits in this PR we moved all of the tests from our
"t/t-resume-http-range.sh" shell script into a new script named
"t/t-batch-storage-retries.sh", and renamed the tests to
follow a consistent pattern and better reflect their purpose.

At the same time, we also revised the special object content keys those
tests use as the values of the Git LFS objects they create, and updated
our "lfstest-gitserver" test utility to recognize the new keys.  All
of the keys used by the tests in the "t/t-batch-storage-retries.sh"
now begin with the same "storage-download-" prefix.

In particular, we changed the value of the special object content key
used by the "batch storage HTTP download retries after Range header
rejected" test from "batch-resume-fail-fallback" to
"storage-download-retry-range-rejected".

As was pointed out in PR review by larsxschneider, we should also
rename the internal batchResumeFailFallbackStorageAttempts global
variable in our "lfstest-gitserver" utility to align with the new
content key value, since this variable is used exclusively when the
test server responds to requests made by the "batch storage HTTP
download retries after Range header" test.

We therefore rename the batchResumeFailFallbackStorageAttempts variable
to storageDownloadRetryRangeRejectedAttempts now.
In commits c9c457c and
c8817ff of PR git-lfs#1297 we implemented
a pair of tests to verify that the Git LFS client attempts to resume
interrupted object download transfer requests by retrying the request
and adding a Range header.  The first confirms that the client
correctly handles a successful response with a 206 Partial Content status
code, while the second test ensures that if the server replies with a
416 Range Not Satisfiable status code, the client then retries the full
original request without a Range header.

(Note that in prior commits in this PR we moved these tests from their
previous location in the "t/t-resume-http-range.sh" shell script to a
new script file named "t/t-batch-storage-retries.sh", and we also
renamed the tests to better reflect their purpose.)

To simulate an HTTP response being interrupted, the same commits in
PR git-lfs#1297 updated the storageHandler() function in our "lfstest-gitserver"
test utility program to truncate the data it returned when an object's
content matched specific "magic" strings.  The tests added in the PR
create objects with those special keys as their content, and expect
that downloads of those objects may be interrupted, given certain other
conditions.

When these conditions apply, the storageHandler() function truncates the
data it sends in its responses to GET requests by returning only a slice
from the byte array that contains the object data.

At present, the truncation performed by the storageHandler() function
in these cases occurs at fixed offsets.  During the "batch storage HTTP
download retries with Range header" test the function returns only the
first 10 bytes of the object, unless a Range header was provided.  The
test then checks that the Git LFS client makes another request with a
Range header with a start position index of 10 bytes, and that the server
responds with a Content-Range header with the same start position index.

In a similar manner, during the "batch storage HTTP download retries
after Range header rejected" test, the storageHandler() function replies
to the first request made without a Range header by truncating its
response after 8 bytes.  It then rejects any requests made with a Range
header, but returns a complete response to subsequent requests made
without Range headers.  The test checks that the client makes one
request with a Range header with a start position index of 8 bytes,
before making another request without a Range header.

In both cases, though, the specific byte offsets are hard-coded in the
"lfstest-gitserver" utility.  This design dates from the original
versions of the tests from PR git-lfs#1297.  We then updated the tests in a
previous commit in this PR to search for these same hard-coded offsets
in the HTTP headers sent and received by the client.

During review of this PR, larsxschneider suggested documenting the source
of these hard-coded start position indexes using code comments.  However,
we can further improve on the original test design by changing the
"lfstest-gitserver" utility to truncate its responses programmatically,
and by changing the tests to expect these truncation points using the
same logic.  Specifically, we update the utility so that it returns the
first half of the object data (after rounding down to the nearest byte)
rather than at a fixed offset of either 8 or 10 bytes.

Next, we update the two affected tests so they calculate the same
midpoint offsets into their object data and then check for these values
in the start position indexes of the Range and Content-Range headers.

Finally, we adjust the comments in both tests to reflect the fact that
they expect the "lfstest-gitserver" test server to interrupt some of
its responses halfway through the object data.
@chrisd8088
chrisd8088 merged commit 52e7457 into git-lfs:main Apr 1, 2026
10 checks passed
@chrisd8088
chrisd8088 deleted the fix-http-encoding-range-tests branch April 1, 2026 06:31
chrisd8088 added a commit to mzr/git-lfs that referenced this pull request Apr 9, 2026
In prior commits in this PR we introduced a new configuration option
named "lfs.httpDownloadEncoding", which may be set to either of the
values "gzip" or "zstd", with a default value of "gzip".

When our new option is not defined or is set to "gzip", the Git LFS
client simply follows its legacy behaviour when making object transfer
download requests over HTTP.  Specifically, the client should allow the
"net/http" package in the Go standard library to insert an
"Accept-Encoding: gzip" header into the requests, and if the server
chooses to respond with gzip-encoded data, the client should expect
the "net/http" package to automatically decode the body of the response.

A test of this legacy behaviour was first introduced in PR git-lfs#4345.
More recently, in PR git-lfs#6207, we created the "t/t-batch-storage-encoding.sh"
shell script and moved the test into that script, while also renaming
the test to "batch storage HTTP download with gzip encoding".

We now expand the test to verify that the client continues to behave
as it has previously when our new configuration option is explicitly
set to "gzip", either universally or for a specific URL.
chrisd8088 added a commit to mzr/git-lfs that referenced this pull request Apr 9, 2026
In prior commits in this PR we introduced a new configuration option
named "lfs.httpDownloadEncoding", which may be set to either of the
values "gzip" or "zstd", with a default value of "gzip".  We also
added a set of tests to confirm that the Git LFS client sends an
Accept-Encoding header with the selected encoding type when making
object transfer download HTTP requests, and that the client is able
to decode the response data if the server chooses to encode it.

We now move those tests into the "t/t-batch-storage-encoding.sh"
script we created recently in PR git-lfs#6207, and rename and refactor them
to follow the design of the existing test in that script.  In doing
so, we combine the "lfs.httpDownloadEncoding zstd" test and the
"lfs.<url>.httpDownloadEncoding zstd" test into a single test
named "batch storage HTTP download with zstd encoding".

We also elide the "lfs.httpDownloadEncoding gzip does not set header"
test, as the existing "batch storage HTTP download with gzip encoding"
test should be sufficient to demonstrate that when our new configuration
option is explicitly set to "gzip", all the Accept-Encoding headers
we expect the client to send have the value "gzip".

In our "batch storage HTTP download with zstd encoding" test we check
that the client sends an "Accept-Encoding: zstd" header when downloading
individual objects, but still sends an "Accept-Encoding: gzip" header
in its Batch API requests, since we only support zstd encoding for
object transfer downloads.  We use the -c option to the grep(1) command
to verify that we see the expected number of Accept-Encoding headers
with each value.

As well as checking the Accept-Encoding headers, we also update the
test and our "lfstest-gitserver" utility program so that we can confirm
that the Git LFS client properly decodes the object data it receives
from a server, if a "Content-Encoding: zstd" response header indicates
that the data is zstd-encoded.

First, we enhance the "lfstest-gitserver" utility so that when handling
a GET request for an object, if the object's content is the special key
string "storage-download-encoding-zstd" and if an "Accept-Encoding: zstd"
header was sent, then the program returns a response with a
"Content-Encoding: zstd" header and a zstd-encoded body.

Next, in our "batch storage HTTP download with zstd encoding" test we
check that a single "Content-Encoding: zstd" header was received by each
"git lfs pull" command, and we use our assert_local_object() assertion
to verify that a local copy of the object has been created.

In a previous commit in this PR we updated the "batch storage HTTP
download with gzip encoding" test so that it repeats its checks
of the client's default behaviour with the "lfs.httpDownloadEncoding"
option explicitly set to the value "gzip", and then repeats the checks
again with a URL-specific version of the option set to the value "gzip".

We follow this pattern in our "batch storage HTTP download with zstd
encoding" test, except that the test does not exercise the client's
default behaviour, since the client sends an "Accept-Encoding: gzip"
header when performing object downloads in that case.

In both tests, when we set the URL-specific configuration option, we
use the value of the GITSERVER shell variable for the URL, since it is
conveniently available and generic across all our tests' repositories.

We also ascertain that each "git lfs pull" command succeeds by checking
the first element of the PIPESTATUS array variable.  This is necessary
because the shell's "errexit" attribute ignores the exit codes of all
commands in a pipeline other than the final one, and we pipe the output
of our "git lfs pull" commands into the tee(1) command to capture a
log file.  As in many of the other tests in our suite, we use the name
"pull.log" for this file.
chrisd8088 added a commit to mzr/git-lfs that referenced this pull request Apr 9, 2026
In prior commits in this PR we introduced a new configuration option
named "lfs.transfer.httpDownloadEncoding", which may be set to either
of the values "gzip" or "zstd", with a default value of "gzip".

When our new option is not defined or is set to "gzip", the Git LFS
client simply follows its legacy behaviour when making object transfer
download requests over HTTP.  Specifically, the client should allow the
"net/http" package in the Go standard library to insert an
"Accept-Encoding: gzip" header into the requests, and if the server
chooses to respond with gzip-encoded data, the client should expect
the "net/http" package to automatically decode the body of the response.

A test of this legacy behaviour was first introduced in PR git-lfs#4345.
More recently, in PR git-lfs#6207, we created the "t/t-batch-storage-encoding.sh"
shell script and moved the test into that script, while also renaming
the test to "batch storage HTTP download with gzip encoding".

In order for our test to detect that an object transfer download request
was gzip-encoded, in PR git-lfs#4345 we also updated the DoWithRedirect()
method of the Client structure in our "lfshttp" package.  The method
checks whether the "net/http" package has set the Uncompressed field
in the Response structure it returns, which would indicate that the
response body was gzip-encoded and that it has been decoded for us.

If the Uncompressed field is set, our DoWithRedirect() method outputs
a trace log message.  The test is then able to use the presence of
this message in the logs it captures from each "git lfs pull" command
to determine that the client received a gzip-encoded response to an
object transfer download request and handled it appropriately.

We would now like to include a similar check in the "batch storage
HTTP download with zstd encoding" test that we added to our
"t/t-batch-storage-encoding.sh" shell script in previous commits
in this PR.

This test verifies that the changes we have made in this PR work as
expected to implement support for zstd-encoded object HTTP downloads.
Specifically, when our new configuration option is set to the value
"zstd", the Git LFS client should send an "Accept-Encoding: zstd"
header with each object download request, and should decode the
response body appropriately if the server chooses to encode it using
the zstd algorithm.

We therefore update the download() method of the basicDownloadAdapter
structure in our "tq" package to output a trace log message when
processing a zstd-encoded response to an object transfer download
request.  We then update our "batch storage HTTP download with zstd
encoding" test to check for this message in the logs it captures from
each "git lfs pull" command.
chrisd8088 added a commit to mzr/git-lfs that referenced this pull request Apr 9, 2026
In prior commits in this PR we introduced a new configuration option
named "lfs.transfer.httpDownloadEncoding", which may be set to either
of the values "gzip" or "zstd", with a default value of "gzip".

When the option is set to the value "zstd", the Git LFS client will
send an "Accept-Encoding: zstd" header when it makes an object transfer
download HTTP request.  If the server chooses, it may then reply with
a response that includes a "Content-Encoding: zstd" header and whose
body is zstd-encoded.  When this occurs, the client should recognize
the Content-Encoding header and attempt to decode the response body
using the zstd algorithm.

If the response to an object transfer download request is interrupted,
the client may only receive a portion of the object data, and when the
data is zstd-encoded, the client may only be able to decode an even
smaller amount of the data than was received, or none at all.

During an object transfer download, the Git LFS client writes the data
it receives into a temporary file in the ".git/lfs/incomplete"
directory.  If the download is interrupted but some data has been
received and written to the temporary file, the client renames the
file with the extension ".part" and leaves the file in place so it is
available if the client tries to download the object again.

While executing a single command, the client will retry a failed
object transfer download request up to the limit defined by the
"lfs.transfer.maxRetries" configuration option.  Each time the client
attempts to download the object again, if partial data is available in
a file with the ".part" extension in the ".git/lfs/incomplete" directory,
the client will make an HTTP request with a Range header that specifies
only the outstanding portion of the object data.

Further, if a command fails to completely download an object, but
another Git LFS command is run later to download the same object, it
will also make a request with a Range header if an incomplete portion
of the object's data exists in a file with the ".part" extension.

When a Range header is set for an HTTP request, the "net/http" package
in the Go standard library will not automatically insert an
"Accept-Encoding: gzip" header:

  https://github.com/golang/go/blob/go1.25.7/src/net/http/transport.go#L2768
  https://github.com/golang/go/blob/go1.25.7/src/net/http/transport.go#L2779-L2781

While the HTTP specification allows both Range and Accept-Encoding
headers to appear in a request, if the content is encoded by the server
then the start and end position indexes in the Range header should be
interpreted as offsets into the encoded content:

  https://www.rfc-editor.org/rfc/rfc9110.html#section-14.1.2-3

Since the "net/http" package will decode a gzip-encoded response body
if the package added an "Accept-Encoding: gzip" header to the request,
the caller may never be aware that the server supports encoding.  Hence
any start and end positions provided in a Range header are likely to be
offsets into the full, uncompressed source data, and so the "net/http"
package does not insert an Accept-Encoding header when a Range header
is defined.

In the case where our Git LFS client sends a request with an
"Accept-Encoding: zstd" header, we are responsible for decoding the
response body, if the server chooses to compress the source data with
the zstd algorithm.  Should a zstd-encoded response be interrupted,
we may have written some of the decoded data to a temporary file,
so when we retry the request, the byte indexes we use in the Range
header will be offsets into the original object data and not offsets
into its encoded version.

Therefore, we avoid adding an "Accept-Encoding: zstd" header when we
retry a request, unless no temporary file exists for the given object,
or a temporary file exists but is empty.

To implement this change we simply modify the download() method of the
basicDownloadAdapter structure in our "tq" package so that it only
sends an "Accept-Encoding: zstd" header when the "fromByte" variable
has a zero value.  This variable will be set to the size of any
temporary file with previously-downloaded data for the current object,
and so a Range header will be included in the next request with the
"fromByte" variable's value used as the start position byte index.

(In theory, we could try to determine where the encoded data stream
from the response was interrupted, if we read from the stream before
we passed it the Decoder structure from the "zstd" package of the
"github.com/klauspost/compress" module.  Our intermediate reader
would then be able to detect when an unexpected EOF appeared in the
encoded data stream.  However, this would also entail significant
other changes, as we would need to retain the offset into the
encoded data stream between retry attempts, and potentially even
between attempts made by separate Git LFS commands.)

To verify that our changes are effective, we introduce a new "batch
storage HTTP download with zstd encoding retries with Range header
and without encoding" test to our "t/t-batch-storage-encoding.sh" shell
script.  This test creates an object whose content is the special key
string "storage-download-encoding-zstd-retry-range", which our
"lfstest-gitserver" test utility will now recognize when responding
to a GET request for an object.

We update our "lfstest-gitserver" utility so that when this special
content key is detected, the program calls the handleRangeRequest()
function we recently developed in PR git-lfs#6207.  This function handles
requests that are expected to have a Range header, and will return a
400 Bad Request status code to the client if it also sent an
Accept-Encoding header.

However, when the "git lfs pull" command run by our new test makes
its first object transfer download request, it will not send a Range
header but will send an "Accept-Encoding: zstd" header.  To simulate
an interrupted response to this first request, we further update
the "lfstest-gitserver" utility so that it returns only one frame
of zstd-encoded data, representing half of the complete object, plus
some but not all of the remaining encoded data.

By encoding the object's data in two separate frames, and sending at
least the initial frame, we guarantee that the client will be able to
fully decode the first frame, and so some data will be written to a
temporary file when the request is interrupted.  Otherwise the client
will leave an empty temporary file and so will keep retrying the request
with an "Accept-Encoding: zstd" header and without a Range header.

After the "git lfs pull" command completes, our new test then checks
the messages in the trace log it captured of the command's output.
The test confirms that only one request with an "Accept-Encoding: zstd"
header was made, and that the client resumed by making another request
with a Range header, to which the server responded with a 206 Partial
Content status code and a Content-Range header with the expected
byte range indexes for the missing portion of the object's data.
chrisd8088 added a commit that referenced this pull request Jun 2, 2026
We first introduced support for the use of HTTP/1.1 chunked
transfer encoding during Git LFS object uploads in commit
962be37 of PR #386, and in that
commit we added the original version of what is now the "chunked
transfer encoding" test in our "t/t-chunked-transfer-encoding.sh"
shell test script.

The initial version of that script was located in a directory named
"test" and was named "test-chunked-transfer-encoding.sh".
Then in PR #3125 we renamed the "test" directory to "t", and in
commit 219b7e0 of that PR we also
renamed all the shell test scripts within the directory to have
the prefix "t-" instead of "test-".

One unintended consequence of this change, though, was that it
would have silently rendered the "chunked transfer encoding" test
ineffective, except that for several unrelated reasons the test
was already ineffective and had been so since its initial version.

We explain the chain of events which led to this state of affairs
in more detail below, but the key reason why we have not detected
any of these problems until now is that the test is simply a near-
duplicate of the generic, example "happy path" test from our
"t/t-happy-path.sh" script.  As such it relies entirely on our
"lfstest-gitserver" utility program to modify its behaviour after
recognizing the name of the test's repository, and to cause the
test to fail if the Git LFS client does not behave as the server
expects.

However, the server has never correctly validated that the client
will send a Transfer-Encoding header with the value "chunked" when
instructed to do so.  Because the shell test does not verify this
itself, the test has always trivially passed, even as subsequent
changes further degraded the implementation of the related code in
our server utility.

To help resolve these issues and prevent a similar recurrence in
the future, we start by replacing the test with two new tests
in the "t/t-batch-storage-encoding.sh" script that we added recently
in PR #6207.

The first of these tests, named "batch storage HTTP upload with
chunked transfer encoding", creates a single Git LFS object and
uploads it to the test server utility, as did the original "chunked
transfer encoding" test.  Unlike the original test, though, our new
test captures the trace log output from the Git LFS client and
verifies using the grep(1) command that the headers of the PUT request
used to upload the object include a Transfer-Encoding header with the
value "chunked".  Further, the test also checks that the PUT request's
headers did not include a Content-Length header.

Because this test independently verifies the presence in the trace
log of a Transfer-Encoding header from the object upload PUT request,
we are now guaranteed that the test itself will catch any future
regression of the Git LFS client's support for chunked transfer
encoding, even if our "lfstest-gitserver" program fails to do so.

We want to avoid the possibility that our test's "grep" commands could
match against the HTTP headers captured in the trace log from the
client's Git LFS Batch and Locking API requests, which precede the
final object upload PUT request.  To exclude these prior requests from
the log, we use the awk(1) command to match against the PUT request
line, and then only output that line and all subsequent lines into
a new "put.log" file.

Unlike the legacy "chunked transfer encoding" test, our new test
simply checks that its Git LFS object is successfully uploaded, as
there is no particular value in then performing a download operation
as well.  The legacy test only included this extra step because the
whole test was copied from the sample "happy path" test without
modification, and that test illustrates both upload and download
operations.

Our second new test, named "batch storage HTTP upload without transfer
encoding", performs a similar set of steps as our first new test, but
assumes that the client has not been instructed to use chunked
transfer encoding.  The test therefore checks that the headers of the
object upload PUT request do not include a Transfer-Encoding header,
but do include a Content-Length header whose value is the size of
the object.

While this test does not require the "lfstest-gitserver" utility to
act differently than normal, our first new test obviously does, as
we need the server to instruct the Git LFS client to include a
Transfer-Encoding header when uploading objects.

As with the original "chunked transfer encoding" test from PR #386,
we expect the test server utility to change its behaviour when it
recognizes the name of the test's repository.  Both the utility's
lfsBatchHandler() and storageHandler() functions were revised in that
PR to call the testingChunkedTransferEncoding() function, which is
intended to return "true" if the name of the repository begins with
a specific prefix.

Since the initial implementation of the "chunked transfer encoding"
test, the name of the repository the test creates has been derived
from the name of the enclosing shell script.  Because the script was
named "test/test-chunked-transfer-encoding.sh", the server utility's
testingChunkedTransferEncoding() function was written to compare a
request's URL path with the prefix "/test-chunked-transfer-encoding",
as our shell test suite makes Git LFS Batch API requests whose first
URL path component is the name of a repository.

This design worked as intended for Batch API requests (at least until
the changes from PR #3125).  The Git LFS client would make a Batch API
request to upload a single object, and the server utility would return
a JSON response whose "header" element contained a "Transfer-Encoding"
key with the value "chunked".  The client then uploaded the object
using a PUT request with a "Transfer-Encoding: chunked" header.

Our "lfstest-gitserver" utility handles PUT requests with its
storageHandler() function, and the intent of the changes from
962be37 of PR #386 was that this
function would call the testingChunkedTransferEncoding() function
to determine if the PUT request should include a Transfer-Encoding
header or not.  If the testingChunkedTransferEncoding() function
returned "true", then the storageHandler() function would check
for a Transfer-Encoding header with the value "chunked", and if one
was not provided by the client, log an error and halt the server.
If this occurred, the client would have then reported an error and
exited, causing the test to fail.

However, even if the Git LFS client did not send a Transfer-Encoding
header when instructed to by the Batch API response, the test server
utility's storageHandler() function could never have detected this
because its call to the testingChunkedTransferEncoding() function
always received a return value of "false".

The reason the testingChunkedTransferEncoding() function would always
return "false" when called by the storageHandler() function is that
the URLs generated by the "lfstest-gitserver" utility for individual
Git LFS objects always begin with the path prefix "/storage/" rather
than a repository name.  We do now include the repository name in
these requests as the value of a query parameter named "r", but
this enhancement was only made subsequent to PR #386, specifically
in commit 27cf051 of PR #463.
Even so, the testingChunkedTransferEncoding() function still only
checks the initial component of a request's URL path, and so can
never return "true" when passed an object upload request.

To resolve this long-standing problem, we now revise the
testingChunkedTransferEncoding() function so that it requires a
repository name as its sole parameter, and then returns "true" if
that name matches a specific string (without a leading URL path
separator, since repository names are simply directory names).
Since the function is called from both the lfsBatchHandler() and
storageHandler() functions, we update each of these to pass the
appropriate "repo" variable to the testingChunkedTransferEncoding()
function.  In the case of the lfsBatchHandler() function, the
"repo" variable is populated from the URL path of the current
request, whereas in the storageHandler() function the variable
is set with the value of the "r" query parameter.

We also set the repository name created by our new "batch
storage HTTP upload with chunked transfer encoding" test to
"batch-storage-upload-encoding-chunked", and update the
testingChunkedTransferEncoding() function to compare its input
parameter against that name.  This repository name better reflects
both the name of the test and the name of the test's shell script,
and as such aligns with the repository names used by the other
tests in the same script.

We next address another problem with the storageHandler() function
and the way it responds if the testingChunkedTransferEncoding()
function returns "true".  In the original implementation from commit
962be37 of PR #386, if the
storageHandler() function were to receive a return value of "true"
from the testingChunkedTransferEncoding() function, the
storageHandler() function would then check that the current request
included a Transfer-Encoding header with the value of "chunked".

If this value was not found, then the Fatal() function from the Go
standard library's "log" package would be called, causing the server
to halt, which in turn would cause the client to exit with a
non-zero status code and the test to fail.  Note, though, that as
we described above, this sequence of events could never actually
occur because the testingChunkedTransferEncoding() function would
always return "false" to the storageHandler() function.

When we introduced the debug() function to the "lfstest-gitserver"
utility in commit e2046fd of PR #1375,
we replaced calls to the Fatal() function of the "log" package with
calls to the debug() function.  Our debug() function does not cause
the calling program to halt immediately, unlike the "log" package's
Fatal() function.

In the other two instances where we replaced the Fatal() function
with our own debug() function, this change did not alter the
effectiveness of our tests.  In these instances, while the server
would no longer halt if an unexpected condition occurred, it still
proceeded to return a 4xx status code to the Git LFS client, which
would in turn cause the currently-executing test to fail.

However, the storageHandler() function depended on the "log" package's
Fatal() function to cause the server to exit immediately, and did
not implement any other indication to the client that because a
Transfer-Encoding header was expected but not received, an error
condition had occurred.

To resolve this problem, we change the storageHandler() function
so that if it finds that the Git LFS client has not sent a
"Transfer-Encoding: chunked" header when it was instructed to do so,
the function now returns a 400 Bad Request HTTP status code to the
client.  This will cause the client to retry the request, and then
after repeatedly receiving 400 status code responses, the client
will report an error and exit with a non-zero status, causing the
test to fail.

We also take the opportunity to simplify the code used by the
storageHandler() function to check for a "Transfer-Encoding: chunked"
header.  Previously, the function had to iterate through the list of
values in the TransferEncoding field of the Request structure from
the Go standard library's "http" package, checking each value against
the string "chunked".  We can now replace this explicit loop with
a call to the Contains() function of the Go standard library's
"slices" package, since that package was added in Go v1.21.

Finally, because our new "batch storage HTTP upload with chunked
transfer encoding" test creates a repository with the fixed name of
"batch-storage-upload-encoding-chunked", and because we have modified
the testingChunkedTransferEncoding() function to check for that
specific repository name, we have implicitly addressed the third
problem with our existing "chunked transfer encoding" test.

As noted above, in commit 219b7e0
of PR #3125 we renamed all our shell test scripts to begin with "t-"
rather than "test-", and an unintended consequence of this change
was that the testingChunkedTransferEncoding() function would no longer
recognize the repository name from the "chunked transfer encoding"
test, even when handling Batch API requests made by the test.

As the test was originally written, it passed the name of the script
containing the test to the basename(1) command and then used the
result as the name of the test's primary Git repository.  Since the
test's script was initially named "test-chunked-transfer-encoding.sh",
the testingChunkedTransferEncoding() function checked for a URL path
which began with the prefix "/test-chunked-transfer-encoding" in
order to match the name of the test's repository.

When the script was renamed to "t-chunked-transfer-encoding.sh"
in PR #3125, though, the test's repository name automatically
changed to "t-chunked-transfer-encoding".  As a result, the
testingChunkedTransferEncoding() function could no longer recognize
the repository name, and so the lfsBatchHandler() function stopped
sending Batch API responses with "Transfer-Encoding" keys in the
"header" element.  Since the test made no independent check that
the Git LFS client sent a Transfer-Encoding header, the test still
continued to succeed.

Fortunately, the legacy "chunked transfer encoding" test was the
only test affected in this way by the shell script name changes from
PR #3125.  While the "lfstest-gitserver" utility at that point had
several functions similar to the testingChunkedTransferEncoding()
function, each of which checked for a specific repository name prefix
in the URL path of a request, the tests which relied on these
functions to recognize their repositories all used fixed names for
their repositories rather than deriving them from the names of the
enclosing scripts.
chrisd8088 added a commit that referenced this pull request Jun 2, 2026
In previous commit in this PR we resolved several problems with our
legacy test of the Git LFS client's support for HTTP/1.1 chunked
transfer encoding during Git LFS object uploads.  One of these
problems resulted from a mismatch between the name of the repository
created by the previous version of the test and the URL path prefix
that was expected by the testingChunkedTransferEncoding() function
in our "lfstest-gitserver" test utility program.

When the "chunked transfer encoding" test was first introduced, it was
located in a shell script named "test-chunked-transfer-encoding.sh" in
a directory named "test".  The test passed the name of its containing
script to the basename(1) command and then used the result as the
name of the test's primary Git repository.  Our "lfstest-gitserver"
utility's testingChunkedTransferEncoding() function then checked for a
URL path which began with the prefix "/test-chunked-transfer-encoding"
in order to match the name of the test's repository.

In PR #3125 we renamed the name of our shell test directory from
"test" to "t", and in commit 219b7e0
of that PR we renamed all our shell test scripts to begin with "t-"
rather than "test-".  An unintended consequence of this change,
though, was that the testingChunkedTransferEncoding() function would
no longer recognize the repository name from the "chunked transfer
encoding" test.  Due to the design of the shell test, it continued
to pass, but without exercising the behaviour of the Git LFS client
it was intended to verify.

Fortunately, the legacy "chunked transfer encoding" test was the
only test affected in this way by the shell script name changes from
PR #3125.  While the "lfstest-gitserver" utility at that point had
several functions similar to the testingChunkedTransferEncoding()
function, each of which checked for a specific repository name prefix
in the URL path of a request, the tests which relied on these
functions to recognize their repositories all used fixed names for
their repositories rather than deriving them from the names of the
enclosing scripts.

To help resolve the problems with the "chunked transfer encoding"
test, we replaced it with a new "batch storage HTTP upload with
chunked transfer encoding" test in our "t/t-batch-storage-encoding.sh"
script.  This test uses a fixed name for its repository rather than
one dynamically determined from the name of the containing script.
We then updated the testingChunkedTransferEncoding() function in
our server utility to check that the current request's repository
name exactly matched that fixed string.

We also revised the testingChunkedTransferEncoding() function so
that its sole parameter is a repository name string rather than
a Request structure from the Go standard library's "http" package.
This change resolved another problem with the legacy "chunked
transfer encoding" test that was due to the fact that the URL paths
of requests to upload individual Git LFS objects do not begin with
the repository name as an initial path component, and the original
test's implementation did not take account of this.  Although the
testingChunkedTransferEncoding() function was called while handling
an object upload, it could only ever return "false" in this case
because the URL path would never begin with a repository name.

There are three other functions in our "lfstest-gitserver" utility
which were all modelled on the testingChunkedTransferEncoding()
function, and as such all check the URL path of a Request structure
against a specific prefix.  These functions are named
testingTusUploadInBatchReq(), testingTusUploadInterruptedInBatchReq(),
and testingCustomTransfer().

As we would like to reduce the chance that we might in the future
accidentally introduce problems similar to those we found with the
"chunked transfer encoding" test, we now revise these three
functions so their input parameters are repository names rather
than Request structures.  The functions are all called from the
lfsBatchHandler() function of the server utility, which has already
extracted a repository name from the initial path component of
the current request, so in each case we can just pass the "repo"
variable to the three functions.

We also update the repository names used by the associated shell
tests to eliminate the legacy "test-" prefix and to align the
repository names more closely with the names of the tests as
they presently exist.  For instance, since PR #6207 the tests which
rely on the testingTusUploadInBatchReq() function and the
testingTusUploadInterruptedInBatchReq() function are now in the
"t/t-batch-storage-upload-tus.sh" script, but were originally
introduced in a "test/test-resume-tus.sh" script in PR #1303.
Meanwhile, the tests which rely on the testingCustomTransfer()
function are now in the "t/t-custom-transfers.sh" script but were
first introduced in a "test/test-custom-transfers.sh" script
in PR #1367.

Finally, we update the testingTusUploadInterruptedInBatchReq()
function so it performs a more constrained check of the given
repository name, as it only needs to return "true" if the name
exactly matches the string "batch-storage-upload-tus-interrupt".
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants