From 1e68123eb95e868ee10beb26def74a68ef9b763d Mon Sep 17 00:00:00 2001 From: lpaiu-cs Date: Thu, 20 Aug 2026 03:10:14 +0900 Subject: [PATCH] Revert "Recover the total length for formats that omit contentLength (#235)" This reverts commit f45bbb7aebfcbc1c553769e04af6cd43afa8b7c3. The probe compensated for the stream adopting a partial response's Content-Length as the length of the resource, which is fixed in lavaplayer instead. With that fix the length simply stays unknown and the existing range handling carries the stream to the end, so the extra request is no longer buying anything. --- .../youtube/track/YoutubeAudioTrack.java | 42 ++----------------- 1 file changed, 3 insertions(+), 39 deletions(-) diff --git a/common/src/main/java/dev/lavalink/youtube/track/YoutubeAudioTrack.java b/common/src/main/java/dev/lavalink/youtube/track/YoutubeAudioTrack.java index e9e403d9..5bfa17da 100644 --- a/common/src/main/java/dev/lavalink/youtube/track/YoutubeAudioTrack.java +++ b/common/src/main/java/dev/lavalink/youtube/track/YoutubeAudioTrack.java @@ -18,9 +18,6 @@ import dev.lavalink.youtube.clients.skeleton.Client; import dev.lavalink.youtube.track.format.StreamFormat; import dev.lavalink.youtube.track.format.TrackFormats; -import org.apache.http.Header; -import org.apache.http.client.methods.CloseableHttpResponse; -import org.apache.http.client.methods.HttpGet; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.slf4j.Logger; @@ -138,21 +135,11 @@ private void processWithClient(LocalAudioTrackExecutor localExecutor, boolean isLegacyFormat = query != null && query.contains("itag=18"); boolean isStream = trackInfo.isStream || (!isLegacyFormat && augmentedFormat.format.getContentLength() == CONTENT_LENGTH_UNKNOWN); - long contentLength = augmentedFormat.format.getContentLength(); - - // itag 18 carries no contentLength, and the stream requests ranges by query parameter, so the - // response is a plain 200 whose Content-Length describes the chunk. Without a real total the - // reader stops at the first range boundary. A zero-length ranged request returns Content-Range, - // which does carry it; if that fails we keep CONTENT_LENGTH_UNKNOWN and behave as before. - if (!trackInfo.isStream && contentLength == CONTENT_LENGTH_UNKNOWN) { - contentLength = probeContentLength(httpInterface, augmentedFormat.signedUrl); - } - try { if (isStream) { processStream(localExecutor, httpInterface, augmentedFormat); } else { - processStatic(localExecutor, httpInterface, augmentedFormat, streamPosition, contentLength); + processStatic(localExecutor, httpInterface, augmentedFormat, streamPosition); } } catch (StreamExpiredException e) { processWithClient(localExecutor, httpInterface, client, e.lastStreamPosition); @@ -162,12 +149,11 @@ private void processWithClient(LocalAudioTrackExecutor localExecutor, private void processStatic(LocalAudioTrackExecutor localExecutor, HttpInterface httpInterface, FormatWithUrl augmentedFormat, - long streamPosition, - long contentLength) throws Exception { + long streamPosition) throws Exception { YoutubePersistentHttpStream stream = null; try { - stream = new YoutubePersistentHttpStream(httpInterface, augmentedFormat.signedUrl, contentLength); + stream = new YoutubePersistentHttpStream(httpInterface, augmentedFormat.signedUrl, augmentedFormat.format.getContentLength()); if (streamPosition > 0) { stream.seek(streamPosition); @@ -202,28 +188,6 @@ private void processStream(LocalAudioTrackExecutor localExecutor, processDelegate(new YoutubeMpegStreamAudioTrack(trackInfo, httpInterface, augmentedFormat.signedUrl), localExecutor); } - private long probeContentLength(HttpInterface httpInterface, URI url) { - HttpGet request = new HttpGet(url); - request.setHeader("Range", "bytes=0-0"); - - try (CloseableHttpResponse response = httpInterface.execute(request)) { - Header contentRange = response.getFirstHeader("Content-Range"); - int totalIndex = contentRange != null ? contentRange.getValue().lastIndexOf('/') : -1; - - if (totalIndex != -1) { - String total = contentRange.getValue().substring(totalIndex + 1).trim(); - - if (!total.isEmpty() && !"*".equals(total)) { - return Long.parseLong(total); - } - } - } catch (Exception e) { - log.debug("Failed to probe content length for {}", url, e); - } - - return CONTENT_LENGTH_UNKNOWN; - } - @NotNull private FormatWithUrl loadBestFormatWithUrl(@NotNull HttpInterface httpInterface, @NotNull Client client) throws CannotBeLoaded, Exception {