Percent-encode unsafe characters in a filename when composing each URL.

This commit is contained in:
Akinori MUSHA 2014-08-07 18:11:20 +09:00 committed by Dominik Sander
parent 8cf4b3d3bf
commit b9074a9930
2 changed files with 16 additions and 2 deletions

View file

@ -174,8 +174,8 @@ module Agents
new_files.sort_by { |filename|
found_entries[filename]
}.each { |filename|
create_event :payload => {
'url' => "#{base_uri}#{filename}",
create_event payload: {
'url' => (base_uri + uri_path_escape(filename)).to_s,
'filename' => filename,
'timestamp' => found_entries[filename],
}
@ -192,5 +192,13 @@ module Agents
rescue
false
end
def uri_path_escape(string)
str = string.dup.force_encoding(Encoding::ASCII_8BIT) # string.b in Ruby >=2.0
str.gsub!(/([^A-Za-z0-9\-._~!$&()*+,=@]+)/) { |m|
'%' + m.unpack('H2' * m.bytesize).join('%').upcase
}
str.force_encoding(Encoding::US_ASCII)
end
end
end

View file

@ -76,6 +76,12 @@ describe Agents::FtpsiteAgent do
'timestamp' => '2014-04-02T10:00:00Z',
}
Event.last.payload.should == {
'url' => 'ftp://ftp.example.org/pub/releases/example%20latest.tar.gz',
'filename' => 'example latest.tar.gz',
'timestamp' => '2014-04-02T10:00:01Z',
}
lambda { @checker.check }.should_not change { Event.count }
end
end