| | 1 | | using System.Collections.Generic; |
| | 2 | | using Jellyfin.Api.Middleware; |
| | 3 | | using MediaBrowser.Common.Net; |
| | 4 | | using MediaBrowser.Controller.Configuration; |
| | 5 | | using Microsoft.AspNetCore.Builder; |
| | 6 | | using Microsoft.OpenApi.Models; |
| | 7 | |
|
| | 8 | | namespace Jellyfin.Server.Extensions |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// Extensions for adding API specific functionality to the application pipeline. |
| | 12 | | /// </summary> |
| | 13 | | public static class ApiApplicationBuilderExtensions |
| | 14 | | { |
| | 15 | | /// <summary> |
| | 16 | | /// Adds swagger and swagger UI to the application pipeline. |
| | 17 | | /// </summary> |
| | 18 | | /// <param name="applicationBuilder">The application builder.</param> |
| | 19 | | /// <param name="serverConfigurationManager">The server configuration.</param> |
| | 20 | | /// <returns>The updated application builder.</returns> |
| | 21 | | public static IApplicationBuilder UseJellyfinApiSwagger( |
| | 22 | | this IApplicationBuilder applicationBuilder, |
| | 23 | | IServerConfigurationManager serverConfigurationManager) |
| | 24 | | { |
| | 25 | | // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), |
| | 26 | | // specifying the Swagger JSON endpoint. |
| | 27 | |
|
| 21 | 28 | | var baseUrl = serverConfigurationManager.GetNetworkConfiguration().BaseUrl.Trim('/'); |
| 21 | 29 | | var apiDocBaseUrl = serverConfigurationManager.GetNetworkConfiguration().BaseUrl; |
| 21 | 30 | | if (!string.IsNullOrEmpty(baseUrl)) |
| | 31 | | { |
| 0 | 32 | | baseUrl += '/'; |
| | 33 | | } |
| | 34 | |
|
| 21 | 35 | | return applicationBuilder |
| 21 | 36 | | .UseSwagger(c => |
| 21 | 37 | | { |
| 21 | 38 | | // Custom path requires {documentName}, SwaggerDoc documentName is 'api-docs' |
| 21 | 39 | | c.RouteTemplate = "{documentName}/openapi.json"; |
| 21 | 40 | | c.PreSerializeFilters.Add((swagger, httpReq) => |
| 21 | 41 | | { |
| 21 | 42 | | swagger.Servers = new List<OpenApiServer> { new OpenApiServer { Url = $"{httpReq.Scheme}://{http |
| 21 | 43 | | }); |
| 21 | 44 | | }) |
| 21 | 45 | | .UseSwaggerUI(c => |
| 21 | 46 | | { |
| 21 | 47 | | c.DocumentTitle = "Jellyfin API"; |
| 21 | 48 | | c.SwaggerEndpoint($"/{baseUrl}api-docs/openapi.json", "Jellyfin API"); |
| 21 | 49 | | c.InjectStylesheet($"/{baseUrl}api-docs/swagger/custom.css"); |
| 21 | 50 | | c.RoutePrefix = "api-docs/swagger"; |
| 21 | 51 | | }) |
| 21 | 52 | | .UseReDoc(c => |
| 21 | 53 | | { |
| 21 | 54 | | c.DocumentTitle = "Jellyfin API"; |
| 21 | 55 | | c.SpecUrl($"/{baseUrl}api-docs/openapi.json"); |
| 21 | 56 | | c.InjectStylesheet($"/{baseUrl}api-docs/redoc/custom.css"); |
| 21 | 57 | | c.RoutePrefix = "api-docs/redoc"; |
| 21 | 58 | | }); |
| | 59 | | } |
| | 60 | |
|
| | 61 | | /// <summary> |
| | 62 | | /// Adds IP based access validation to the application pipeline. |
| | 63 | | /// </summary> |
| | 64 | | /// <param name="appBuilder">The application builder.</param> |
| | 65 | | /// <returns>The updated application builder.</returns> |
| | 66 | | public static IApplicationBuilder UseIPBasedAccessValidation(this IApplicationBuilder appBuilder) |
| | 67 | | { |
| 21 | 68 | | return appBuilder.UseMiddleware<IPBasedAccessValidationMiddleware>(); |
| | 69 | | } |
| | 70 | |
|
| | 71 | | /// <summary> |
| | 72 | | /// Adds LAN based access filtering to the application pipeline. |
| | 73 | | /// </summary> |
| | 74 | | /// <param name="appBuilder">The application builder.</param> |
| | 75 | | /// <returns>The updated application builder.</returns> |
| | 76 | | public static IApplicationBuilder UseLanFiltering(this IApplicationBuilder appBuilder) |
| | 77 | | { |
| 21 | 78 | | return appBuilder.UseMiddleware<LanFilteringMiddleware>(); |
| | 79 | | } |
| | 80 | |
|
| | 81 | | /// <summary> |
| | 82 | | /// Enables url decoding before binding to the application pipeline. |
| | 83 | | /// </summary> |
| | 84 | | /// <param name="appBuilder">The <see cref="IApplicationBuilder"/>.</param> |
| | 85 | | /// <returns>The updated application builder.</returns> |
| | 86 | | public static IApplicationBuilder UseQueryStringDecoding(this IApplicationBuilder appBuilder) |
| | 87 | | { |
| 21 | 88 | | return appBuilder.UseMiddleware<QueryStringDecodingMiddleware>(); |
| | 89 | | } |
| | 90 | |
|
| | 91 | | /// <summary> |
| | 92 | | /// Adds base url redirection to the application pipeline. |
| | 93 | | /// </summary> |
| | 94 | | /// <param name="appBuilder">The application builder.</param> |
| | 95 | | /// <returns>The updated application builder.</returns> |
| | 96 | | public static IApplicationBuilder UseBaseUrlRedirection(this IApplicationBuilder appBuilder) |
| | 97 | | { |
| 21 | 98 | | return appBuilder.UseMiddleware<BaseUrlRedirectionMiddleware>(); |
| | 99 | | } |
| | 100 | |
|
| | 101 | | /// <summary> |
| | 102 | | /// Adds a custom message during server startup to the application pipeline. |
| | 103 | | /// </summary> |
| | 104 | | /// <param name="appBuilder">The application builder.</param> |
| | 105 | | /// <returns>The updated application builder.</returns> |
| | 106 | | public static IApplicationBuilder UseServerStartupMessage(this IApplicationBuilder appBuilder) |
| | 107 | | { |
| 21 | 108 | | return appBuilder.UseMiddleware<ServerStartupMessageMiddleware>(); |
| | 109 | | } |
| | 110 | |
|
| | 111 | | /// <summary> |
| | 112 | | /// Adds a WebSocket request handler to the application pipeline. |
| | 113 | | /// </summary> |
| | 114 | | /// <param name="appBuilder">The application builder.</param> |
| | 115 | | /// <returns>The updated application builder.</returns> |
| | 116 | | public static IApplicationBuilder UseWebSocketHandler(this IApplicationBuilder appBuilder) |
| | 117 | | { |
| 21 | 118 | | return appBuilder.UseMiddleware<WebSocketHandlerMiddleware>(); |
| | 119 | | } |
| | 120 | |
|
| | 121 | | /// <summary> |
| | 122 | | /// Adds robots.txt redirection to the application pipeline. |
| | 123 | | /// </summary> |
| | 124 | | /// <param name="appBuilder">The application builder.</param> |
| | 125 | | /// <returns>The updated application builder.</returns> |
| | 126 | | public static IApplicationBuilder UseRobotsRedirection(this IApplicationBuilder appBuilder) |
| | 127 | | { |
| 21 | 128 | | return appBuilder.UseMiddleware<RobotsRedirectionMiddleware>(); |
| | 129 | | } |
| | 130 | |
|
| | 131 | | /// <summary> |
| | 132 | | /// Adds /emby and /mediabrowser route trimming to the application pipeline. |
| | 133 | | /// </summary> |
| | 134 | | /// <remarks> |
| | 135 | | /// This must be injected before any path related middleware. |
| | 136 | | /// </remarks> |
| | 137 | | /// <param name="appBuilder">The application builder.</param> |
| | 138 | | /// <returns>The updated application builder.</returns> |
| | 139 | | public static IApplicationBuilder UsePathTrim(this IApplicationBuilder appBuilder) |
| | 140 | | { |
| 21 | 141 | | return appBuilder.UseMiddleware<LegacyEmbyRouteRewriteMiddleware>(); |
| | 142 | | } |
| | 143 | | } |
| | 144 | | } |