< Summary - Jellyfin

Information
Class: Jellyfin.Server.Extensions.WebHostBuilderExtensions
Assembly: jellyfin
File(s): /srv/git/jellyfin/Jellyfin.Server/Extensions/WebHostBuilderExtensions.cs
Line coverage
33%
Covered lines: 15
Uncovered lines: 30
Coverable lines: 45
Total lines: 136
Line coverage: 33.3%
Branch coverage
0%
Covered branches: 0
Total branches: 20
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
ConfigureWebHostBuilder(...)100%11100%
SetupJellyfinWebServer(...)0%420200%

File(s)

/srv/git/jellyfin/Jellyfin.Server/Extensions/WebHostBuilderExtensions.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.IO;
 4using System.Net;
 5using System.Security.Cryptography.X509Certificates;
 6using Jellyfin.Server.Helpers;
 7using MediaBrowser.Common.Configuration;
 8using MediaBrowser.Controller.Extensions;
 9using MediaBrowser.Model.Net;
 10using Microsoft.AspNetCore.Hosting;
 11using Microsoft.AspNetCore.Server.Kestrel.Core;
 12using Microsoft.Extensions.Configuration;
 13using Microsoft.Extensions.Hosting;
 14using Microsoft.Extensions.Logging;
 15
 16namespace Jellyfin.Server.Extensions;
 17
 18/// <summary>
 19/// Extensions for configuring the web host builder.
 20/// </summary>
 21public static class WebHostBuilderExtensions
 22{
 23    /// <summary>
 24    /// Configure the web host builder.
 25    /// </summary>
 26    /// <param name="builder">The builder to configure.</param>
 27    /// <param name="appHost">The application host.</param>
 28    /// <param name="startupConfig">The application configuration.</param>
 29    /// <param name="appPaths">The application paths.</param>
 30    /// <param name="logger">The logger.</param>
 31    /// <returns>The configured web host builder.</returns>
 32    public static IWebHostBuilder ConfigureWebHostBuilder(
 33        this IWebHostBuilder builder,
 34        CoreAppHost appHost,
 35        IConfiguration startupConfig,
 36        IApplicationPaths appPaths,
 37        ILogger logger)
 38    {
 2139        return builder
 2140            .UseKestrel((builderContext, options) =>
 2141            {
 2142                SetupJellyfinWebServer(
 2143                    appHost.NetManager.GetAllBindInterfaces(false),
 2144                    appHost.HttpPort,
 2145                    appHost.ListenWithHttps ? appHost.HttpsPort : null,
 2146                    appHost.Certificate,
 2147                    startupConfig,
 2148                    appPaths,
 2149                    logger,
 2150                    builderContext,
 2151                    options);
 2152            })
 2153            .UseStartup(context => new Startup(appHost, context.Configuration));
 54    }
 55
 56    /// <summary>
 57    /// Configures a Kestrel type webServer to bind to the specific arguments.
 58    /// </summary>
 59    /// <param name="addresses">The IP addresses that should be listend to.</param>
 60    /// <param name="httpPort">The http port.</param>
 61    /// <param name="httpsPort">If set the https port. If set you must also set the certificate.</param>
 62    /// <param name="certificate">The certificate used for https port.</param>
 63    /// <param name="startupConfig">The startup config.</param>
 64    /// <param name="appPaths">The app paths.</param>
 65    /// <param name="logger">A logger.</param>
 66    /// <param name="builderContext">The kestrel build pipeline context.</param>
 67    /// <param name="options">The kestrel server options.</param>
 68    /// <exception cref="InvalidOperationException">Will be thrown when a https port is set but no or an invalid certifi
 69    public static void SetupJellyfinWebServer(
 70        IReadOnlyList<IPData> addresses,
 71        int httpPort,
 72        int? httpsPort,
 73        X509Certificate2? certificate,
 74        IConfiguration startupConfig,
 75        IApplicationPaths appPaths,
 76        ILogger logger,
 77        WebHostBuilderContext builderContext,
 78        KestrelServerOptions options)
 79    {
 080        bool flagged = false;
 081        foreach (var netAdd in addresses)
 82        {
 083            var address = netAdd.Address;
 084            logger.LogInformation("Kestrel is listening on {Address}", address.Equals(IPAddress.IPv6Any) ? "all interfac
 085            options.Listen(netAdd.Address, httpPort);
 086            if (httpsPort.HasValue)
 87            {
 088                if (builderContext.HostingEnvironment.IsDevelopment())
 89                {
 90                    try
 91                    {
 092                        options.Listen(
 093                            address,
 094                            httpsPort.Value,
 095                            listenOptions => listenOptions.UseHttps());
 096                    }
 097                    catch (InvalidOperationException)
 98                    {
 099                        if (!flagged)
 100                        {
 0101                            logger.LogWarning("Failed to listen to HTTPS using the ASP.NET Core HTTPS development certif
 0102                            flagged = true;
 103                        }
 0104                    }
 105                }
 106                else
 107                {
 0108                    if (certificate is null)
 109                    {
 0110                        throw new InvalidOperationException("Cannot run jellyfin with https without setting a valid cert
 111                    }
 112
 0113                    options.Listen(
 0114                        address,
 0115                        httpsPort.Value,
 0116                        listenOptions => listenOptions.UseHttps(certificate));
 117                }
 118            }
 119        }
 120
 121        // Bind to unix socket (only on unix systems)
 0122        if (startupConfig.UseUnixSocket() && Environment.OSVersion.Platform == PlatformID.Unix)
 123        {
 0124            var socketPath = StartupHelpers.GetUnixSocketPath(startupConfig, appPaths);
 125
 126            // Workaround for https://github.com/aspnet/AspNetCore/issues/14134
 0127            if (File.Exists(socketPath))
 128            {
 0129                File.Delete(socketPath);
 130            }
 131
 0132            options.ListenUnixSocket(socketPath);
 0133            logger.LogInformation("Kestrel listening to unix socket {SocketPath}", socketPath);
 134        }
 0135    }
 136}