Skip to content

Create new lfshttp package - #3244

Merged
PastelMobileSuit merged 4 commits into
masterfrom
lfshttp
Sep 14, 2018
Merged

PastelMobileSuit merged 4 commits into
masterfrom
lfshttp

Conversation

@PastelMobileSuit

Copy link
Copy Markdown
Contributor

This PR extracts much of the lower level, http request-handling functionality out of the lfsapi package and into a new lfshttp package.

The first commit (d101bdb) largely just extracts files into the new package with minimal changes, and updates lfsapi and other packages to rely on it. The second commit (e6c4c63) refactors the way redirects are handled so that lfsapi can re-authenticate on a redirect when the original request was authenticated.

PastelMobileSuit and others added 3 commits September 11, 2018 14:51
Extract more basic http-related functionality out of lfsapi and
into a new package, lfshttp. Everything is currently functional
aside from authorization.
Refactor the client redirect code to allow lfsapi to
re-authenticate redirected requests
@PastelMobileSuit

Copy link
Copy Markdown
Contributor Author

This is the first PR to handle #3194. The actual changes to the access mode management will land in a separate PR after this is merged.

@bk2204 bk2204 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Overall, this looks good. I made one suggestion, but I'd be happy with it where it stands.

Comment thread lfsapi/auth_test.go Outdated
return errors.New("Not implemented")
}

// func TestClientRedirectReauthenticate(t *testing.T) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It looks like you added this code commented out in the first commit which was uncommented in this commit. I wonder if it might be tidier if we deferred adding it until the second commit.

@PastelMobileSuit

Copy link
Copy Markdown
Contributor Author

Some further information on reviewing:

d101bdb is exclusively functionality extraction, moving client.go and any other files needed to get it functional out of lfsapi and into lfshttp. Most files were just moved straight over. Aside from that, there were some updates to lfsapi to make its client wrap the lfshttp client, and some minor test changes to get things functional (changing the package on some calls to lfshttp, manually constructing Endpoints in lfshttp tests instead of creating an EndpointFinder). All the previous tests are still present, and the functionality they're testing remain unchanged, with the exception of the Authorization Redirect test, which is removed in this commit, but added back in e6c4c63.

To review this commit, I'd take a look at lfsapi/lfsapi.go, lfsapi/client.go, lfshttp/lfshttp.go, and lfshttp/client.go to make sure the functionality split looks reasonable. Aside from that, I'd do a quick scan of the list of files moved into lfshttp to make sure the right functionality is present.

e6c4c63 moves some of the lfshttp.doWithRedirects() functionality out into a separate, publicly-accessible function so that it can be called from lfsapi.doWithCreds(). This allows us to have control over the redirect functionality from lfsapi.doWithCreds(), as we need to re-authenticate when an authenticated request returns a redirect. This also adds back the Authorization Redirect test removed from d101bdb. This commit can be reviewed normally.

@ttaylorr ttaylorr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@PastelMobileSuit Huge ❇️'s from me on this -- I'm unreasonably happy with how everything shook out between lfsapi and lfshttp.

It seems like we have a great path forward to write code in package tq that can't assume it's talking to a Git LFS server, and instead can use lfshttp directly (or choose between the two).

Thanks for your comment in #3244 (comment), it certainly helped me during the review process. In the future, do you think it would be possible to include some of that information in your commits, too? I like that we've started to include this type of historical record in the commits themselves, since it makes it easier when trying to recall why a change was made in the future to run git blame and read the last commit that touched a line (instead of opening the commit in the browser, finding the affected PR, and reading through its comments).

I think that in general the division between the commits is good. I think in general I find it difficult to review two commits that have nearly 2,000 SLOC worth of diff, but I think for changes like these there's no great atomic way to separate things out.

Anyhow, my TL;DR is: this change is great, so is the writeup you posted, and in the future I would love that same information in the commit.

👏

@PastelMobileSuit

Copy link
Copy Markdown
Contributor Author

@ttaylorr and @bk2204 Thanks so much for both of your reviews! @ttaylorr Thanks for the feedback with regards to the writeup, I will endeavor to add relevant info like that to the commit messages in the future!

@PastelMobileSuit
PastelMobileSuit merged commit 002c263 into master Sep 14, 2018
@PastelMobileSuit
PastelMobileSuit deleted the lfshttp branch September 14, 2018 19:36
@chrisd8088 chrisd8088 mentioned this pull request Nov 27, 2025
chrisd8088 added a commit to chrisd8088/git-lfs that referenced this pull request Jan 21, 2026
At present, when the GIT_CURL_VERBOSE environment variable is set to
a value such as "1" or "true", Git LFS client outputs some of the details
of each HTTP request and response it makes to its standard error stream.

