Fix kiwix proxy 502ing on article links with non-ASCII titles
Sinatra decodes a route's splat before handing it to the block, so an article link like /kiwix/content/wikipedia/Dlhé_diely arrived as raw UTF-8, not percent-encoded. URI#path= rejects that outright (URI::InvalidComponentError) rather than escaping it, which .proxy's rescue turned into a silent nil -> every article link 502'd, while the search results page linking to them worked fine (its own path, /kiwix/search, is plain ASCII).
This commit is contained in:
1 parent
5ff9b113d9
commit
2b49eda7cb
2 files changed
+33
-1
No files matched your search
@@ -26,9 +26,16 @@ module Maptool
|
||||
# correct without this having to touch response bodies at all. Returns
|
||||
# nil (halted as a 502 by the caller) rather than raising, same
|
||||
# not-reachable-isn't-exceptional stance as .reachable? above.
|
||||
#
|
||||
# path arrives already URL-*decoded* — Sinatra decodes a route's splat
|
||||
# before handing it over — so an article title with non-ASCII
|
||||
# characters (e.g. "/kiwix/content/wikipedia/Dlhé_diely") has to be
|
||||
# re-escaped before URI#path= will accept it at all; it raises
|
||||
# URI::InvalidComponentError on raw UTF-8 bytes rather than escaping
|
||||
# them itself. Article links otherwise 502'd for exactly this reason.
|
||||
def self.proxy(base_url, path, query_string, open_timeout: 2, read_timeout: 10)
|
||||
uri = URI(base_url)
|
||||
uri.path = path
|
||||
uri.path = URI::RFC2396_PARSER.escape(path)
|
||||
uri.query = query_string unless query_string.to_s.empty?
|
||||
Net::HTTP.start(uri.host, uri.port, open_timeout: open_timeout, read_timeout: read_timeout) { |http| http.request(Net::HTTP::Get.new(uri)) }
|
||||
rescue StandardError
|
||||
|
||||
Reference in new issue
Block a user