From dbd7a9ed1e636b3d15f47034ef48a8a130141a2d Mon Sep 17 00:00:00 2001 From: pedroSG94 Date: Tue, 26 Nov 2024 22:34:26 +0100 Subject: [PATCH] fix getAppName parser --- .../main/java/com/pedro/common/UrlParser.kt | 18 +-- .../java/com/pedro/common/UrlParserTest.kt | 127 ++++++------------ 2 files changed, 47 insertions(+), 98 deletions(-) diff --git a/common/src/main/java/com/pedro/common/UrlParser.kt b/common/src/main/java/com/pedro/common/UrlParser.kt index d04b4a8c3..8cbac9665 100644 --- a/common/src/main/java/com/pedro/common/UrlParser.kt +++ b/common/src/main/java/com/pedro/common/UrlParser.kt @@ -67,22 +67,16 @@ class UrlParser private constructor( } fun getAppName(): String { - val fullPath = getFullPath() - val indexes = fullPath.getIndexes('/') + val app = path.ifEmpty { query ?: "" } + val indexes = app.getIndexes('/') return when (indexes.size) { - 0 -> fullPath - 1 -> fullPath.substring(0, indexes[0]) - else -> { - if (getAllQueries().isEmpty()) { - fullPath.substring(0, indexes[1]) - } else { - fullPath.substring(0, indexes[0]) - } - } + 0 -> app + 1 -> app.substring(0, indexes[0]) + else -> app.substring(0, indexes[1]) } } - fun getStreamName(): String = getFullPath().removePrefix(getAppName()).removePrefix("/") + fun getStreamName(): String = getFullPath().removePrefix(getAppName()).removePrefix("/").removePrefix("?") fun getTcUrl(): String { val port = if (port != null) ":$port" else "" diff --git a/common/src/test/java/com/pedro/common/UrlParserTest.kt b/common/src/test/java/com/pedro/common/UrlParserTest.kt index 7054376a1..74edcd311 100644 --- a/common/src/test/java/com/pedro/common/UrlParserTest.kt +++ b/common/src/test/java/com/pedro/common/UrlParserTest.kt @@ -3,7 +3,6 @@ package com.pedro.common import junit.framework.TestCase.assertEquals import org.junit.Test import java.net.URISyntaxException -import java.util.regex.Pattern /** * Created by pedro on 2/9/24. @@ -18,9 +17,9 @@ class UrlParserTest { assertEquals("rtmp", urlParser.scheme) assertEquals("localhost", urlParser.host) assertEquals(1935, urlParser.port) - assertEquals("live?test", urlParser.getAppName()) - assertEquals("fake", urlParser.getStreamName()) - assertEquals("rtmp://localhost:1935/live?test", urlParser.getTcUrl()) + assertEquals("live", urlParser.getAppName()) + assertEquals("test/fake", urlParser.getStreamName()) + assertEquals("rtmp://localhost:1935/live", urlParser.getTcUrl()) val url0 = "rtmp://192.168.238.182:1935/live/100044?userId=100044&roomTitle=123123&roomCover=http://192.168.238.182/xxxx.png" val urlParser0 = UrlParser.parse(url0, arrayOf("rtmp")) @@ -36,9 +35,9 @@ class UrlParserTest { assertEquals("rtmp", urlParser1.scheme) assertEquals("localhost", urlParser1.host) assertEquals(1935, urlParser1.port) - assertEquals("live?test", urlParser1.getAppName()) - assertEquals("fake", urlParser1.getStreamName()) - assertEquals("rtmp://localhost:1935/live?test", urlParser1.getTcUrl()) + assertEquals("live", urlParser1.getAppName()) + assertEquals("test/fake", urlParser1.getStreamName()) + assertEquals("rtmp://localhost:1935/live", urlParser1.getTcUrl()) assertEquals("user", urlParser1.getAuthUser()) assertEquals("pass", urlParser1.getAuthPassword()) @@ -86,6 +85,24 @@ class UrlParserTest { assertEquals("live/test", urlParser6.getAppName()) assertEquals("fake", urlParser6.getStreamName()) assertEquals("rtmp://192.168.0.1:1234/live/test", urlParser6.getTcUrl()) + + val url7 = "rtmp://192.168.0.1:1935/v2/pub/streamName?token=streamToken" + val urlParser7 = UrlParser.parse(url7, arrayOf("rtmp")) + assertEquals("rtmp", urlParser7.scheme) + assertEquals("192.168.0.1", urlParser7.host) + assertEquals(1935, urlParser7.port) + assertEquals("v2/pub", urlParser7.getAppName()) + assertEquals("streamName?token=streamToken", urlParser7.getStreamName()) + assertEquals("rtmp://192.168.0.1:1935/v2/pub", urlParser7.getTcUrl()) + + val url8 = "rtmp://192.168.0.1:1935/v2/pub/streamName?token" + val urlParser8 = UrlParser.parse(url8, arrayOf("rtmp")) + assertEquals("rtmp", urlParser8.scheme) + assertEquals("192.168.0.1", urlParser8.host) + assertEquals(1935, urlParser8.port) + assertEquals("v2/pub", urlParser8.getAppName()) + assertEquals("streamName?token", urlParser8.getStreamName()) + assertEquals("rtmp://192.168.0.1:1935/v2/pub", urlParser8.getTcUrl()) } catch (e: URISyntaxException) { assert(false) } @@ -203,95 +220,33 @@ class UrlParserTest { @Test fun testUdpUrls() { - try { - val url = "udp://localhost:1935" - val urlParser = UrlParser.parse(url, arrayOf("udp")) - assertEquals("udp", urlParser.scheme) - assertEquals("localhost", urlParser.host) - assertEquals(1935, urlParser.port) - - val url0 = "udp://localhost:1935/" - val urlParser0 = UrlParser.parse(url0, arrayOf("udp")) - assertEquals("udp", urlParser0.scheme) - assertEquals("localhost", urlParser0.host) - assertEquals(1935, urlParser0.port) - } catch (e: URISyntaxException) { - assert(false) - } + val url = "udp://localhost:1935" + val urlParser = UrlParser.parse(url, arrayOf("udp")) + assertEquals("udp", urlParser.scheme) + assertEquals("localhost", urlParser.host) + assertEquals(1935, urlParser.port) + + val url0 = "udp://localhost:1935/" + val urlParser0 = UrlParser.parse(url0, arrayOf("udp")) + assertEquals("udp", urlParser0.scheme) + assertEquals("localhost", urlParser0.host) + assertEquals(1935, urlParser0.port) } @Test fun testUrl2() { - try { - val urlParser = UrlParser.parse( - "srt://push.domain.com:1105?streamid=#!::h=push.domain.com,r=/live/stream,m=publish&live=asd", - arrayOf("srt") - ) - println(urlParser.toString()) - println(urlParser.getQuery("streamid")) - } catch (e: IllegalArgumentException) { - - } + val urlParser = UrlParser.parse( + "srt://push.domain.com:1105?streamid=#!::h=push.domain.com,r=/live/stream,m=publish&live=asd", + arrayOf("srt") + ) + assertEquals("#!::h=push.domain.com,r=/live/stream,m=publish", urlParser.getQuery("streamid")) } @Test fun testUrl3() { - try { - val urlParser = UrlParser.parse( + UrlParser.parse( "rtmp://localhost?live=adasdasd", arrayOf("rtmp") ) - println(urlParser.toString()) - } catch (e: IllegalArgumentException) { - - } - } - - private fun oldParser(url: String) { - val urlPattern: Pattern = Pattern.compile("^rtmpt?s?://([^/:]+)(?::(\\d+))*/([^/]+)/?([^*]*)$") - val tunneled: Boolean - val tlsEnabled: Boolean - val rtmpMatcher = urlPattern.matcher(url) - if (!rtmpMatcher.matches()) { - assert(false) - } - val schema = rtmpMatcher.group(0) ?: "" - tunneled = schema.startsWith("rtmpt") - tlsEnabled = schema.startsWith("rtmps") || schema.startsWith("rtmpts") - - val host = rtmpMatcher.group(1) ?: "" - val portStr = rtmpMatcher.group(2) - val defaultPort = if (tlsEnabled) 443 else if (tunneled) 80 else 1935 - val port = portStr?.toInt() ?: defaultPort - val appName = getAppName(rtmpMatcher.group(3) ?: "", rtmpMatcher.group(4) ?: "") - val streamName = getStreamName(rtmpMatcher.group(4) ?: "") - val tcUrl = getTcUrl((rtmpMatcher.group(0) - ?: "").substring(0, (rtmpMatcher.group(0) - ?: "").length - streamName.length)) - println("OldParser(scheme='$schema', host='$host', port=$port, appName='$appName', streamName='$streamName', tcUrl='$tcUrl'") - } - - private fun getAppName(app: String, name: String): String { - return if (!name.contains("/")) { - app - } else { - app + "/" + name.substring(0, name.indexOf("/")) - } - } - - private fun getStreamName(name: String): String { - return if (!name.contains("/")) { - name - } else { - name.substring(name.indexOf("/") + 1) - } - } - - private fun getTcUrl(url: String): String { - return if (url.endsWith("/")) { - url.substring(0, url.length - 1) - } else { - url - } } } \ No newline at end of file