-
-
Notifications
You must be signed in to change notification settings - Fork 404
add cli httpclient with support for proxy configuration #672
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rsora
merged 9 commits into
arduino:master
from
hdiniz:hdiniz/add-proxy-setting-to-default-http-client
May 19, 2020
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
bc26218
add configuration for cli httpclient
hdiniz a383457
add httpclient roundtripper with proxy and user-agent
hdiniz e5bdd84
add cli httpclient
hdiniz ac92e32
set downloader user-agent tp equal cli httpclient
hdiniz 5424e22
use cli httpclient on board list command
hdiniz 7eb1acd
add unit tests for httpclient
hdiniz 039d07d
add copyright notice
hdiniz 712e2a7
upgrade downloader to v2 with httpclient injection
hdiniz 70e7dec
change go.mod to point to upstream downloader project v2
hdiniz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,40 @@ | ||
// This file is part of arduino-cli. | ||
// | ||
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) | ||
// | ||
// This software is released under the GNU General Public License version 3, | ||
// which covers the main part of arduino-cli. | ||
// The terms of this license can be found at: | ||
// https://www.gnu.org/licenses/gpl-3.0.en.html | ||
// | ||
// You can be released from the requirements of the above licenses by purchasing | ||
// a commercial license. Buying such a license is mandatory if you want to | ||
// modify or otherwise use the software for commercial activities involving the | ||
// Arduino software without disclosing the source code of your own applications. | ||
// To purchase a commercial license, send an email to [email protected]. | ||
|
||
package httpclient | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
// New returns a default http client for use in the cli API calls | ||
func New() (*http.Client, error) { | ||
config, err := DefaultConfig() | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return NewWithConfig(config), nil | ||
} | ||
|
||
// NewWithConfig creates a http client for use in the cli API calls with a given configuration | ||
func NewWithConfig(config *Config) *http.Client { | ||
transport := newHTTPClientTransport(config) | ||
|
||
return &http.Client{ | ||
Transport: transport, | ||
} | ||
} |
This file contains hidden or 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,64 @@ | ||
// This file is part of arduino-cli. | ||
// | ||
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) | ||
// | ||
// This software is released under the GNU General Public License version 3, | ||
// which covers the main part of arduino-cli. | ||
// The terms of this license can be found at: | ||
// https://www.gnu.org/licenses/gpl-3.0.en.html | ||
// | ||
// You can be released from the requirements of the above licenses by purchasing | ||
// a commercial license. Buying such a license is mandatory if you want to | ||
// modify or otherwise use the software for commercial activities involving the | ||
// Arduino software without disclosing the source code of your own applications. | ||
// To purchase a commercial license, send an email to [email protected]. | ||
|
||
package httpclient | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/url" | ||
"runtime" | ||
|
||
"github.com/arduino/arduino-cli/cli/globals" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
// Config is the configuration of the http client | ||
type Config struct { | ||
UserAgent string | ||
Proxy *url.URL | ||
} | ||
|
||
// DefaultConfig returns the default http client config | ||
func DefaultConfig() (*Config, error) { | ||
var proxy *url.URL | ||
var err error | ||
if viper.IsSet("network.proxy") { | ||
proxyConfig := viper.GetString("network.proxy") | ||
if proxy, err = url.Parse(proxyConfig); err != nil { | ||
return nil, errors.New("Invalid network.proxy '" + proxyConfig + "': " + err.Error()) | ||
} | ||
} | ||
|
||
return &Config{ | ||
UserAgent: UserAgent(), | ||
Proxy: proxy, | ||
}, nil | ||
} | ||
|
||
// UserAgent returns the user agent for the cli http client | ||
func UserAgent() string { | ||
subComponent := viper.GetString("network.user_agent_ext") | ||
if subComponent != "" { | ||
subComponent = " " + subComponent | ||
} | ||
|
||
return fmt.Sprintf("%s/%s%s (%s; %s; %s) Commit:%s", | ||
globals.VersionInfo.Application, | ||
globals.VersionInfo.VersionString, | ||
subComponent, | ||
runtime.GOARCH, runtime.GOOS, runtime.Version(), | ||
globals.VersionInfo.Commit) | ||
} |
This file contains hidden or 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,70 @@ | ||
// This file is part of arduino-cli. | ||
// | ||
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) | ||
// | ||
// This software is released under the GNU General Public License version 3, | ||
// which covers the main part of arduino-cli. | ||
// The terms of this license can be found at: | ||
// https://www.gnu.org/licenses/gpl-3.0.en.html | ||
// | ||
// You can be released from the requirements of the above licenses by purchasing | ||
// a commercial license. Buying such a license is mandatory if you want to | ||
// modify or otherwise use the software for commercial activities involving the | ||
// Arduino software without disclosing the source code of your own applications. | ||
// To purchase a commercial license, send an email to [email protected]. | ||
|
||
package httpclient | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestUserAgentHeader(t *testing.T) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
fmt.Fprint(w, r.Header.Get("User-Agent")) | ||
})) | ||
defer ts.Close() | ||
|
||
client := NewWithConfig(&Config{ | ||
UserAgent: "test-user-agent", | ||
}) | ||
|
||
request, err := http.NewRequest("GET", ts.URL, nil) | ||
require.NoError(t, err) | ||
|
||
response, err := client.Do(request) | ||
require.NoError(t, err) | ||
|
||
b, err := ioutil.ReadAll(response.Body) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, "test-user-agent", string(b)) | ||
} | ||
|
||
func TestProxy(t *testing.T) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusNoContent) | ||
})) | ||
defer ts.Close() | ||
|
||
proxyURL, err := url.Parse(ts.URL) | ||
require.NoError(t, err) | ||
|
||
client := NewWithConfig(&Config{ | ||
Proxy: proxyURL, | ||
}) | ||
|
||
request, err := http.NewRequest("GET", "http://arduino.cc", nil) | ||
require.NoError(t, err) | ||
|
||
response, err := client.Do(request) | ||
require.NoError(t, err) | ||
require.Equal(t, http.StatusNoContent, response.StatusCode) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.