Skip to content

Commit 16e0871

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 0f0dd75 commit 16e0871

File tree

12 files changed

+109
-69
lines changed

12 files changed

+109
-69
lines changed

Diff for: arduino/cores/packagemanager/download.go

+4-4
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
}

Diff for: arduino/libraries/librariesmanager/download.go

+2-2
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
}

Diff for: arduino/resources/helpers.go

+2-2
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
}

Diff for: arduino/resources/helpers_test.go

+1-4
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)

Diff for: arduino/resources/resources_test.go

+3-2
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

Diff for: cli/cli.go

-19
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
}

Diff for: cli/daemon/daemon.go

+1-5
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{

Diff for: commands/bundled_tools.go

+5-1
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
}

Diff for: commands/core/download.go

+5-1
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
}

Diff for: commands/download.go

+71
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 [email protected].
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+
}

Diff for: commands/instances.go

+10-28
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"io/ioutil"
2222
"net/url"
2323
"path"
24-
"time"
2524

2625
"github.com/arduino/arduino-cli/arduino/cores"
2726
"github.com/arduino/arduino-cli/arduino/cores/packageindex"
@@ -171,7 +170,11 @@ func UpdateLibrariesIndex(ctx context.Context, req *rpc.UpdateLibrariesIndexReq,
171170
if lm == nil {
172171
return fmt.Errorf("invalid handle")
173172
}
174-
d, err := lm.UpdateIndex()
173+
config, err := GetDownloaderConfig()
174+
if err != nil {
175+
return err
176+
}
177+
d, err := lm.UpdateIndex(config)
175178
if err != nil {
176179
return err
177180
}
@@ -215,7 +218,11 @@ func UpdateIndex(ctx context.Context, req *rpc.UpdateIndexReq, downloadCB Downlo
215218
tmp := paths.New(tmpFile.Name())
216219
defer tmp.Remove()
217220

218-
d, err := downloader.Download(tmp.String(), URL.String())
221+
config, err := GetDownloaderConfig()
222+
if err != nil {
223+
return nil, fmt.Errorf("downloading index %s: %s", URL, err)
224+
}
225+
d, err := downloader.DownloadWithConfig(tmp.String(), URL.String(), *config)
219226
if err != nil {
220227
return nil, fmt.Errorf("downloading index %s: %s", URL, err)
221228
}
@@ -348,28 +355,3 @@ func createInstance(ctx context.Context, getLibOnly bool) (*createInstanceResult
348355

349356
return res, nil
350357
}
351-
352-
// Download FIXMEDOC
353-
func Download(d *downloader.Downloader, label string, downloadCB DownloadProgressCB) error {
354-
if d == nil {
355-
// This signal means that the file is already downloaded
356-
downloadCB(&rpc.DownloadProgress{
357-
File: label,
358-
Completed: true,
359-
})
360-
return nil
361-
}
362-
downloadCB(&rpc.DownloadProgress{
363-
File: label,
364-
Url: d.URL,
365-
TotalSize: d.Size(),
366-
})
367-
d.RunAndPoll(func(downloaded int64) {
368-
downloadCB(&rpc.DownloadProgress{Downloaded: downloaded})
369-
}, 250*time.Millisecond)
370-
if d.Error() != nil {
371-
return d.Error()
372-
}
373-
downloadCB(&rpc.DownloadProgress{Completed: true})
374-
return nil
375-
}

Diff for: commands/lib/download.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ func downloadLibrary(lm *librariesmanager.LibrariesManager, libRelease *librarie
5050
downloadCB commands.DownloadProgressCB, taskCB commands.TaskProgressCB) error {
5151

5252
taskCB(&rpc.TaskProgress{Name: "Downloading " + libRelease.String()})
53-
if d, err := libRelease.Resource.Download(lm.DownloadsDir); err != nil {
53+
config, err := commands.GetDownloaderConfig()
54+
if err != nil {
55+
return err
56+
}
57+
if d, err := libRelease.Resource.Download(lm.DownloadsDir, config); err != nil {
5458
return err
5559
} else if err := commands.Download(d, libRelease.String(), downloadCB); err != nil {
5660
return err

0 commit comments

Comments
 (0)