Merge pull request #11495 from jellyfin/workaround-tuner-head-method-405

Workarounds TV tuners rejecting HEAD method
This commit is contained in:
Joshua M. Boniface 2024-05-05 21:47:00 -04:00 committed by GitHub
commit 5bf738dafb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -5,7 +5,9 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
@ -38,6 +40,14 @@ namespace Jellyfin.LiveTv.TunerHosts
"video/vnd.mpeg.dash.mpd"
};
private static readonly string[] _disallowedSharedStreamExtensions =
{
".mkv",
".mp4",
".m3u8",
".mpd"
};
private readonly IHttpClientFactory _httpClientFactory;
private readonly IServerApplicationHost _appHost;
private readonly INetworkManager _networkManager;
@ -106,13 +116,28 @@ namespace Jellyfin.LiveTv.TunerHosts
.SendAsync(message, cancellationToken)
.ConfigureAwait(false);
response.EnsureSuccessStatusCode();
if (response.IsSuccessStatusCode)
{
if (!_disallowedMimeTypes.Contains(response.Content.Headers.ContentType?.ToString(), StringComparison.OrdinalIgnoreCase))
{
return new SharedHttpStream(mediaSource, tunerHost, streamId, FileSystem, _httpClientFactory, Logger, Config, _appHost, _streamHelper);
}
}
else if (response.StatusCode == HttpStatusCode.MethodNotAllowed)
{
// Fallback to check path extension when the server does not support HEAD method
// Use UriBuilder to remove all query string as GetExtension will include them when used directly
var extension = Path.GetExtension(new UriBuilder(mediaSource.Path).Path);
if (!_disallowedSharedStreamExtensions.Contains(extension, StringComparison.OrdinalIgnoreCase))
{
return new SharedHttpStream(mediaSource, tunerHost, streamId, FileSystem, _httpClientFactory, Logger, Config, _appHost, _streamHelper);
}
}
else
{
response.EnsureSuccessStatusCode();
}
}
return new LiveStream(mediaSource, tunerHost, FileSystem, Logger, Config, _streamHelper);
}