We gather the HTTP request headers to be logged in the traceRequest()
method of the Client structure from our "lfshttp" package.  To gather
the headers, the traceRequest() method uses the DumpRequest() function
from the from the "net/http/httputil" package in the Go standard library.

Our use of the DumpRequest() function for this purpose dates back to
commit 6fafade of PR git-lfs#366 in 2015,
when it was first introduced into what was then the traceHttpRequest()
function in our "lfs" package.  This function gradually evolved into the
current traceRequest() method through a series of PRs.  First, portions
of the "lfs" package were migrated into a new "httputil" package in
PR git-lfs#1226.  That package was later removed in PR git-lfs#1846 after a new
"lfsapi" package was added in PR git-lfs#1794 with a version of the
traceRequest() method very similar to the current one.  Finally, in
PR git-lfs#3244 we moved the method into the current "lfshttp" package.

Throughout these changes, however, our use of the DumpRequest()
function has remained the same.  However, since the Git LFS client only
acts as an HTTP client and not an HTTP server, we should instead be
using the DumpRequestOut() function from the standard "httputil" package
to gather the headers of an HTTP request for logging purposes.

Replacing our use of the DumpRequest() function with the DumpRequestOut()
function would allow us to output a number of request headers that we
currently fail to log, as was noted with respect to the Accept-Encoding
header in issue git-lfs#6193.

Since Go version 1.6, the "httputil" package's documentation has stated
that the DumpRequest() function "should only be used by servers to debug
client requests", and that the DumpRequestOut() function "is like
DumpRequest but for outgoing client requests":

  https://pkg.go.dev/net/http/[email protected]#DumpRequest
  https://pkg.go.dev/net/http/[email protected]#DumpRequestOut

However, at the time we first adopted the use of the DumpRequest()
function, the Go documentation did not include these directives, and
merely noted that "DumpRequest returns the as-received wire representation
of req, optionally including the request body, for debugging" and that
"DumpRequestOut is like DumpRequest but includes headers that the
standard http.Transport adds, such as User-Agent":

  https://pkg.go.dev/net/http/[email protected]#DumpRequest
  https://pkg.go.dev/net/http/[email protected]#DumpRequestOut

It is reasonable to assume, therefore, that when PR git-lfs#366 was written
its authors simply assumed that the DumpRequest() function was sufficient,
given the state of the Go documentation at that time.

As we would prefer to log a more complete set of client HTTP request
headers, though, particular the Accept-Encoding header which the
Go "net/http" package automatically adds to outgoing requests, we
now update our traceRequest() method to use the DumpRequestOut()
function.

We then also update our Go language TestVerbose*() test functions
to confirm that the Accept-Encoding request header is now captured and
reported when verbose logging is enabled.  Note that without the change
in this PR to the use of the DumpRequestOut() function, these revised
tests would fail, since the Accept-Encoding header would not appear in
headers returned by the DumpRequest() function.
chrisd8088 added a commit to chrisd8088/git-lfs that referenced this pull request Jan 21, 2026
At present, when the GIT_CURL_VERBOSE environment variable is set to
a value such as "1" or "true", the Git LFS client outputs some of the
details of each HTTP request and response it makes to its standard
error stream.

We gather the HTTP request headers to be logged in the traceRequest()
method of the Client structure from our "lfshttp" package.  To gather
the headers, the traceRequest() method uses the DumpRequest() function
from the from the "net/http/httputil" package in the Go standard library.

Our use of the DumpRequest() function for this purpose dates back to
commit 6fafade of PR git-lfs#366 in 2015,
when it was first introduced into what was then the traceHttpRequest()
function in our "lfs" package.  This function gradually evolved into the
current traceRequest() method through a series of PRs.  First, portions
of the "lfs" package were migrated into a new "httputil" package in
PR git-lfs#1226.  That package was later removed in PR git-lfs#1846 after a new
"lfsapi" package was added in PR git-lfs#1794 with a version of the
traceRequest() method very similar to the current one.  Finally, in
PR git-lfs#3244 we moved the method into the current "lfshttp" package.

Throughout these changes, however, our use of the DumpRequest()
function has remained the same.  However, since the Git LFS client only
acts as an HTTP client and not an HTTP server, we should instead be
using the DumpRequestOut() function from the standard "httputil" package
to gather the headers of an HTTP request for logging purposes.

Replacing our use of the DumpRequest() function with the DumpRequestOut()
function would allow us to output a number of request headers that we
currently fail to log, as was noted with respect to the Accept-Encoding
header in issue git-lfs#6193.

