Skip to content

Commit f5b386e

Browse files
committed
Partially revert global network configuration.
It's better to read the network configuration before each http request so in case it is changed using "Settings" functions in daemon mode the changes are immediately applied.
1 parent 7f02f36 commit f5b386e

File tree

12 files changed

+109
-69
lines changed

12 files changed

+109
-69
lines changed

arduino/cores/packagemanager/download.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,16 @@ func (pm *PackageManager) FindPlatformReleaseDependencies(item *PlatformReferenc
100100

101101
// DownloadToolRelease downloads a ToolRelease. If the tool is already downloaded a nil Downloader
102102
// is returned.
103-
func (pm *PackageManager) DownloadToolRelease(tool *cores.ToolRelease) (*downloader.Downloader, error) {
103+
func (pm *PackageManager) DownloadToolRelease(tool *cores.ToolRelease, config *downloader.Config) (*downloader.Downloader, error) {
104104
resource := tool.GetCompatibleFlavour()
105105
if resource == nil {
106106
return nil, fmt.Errorf("tool not available for your OS")
107107
}
108-
return resource.Download(pm.DownloadDir)
108+
return resource.Download(pm.DownloadDir, config)
109109
}
110110

111111
// DownloadPlatformRelease downloads a PlatformRelease. If the platform is already downloaded a
112112
// nil Downloader is returned.
113-
func (pm *PackageManager) DownloadPlatformRelease(platform *cores.PlatformRelease) (*downloader.Downloader, error) {
114-
return platform.Resource.Download(pm.DownloadDir)
113+
func (pm *PackageManager) DownloadPlatformRelease(platform *cores.PlatformRelease, config *downloader.Config) (*downloader.Downloader, error) {
114+
return platform.Resource.Download(pm.DownloadDir, config)
115115
}

arduino/libraries/librariesmanager/download.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ import (
2525
var LibraryIndexURL, _ = url.Parse("https://downloads.arduino.cc/libraries/library_index.json")
2626

2727
// UpdateIndex downloads the libraries index file from Arduino repository.
28-
func (lm *LibrariesManager) UpdateIndex() (*downloader.Downloader, error) {
28+
func (lm *LibrariesManager) UpdateIndex(config *downloader.Config) (*downloader.Downloader, error) {
2929
lm.IndexFile.Parent().MkdirAll()
3030
// TODO: Download from gzipped URL index
31-
return downloader.Download(lm.IndexFile.String(), LibraryIndexURL.String(), downloader.NoResume)
31+
return downloader.DownloadWithConfig(lm.IndexFile.String(), LibraryIndexURL.String(), *config, downloader.NoResume)
3232
}

arduino/resources/helpers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func (r *DownloadResource) IsCached(downloadDir *paths.Path) (bool, error) {
4343
}
4444

4545
// Download a DownloadResource.
46-
func (r *DownloadResource) Download(downloadDir *paths.Path) (*downloader.Downloader, error) {
46+
func (r *DownloadResource) Download(downloadDir *paths.Path, config *downloader.Config) (*downloader.Downloader, error) {
4747
cached, err := r.TestLocalArchiveIntegrity(downloadDir)
4848
if err != nil {
4949
return nil, fmt.Errorf("testing local archive integrity: %s", err)
@@ -71,5 +71,5 @@ func (r *DownloadResource) Download(downloadDir *paths.Path) (*downloader.Downlo
7171
return nil, fmt.Errorf("getting archive file info: %s", err)
7272
}
7373

74-
return downloader.Download(path.String(), r.URL)
74+
return downloader.DownloadWithConfig(path.String(), r.URL, *config)
7575
}

arduino/resources/helpers_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,7 @@ func TestDownloadApplyUserAgentHeaderUsingConfig(t *testing.T) {
5353
URL: srv.URL,
5454
}
5555

56-
prev := downloader.GetDefaultConfig()
57-
downloader.SetDefaultConfig(downloader.Config{RequestHeaders: http.Header{"User-Agent": []string{goldUserAgentValue}}})
58-
d, err := r.Download(tmp)
59-
downloader.SetDefaultConfig(prev)
56+
d, err := r.Download(tmp, &downloader.Config{RequestHeaders: http.Header{"User-Agent": []string{goldUserAgentValue}}})
6057
require.NoError(t, err)
6158
err = d.Run()
6259
require.NoError(t, err)

arduino/resources/resources_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

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

2728
func TestDownloadAndChecksums(t *testing.T) {
@@ -41,7 +42,7 @@ func TestDownloadAndChecksums(t *testing.T) {
4142
require.NoError(t, err)
4243

4344
downloadAndTestChecksum := func() {
44-
d, err := r.Download(tmp)
45+
d, err := r.Download(tmp, &downloader.Config{})
4546
require.NoError(t, err)
4647
err = d.Run()
4748
require.NoError(t, err)
@@ -57,7 +58,7 @@ func TestDownloadAndChecksums(t *testing.T) {
5758
downloadAndTestChecksum()
5859

5960
// Download with cached file
60-
d, err := r.Download(tmp)
61+
d, err := r.Download(tmp, &downloader.Config{})
6162
require.NoError(t, err)
6263
require.Nil(t, d)
6364

cli/cli.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package cli
1818
import (
1919
"fmt"
2020
"io/ioutil"
21-
"net/url"
2221
"os"
2322
"path/filepath"
2423
"strings"
@@ -46,7 +45,6 @@ import (
4645
"github.com/sirupsen/logrus"
4746
"github.com/spf13/cobra"
4847
"github.com/spf13/viper"
49-
"go.bug.st/downloader"
5048
)
5149

5250
var (
@@ -256,21 +254,4 @@ func preRun(cmd *cobra.Command, args []string) {
256254
os.Exit(errorcodes.ErrBadCall)
257255
})
258256
}
259-
260-
//
261-
// Configure network
262-
//
263-
netConf := downloader.Config{
264-
RequestHeaders: globals.NewHTTPClientHeader(""),
265-
}
266-
if viper.IsSet("network.proxy") {
267-
proxy := viper.GetString("network.proxy")
268-
if _, err := url.Parse(proxy); err != nil {
269-
feedback.Error("Invalid network.proxy '" + proxy + "': " + err.Error())
270-
os.Exit(errorcodes.ErrBadArgument)
271-
}
272-
netConf.ProxyURL = proxy
273-
logrus.Infof("Using proxy %s", proxy)
274-
}
275-
downloader.SetDefaultConfig(netConf)
276257
}

cli/daemon/daemon.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import (
3737
"github.com/sirupsen/logrus"
3838
"github.com/spf13/cobra"
3939
"github.com/spf13/viper"
40-
"go.bug.st/downloader"
4140
"google.golang.org/grpc"
4241
)
4342

@@ -66,14 +65,11 @@ func runDaemonCommand(cmd *cobra.Command, args []string) {
6665
stats.Incr("daemon", stats.T("success", "true"))
6766
defer stats.Flush()
6867
}
69-
7068
port := viper.GetString("daemon.port")
7169
s := grpc.NewServer()
7270

7371
// Set specific user-agent for the daemon
74-
netConf := downloader.GetDefaultConfig()
75-
netConf.RequestHeaders = globals.NewHTTPClientHeader("daemon")
76-
downloader.SetDefaultConfig(netConf)
72+
viper.Set("network.user_agent_ext", "daemon")
7773

7874
// register the commands service
7975
srv_commands.RegisterArduinoCoreServer(s, &daemon.ArduinoCoreServerImpl{

commands/bundled_tools.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ import (
2525

2626
// DownloadToolRelease downloads a ToolRelease
2727
func DownloadToolRelease(pm *packagemanager.PackageManager, toolRelease *cores.ToolRelease, downloadCB DownloadProgressCB) error {
28-
resp, err := pm.DownloadToolRelease(toolRelease)
28+
config, err := GetDownloaderConfig()
29+
if err != nil {
30+
return err
31+
}
32+
resp, err := pm.DownloadToolRelease(toolRelease, config)
2933
if err != nil {
3034
return err
3135
}

commands/core/download.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ func PlatformDownload(ctx context.Context, req *rpc.PlatformDownloadReq, downloa
6464

6565
func downloadPlatform(pm *packagemanager.PackageManager, platformRelease *cores.PlatformRelease, downloadCB commands.DownloadProgressCB) error {
6666
// Download platform
67-
resp, err := pm.DownloadPlatformRelease(platformRelease)
67+
config, err := commands.GetDownloaderConfig()
68+
if err != nil {
69+
return err
70+
}
71+
resp, err := pm.DownloadPlatformRelease(platformRelease, config)
6872
if err != nil {
6973
return err
7074
}

commands/download.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to license@arduino.cc.
15+
16+
package commands
17+
18+
import (
19+
"net/url"
20+
"time"
21+
22+
"github.com/arduino/arduino-cli/cli/globals"
23+
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"
28+
)
29+
30+
// GetDownloaderConfig returns the downloader configuration based on
31+
// current settings.
32+
func GetDownloaderConfig() (*downloader.Config, error) {
33+
res := &downloader.Config{
34+
RequestHeaders: globals.NewHTTPClientHeader(viper.GetString("network.user_agent_ext")),
35+
}
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)
43+
}
44+
return res, nil
45+
}
46+
47+
// Download performs a download loop using the provided downloader.Downloader.
48+
// Messages are passed back to the DownloadProgressCB using label as text for the File field.
49+
func Download(d *downloader.Downloader, label string, downloadCB DownloadProgressCB) error {
50+
if d == nil {
51+
// This signal means that the file is already downloaded
52+
downloadCB(&rpc.DownloadProgress{
53+
File: label,
54+
Completed: true,
55+
})
56+
return nil
57+
}
58+
downloadCB(&rpc.DownloadProgress{
59+
File: label,
60+
Url: d.URL,
61+
TotalSize: d.Size(),
62+
})
63+
d.RunAndPoll(func(downloaded int64) {
64+
downloadCB(&rpc.DownloadProgress{Downloaded: downloaded})
65+
}, 250*time.Millisecond)
66+
if d.Error() != nil {
67+
return d.Error()
68+
}
69+
downloadCB(&rpc.DownloadProgress{Completed: true})
70+
return nil
71+
}

0 commit comments

Comments
 (0)