diff options
| author | William Boman <william@redwill.se> | 2022-01-09 16:33:07 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-01-09 16:33:07 +0100 |
| commit | 93acd71d9176d114bf62a8e2e49bb6a9a266bc1c (patch) | |
| tree | f52b7ba6a08be4620cd8b829a96ca08026f80d59 /lua | |
| parent | fix(jdtls): redefine cmd in on_new_config (diff) | |
| download | mason-93acd71d9176d114bf62a8e2e49bb6a9a266bc1c.tar mason-93acd71d9176d114bf62a8e2e49bb6a9a266bc1c.tar.gz mason-93acd71d9176d114bf62a8e2e49bb6a9a266bc1c.tar.bz2 mason-93acd71d9176d114bf62a8e2e49bb6a9a266bc1c.tar.lz mason-93acd71d9176d114bf62a8e2e49bb6a9a266bc1c.tar.xz mason-93acd71d9176d114bf62a8e2e49bb6a9a266bc1c.tar.zst mason-93acd71d9176d114bf62a8e2e49bb6a9a266bc1c.zip | |
fetch: add User-Agent header (#409)
Diffstat (limited to 'lua')
| -rw-r--r-- | lua/nvim-lsp-installer/core/fetch.lua | 30 | ||||
| -rw-r--r-- | lua/nvim-lsp-installer/installers/std.lua | 9 |
2 files changed, 32 insertions, 7 deletions
diff --git a/lua/nvim-lsp-installer/core/fetch.lua b/lua/nvim-lsp-installer/core/fetch.lua index 981ad376..e3204cf2 100644 --- a/lua/nvim-lsp-installer/core/fetch.lua +++ b/lua/nvim-lsp-installer/core/fetch.lua @@ -2,6 +2,21 @@ local log = require "nvim-lsp-installer.log" local process = require "nvim-lsp-installer.process" local platform = require "nvim-lsp-installer.platform" +local USER_AGENT = "nvim-lsp-installer (+https://github.com/williamboman/nvim-lsp-installer)" + +local HEADERS = { + wget = { "--header", ("User-Agent: %s"):format(USER_AGENT) }, + curl = { "-H", ("User-Agent: %s"):format(USER_AGENT) }, + iwr = ("-Headers @{'User-Agent' = '%s'}"):format(USER_AGENT), +} + +local function with_headers(headers, args) + local result = {} + vim.list_extend(result, headers) + vim.list_extend(result, args) + return result +end + ---@param url string The url to fetch. ---@param callback fun(err: string|nil, raw_data: string) ---@param opts {custom_fetcher: { cmd: string, args: string[] }} @@ -22,11 +37,11 @@ local function fetch(url, callback, opts) local job_variants = { process.lazy_spawn("wget", { - args = { "-nv", "-O", "-", url }, + args = with_headers(HEADERS.wget, { "-nv", "-O", "-", url }), stdio_sink = stdio.sink, }), process.lazy_spawn("curl", { - args = { "-fsSL", url }, + args = with_headers(HEADERS.curl, { "-fsSL", url }), stdio_sink = stdio.sink, }), } @@ -34,7 +49,7 @@ local function fetch(url, callback, opts) if platform.is_win then local ps_script = { "$ProgressPreference = 'SilentlyContinue'", - ("Write-Output (iwr -UseBasicParsing -Uri %q).Content"):format(url), + ("Write-Output (iwr %s -UseBasicParsing -Uri %q).Content"):format(HEADERS.iwr, url), } table.insert( job_variants, @@ -69,4 +84,11 @@ local function fetch(url, callback, opts) } end -return fetch +return setmetatable({ + with_headers = with_headers, + HEADERS = HEADERS, +}, { + __call = function(_, ...) + return fetch(...) + end, +}) diff --git a/lua/nvim-lsp-installer/installers/std.lua b/lua/nvim-lsp-installer/installers/std.lua index 2d3a8155..ceaa6a0f 100644 --- a/lua/nvim-lsp-installer/installers/std.lua +++ b/lua/nvim-lsp-installer/installers/std.lua @@ -5,6 +5,7 @@ local platform = require "nvim-lsp-installer.platform" local installers = require "nvim-lsp-installer.installers" local shell = require "nvim-lsp-installer.installers.shell" local Data = require "nvim-lsp-installer.data" +local fetch = require "nvim-lsp-installer.core.fetch" local list_not_nil, when = Data.list_not_nil, Data.when @@ -20,12 +21,12 @@ function M.download_file(url, out_file) process.attempt { jobs = { process.lazy_spawn("wget", { - args = { "-nv", "-O", out_file, url }, + args = fetch.with_headers(fetch.HEADERS.wget, { "-nv", "-O", out_file, url }), cwd = context.install_dir, stdio_sink = context.stdio_sink, }), process.lazy_spawn("curl", { - args = { "-fsSL", "-o", out_file, url }, + args = fetch.with_headers(fetch.HEADERS.curl, { "-fsSL", "-o", out_file, url }), cwd = context.install_dir, stdio_sink = context.stdio_sink, }), @@ -33,7 +34,9 @@ function M.download_file(url, out_file) on_finish = callback, } end, - win = shell.powershell(("iwr -UseBasicParsing -Uri %q -OutFile %q"):format(url, out_file)), + win = shell.powershell( + ("iwr %s -UseBasicParsing -Uri %q -OutFile %q"):format(fetch.HEADERS.iwr, url, out_file) + ), } end |
