Decode and encode urls with non-authorized characters #1174

Merged
kekkyojin merged 2 commits from decode-urls into master 2021-03-19 11:00:24 +01:00
2 changed files with 48 additions and 3 deletions
Showing only changes of commit e49f675266 - Show all commits

View file

@ -2,6 +2,7 @@ package io.lbry.browser.utils;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
@ -199,7 +200,18 @@ public class LbryUri {
if (channelName != null) {
formattedChannelName = channelName.startsWith("@") ? channelName : String.format("@%s", channelName);
}
String primaryClaimName = claimName;
String primaryClaimName = null;
if (protocol.equals(LBRY_TV_BASE_URL) && Helper.isNullOrEmpty(formattedChannelName)) {
try {
primaryClaimName = URLEncoder.encode(claimName, UTF_8);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} else {
primaryClaimName = claimName;
}
if (Helper.isNullOrEmpty(primaryClaimName)) {
primaryClaimName = contentName;
}
@ -229,7 +241,15 @@ public class LbryUri {
secondaryClaimName = contentName;
}
if (Helper.isNullOrEmpty(secondaryClaimName)) {
secondaryClaimName = !Helper.isNullOrEmpty(formattedChannelName) ? streamName : null;
if (protocol.equals(LBRY_TV_BASE_URL)) {
try {
secondaryClaimName = !Helper.isNullOrEmpty(formattedChannelName) ? URLEncoder.encode(streamName, UTF_8) : null;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} else {
secondaryClaimName = !Helper.isNullOrEmpty(formattedChannelName) ? streamName : null;
}
}
String secondaryClaimId = !Helper.isNullOrEmpty(secondaryClaimName) ? streamClaimId : null;

View file

@ -137,7 +137,6 @@ public class LbryUriTest {
}
assertEquals(expected, obtained);
}
@Test
@ -153,6 +152,32 @@ public class LbryUriTest {
assertEquals("https://lbry.tv/@lbry:3f/lbryturns4:6", obtained.toTvString());
}
@Test
public void lbryToTvStringWithEncodedChars() {
LbryUri obtained = new LbryUri();
try {
obtained = LbryUri.parse("lbry://La-Peur,-Nos-Attentats,-c'est-VOTRE-Sécurité!-Les-Guignols#6",false);
} catch (LbryUriException e) {
e.printStackTrace();
}
assertEquals("https://lbry.tv/La-Peur%2C-Nos-Attentats%2C-c%27est-VOTRE-Se%CC%81curite%CC%81%21-Les-Guignols:6", obtained.toTvString());
}
@Test
public void lbryToTvStringWithChannelAndEncodedChars() {
LbryUri obtained = new LbryUri();
try {
obtained = LbryUri.parse("lbry://@test#1/La-Peur,-Nos-Attentats,-c'est-VOTRE-Sécurité!-Les-Guignols#6",false);
} catch (LbryUriException e) {
e.printStackTrace();
}
assertEquals("https://lbry.tv/@test:1/La-Peur%2C-Nos-Attentats%2C-c%27est-VOTRE-Se%CC%81curite%CC%81%21-Les-Guignols:6", obtained.toTvString());
}
@NotNull
private LbryUri sinthesizeExpected() {
LbryUri expectedForChannel = new LbryUri();