Skip to content

Commit 2ad7fb3

Browse files
authored
add cli httpclient with support for proxy configuration (#672)
* add configuration for cli httpclient * add httpclient roundtripper with proxy and user-agent * add cli httpclient * set downloader user-agent tp equal cli httpclient * use cli httpclient on board list command * add unit tests for httpclient * add copyright notice * upgrade downloader to v2 with httpclient injection * change go.mod to point to upstream downloader project v2
1 parent 06bc37e commit 2ad7fb3

File tree

15 files changed

+245
-46
lines changed

15 files changed

+245
-46
lines changed

arduino/cores/packagemanager/download.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"fmt"
2020

2121
"github.com/arduino/arduino-cli/arduino/cores"
22-
"go.bug.st/downloader"
22+
"go.bug.st/downloader/v2"
2323
semver "go.bug.st/relaxed-semver"
2424
)
2525

arduino/libraries/librariesmanager/download.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ package librariesmanager
1818
import (
1919
"net/url"
2020

21-
"go.bug.st/downloader"
21+
"go.bug.st/downloader/v2"
2222
)
2323

2424
// LibraryIndexURL is the URL where to get library index.

arduino/resources/helpers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"os"
2121

2222
"github.com/arduino/go-paths-helper"
23-
"go.bug.st/downloader"
23+
"go.bug.st/downloader/v2"
2424
)
2525

2626
// ArchivePath returns the path of the Archive of the specified DownloadResource relative

arduino/resources/helpers_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ import (
2323
"strings"
2424
"testing"
2525

26+
"github.com/arduino/arduino-cli/httpclient"
2627
"github.com/arduino/go-paths-helper"
2728
"github.com/stretchr/testify/require"
28-
"go.bug.st/downloader"
29+
"go.bug.st/downloader/v2"
2930
)
3031

3132
type EchoHandler struct{}
@@ -53,7 +54,9 @@ func TestDownloadApplyUserAgentHeaderUsingConfig(t *testing.T) {
5354
URL: srv.URL,
5455
}
5556

56-
d, err := r.Download(tmp, &downloader.Config{RequestHeaders: http.Header{"User-Agent": []string{goldUserAgentValue}}})
57+
httpClient := httpclient.NewWithConfig(&httpclient.Config{UserAgent: goldUserAgentValue})
58+
59+
d, err := r.Download(tmp, &downloader.Config{HttpClient: *httpClient})
5760
require.NoError(t, err)
5861
err = d.Run()
5962
require.NoError(t, err)

arduino/resources/resources_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222

2323
"github.com/arduino/go-paths-helper"
2424
"github.com/stretchr/testify/require"
25-
"go.bug.st/downloader"
25+
"go.bug.st/downloader/v2"
2626
)
2727

