Skip to content

Commit

Permalink
fix: Avoid decoding already encoded character in URI (#1679)
Browse files Browse the repository at this point in the history
Fixes #1669
  • Loading branch information
Gustl22 authored Oct 23, 2023
1 parent 1ea9353 commit 0119378
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
10 changes: 6 additions & 4 deletions packages/audioplayers/lib/src/uri_ext.dart
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);
}
}
41 changes: 41 additions & 0 deletions packages/audioplayers/test/uri_coder_test.dart
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);
},
);
});
}

0 comments on commit 0119378

Please sign in to comment.