-
Notifications
You must be signed in to change notification settings - Fork 845
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Avoid decoding already encoded character in URI (#1679)
Fixes #1669
- Loading branch information
Showing
2 changed files
with
47 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
extension UriCoder on Uri { | ||
static String encodeOnce(String uri) { | ||
var tmpUri = uri; | ||
try { | ||
// Try decoding first to avoid encoding twice: | ||
tmpUri = Uri.decodeFull(tmpUri); | ||
// If decoded differs, the uri was already encoded. | ||
final decodedUri = Uri.decodeFull(uri); | ||
if (decodedUri != uri) { | ||
return uri; | ||
} | ||
} on ArgumentError catch (_) {} | ||
return Uri.encodeFull(tmpUri); | ||
return Uri.encodeFull(uri); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import 'package:audioplayers/src/uri_ext.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
void main() { | ||
TestWidgetsFlutterBinding.ensureInitialized(); | ||
|
||
group('UriCoder', () { | ||
test( | ||
'Encode Special Character', | ||
() { | ||
const uri = '/coins_non_ascii_и.wav'; | ||
final encoded = UriCoder.encodeOnce(uri); | ||
expect(encoded, '/coins_non_ascii_%D0%B8.wav'); | ||
}, | ||
); | ||
test( | ||
'Encode Space', | ||
() { | ||
const uri = '/coins .wav'; | ||
final encoded = UriCoder.encodeOnce(uri); | ||
expect(encoded, '/coins%20.wav'); | ||
}, | ||
); | ||
test( | ||
'Already encoded Character', | ||
() { | ||
const uri = 'https://myurl/audio%2F_music.mp4?alt=media&token=abc'; | ||
final encoded = UriCoder.encodeOnce(uri); | ||
expect(encoded, uri); | ||
}, | ||
); | ||
test( | ||
'Encoded and decoded are the same', | ||
() { | ||
const uri = 'https://myurl/audio'; | ||
final encoded = UriCoder.encodeOnce(uri); | ||
expect(encoded, uri); | ||
}, | ||
); | ||
}); | ||
} |