2828
func TestDownloadAndChecksums(t *testing.T) {

cli/globals/globals.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@
1616
package globals
1717

1818
import (
19-
"fmt"
20-
"net/http"
2119
"os"
2220
"path/filepath"
23-
"runtime"
2421

2522
"github.com/arduino/arduino-cli/version"
2623
)
@@ -31,18 +28,3 @@ var (
3128
// DefaultIndexURL is the default index url
3229
DefaultIndexURL = "https://downloads.arduino.cc/packages/package_index.json"
3330
)
34-
35-
// NewHTTPClientHeader returns the http.Header object that must be used by the clients inside the downloaders
36-
// and adds the subComponent if specified
37-
func NewHTTPClientHeader(subComponent string) http.Header {
38-
if subComponent != "" {
39-
subComponent = " " + subComponent
40-
}
41-
userAgentValue := fmt.Sprintf("%s/%s%s (%s; %s; %s) Commit:%s",
42-
VersionInfo.Application,
43-
VersionInfo.VersionString,
44-
subComponent,
45-
runtime.GOARCH, runtime.GOOS, runtime.Version(),
46-
VersionInfo.Commit)
47-
return http.Header{"User-Agent": []string{userAgentValue}}
48-
}

commands/board/list.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ import (
2323
"regexp"
2424
"sync"
2525

26-
"github.com/arduino/arduino-cli/cli/globals"
2726
"github.com/arduino/arduino-cli/commands"
27+
"github.com/arduino/arduino-cli/httpclient"
2828
rpc "github.com/arduino/arduino-cli/rpc/commands"
2929
"github.com/pkg/errors"
3030
"github.com/segmentio/stats/v4"
@@ -51,12 +51,17 @@ func apiByVidPid(vid, pid string) ([]*rpc.BoardListItem, error) {
5151
url := fmt.Sprintf("%s/%s/%s", vidPidURL, vid, pid)
5252
retVal := []*rpc.BoardListItem{}
5353
req, _ := http.NewRequest("GET", url, nil)
54-
req.Header = globals.NewHTTPClientHeader("")
5554
req.Header.Set("Content-Type", "application/json")
5655

5756
// TODO: use proxy if set
5857

59-
if res, err := http.DefaultClient.Do(req); err == nil {
58+
httpClient, err := httpclient.New()
59+
60+
if err != nil {
61+
return nil, errors.Wrap(err, "failed to initialize http client")
62+
}
63+
64+
if res, err := httpClient.Do(req); err == nil {
6065
if res.StatusCode >= 400 {
6166
if res.StatusCode == 404 {
6267
return nil, ErrNotFound

commands/download.go

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,24 @@
1616
package commands
1717

1818
import (
19-
"net/url"
2019
"time"
2120

22-
"github.com/arduino/arduino-cli/cli/globals"
21+
"github.com/arduino/arduino-cli/httpclient"
2322
rpc "github.com/arduino/arduino-cli/rpc/commands"
24-
"github.com/pkg/errors"
25-
"github.com/sirupsen/logrus"
26-
"github.com/spf13/viper"
27-
"go.bug.st/downloader"
23+
"go.bug.st/downloader/v2"
2824
)
2925

3026
// GetDownloaderConfig returns the downloader configuration based on
3127
// current settings.
3228
func GetDownloaderConfig() (*downloader.Config, error) {
33-
res := &downloader.Config{
34-
RequestHeaders: globals.NewHTTPClientHeader(viper.GetString("network.user_agent_ext")),
29+
30+
httpClient, err := httpclient.New()
31+
if err != nil {
32+
return nil, err
3533
}
36-
if viper.IsSet("network.proxy") {
37-
proxy := viper.GetString("network.proxy")
38-
if _, err := url.Parse(proxy); err != nil {
39-
return nil, errors.New("Invalid network.proxy '" + proxy + "': " + err.Error())
40-
}
41-
res.ProxyURL = proxy
42-
logrus.Infof("Using proxy %s", proxy)
34+
35+
res := &downloader.Config{
36+
HttpClient: *httpClient,
4337
}
4438
return res, nil
4539
}

commands/instances.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import (
3333
paths "github.com/arduino/go-paths-helper"
3434
"github.com/sirupsen/logrus"
3535
"github.com/spf13/viper"
36-
"go.bug.st/downloader"
36+
"go.bug.st/downloader/v2"
3737
)
3838

3939
// this map contains all the running Arduino Core Services instances

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ require (
3838
github.com/spf13/viper v1.6.2
3939
github.com/stretchr/testify v1.4.0
4040
go.bug.st/cleanup v1.0.0
41-
go.bug.st/downloader v1.2.0
41+
go.bug.st/downloader/v2 v2.0.1
4242
go.bug.st/relaxed-semver v0.0.0-20190922224835-391e10178d18
4343
go.bug.st/serial v1.0.0
4444
go.bug.st/serial.v1 v0.0.0-20180827123349-5f7892a7bb45 // indirect

0 commit comments

Comments
 (0)