From d9bae453a4456e720a0d6432ab02dde7cac0a176 Mon Sep 17 00:00:00 2001 From: devoxin <15076404+devoxin@users.noreply.github.com> Date: Tue, 26 Dec 2023 18:17:23 +0000 Subject: [PATCH] [v1-legacy] Fix `Target host is not specified` with HLS relative urls in m3u8 playlists (#49) * Backporting relative URL to absolute URL conversion. * Fix bad logic --- .../HlsStreamSegmentUrlProvider.java | 1 + .../stream/M3uStreamSegmentUrlProvider.java | 35 ++++++++++++++++++- .../TwitchStreamSegmentUrlProvider.java | 1 + 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/main/src/main/java/com/sedmelluq/discord/lavaplayer/container/playlists/HlsStreamSegmentUrlProvider.java b/main/src/main/java/com/sedmelluq/discord/lavaplayer/container/playlists/HlsStreamSegmentUrlProvider.java index a22ff8eb..4127bee2 100644 --- a/main/src/main/java/com/sedmelluq/discord/lavaplayer/container/playlists/HlsStreamSegmentUrlProvider.java +++ b/main/src/main/java/com/sedmelluq/discord/lavaplayer/container/playlists/HlsStreamSegmentUrlProvider.java @@ -19,6 +19,7 @@ public class HlsStreamSegmentUrlProvider extends M3uStreamSegmentUrlProvider { private volatile String segmentPlaylistUrl; public HlsStreamSegmentUrlProvider(String streamListUrl, String segmentPlaylistUrl) { + super(streamListUrl); this.streamListUrl = streamListUrl; this.segmentPlaylistUrl = segmentPlaylistUrl; } diff --git a/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/stream/M3uStreamSegmentUrlProvider.java b/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/stream/M3uStreamSegmentUrlProvider.java index f2fc27e3..45b6a3d1 100644 --- a/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/stream/M3uStreamSegmentUrlProvider.java +++ b/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/stream/M3uStreamSegmentUrlProvider.java @@ -13,6 +13,7 @@ import java.io.IOException; import java.io.InputStream; import java.net.URI; +import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; @@ -29,8 +30,25 @@ public abstract class M3uStreamSegmentUrlProvider { private static final long SEGMENT_WAIT_STEP_MS = 200; private static final RequestConfig streamingRequestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectionRequestTimeout(5000).setConnectTimeout(5000).build(); + protected String baseUrl; protected SegmentInfo lastSegment; + protected M3uStreamSegmentUrlProvider() { + this(null); + } + + protected M3uStreamSegmentUrlProvider(String originUrl) { + if (originUrl != null) { + if (originUrl.endsWith("/")) { + originUrl = originUrl.substring(0, originUrl.length() - 1); + } + + this.baseUrl = originUrl.substring(0, originUrl.lastIndexOf("/")); + } else { + this.baseUrl = null; + } + } + protected static String createSegmentUrl(String playlistUrl, String segmentName) { return URI.create(playlistUrl).resolve(segmentName).toString(); } @@ -118,6 +136,20 @@ public InputStream getNextSegmentStream(HttpInterface httpInterface) { protected abstract HttpUriRequest createSegmentGetRequest(String url); + protected boolean isAbsoluteUrl(String url) { + try { + // A URL is considered absolute if we don't have a baseUrl (so cannot convert a relative URL) + // or if URI#isAbsolute returns true. + return this.baseUrl == null || new URI(url).isAbsolute(); + } catch (URISyntaxException e) { + return false; + } + } + + protected String getAbsoluteUrl(String url) { + return baseUrl + ((url.startsWith("/")) ? url : "/" + url); + } + protected List loadChannelStreamsList(String[] lines) { ExtendedM3uParser.Line streamInfoLine = null; @@ -129,7 +161,8 @@ protected List loadChannelStreamsList(String[] lines) { if (line.isData() && streamInfoLine != null) { String quality = getQualityFromM3uDirective(streamInfoLine); if (quality != null) { - streams.add(new ChannelStreamInfo(quality, line.lineData)); + String lineData = line.lineData; + streams.add(new ChannelStreamInfo(quality, isAbsoluteUrl(lineData) ? lineData : getAbsoluteUrl(lineData))); } streamInfoLine = null; diff --git a/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/twitch/TwitchStreamSegmentUrlProvider.java b/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/twitch/TwitchStreamSegmentUrlProvider.java index 8e82b998..6a37c051 100644 --- a/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/twitch/TwitchStreamSegmentUrlProvider.java +++ b/main/src/main/java/com/sedmelluq/discord/lavaplayer/source/twitch/TwitchStreamSegmentUrlProvider.java @@ -35,6 +35,7 @@ public class TwitchStreamSegmentUrlProvider extends M3uStreamSegmentUrlProvider * @param manager Twitch source manager. */ public TwitchStreamSegmentUrlProvider(String channelName, TwitchStreamAudioSourceManager manager) { + super(null); this.channelName = channelName; this.manager = manager; this.tokenExpirationTime = -1;