From 30df1f340f74d0eacc979215adff9c25a48047f0 Mon Sep 17 00:00:00 2001 From: Adam Harrison Date: Tue, 12 Mar 2024 15:34:15 -0400 Subject: Fixed some stuff with chunked encoding. --- src/lpm.c | 8 ++++++-- src/lpm.lua | 14 +++++++++----- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/lpm.c b/src/lpm.c index 477dc98..16619c5 100644 --- a/src/lpm.c +++ b/src/lpm.c @@ -1174,8 +1174,9 @@ static int lpm_get(lua_State* L) { while (1) { // If we have an unknown amount of chunk bytes to be fetched, determine the size of the next chunk. while (chunk_length == -1) { - const char* newline = strnstr(buffer, "\r\n", buffer_length); + char* newline = (char*)strnstr(buffer, "\r\n", buffer_length); if (newline) { + *newline = '\0'; if (sscanf(buffer, "%x", &chunk_length) != 1) { snprintf(err, sizeof(err), "error retrieving chunk length %s%s", hostname, rest); goto cleanup; @@ -1206,7 +1207,10 @@ static int lpm_get(lua_State* L) { if (callback_function) { lua_pushvalue(L, callback_function); lua_pushinteger(L, total_downloaded); - lua_pushinteger(L, content_length); + if (content_length == -1) + lua_pushnil(L); + else + lua_pushinteger(L, content_length); lua_call(L, 2, 0); } if (file) diff --git a/src/lpm.lua b/src/lpm.lua index 0289915..83f833f 100644 --- a/src/lpm.lua +++ b/src/lpm.lua @@ -599,14 +599,14 @@ function common.get(source, options) if not port or port == "" then port = protocol == "https" and 443 or 80 end if not rest or rest == "" then rest = "/" end local res, headers - if not checksum then + if checksum == "SKIP" and not target then res, headers = system.get(protocol, hostname, port, rest, target, callback) if headers.location then return common.get(headers.location, common.merge(options, { depth = (depth or 0) + 1 })) end return res end - local cache_dir = options.cache or CACHEDIR + local cache_dir = checksum ~= "SKIP" and TMPDIR or (options.cache or CACHEDIR) if not system.stat(cache_dir .. PATHSEP .. "files") then common.mkdirp(cache_dir .. PATHSEP .. "files") end - local cache_path = cache_dir .. PATHSEP .. "files" .. PATHSEP .. system.hash(source) + local cache_path = cache_dir .. PATHSEP .. "files" .. PATHSEP .. system.hash(checksum .. source) if checksum ~= "SKIP" and system.stat(cache_path) and system.hash(cache_path, "file") ~= checksum then common.rmrf(cache_path) end local res if not system.stat(cache_path) then @@ -619,6 +619,7 @@ function common.get(source, options) common.rename(cache_path .. ".part", cache_path) end if target then common.copy(cache_path, target) else res = io.open(cache_path, "rb"):read("*all") end + if checksum == "SKIP" then common.rmrf(cache_path) end return res end @@ -2434,7 +2435,9 @@ not commonly used publically. return end if not start_time or not last_read or total_read < last_read then start_time = system.time() end - local status_line = string.format("%s [%s/s][%03d%%]: ", format_bytes(total_read), format_bytes(total_read / (system.time() - start_time)), math.floor((received_objects and (received_objects/total_objects_or_content_length) or (total_read/total_objects_or_content_length) or 0)*100)) + local status_line = total_objects_or_content_length and + string.format("%s [%s/s][%03d%%]: ", format_bytes(total_read), format_bytes(total_read / (system.time() - start_time)), math.floor((received_objects and (received_objects/total_objects_or_content_length) or (total_read/total_objects_or_content_length) or 0)*100)) or + string.format("%s [%s/s]: ", format_bytes(total_read), format_bytes(total_read / (system.time() - start_time))) local terminal_width = system.tcwidth(1) if not terminal_width then terminal_width = #status_line + #progress_bar_label end local characters_remaining = terminal_width - #status_line @@ -2527,7 +2530,8 @@ not commonly used publically. os.exit(0) end if ARGS[2] == "download" then - local file = common.get(ARGS[3], { target = ARGS[4] }); + if ARGS[4] then log.progress_action("Downloading " .. ARGS[3]) end + local file = common.get(ARGS[3], { target = ARGS[4], callback = write_progress_bar }); if file then print(file) end os.exit(0) end -- cgit v1.2.3