diff --git a/src/Jellyfin.LiveTv/TunerHosts/M3UTunerHost.cs b/src/Jellyfin.LiveTv/TunerHosts/M3UTunerHost.cs index 902ca76af8..e46249bba7 100644 --- a/src/Jellyfin.LiveTv/TunerHosts/M3UTunerHost.cs +++ b/src/Jellyfin.LiveTv/TunerHosts/M3UTunerHost.cs @@ -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); diff --git a/src/Jellyfin.LiveTv/TunerHosts/SharedHttpStream.cs b/src/Jellyfin.LiveTv/TunerHosts/SharedHttpStream.cs index 5ef04ad9e9..5bebe40ef0 100644 --- a/src/Jellyfin.LiveTv/TunerHosts/SharedHttpStream.cs +++ b/src/Jellyfin.LiveTv/TunerHosts/SharedHttpStream.cs @@ -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(TaskCreationOptions.RunContinuationsAsynchronously); diff --git a/tests/Jellyfin.LiveTv.Tests/M3UTunerHostTests.cs b/tests/Jellyfin.LiveTv.Tests/M3UTunerHostTests.cs index 4487a5ff2b..1be12fd90b 100644 --- a/tests/Jellyfin.LiveTv.Tests/M3UTunerHostTests.cs +++ b/tests/Jellyfin.LiveTv.Tests/M3UTunerHostTests.cs @@ -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(); + messageHandler.Protected() + .Setup>("SendAsync", ItExpr.IsAny(), ItExpr.IsAny()) + .Callback((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(); + http.Setup(x => x.CreateClient(It.IsAny())).Returns(client); + var config = new Mock(); + config.Setup(x => x.GetConfiguration("encoding")).Returns(new EncodingOptions { TranscodingTempPath = "/tmp" }); + config.SetupGet(x => x.CommonApplicationPaths).Returns(Mock.Of()); + var mediaSourceManager = new Mock(); + mediaSourceManager.Setup(x => x.GetPathProtocol(It.IsAny())).Returns(MediaProtocol.Http); + + var host = new TestableM3UTunerHost( + config.Object, + mediaSourceManager.Object, + Mock.Of>(), + Mock.Of(), + http.Object, + Mock.Of(), + Mock.Of(), + Mock.Of()); + + 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 GetStream(TunerHostInfo tuner, ChannelInfo channel) + => GetChannelStream(tuner, channel, "stream", new List(), CancellationToken.None); + public Task> GetMediaSources(TunerHostInfo tuner, ChannelInfo channel) => GetChannelStreamMediaSources(tuner, channel, CancellationToken.None); }