Since Go version 1.6, the "httputil" package's documentation has stated
that the DumpRequest() function "should only be used by servers to debug
client requests", and that the DumpRequestOut() function "is like
DumpRequest but for outgoing client requests":

  https://pkg.go.dev/net/http/[email protected]#DumpRequest
  https://pkg.go.dev/net/http/[email protected]#DumpRequestOut

However, at the time we first adopted the use of the DumpRequest()
function, the Go documentation did not include these directives, and
merely noted that "DumpRequest returns the as-received wire representation
of req, optionally including the request body, for debugging" and that
"DumpRequestOut is like DumpRequest but includes headers that the
standard http.Transport adds, such as User-Agent":

  https://pkg.go.dev/net/http/[email protected]#DumpRequest
  https://pkg.go.dev/net/http/[email protected]#DumpRequestOut

It is reasonable to assume, therefore, that when PR git-lfs#366 was written
its authors simply assumed that the DumpRequest() function was sufficient,
given the state of the Go documentation at that time.

As we would prefer to log a more complete set of client HTTP request
headers, though, particularly the Accept-Encoding header which the
Go "net/http" package automatically adds to outgoing requests, we
now update our traceRequest() method to use the DumpRequestOut()
function.

We then also update some of our Go language TestVerbose*() test functions
to confirm that the Accept-Encoding request header is now captured and
reported when verbose logging is enabled.  Note that without the change
in this PR to the use of the DumpRequestOut() function, these revised
tests would fail, since the Accept-Encoding header would not appear in
headers returned by the DumpRequest() function.
chrisd8088 added a commit to chrisd8088/git-lfs that referenced this pull request Apr 26, 2026
In commit d101bdb of PR git-lfs#3244 we
extracted the portions of our "lfsapi" package specifically related
to HTTP request handling into a new package named "lfshttp".

As part of that process, we moved some of the initialization steps
formerly performed by the NewClient() function of our "lfsapi" package
into a separate NewClient() function in the "lfshttp" package.

Both NewClient() functions return an "error" type as their second
return value, and at the time of PR git-lfs#3244, the NewClient() function
in the "lfsapi" package could encounter an error condition and
return a non-nil "error" value.

However, none of the initialization steps that we moved to the
NewClient() function of the "lfshttp" package could encounter an
error condition, so there was no specific need for the function
to continue to return an "error" value.

As this remains the case, we can simplify the NewClient() function
in our "lfshttp" package by just eliminating its "error" return value.

We then update all the callers of this function, including those in
our Go test functions, as they no longer need to check for an "error"
return value from the function.
chrisd8088 added a commit that referenced this pull request May 21, 2026
In PR #1839 we first developed our "lfsapi" package, and in commit
fecdc9e of that PR we introduced the
getAPIClient() function to our "commands" package to ensure that we
only initialize a single, global Client structure from the "lfsapi"
package.

The getAPIClient() function acquires a global mutex, appropriately
named "global", and then checks if the "commands" package's "apiClient"
global variable has been initialized or not.  If not, the function
calls the NewClient() function of the "lfsapi" package and assigns
the newly instantiated Client structure to the "apiClient" variable.

Later, in commit 2435a67 of PR #2184,
we added an HTTPLogger field to the "lfsapi" package's Client
structure.  This field's type was that of a WriteCloser structure from
the Go standard library's "io" package, and since this type has a
Close() method, we also defined a Close() method for the Client
structure that called the HTTPLogger field's Close() method, unless
the field was nil.

In the same commit in PR #2184 we then updated the Run() function in
our "commands" package to call the new Close() method of the
Client structure, after first calling the getAPIClient() function
to ensure that the "apiClient" variable was not nil.

Subsequently, in commit d16a320
of PR #2633, we introduced a closeAPIClient() function to the
"lfsapi" package, which acquires the "global" mutex and then
calls the "apiClient" variable's Close() method, but only if the
variable is not nil.  By revising the Run() function to then just
call the closeAPIClient() function before returning, this change
avoided the need to instantiate a Client structure only in order
to close it.

Note that later, in PR #3244, we refactored the "lfsapi" package and
moved portions of its functionality into a new "lfshttp" package,
including the HTTP logging subsystem.  The Close() method of the
"lfsapi" package's Client structure now just calls the Close() method
of the "lfshttp" package's Client structure, and that structure is
the one which contains an httpLogger field.  Regardless, it remains
the responsibility of the "commands" package to call the Close()
method on its "apiClient" variable when necessary, in order to
release any resources associated with it or its internal components.

