diff options
| author | Claus Vium <clausvium@gmail.com> | 2020-09-03 11:32:22 +0200 |
|---|---|---|
| committer | Claus Vium <clausvium@gmail.com> | 2020-09-03 11:32:22 +0200 |
| commit | 571d0570f5560bde79d21c33173742f6a31e24cf (patch) | |
| tree | 7e88d0627175556948e25bcb80ca6c92583eb7f7 /Jellyfin.Server/Middleware/CorsOptionsResponseMiddleware.cs | |
| parent | 6ff372a550383fdc81a895b5447dee7713ffbc6f (diff) | |
Kill HttpListenerHost
Diffstat (limited to 'Jellyfin.Server/Middleware/CorsOptionsResponseMiddleware.cs')
| -rw-r--r-- | Jellyfin.Server/Middleware/CorsOptionsResponseMiddleware.cs | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/Jellyfin.Server/Middleware/CorsOptionsResponseMiddleware.cs b/Jellyfin.Server/Middleware/CorsOptionsResponseMiddleware.cs new file mode 100644 index 000000000..8214f8907 --- /dev/null +++ b/Jellyfin.Server/Middleware/CorsOptionsResponseMiddleware.cs @@ -0,0 +1,69 @@ +using System; +using System.Collections.Generic; +using System.Net.Mime; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Primitives; + +namespace Jellyfin.Server.Middleware +{ + /// <summary> + /// Middleware for handling OPTIONS requests. + /// </summary> + public class CorsOptionsResponseMiddleware + { + private readonly RequestDelegate _next; + + /// <summary> + /// Initializes a new instance of the <see cref="CorsOptionsResponseMiddleware"/> class. + /// </summary> + /// <param name="next">The next delegate in the pipeline.</param> + public CorsOptionsResponseMiddleware(RequestDelegate next) + { + _next = next; + } + + /// <summary> + /// Executes the middleware action. + /// </summary> + /// <param name="httpContext">The current HTTP context.</param> + /// <returns>The async task.</returns> + public async Task Invoke(HttpContext httpContext) + { + if (string.Equals(httpContext.Request.Method, HttpMethods.Options, StringComparison.OrdinalIgnoreCase)) + { + httpContext.Response.StatusCode = 200; + foreach (var (key, value) in GetDefaultCorsHeaders(httpContext)) + { + httpContext.Response.Headers.Add(key, value); + } + + httpContext.Response.ContentType = MediaTypeNames.Text.Plain; + await httpContext.Response.WriteAsync(string.Empty, httpContext.RequestAborted).ConfigureAwait(false); + return; + } + + await _next(httpContext).ConfigureAwait(false); + } + + private static IDictionary<string, string> GetDefaultCorsHeaders(HttpContext httpContext) + { + var origin = httpContext.Request.Headers["Origin"]; + if (origin == StringValues.Empty) + { + origin = httpContext.Request.Headers["Host"]; + if (origin == StringValues.Empty) + { + origin = "*"; + } + } + + var headers = new Dictionary<string, string>(); + headers.Add("Access-Control-Allow-Origin", origin); + headers.Add("Access-Control-Allow-Credentials", "true"); + headers.Add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, PATCH, OPTIONS"); + headers.Add("Access-Control-Allow-Headers", "Content-Type, Authorization, Range, X-MediaBrowser-Token, X-Emby-Authorization, Cookie"); + return headers; + } + } +} |
