forked from ollama/ollama
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
129 additions
and
102 deletions.
There are no files selected for viewing
This file contains 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 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,72 @@ | ||
package auth | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
"runtime" | ||
"strconv" | ||
|
||
"github.com/jmorganca/ollama/version" | ||
) | ||
|
||
type RegistryOptions struct { | ||
Insecure bool | ||
Username string | ||
Password string | ||
Token string | ||
} | ||
|
||
func MakeRequest(ctx context.Context, method string, requestURL *url.URL, headers http.Header, body io.Reader, regOpts *RegistryOptions) (*http.Response, error) { | ||
if requestURL.Scheme != "http" && regOpts != nil && regOpts.Insecure { | ||
requestURL.Scheme = "http" | ||
} | ||
|
||
req, err := http.NewRequestWithContext(ctx, method, requestURL.String(), body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if headers != nil { | ||
req.Header = headers | ||
} | ||
|
||
if regOpts != nil { | ||
if regOpts.Token != "" { | ||
req.Header.Set("Authorization", "Bearer "+regOpts.Token) | ||
} else if regOpts.Username != "" && regOpts.Password != "" { | ||
req.SetBasicAuth(regOpts.Username, regOpts.Password) | ||
} | ||
} | ||
|
||
req.Header.Set("User-Agent", fmt.Sprintf("ollama/%s (%s %s) Go/%s", version.Version, runtime.GOARCH, runtime.GOOS, runtime.Version())) | ||
|
||
if s := req.Header.Get("Content-Length"); s != "" { | ||
contentLength, err := strconv.ParseInt(s, 10, 64) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
req.ContentLength = contentLength | ||
} | ||
|
||
proxyURL, err := http.ProxyFromEnvironment(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
client := http.Client{ | ||
Transport: &http.Transport{ | ||
Proxy: http.ProxyURL(proxyURL), | ||
}, | ||
} | ||
|
||
resp, err := client.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return resp, nil | ||
} |
This file contains 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 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 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
Oops, something went wrong.