Properly handle subrequests for redirected URLs

This commit is contained in:
Vanilagy
2026-06-16 14:53:27 +02:00
parent b4a6757739
commit 2f0c040fcd
3 changed files with 32 additions and 9 deletions
+12 -5
View File
@@ -20,7 +20,7 @@ import {
base64ToBytes,
} from '../misc';
import { readAllLines, readBytes, Reader } from '../reader';
import { CustomPathedSource, ReadableStreamSource, SourceRef, SourceRequest } from '../source';
import { CustomPathedSource, PathedSource, ReadableStreamSource, SourceRef, SourceRequest } from '../source';
import { HlsDemuxer } from './hls-demuxer';
import {
AttributeList,
@@ -68,6 +68,7 @@ export type HlsSegmentLocation = {
};
export class HlsSegmentedInput extends SegmentedInput {
rootPath: string;
demuxer: HlsDemuxer;
segments: HlsSegment[] = [];
nextLines: string[] | null = null;
@@ -84,6 +85,7 @@ export class HlsSegmentedInput extends SegmentedInput {
) {
super(demuxer.input, path, trackDeclarations);
this.rootPath = path;
this.demuxer = demuxer;
this.nextLines = lines;
}
@@ -126,12 +128,17 @@ export class HlsSegmentedInput extends SegmentedInput {
this.nextLines = null;
if (!lines) {
using ref = await this.demuxer.input._getSourceUncached({ path: this.path, isRoot: false });
using ref = await this.demuxer.input._getSourceUncached({ path: this.rootPath, isRoot: false });
const reader = new Reader(ref.source);
const slice = await reader.requestEntireFile();
assert(slice);
lines = readAllLines(slice, slice.length, { ignore: canIgnoreLine });
if (ref.source instanceof PathedSource) {
// Copy back the source's path to become aware of potential redirects
this.rootPath = ref.source.rootPath;
}
}
let headerRead = false;
@@ -219,7 +226,7 @@ export class HlsSegmentedInput extends SegmentedInput {
key = { ...key, iv };
}
const fullPath = joinPaths(this.path, line);
const fullPath = joinPaths(this.rootPath, line);
const location: HlsSegmentLocation = {
path: fullPath,
offset: nextByteRange?.offset ?? 0,
@@ -299,7 +306,7 @@ export class HlsSegmentedInput extends SegmentedInput {
}
if (!prevLastSegment) {
const fullPath = joinPaths(this.path, uri);
const fullPath = joinPaths(this.rootPath, uri);
const location: HlsSegmentLocation = {
path: fullPath,
offset: parsedByteRange?.offset ?? 0,
@@ -375,7 +382,7 @@ export class HlsSegmentedInput extends SegmentedInput {
currentKey = {
method: 'AES-128',
keyUri: joinPaths(this.path, uri),
keyUri: joinPaths(this.rootPath, uri),
iv,
keyFormat,
};