Skip to content

Commit d26347a

Browse files
authored
Merge pull request #1831 from deltachat/trailing-slash
be more tolerant on webrtc-servers set by the user
2 parents a3c700c + 54edd4d commit d26347a

File tree

2 files changed

+75
-7
lines changed

2 files changed

+75
-7
lines changed

src/chat.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -1629,13 +1629,7 @@ pub async fn send_videochat_invitation(context: &Context, chat_id: ChatId) -> Re
16291629
bail!("webrtc_instance not set");
16301630
};
16311631

1632-
let room = dc_create_id();
1633-
1634-
let instance = if instance.contains("$ROOM") {
1635-
instance.replace("$ROOM", &room)
1636-
} else {
1637-
format!("{}{}", instance, room)
1638-
};
1632+
let instance = Message::create_webrtc_instance(&instance, &dc_create_id());
16391633

16401634
let mut msg = Message::new(Viewtype::VideochatInvitation);
16411635
msg.param.set(Param::WebrtcRoom, &instance);

src/message.rs

+74
Original file line numberDiff line numberDiff line change
@@ -643,8 +643,45 @@ impl Message {
643643
None
644644
}
645645

646+
// add room to a webrtc_instance as defined by the corresponding config-value;
647+
// the result may still be prefixed by the type
648+
pub fn create_webrtc_instance(instance: &str, room: &str) -> String {
649+
let (videochat_type, mut url) = Message::parse_webrtc_instance(instance);
650+
651+
// make sure, there is a scheme in the url
652+
if !url.contains(':') {
653+
url = format!("https://{}", url);
654+
}
655+
656+
// add/replace room
657+
let url = if url.contains("$ROOM") {
658+
url.replace("$ROOM", &room)
659+
} else {
660+
// if there nothing that would separate the room, add a slash as a separator;
661+
// this way, urls can be given as "https://meet.jit.si" as well as "https://meet.jit.si/"
662+
let maybe_slash = if url.ends_with('/')
663+
|| url.ends_with('?')
664+
|| url.ends_with('#')
665+
|| url.ends_with('=')
666+
{
667+
""
668+
} else {
669+
"/"
670+
};
671+
format!("{}{}{}", url, maybe_slash, room)
672+
};
673+
674+
// re-add and normalize type
675+
match videochat_type {
676+
VideochatType::BasicWebrtc => format!("basicwebrtc:{}", url),
677+
VideochatType::Jitsi => format!("jitsi:{}", url),
678+
VideochatType::Unknown => url,
679+
}
680+
}
681+
646682
/// split a webrtc_instance as defined by the corresponding config-value into a type and a url
647683
pub fn parse_webrtc_instance(instance: &str) -> (VideochatType, String) {
684+
let instance: String = instance.split_whitespace().collect();
648685
let mut split = instance.splitn(2, ':');
649686
let type_str = split.next().unwrap_or_default().to_lowercase();
650687
let url = split.next();
@@ -1877,6 +1914,43 @@ mod tests {
18771914
assert_eq!(url, "https://j.si/foo");
18781915
}
18791916

1917+
#[async_std::test]
1918+
async fn test_create_webrtc_instance() {
1919+
// webrtc_instance may come from an input field of the ui, be pretty tolerant on input
1920+
let instance = Message::create_webrtc_instance("https://meet.jit.si/", "123");
1921+
assert_eq!(instance, "https://meet.jit.si/123");
1922+
1923+
let instance = Message::create_webrtc_instance("https://meet.jit.si", "456");
1924+
assert_eq!(instance, "https://meet.jit.si/456");
1925+
1926+
let instance = Message::create_webrtc_instance("meet.jit.si", "789");
1927+
assert_eq!(instance, "https://meet.jit.si/789");
1928+
1929+
let instance = Message::create_webrtc_instance("bla.foo?", "123");
1930+
assert_eq!(instance, "https://bla.foo?123");
1931+
1932+
let instance = Message::create_webrtc_instance("jitsi:bla.foo#", "456");
1933+
assert_eq!(instance, "jitsi:https://bla.foo#456");
1934+
1935+
let instance = Message::create_webrtc_instance("bla.foo#room=", "789");
1936+
assert_eq!(instance, "https://bla.foo#room=789");
1937+
1938+
let instance = Message::create_webrtc_instance("https://bla.foo#room", "123");
1939+
assert_eq!(instance, "https://bla.foo#room/123");
1940+
1941+
let instance = Message::create_webrtc_instance("bla.foo#room$ROOM", "123");
1942+
assert_eq!(instance, "https://bla.foo#room123");
1943+
1944+
let instance = Message::create_webrtc_instance("bla.foo#room=$ROOM&after=cont", "234");
1945+
assert_eq!(instance, "https://bla.foo#room=234&after=cont");
1946+
1947+
let instance = Message::create_webrtc_instance(" meet.jit .si ", "789");
1948+
assert_eq!(instance, "https://meet.jit.si/789");
1949+
1950+
let instance = Message::create_webrtc_instance(" basicwebrtc: basic . stuff\n ", "12345ab");
1951+
assert_eq!(instance, "basicwebrtc:https://basic.stuff/12345ab");
1952+
}
1953+
18801954
#[async_std::test]
18811955
async fn test_get_width_height() {
18821956
let t = test::TestContext::new().await;

0 commit comments

Comments
 (0)