However, because the "apiClient" variable's Close() method is only
called when the Run() function is about to return, the resources
associated with our HTTP logging subsystem are only properly released
when the Git LFS client exits normally.  If the client encounters an
unrecoverable error condition, it invokes one of the exit wrapper
functions in our "commands" package, which call the Exit() function
of the Go standard library's "os" package and therefore do not
return.  As a result, the Run() function is not completely executed,
and the closeAPIClient() function will never be called.

Likewise, if the client receives a SIGINT or SIGKILL signal, the
signal handler established by our main() function in the "main"
package will halt the program by calling the "os" package's Exit()
function, and so the closeAPIClient() function will also not be
called in this case.

Fortunately, in previous commits in this PR as well as in PR #6255
we have made a series of revisions which ensure that when exiting,
the Git LFS client will always call the Cleanup() function in our
"commands" package, regardless of the circumstances under which the
client needs to halt.

We therefore now revise our "commands" package so that the
closeAPIClient() function is called from the Cleanup() function
rather than the Run() function.  This change will guarantee that
the resources associated with our HTTP logging subsystem are always
properly released when the Git LFS client exits.

We also take the opportunity to add a check of the "error" return
value from the closeAPIClient() function, so that if it is not nil,
the Cleanup() function will report the corresponding error message to
the standard error file descriptor.
chrisd8088 added a commit that referenced this pull request Aug 5, 2026
We define a "closingByteReader" structure with a no-op Close() method
in several of our packages, including the "tools", "lfsapi", and
"lfshttp" packages.  In each case, the purpose of the structure is
to allow us to use the structure returned by the NewReader() function
of the Go standard library's "bytes" package in contexts that require
a structure that implements the ReadSeekCloser interface from the
standard library's "io" package.

We introduced the first of these structures to our "lfsapi" package
in commit 6659b9d of PR #1839.
In a subsequent commit in that PR we then added a NewByteBody()
factory function which initializes a "closingByteReader" structure
by passing the byte slice provided as a parameter to the "bytes"
package's NewReader() function.  Next, in the same PR we replicated
the structure, its Close() method, and the NewByteBody() function
into what was at that time called our "progress" package.

In PR #2749 we later moved the "progress" package's contents to
our "tools" package, and then in PR #3244 we replicated the
structure, its Close() method, and the NewByteBody() function a
third time when we refactored portions of the "lfsapi" package
into our "lfshttp" package.

In our "tools" package we also provide a NewByteBodyWithCallback()
factory function which calls the NewByteBody() function and uses
the "closingByteReader" structure it returns as the "body"
parameter in a call to the NewBodyWithCallback() function.
The result is that a new BodyWithCallback structure is initialized
with the byte slice passed to the NewByteBodyWithCallback()
function as its underlying source of data.

This NewByteBodyWithCallback() function is used exclusively in
various test functions, both in our "tools" package and our "lfs"
package.  Ideally, though, we should not define functions or methods
in our non-test source files that are intended for use only in test
functions.

In prior commits in this PR we updated all of the test functions
which use the NewByteBodyWithCallback() function, and revised a
number of them so that they now perform their checks against the
methods of both a BodyWithCallback structure and the methods of a
CallbackReader structure.

To simplify the initialization of the CallbackReader structures, in
another prior commit in this PR we introduced a NewCallbackReader()
factory function, which operates in a similar manner to the
NewBodyWithCallback() function except that it returns a CallbackReader
structure instead of a BodyWithCallback structure.  We did not,
though, introduce a NewByteCallbackReader() function, as it is
relatively straightforward for our tests to simply pass the return
value from a call to the "bytes" package's NewReader() function to
our NewCallbackReader() function.

Given the marginal utility of the NewByteBodyWithCallback() function
and the fact that it is only used in our test functions, we remove
it now.  This also helps align the structure initialization steps
performed by our test functions, since they now just call the
NewBodyWithCallback() and NewCallbackReader() functions, which
initialize their respective structures in an equivalent manner.

To bring these initialization steps into even closer alignment, we
also rename the "closingByteReader" structure in the "tools" package
to ClosingByteReader, and rename its factory function from
NewByteBody() to NewClosingByteReader().

These changes will allow us to make use of the structure in our
"lfsapi" and "lfshttp" packages, replacing the identical definitions
of the "closingByteReader" structure and the related functions and
methods in those packages.  We defer this refactoring step to a
future PR, however, as it is outside of this PR's scope.

Finally, we move the definition of the ClosingByteReader structure,
its Close() method, and the NewClosingByteReader() function to
our "tools/iotools.go" source file, and add a new test function
named TestClosingByteReaderNopClose() to the "tools/iotools_test.go"
source file.  This test function simply verifies that the Close()
method of the ClosingByteReader structure performs no action, as we
expect that it should not do so.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants