File tree 2 files changed +21
-3
lines changed
lib/carrierwave/downloader 2 files changed +21
-3
lines changed Original file line number Diff line number Diff line change @@ -31,7 +31,7 @@ def download(url, remote_headers = {})
31
31
end
32
32
33
33
##
34
- # Processes the given URL by parsing and escaping it . Public to allow overriding.
34
+ # Processes the given URL by parsing it, and escaping if necessary . Public to allow overriding.
35
35
#
36
36
# === Parameters
37
37
#
@@ -40,8 +40,12 @@ def download(url, remote_headers = {})
40
40
def process_uri ( uri )
41
41
uri_parts = uri . split ( '?' )
42
42
encoded_uri = Addressable ::URI . parse ( uri_parts . shift ) . normalize . to_s
43
- encoded_uri << '?' << Addressable ::URI . encode ( uri_parts . join ( '?' ) ) . gsub ( '%5B' , '[' ) . gsub ( '%5D' , ']' ) if uri_parts . any?
44
- URI . parse ( encoded_uri )
43
+ query = uri_parts . any? ? "?#{ uri_parts . join ( '?' ) } " : ''
44
+ begin
45
+ URI . parse ( "#{ encoded_uri } #{ query } " )
46
+ rescue URI ::InvalidURIError
47
+ URI . parse ( "#{ encoded_uri } #{ URI ::DEFAULT_PARSER . escape ( query ) } " )
48
+ end
45
49
rescue URI ::InvalidURIError , Addressable ::URI ::InvalidURIError
46
50
raise CarrierWave ::DownloadError , "couldn't parse URL: #{ uri } "
47
51
end
Original file line number Diff line number Diff line change 159
159
expect ( processed . to_s ) . to eq ( 'http://example.com/%5D.jpg?test[]' )
160
160
end
161
161
162
+ it "escapes and parse unescaped characters in path" do
163
+ uri = 'http://example.com/あああ.jpg'
164
+ processed = subject . process_uri ( uri )
165
+ expect ( processed . class ) . to eq ( URI ::HTTP )
166
+ expect ( processed . to_s ) . to eq ( 'http://example.com/%E3%81%82%E3%81%82%E3%81%82.jpg' )
167
+ end
168
+
169
+ it "escapes and parse unescaped characters in query string" do
170
+ uri = 'http://example.com/?q=あああ'
171
+ processed = subject . process_uri ( uri )
172
+ expect ( processed . class ) . to eq ( URI ::HTTP )
173
+ expect ( processed . to_s ) . to eq ( 'http://example.com/?q=%E3%81%82%E3%81%82%E3%81%82' )
174
+ end
175
+
162
176
it "throws an exception on bad uris" do
163
177
uri = '~http:'
164
178
expect { subject . process_uri ( uri ) } . to raise_error ( CarrierWave ::DownloadError )
You can’t perform that action at this time.
0 commit comments