summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2017-09-26 23:15:11 +0800
committerruki <[email protected]>2017-09-26 13:31:08 +0800
commit516eef07b377f938772a8047e122e8d0287e55ad (patch)
tree365a2b4e36a4931ce82116e144810a9957a535c1
parenta158b677a28eab3f06c7798b8af9b1ef6523221c (diff)
add user-agent
-rw-r--r--xmake/core/base/os.lua26
-rw-r--r--xmake/core/sandbox/modules/os.lua1
-rw-r--r--xmake/modules/net/http/download.lua48
3 files changed, 61 insertions, 14 deletions
diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua
index 6d4c4a96e..cef1af966 100644
--- a/xmake/core/base/os.lua
+++ b/xmake/core/base/os.lua
@@ -689,6 +689,32 @@ function os.nuldev()
return xmake._NULDEV
end
+-- get user agent
+function os.user_agent()
+
+ -- init user agent
+ if os._USER_AGENT == nil then
+
+ -- init systems
+ local systems = {macosx = "Macintosh", linux = "Linux", windows = "Windows"}
+
+ -- os user agent
+ local os_user_agent = ""
+ if os.host() == "macosx" then
+ local ok, osver = os.iorun("/usr/bin/sw_vers -productVersion")
+ if ok then
+ os_user_agent = "Intel Mac OS X " .. (osver or ""):trim()
+ end
+ end
+
+ -- make user agent
+ os._USER_AGENT = string.format("Xmake/%s (%s;%s)", xmake._VERSION_SHORT, systems[os.host()] or os.host(), os_user_agent)
+ end
+
+ -- ok?
+ return os._USER_AGENT
+end
+
-- get uid
function os.uid(...)
-- get uid
diff --git a/xmake/core/sandbox/modules/os.lua b/xmake/core/sandbox/modules/os.lua
index 4bfbd284b..e761140a8 100644
--- a/xmake/core/sandbox/modules/os.lua
+++ b/xmake/core/sandbox/modules/os.lua
@@ -58,6 +58,7 @@ sandbox_os.projectdir = os.projectdir
sandbox_os.projectfile = os.projectfile
sandbox_os.versioninfo = os.versioninfo
sandbox_os.getwinsize = os.getwinsize
+sandbox_os.user_agent = os.user_agent
-- copy file or directory
function sandbox_os.cp(...)
diff --git a/xmake/modules/net/http/download.lua b/xmake/modules/net/http/download.lua
index ad9c17dcb..f319d1123 100644
--- a/xmake/modules/net/http/download.lua
+++ b/xmake/modules/net/http/download.lua
@@ -24,11 +24,10 @@
-- imports
import("core.base.option")
-import("detect.tools.find_curl")
-import("detect.tools.find_wget")
+import("lib.detect.find_tool")
-- download url using curl
-function _curl_download(program, url, outputfile)
+function _curl_download(tool, url, outputfile)
-- set basic arguments
local argv = {}
@@ -38,6 +37,17 @@ function _curl_download(program, url, outputfile)
table.insert(argv, "-fsSL")
end
+ -- set user-agent
+ local user_agent = os.user_agent()
+ if user_agent then
+ if tool.version then
+ user_agent = user_agent .. " curl/" .. tool.version
+ end
+ table.insert(argv, "-A")
+ table.insert(argv, user_agent)
+ end
+ print(user_agent)
+
-- set url
table.insert(argv, url)
@@ -51,12 +61,12 @@ function _curl_download(program, url, outputfile)
table.insert(argv, "-o")
table.insert(argv, outputfile)
- -- clone it
- os.vrunv(program, argv)
+ -- download it
+ os.vrunv(tool.program, argv)
end
-- download url using wget
-function _wget_download(program, url, outputfile)
+function _wget_download(tool, url, outputfile)
-- ensure output directory
local argv = {url}
@@ -65,12 +75,22 @@ function _wget_download(program, url, outputfile)
os.mkdir(outputdir)
end
+ -- set user-agent
+ local user_agent = os.user_agent()
+ if user_agent then
+ if tool.version then
+ user_agent = user_agent .. " wget/" .. tool.version
+ end
+ table.insert(argv, "-U")
+ table.insert(argv, user_agent)
+ end
+
-- set outputfile
table.insert(argv, "-O")
table.insert(argv, outputfile)
- -- clone it
- os.vrunv(program, argv)
+ -- download it
+ os.vrunv(tool.program, argv)
end
-- download url
@@ -85,14 +105,14 @@ function main(url, outputfile)
outputfile = outputfile or path.filename(url)
-- attempt to download url using curl first
- local program = find_curl()
- if program then
- return _curl_download(program, url, outputfile)
+ local tool = find_tool("curl", {version = true})
+ if tool then
+ return _curl_download(tool, url, outputfile)
end
-- download url using wget
- program = find_wget()
- if program then
- return _wget_download(program, url, outputfile)
+ tool = find_tool("wget", {version = true})
+ if tool then
+ return _wget_download(tool, url, outputfile)
end
end