feat(http): use proxy from environment for HTTP calls

resolves #3897
This commit is contained in:
Jan De Dobbeleer 2023-05-25 14:52:01 +02:00 committed by Jan De Dobbeleer
parent 7d5a6b881e
commit edf93c3883
5 changed files with 21 additions and 18 deletions

View file

@ -6,6 +6,8 @@ import (
"net/http"
"strconv"
"time"
"github.com/jandedobbeleer/oh-my-posh/src/platform"
)
type codePoints map[int]int
@ -13,7 +15,6 @@ type codePoints map[int]int
func getGlyphCodePoints() (codePoints, error) {
var codePoints = make(codePoints)
client := &http.Client{}
ctx, cncl := context.WithTimeout(context.Background(), time.Millisecond*time.Duration(5000))
defer cncl()
@ -22,7 +23,7 @@ func getGlyphCodePoints() (codePoints, error) {
return codePoints, &ConnectionError{reason: err.Error()}
}
response, err := client.Do(request)
response, err := platform.Client.Do(request)
if err != nil {
return codePoints, err
}

View file

@ -8,10 +8,10 @@ import (
"errors"
"fmt"
"io"
"net"
"net/http"
"net/url"
"time"
"github.com/jandedobbeleer/oh-my-posh/src/platform"
)
func Download(fontPath string) ([]byte, error) {
@ -38,20 +38,11 @@ func isZipFile(data []byte) bool {
}
func getRemoteFile(location string) (data []byte, err error) {
client := &http.Client{
Transport: &http.Transport{
Dial: (&net.Dialer{
Timeout: 10 * time.Second,
}).Dial,
TLSHandshakeTimeout: 10 * time.Second,
ResponseHeaderTimeout: 10 * time.Second,
},
}
req, err := http.NewRequestWithContext(context.Background(), "GET", location, nil)
if err != nil {
return nil, err
}
resp, err := client.Do(req)
resp, err := platform.Client.Do(req)
if err != nil {
return
}

View file

@ -7,6 +7,8 @@ import (
"net/http"
"strings"
"time"
"github.com/jandedobbeleer/oh-my-posh/src/platform"
)
type release struct {
@ -22,7 +24,6 @@ type Asset struct {
func (a Asset) FilterValue() string { return a.Name }
func Nerds() ([]*Asset, error) {
client := &http.Client{}
ctx, cancelF := context.WithTimeout(context.Background(), time.Second*time.Duration(20))
defer cancelF()
req, err := http.NewRequestWithContext(ctx, "GET", "https://api.github.com/repos/ryanoasis/nerd-fonts/releases/latest", nil)
@ -30,7 +31,7 @@ func Nerds() ([]*Asset, error) {
return nil, err
}
req.Header.Add("Accept", "application/vnd.github.v3+json")
response, err := client.Do(req)
response, err := platform.Client.Do(req)
if err != nil || response.StatusCode != http.StatusOK {
return nil, errors.New("failed to get nerd fonts release")
}

View file

@ -1,7 +1,9 @@
package platform
import (
"net"
"net/http"
"time"
)
// Inspired by: https://www.thegreatcodeadventure.com/mocking-http-requests-in-golang/
@ -11,5 +13,13 @@ type httpClient interface {
}
var (
client httpClient = &http.Client{}
defaultTransport http.RoundTripper = &http.Transport{
Proxy: http.ProxyFromEnvironment,
Dial: (&net.Dialer{
Timeout: 10 * time.Second,
}).Dial,
TLSHandshakeTimeout: 10 * time.Second,
ResponseHeaderTimeout: 10 * time.Second,
}
Client httpClient = &http.Client{Transport: defaultTransport}
)

View file

@ -688,7 +688,7 @@ func (env *Shell) HTTPRequest(targetURL string, body io.Reader, timeout int, req
dump, _ := httputil.DumpRequestOut(request, true)
env.Debug(string(dump))
}
response, err := client.Do(request)
response, err := Client.Do(request)
if err != nil {
env.Error(err)
return nil, env.unWrapError(err)