Skip to content

NewsDownloader: Add URL path encoding to improve request handling#14870

Merged
Frenzie merged 4 commits into
koreader:masterfrom
leakspin:master
Jan 26, 2026
Merged

NewsDownloader: Add URL path encoding to improve request handling#14870
Frenzie merged 4 commits into
koreader:masterfrom
leakspin:master

Conversation

@leakspin

@leakspin leakspin commented Jan 23, 2026

Copy link
Copy Markdown
Contributor

Original problem

I use the News/Feed Downloader to download webcomics. As I am using it, there are some images that could not be downloaded if it contains blank spaces in its path, i.e. [https://fandogamia.com/fanternet/content/nadadelotromundo/Curso de cocina para exdioses - 034.jpg](https://fandogamia.com/fanternet/content/nadadelotromundo/Curso de cocina para exdioses - 034.jpg).

Solution

I have implemented a small function that, according to the RFC 3986, converts invalid path characters to their percent-encoded equivalent.


This change is Reviewable

@leakspin leakspin changed the title Add URL path encoding to improve request handling NewsDownloader: Add URL path encoding to improve request handling Jan 23, 2026
Comment on lines +219 to +231
local encoded_path = ""

for i = 1, #path do
local char = path:sub(i, i)
if char:match("[a-zA-Z0-9.-_~!$&'()*+,;=:@]") then -- Unreserved characters
encoded_path = encoded_path .. char
else
local code = string.byte(char)
encoded_path = encoded_path .. string.format("%%%02X", code)
end
end

parsed_url.path = encoded_path

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do this using something like

for c in str:gmatch"." do
    print(c)
end

Which would avoid the overhead of allocating all those strings (granted, probably not too much of a concern here).

But just file that away for future reference, because please use util.urlEncode() instead.

socketutil:set_timeout(timeout, maxtime or 30)
local request = {
url = url,
url = path_percentage_encode(url),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd skip the separate function to keep it simple to follow. Just stick two or three lines above, along the lines of:

local parsed_url = socket_url.parse(url)
local path = parsed_url.path -- not really sure what that or "/" is about though, better to just ignore it
if path then
    parsed_url.path = util.urlEncode(path)
    url = socket_url.build(parsed_url)
end

@Frenzie Frenzie added the Plugin label Jan 23, 2026
@Frenzie Frenzie added this to the 2026.01 milestone Jan 23, 2026
@leakspin

Copy link
Copy Markdown
Contributor Author

Hi @Frenzie!

Just implemented your suggestions, I didn't notice the util.urlEncode function that solves many problems that I thought I had. Thanks for the heads up!

Comment thread plugins/newsdownloader.koplugin/epubdownloadbackend.lua
@Frenzie Frenzie merged commit 7162708 into koreader:master Jan 26, 2026
1 of 2 checks passed
@Frenzie

Frenzie commented Jan 26, 2026

Copy link
Copy Markdown
Member

Thanks!

0xstillb pushed a commit to 0xstillb/koreader-thai that referenced this pull request May 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants