diff options
| author | ruki <[email protected]> | 2023-02-04 12:59:12 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-02-04 12:59:12 +0800 |
| commit | 2301d3426456ef56abb11c5f65f2b430ecc87369 (patch) | |
| tree | 27440e65e8fb09d751c36503275e0db67affea42 | |
| parent | c814b6d5fde13577d46184ef9aae0c1bd21e24e2 (diff) | |
| parent | 2043b97f06b61f8975067be5489cfa68c8bb1ac8 (diff) | |
Merge pull request #3332 from xmake-io/download
add custom http headers when downloading packages
| -rw-r--r-- | CHANGELOG.md | 2 | ||||
| -rw-r--r-- | xmake/core/package/package.lua | 5 | ||||
| -rw-r--r-- | xmake/core/project/policy.lua | 2 | ||||
| -rw-r--r-- | xmake/modules/net/http/download.lua | 15 | ||||
| -rw-r--r-- | xmake/modules/private/action/require/impl/actions/download.lua | 35 | ||||
| -rw-r--r-- | xmake/modules/private/action/require/impl/actions/download_resources.lua | 4 | ||||
| -rw-r--r-- | xmake/modules/private/action/require/impl/actions/patch_sources.lua | 4 |
7 files changed, 52 insertions, 15 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index bee87529b..dd4c8cd7a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ * Add Haiku support * [#3326](https://github.com/xmake-io/xmake/issues/3326): Add `xmake check` to check project code (clang-tidy) and configuration +* [#3332](https://github.com/xmake-io/xmake/pull/3332): add custom http headers when downloading packages ### Changes @@ -1530,6 +1531,7 @@ * 添加 Haiku 支持 * [#3326](https://github.com/xmake-io/xmake/issues/3326): 添加 `xmake check` 去检测工程代码 (clang-tidy) 和 API 参数配置 +* [#3332](https://github.com/xmake-io/xmake/pull/3332): 在包中配置添加自定义 http headers ### 改进 diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua index 99eec0764..078e7d534 100644 --- a/xmake/core/package/package.lua +++ b/xmake/core/package/package.lua @@ -274,6 +274,11 @@ function _instance:url_excludes(url) return self:extraconf("urls", url, "excludes") end +-- get the http headers of url, @note need raw url +function _instance:url_http_headers(url) + return self:extraconf("urls", url, "http_headers") +end + -- set artifacts info function _instance:artifacts_set(artifacts_info) local versions = self:get("versions") diff --git a/xmake/core/project/policy.lua b/xmake/core/project/policy.lua index 1a466aa68..52f13c9b7 100644 --- a/xmake/core/project/policy.lua +++ b/xmake/core/project/policy.lua @@ -76,6 +76,8 @@ function policy.policies() ["package.install_only"] = {description = "Only install packages from remote.", type = "boolean"}, -- always install packages every time ["package.install_always"] = {description = "Always install packages every time.", type = "boolean"}, + -- set custom headers when downloading package + ["package.download.http_headers"] = {description = "Set the custom http headers when downloading package."}, -- use includes as external header files? e.g. -isystem .. ["package.include_external_headers"] = {description = "Use includes as external headers.", type = "boolean"}, -- inherit the configs from the external command arguments, e.g. toolchains, `xmake f --toolchain=` diff --git a/xmake/modules/net/http/download.lua b/xmake/modules/net/http/download.lua index 0b266a248..038d3fb03 100644 --- a/xmake/modules/net/http/download.lua +++ b/xmake/modules/net/http/download.lua @@ -85,6 +85,14 @@ function _curl_download(tool, url, outputfile, opt) table.insert(argv, "-k") end + -- add custom headers + if opt.headers then + for _, header in ipairs(opt.headers) do + table.insert(argv, "-H") + table.insert(argv, header) + end + end + -- continue to download? if opt.continue then table.insert(argv, "-C") @@ -150,6 +158,13 @@ function _wget_download(tool, url, outputfile, opt) table.insert(argv, "--no-check-certificate") end + -- add custom headers + if opt.headers then + for _, header in ipairs(opt.headers) do + table.insert(argv, "--header=" .. header) + end + end + -- continue to download? if opt.continue then table.insert(argv, "-c") diff --git a/xmake/modules/private/action/require/impl/actions/download.lua b/xmake/modules/private/action/require/impl/actions/download.lua index f6070021d..a3f679a7d 100644 --- a/xmake/modules/private/action/require/impl/actions/download.lua +++ b/xmake/modules/private/action/require/impl/actions/download.lua @@ -35,7 +35,8 @@ import("devel.git") import("utils.archive") -- checkout codes from git -function _checkout(package, url, sourcedir, url_alias) +function _checkout(package, url, sourcedir, opt) + opt = opt or {} -- use previous source directory if exists local packagedir = path.join(sourcedir, package:name()) @@ -104,7 +105,7 @@ function _checkout(package, url, sourcedir, url_alias) git.clone(url, {longpaths = longpaths, outputdir = packagedir}) -- attempt to checkout the given version - local revision = package:revision(url_alias) or package:tag() or package:commit() or package:version_str() + local revision = package:revision(opt.url_alias) or package:tag() or package:commit() or package:version_str() git.checkout(revision, {repodir = packagedir}) -- update all submodules @@ -123,7 +124,8 @@ function _checkout(package, url, sourcedir, url_alias) end -- download codes from ftp/http/https -function _download(package, url, sourcedir, url_alias, url_excludes) +function _download(package, url, sourcedir, opt) + opt = opt or {} -- get package file local packagefile = url_filename(url) @@ -134,7 +136,7 @@ function _download(package, url, sourcedir, url_alias, url_excludes) -- @see https://github.com/xmake-io/xmake/issues/930 -- https://github.com/xmake-io/xmake/issues/1009 -- - local sourcehash = package:sourcehash(url_alias) + local sourcehash = package:sourcehash(opt.url_alias) assert(not package:is_verify() or not package:get("versions") or sourcehash, "cannot get source hash of %s in package(%s)", url, package:name()) -- the package file have been downloaded? @@ -164,7 +166,9 @@ function _download(package, url, sourcedir, url_alias, url_excludes) -- we can use local package from the search directories directly if network is too slow os.cp(localfile, packagefile) else - http.download(url, packagefile, {insecure = global.get("insecure-ssl")}) + http.download(url, packagefile, { + insecure = global.get("insecure-ssl"), + headers = opt.url_http_headers or package:policy("package.download.http_headers")}) end end @@ -181,7 +185,7 @@ function _download(package, url, sourcedir, url_alias, url_excludes) local sourcedir_tmp = sourcedir .. ".tmp" os.rm(sourcedir_tmp) local extension = archive.extension(packagefile) - if archive.extract(packagefile, sourcedir_tmp, {excludes = url_excludes}) then + if archive.extract(packagefile, sourcedir_tmp, {excludes = opt.url_excludes}) then -- move to source directory and we skip it to avoid long path issues on windows if only one root directory os.rm(sourcedir) local filedirs = os.filedirs(path.join(sourcedir_tmp, "*")) @@ -256,12 +260,9 @@ function main(package) local ok = false local urls_failed = {} for idx, url in ipairs(urls) do - - -- get url alias local url_alias = package:url_alias(url) - - -- get url excludes local url_excludes = package:url_excludes(url) + local url_http_headers = package:url_http_headers(url) -- filter url url = filter.handle(url, package) @@ -289,11 +290,19 @@ function main(package) local sourcedir = "source" local script = package:script("download") if script then - script(package, {sourcedir = sourcedir, url = url, url_alias = url_alias, url_excludes = url_excludes}) + script(package, { + sourcedir = sourcedir, + url = url, + url_alias = url_alias, + url_excludes = url_excludes}) elseif git.checkurl(url) then - _checkout(package, url, sourcedir, url_alias) + _checkout(package, url, sourcedir, { + url_alias = url_alias}) else - _download(package, url, sourcedir, url_alias, url_excludes) + _download(package, url, sourcedir, { + url_alias = url_alias, + url_excludes = url_excludes, + url_http_headers = url_http_headers}) end return true end, diff --git a/xmake/modules/private/action/require/impl/actions/download_resources.lua b/xmake/modules/private/action/require/impl/actions/download_resources.lua index 5373fd81f..a62d3a3af 100644 --- a/xmake/modules/private/action/require/impl/actions/download_resources.lua +++ b/xmake/modules/private/action/require/impl/actions/download_resources.lua @@ -114,7 +114,9 @@ function _download(package, resource_name, resource_url, resource_hash) -- we can use local resource from the search directories directly if network is too slow os.cp(localfile, resource_file) elseif resource_url:find(string.ipattern("https-://")) or resource_url:find(string.ipattern("ftps-://")) then - http.download(resource_url, resource_file, {insecure = global.get("insecure-ssl")}) + http.download(resource_url, resource_file, { + insecure = global.get("insecure-ssl"), + headers = package:policy("package.download.http_headers")}) else raise("invalid resource url(%s)", resource_url) end diff --git a/xmake/modules/private/action/require/impl/actions/patch_sources.lua b/xmake/modules/private/action/require/impl/actions/patch_sources.lua index 10b89c911..cef9aa692 100644 --- a/xmake/modules/private/action/require/impl/actions/patch_sources.lua +++ b/xmake/modules/private/action/require/impl/actions/patch_sources.lua @@ -73,7 +73,9 @@ function _patch(package, patch_url, patch_hash) -- download the patch file if patch_url:find(string.ipattern("https-://")) or patch_url:find(string.ipattern("ftps-://")) then - http.download(patch_url, patch_file) + http.download(patch_url, patch_file, { + insecure = global.get("insecure-ssl"), + headers = package:policy("package.download.http_headers")}) else -- copy the patch file if os.isfile(patch_url) then |
