-
Notifications
You must be signed in to change notification settings - Fork 28
Add http.RoundTripper to remotefs FS via curl/PowerShell #322
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+777
−11
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| package remotefs | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "bytes" | ||
| "context" | ||
| "encoding/base64" | ||
| "errors" | ||
| "fmt" | ||
| "net/http" | ||
| "net/url" | ||
| "strings" | ||
| ) | ||
|
|
||
| const maxEncodedResponseSize = 32 * 1024 * 1024 // base64-encoded limit (~24 MiB decoded) | ||
|
|
||
| var ( | ||
| errNilRequest = errors.New("net/http: nil Request") | ||
| errNilRequestURL = errors.New("net/http: nil Request.URL") | ||
| errUnsupportedScheme = errors.New("unsupported URL scheme") | ||
| errMissingURLHost = errors.New("missing URL host") | ||
| errUserinfoInURL = errors.New("URL must not contain userinfo (credentials in URLs leak to remote command line)") | ||
| errInvalidURLChars = errors.New("URL contains control characters (CR, LF, or NUL)") | ||
| errInvalidHeader = errors.New("invalid header: contains CR, LF, or NUL") | ||
| errResponseTooLarge = errors.New("http response exceeds maximum size") | ||
| ) | ||
|
|
||
| // validateRoundTripURL returns an error if u has an unsupported scheme, no host, userinfo, or control characters. | ||
| // Must be called after a nil-URL guard. | ||
| func validateRoundTripURL(target *url.URL) error { | ||
| if strings.ContainsAny(target.String(), "\r\n\x00") { | ||
| return errInvalidURLChars | ||
| } | ||
|
kke marked this conversation as resolved.
|
||
| switch strings.ToLower(target.Scheme) { | ||
| case "http", "https": | ||
| default: | ||
| return fmt.Errorf("%w: %q", errUnsupportedScheme, target.Scheme) | ||
| } | ||
| if target.Host == "" { | ||
| return errMissingURLHost | ||
| } | ||
| if target.User != nil { | ||
| return errUserinfoInURL | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // HTTPTransport is implemented by remote filesystems that can proxy HTTP requests | ||
| // through the remote host. Since RoundTrip matches the http.RoundTripper signature, | ||
| // any FS value satisfies http.RoundTripper and can be used directly as http.Client.Transport. | ||
| // | ||
| // Note: RoundTrip materializes the entire HTTP response on the remote side before | ||
| // transferring it to the caller as a base64-encoded string. Depending on the | ||
| // implementation, the remote side may buffer the response in memory or write it to a | ||
| // temporary file, and the caller also buffers the full response while decoding and | ||
| // parsing it. It is not suitable for large response bodies; use DownloadURL for | ||
| // downloading large files instead. | ||
| type HTTPTransport interface { | ||
| DownloadURL(url, dst string) error | ||
| RoundTrip(req *http.Request) (*http.Response, error) | ||
| } | ||
|
kke marked this conversation as resolved.
|
||
|
|
||
| // HTTPStatus issues a HEAD request via t and returns the HTTP status code. | ||
| func HTTPStatus(ctx context.Context, t http.RoundTripper, url string) (int, error) { | ||
| req, err := http.NewRequestWithContext(ctx, http.MethodHead, url, nil) | ||
| if err != nil { | ||
| return 0, fmt.Errorf("http-status: %w", err) | ||
| } | ||
| resp, err := t.RoundTrip(req) | ||
| if err != nil { | ||
|
kke marked this conversation as resolved.
kke marked this conversation as resolved.
|
||
| return 0, fmt.Errorf("http-status %s: %w", req.URL.Redacted(), err) | ||
| } | ||
|
kke marked this conversation as resolved.
|
||
| _ = resp.Body.Close() | ||
| return resp.StatusCode, nil | ||
| } | ||
|
|
||
| // parseRawHTTPResponse decodes a base64-encoded raw HTTP/1.1 response and parses it. | ||
| // 100 Continue responses are consumed and discarded; all other responses are returned as-is. | ||
| func parseRawHTTPResponse(encoded string, req *http.Request) (*http.Response, error) { | ||
| cleaned := strings.NewReplacer("\r", "", "\n", "").Replace(strings.TrimSpace(encoded)) | ||
| if len(cleaned) > maxEncodedResponseSize { | ||
| return nil, fmt.Errorf("%w (%d bytes)", errResponseTooLarge, len(cleaned)) | ||
| } | ||
| raw, err := base64.StdEncoding.DecodeString(cleaned) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("decode http response: %w", err) | ||
| } | ||
| reader := bufio.NewReader(bytes.NewReader(raw)) | ||
|
kke marked this conversation as resolved.
|
||
| for { | ||
| resp, err := http.ReadResponse(reader, req) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("parse http response: %w", err) | ||
| } | ||
| if resp.StatusCode != http.StatusContinue { | ||
| return resp, nil | ||
| } | ||
| _ = resp.Body.Close() | ||
| } | ||
|
kke marked this conversation as resolved.
|
||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.