This commit is contained in:
isolson 2026-11-20 07:15:55 -05:00 • committed by GitHub
commit ef36ee95d9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 55 additions and 1 deletions

View file

@ -104,6 +104,11 @@ namespace Jellyfin.LiveTv.TunerHosts
try
{
using var message = new HttpRequestMessage(HttpMethod.Head, mediaSource.Path);
if (mediaSource.RequiredHttpHeaders.TryGetValue(HeaderNames.UserAgent, out var userAgent))
{
message.Headers.TryAddWithoutValidation(HeaderNames.UserAgent, userAgent);
}
using var response = await _httpClientFactory.CreateClient(NamedClient.Default)
.SendAsync(message, cancellationToken)
.ConfigureAwait(false);

View file

@ -16,6 +16,7 @@ using MediaBrowser.Model.IO;
using MediaBrowser.Model.LiveTv;
using MediaBrowser.Model.MediaInfo;
using Microsoft.Extensions.Logging;
using Microsoft.Net.Http.Headers;
namespace Jellyfin.LiveTv.TunerHosts
{
@ -54,9 +55,15 @@ namespace Jellyfin.LiveTv.TunerHosts
var typeName = GetType().Name;
Logger.LogInformation("Opening {StreamType} Live stream from {Url}", typeName, url);
using var requestMessage = new HttpRequestMessage(HttpMethod.Get, url);
if (mediaSource.RequiredHttpHeaders.TryGetValue(HeaderNames.UserAgent, out var userAgent))
{
requestMessage.Headers.TryAddWithoutValidation(HeaderNames.UserAgent, userAgent);
}
// Response stream is disposed manually.
var response = await _httpClientFactory.CreateClient(NamedClient.Default)
.GetAsync(url, HttpCompletionOption.ResponseHeadersRead, CancellationToken.None)
.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead, CancellationToken.None)
.ConfigureAwait(false);
var taskCompletionSource = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);

View file

@ -1,19 +1,23 @@
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.LiveTv.TunerHosts;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.LiveTv;
using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.LiveTv;
using MediaBrowser.Model.MediaInfo;
using Microsoft.Extensions.Logging;
using Moq;
using Moq.Protected;
using Xunit;
namespace Jellyfin.LiveTv.Tests
@ -50,6 +54,41 @@ namespace Jellyfin.LiveTv.Tests
Assert.Equal(expectDirectPlay, sources[0].SupportsDirectPlay);
}
[Fact]
public async Task GetChannelStream_HeadProbe_UsesTunerUserAgent()
{
HttpRequestMessage? sent = null;
var messageHandler = new Mock<HttpMessageHandler>();
messageHandler.Protected()
.Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
.Callback<HttpRequestMessage, CancellationToken>((m, _) => sent = m)
.ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK));
using var client = new HttpClient(messageHandler.Object);
client.DefaultRequestHeaders.UserAgent.ParseAdd("Jellyfin-Server/1.0");
var http = new Mock<IHttpClientFactory>();
http.Setup(x => x.CreateClient(It.IsAny<string>())).Returns(client);
var config = new Mock<IServerConfigurationManager>();
config.Setup(x => x.GetConfiguration("encoding")).Returns(new EncodingOptions { TranscodingTempPath = "/tmp" });
config.SetupGet(x => x.CommonApplicationPaths).Returns(Mock.Of<IApplicationPaths>());
var mediaSourceManager = new Mock<IMediaSourceManager>();
mediaSourceManager.Setup(x => x.GetPathProtocol(It.IsAny<string>())).Returns(MediaProtocol.Http);
var host = new TestableM3UTunerHost(
config.Object,
mediaSourceManager.Object,
Mock.Of<ILogger<M3UTunerHost>>(),
Mock.Of<IFileSystem>(),
http.Object,
Mock.Of<IServerApplicationHost>(),
Mock.Of<INetworkManager>(),
Mock.Of<IStreamHelper>());
await host.GetStream(new TunerHostInfo { UserAgent = "Tuner/1.0" }, new ChannelInfo { Path = "http://example.com/live/1234" });
Assert.Equal(HttpMethod.Head, sent?.Method);
Assert.Equal("Tuner/1.0", sent?.Headers.UserAgent.ToString());
}
private sealed class TestableM3UTunerHost : M3UTunerHost
{
public TestableM3UTunerHost(
@ -65,6 +104,9 @@ namespace Jellyfin.LiveTv.Tests
{
}
public Task<ILiveStream> GetStream(TunerHostInfo tuner, ChannelInfo channel)
=> GetChannelStream(tuner, channel, "stream", new List<ILiveStream>(), CancellationToken.None);
public Task<List<MediaSourceInfo>> GetMediaSources(TunerHostInfo tuner, ChannelInfo channel)
=> GetChannelStreamMediaSources(tuner, channel, CancellationToken.None);
}