-
Notifications
You must be signed in to change notification settings - Fork 601
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract creating logging http client and user agent handling to httputil
- Loading branch information
Showing
15 changed files
with
200 additions
and
75 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,30 @@ | ||
// -*- Mode: Go; indent-tabs-mode: t -*- | ||
|
||
/* | ||
* Copyright (C) 2016-2017 Canonical Ltd | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License version 3 as | ||
* published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
package httputil | ||
|
||
var GetFlags = (*LoggedTransport).getFlags | ||
|
||
func MockUserAgent(mock string) (restore func()) { | ||
old := userAgent | ||
userAgent = mock | ||
return func() { | ||
userAgent = old | ||
} | ||
} |
This file contains 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 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 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,69 @@ | ||
// -*- Mode: Go; indent-tabs-mode: t -*- | ||
|
||
/* | ||
* Copyright (C) 2016-2017 Canonical Ltd | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License version 3 as | ||
* published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
package httputil | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/snapcore/snapd/arch" | ||
"github.com/snapcore/snapd/osutil" | ||
"github.com/snapcore/snapd/release" | ||
) | ||
|
||
// UserAgent to send | ||
// TODO: this should actually be set per client request, and include the client user agent | ||
var userAgent = "unset" | ||
|
||
var isTesting bool | ||
|
||
func init() { | ||
if osutil.GetenvBool("SNAPPY_TESTING") { | ||
isTesting = true | ||
} | ||
} | ||
|
||
// SetUserAgentFromVersion sets up a user-agent string. | ||
func SetUserAgentFromVersion(version string, extraProds ...string) { | ||
extras := make([]string, 1, 3) | ||
extras[0] = "series " + release.Series | ||
if release.OnClassic { | ||
extras = append(extras, "classic") | ||
} | ||
if release.ReleaseInfo.ForceDevMode() { | ||
extras = append(extras, "devmode") | ||
} | ||
if isTesting { | ||
extras = append(extras, "testing") | ||
} | ||
extraProdStr := "" | ||
if len(extraProds) != 0 { | ||
extraProdStr = " " + strings.Join(extraProds, " ") | ||
} | ||
// xxx this assumes ReleaseInfo's ID and VersionID don't have weird characters | ||
// (see rfc 7231 for values of weird) | ||
// assumption checks out in practice, q.v. https://github.com/zyga/os-release-zoo | ||
userAgent = fmt.Sprintf("snapd/%v (%s)%s %s/%s (%s)", version, strings.Join(extras, "; "), extraProdStr, release.ReleaseInfo.ID, release.ReleaseInfo.VersionID, string(arch.UbuntuArchitecture())) | ||
} | ||
|
||
// UserAgent returns the user-agent string setup through SetUserAgentFromVersion. | ||
func UserAgent() string { | ||
return userAgent | ||
} |
This file contains 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,52 @@ | ||
// -*- Mode: Go; indent-tabs-mode: t -*- | ||
|
||
/* | ||
* Copyright (C) 2016-2017 Canonical Ltd | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License version 3 as | ||
* published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
package httputil_test | ||
|
||
import ( | ||
"strings" | ||
|
||
. "gopkg.in/check.v1" | ||
|
||
"github.com/snapcore/snapd/httputil" | ||
) | ||
|
||
type UASuite struct { | ||
restore func() | ||
} | ||
|
||
var _ = Suite(&UASuite{}) | ||
|
||
func (s *UASuite) SetUpTest(c *C) { | ||
s.restore = httputil.MockUserAgent("-") | ||
} | ||
|
||
func (s *UASuite) TearDownTest(c *C) { | ||
s.restore() | ||
} | ||
|
||
func (s *UASuite) TestUserAgent(c *C) { | ||
httputil.SetUserAgentFromVersion("10") | ||
ua := httputil.UserAgent() | ||
c.Check(strings.HasPrefix(ua, "snapd/10 "), Equals, true) | ||
|
||
httputil.SetUserAgentFromVersion("10", "extraProd") | ||
ua = httputil.UserAgent() | ||
c.Check(strings.Contains(ua, "extraProd"), Equals, true) | ||
} |
This file contains 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 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
Oops, something went wrong.