Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
packer
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kristopher Ruzic
packer
Commits
3f77b2c5
Commit
3f77b2c5
authored
Jan 19, 2014
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #803 from mark-rushakoff/useragent
common: set user agent in downloader
parents
d6432f55
45484951
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
111 additions
and
10 deletions
+111
-10
common/download.go
common/download.go
+13
-4
common/download_test.go
common/download_test.go
+87
-0
common/step_download.go
common/step_download.go
+1
-0
packer/version.go
packer/version.go
+10
-6
No files found.
common/download.go
View file @
3f77b2c5
...
@@ -43,6 +43,10 @@ type DownloadConfig struct {
...
@@ -43,6 +43,10 @@ type DownloadConfig struct {
// for the downloader will be used to verify with this checksum after
// for the downloader will be used to verify with this checksum after
// it is downloaded.
// it is downloaded.
Checksum
[]
byte
Checksum
[]
byte
// What to use for the user agent for HTTP requests. If set to "", use the
// default user agent provided by Go.
UserAgent
string
}
}
// A DownloadClient helps download, verify checksums, etc.
// A DownloadClient helps download, verify checksums, etc.
...
@@ -73,8 +77,8 @@ func HashForType(t string) hash.Hash {
...
@@ -73,8 +77,8 @@ func HashForType(t string) hash.Hash {
func
NewDownloadClient
(
c
*
DownloadConfig
)
*
DownloadClient
{
func
NewDownloadClient
(
c
*
DownloadConfig
)
*
DownloadClient
{
if
c
.
DownloaderMap
==
nil
{
if
c
.
DownloaderMap
==
nil
{
c
.
DownloaderMap
=
map
[
string
]
Downloader
{
c
.
DownloaderMap
=
map
[
string
]
Downloader
{
"http"
:
new
(
HTTPDownloader
)
,
"http"
:
&
HTTPDownloader
{
userAgent
:
c
.
UserAgent
}
,
"https"
:
new
(
HTTPDownloader
)
,
"https"
:
&
HTTPDownloader
{
userAgent
:
c
.
UserAgent
}
,
}
}
}
}
...
@@ -182,8 +186,9 @@ func (d *DownloadClient) VerifyChecksum(path string) (bool, error) {
...
@@ -182,8 +186,9 @@ func (d *DownloadClient) VerifyChecksum(path string) (bool, error) {
// HTTPDownloader is an implementation of Downloader that downloads
// HTTPDownloader is an implementation of Downloader that downloads
// files over HTTP.
// files over HTTP.
type
HTTPDownloader
struct
{
type
HTTPDownloader
struct
{
progress
uint
progress
uint
total
uint
total
uint
userAgent
string
}
}
func
(
*
HTTPDownloader
)
Cancel
()
{
func
(
*
HTTPDownloader
)
Cancel
()
{
...
@@ -197,6 +202,10 @@ func (d *HTTPDownloader) Download(dst io.Writer, src *url.URL) error {
...
@@ -197,6 +202,10 @@ func (d *HTTPDownloader) Download(dst io.Writer, src *url.URL) error {
return
err
return
err
}
}
if
d
.
userAgent
!=
""
{
req
.
Header
.
Set
(
"User-Agent"
,
d
.
userAgent
)
}
httpClient
:=
&
http
.
Client
{
httpClient
:=
&
http
.
Client
{
Transport
:
&
http
.
Transport
{
Transport
:
&
http
.
Transport
{
Proxy
:
http
.
ProxyFromEnvironment
,
Proxy
:
http
.
ProxyFromEnvironment
,
...
...
common/download_test.go
View file @
3f77b2c5
...
@@ -4,6 +4,8 @@ import (
...
@@ -4,6 +4,8 @@ import (
"crypto/md5"
"crypto/md5"
"encoding/hex"
"encoding/hex"
"io/ioutil"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"os"
"testing"
"testing"
)
)
...
@@ -41,6 +43,91 @@ func TestDownloadClient_VerifyChecksum(t *testing.T) {
...
@@ -41,6 +43,91 @@ func TestDownloadClient_VerifyChecksum(t *testing.T) {
}
}
}
}
func
TestDownloadClientUsesDefaultUserAgent
(
t
*
testing
.
T
)
{
tf
,
err
:=
ioutil
.
TempFile
(
""
,
"packer"
)
if
err
!=
nil
{
t
.
Fatalf
(
"tempfile error: %s"
,
err
)
}
defer
os
.
Remove
(
tf
.
Name
())
defaultUserAgent
:=
""
asserted
:=
false
server
:=
httptest
.
NewServer
(
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
if
defaultUserAgent
==
""
{
defaultUserAgent
=
r
.
UserAgent
()
}
else
{
incomingUserAgent
:=
r
.
UserAgent
()
if
incomingUserAgent
!=
defaultUserAgent
{
t
.
Fatalf
(
"Expected user agent %s, got: %s"
,
defaultUserAgent
,
incomingUserAgent
)
}
asserted
=
true
}
}))
req
,
err
:=
http
.
NewRequest
(
"GET"
,
server
.
URL
,
nil
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
httpClient
:=
&
http
.
Client
{
Transport
:
&
http
.
Transport
{
Proxy
:
http
.
ProxyFromEnvironment
,
},
}
_
,
err
=
httpClient
.
Do
(
req
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
config
:=
&
DownloadConfig
{
Url
:
server
.
URL
,
TargetPath
:
tf
.
Name
(),
}
client
:=
NewDownloadClient
(
config
)
_
,
err
=
client
.
Get
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
!
asserted
{
t
.
Fatal
(
"User-Agent never observed"
)
}
}
func
TestDownloadClientSetsUserAgent
(
t
*
testing
.
T
)
{
tf
,
err
:=
ioutil
.
TempFile
(
""
,
"packer"
)
if
err
!=
nil
{
t
.
Fatalf
(
"tempfile error: %s"
,
err
)
}
defer
os
.
Remove
(
tf
.
Name
())
asserted
:=
false
server
:=
httptest
.
NewServer
(
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
asserted
=
true
if
r
.
UserAgent
()
!=
"fancy user agent"
{
t
.
Fatalf
(
"Expected useragent fancy user agent, got: %s"
,
r
.
UserAgent
())
}
}))
config
:=
&
DownloadConfig
{
Url
:
server
.
URL
,
TargetPath
:
tf
.
Name
(),
UserAgent
:
"fancy user agent"
,
}
client
:=
NewDownloadClient
(
config
)
_
,
err
=
client
.
Get
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
!
asserted
{
t
.
Fatal
(
"HTTP request never made"
)
}
}
func
TestHashForType
(
t
*
testing
.
T
)
{
func
TestHashForType
(
t
*
testing
.
T
)
{
if
h
:=
HashForType
(
"md5"
);
h
==
nil
{
if
h
:=
HashForType
(
"md5"
);
h
==
nil
{
t
.
Fatalf
(
"md5 hash is nil"
)
t
.
Fatalf
(
"md5 hash is nil"
)
...
...
common/step_download.go
View file @
3f77b2c5
...
@@ -70,6 +70,7 @@ func (s *StepDownload) Run(state multistep.StateBag) multistep.StepAction {
...
@@ -70,6 +70,7 @@ func (s *StepDownload) Run(state multistep.StateBag) multistep.StepAction {
CopyFile
:
false
,
CopyFile
:
false
,
Hash
:
HashForType
(
s
.
ChecksumType
),
Hash
:
HashForType
(
s
.
ChecksumType
),
Checksum
:
checksum
,
Checksum
:
checksum
,
UserAgent
:
packer
.
VersionString
(),
}
}
path
,
err
,
retry
:=
s
.
download
(
config
,
state
)
path
,
err
,
retry
:=
s
.
download
(
config
,
state
)
...
...
packer/version.go
View file @
3f77b2c5
...
@@ -31,6 +31,15 @@ func (versionCommand) Run(env Environment, args []string) int {
...
@@ -31,6 +31,15 @@ func (versionCommand) Run(env Environment, args []string) int {
env
.
Ui
()
.
Machine
(
"version-prelease"
,
VersionPrerelease
)
env
.
Ui
()
.
Machine
(
"version-prelease"
,
VersionPrerelease
)
env
.
Ui
()
.
Machine
(
"version-commit"
,
GitCommit
)
env
.
Ui
()
.
Machine
(
"version-commit"
,
GitCommit
)
env
.
Ui
()
.
Say
(
VersionString
())
return
0
}
func
(
versionCommand
)
Synopsis
()
string
{
return
"print Packer version"
}
func
VersionString
()
string
{
var
versionString
bytes
.
Buffer
var
versionString
bytes
.
Buffer
fmt
.
Fprintf
(
&
versionString
,
"Packer v%s"
,
Version
)
fmt
.
Fprintf
(
&
versionString
,
"Packer v%s"
,
Version
)
if
VersionPrerelease
!=
""
{
if
VersionPrerelease
!=
""
{
...
@@ -41,10 +50,5 @@ func (versionCommand) Run(env Environment, args []string) int {
...
@@ -41,10 +50,5 @@ func (versionCommand) Run(env Environment, args []string) int {
}
}
}
}
env
.
Ui
()
.
Say
(
versionString
.
String
())
return
versionString
.
String
()
return
0
}
func
(
versionCommand
)
Synopsis
()
string
{
return
"print Packer version"
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment