mirror of https://github.com/Squidex/squidex.git
7 changed files with 130 additions and 45 deletions
@ -0,0 +1,30 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using Microsoft.AspNetCore.Http; |
|||
|
|||
namespace Squidex.Areas.Frontend.Middlewares |
|||
{ |
|||
public static class IndexExtensions |
|||
{ |
|||
public static bool IsIndexHtml(this HttpContext context) |
|||
{ |
|||
return context.IsIndex() && context.IsHtml(); |
|||
} |
|||
|
|||
public static bool IsIndex(this HttpContext context) |
|||
{ |
|||
return context.Request.Path.Value.EndsWith("/index.html", StringComparison.OrdinalIgnoreCase); |
|||
} |
|||
|
|||
public static bool IsHtml(this HttpContext context) |
|||
{ |
|||
return context.Response.ContentType?.ToLower().Contains("text/html") == true; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,66 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System.IO; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Http; |
|||
|
|||
namespace Squidex.Areas.Frontend.Middlewares |
|||
{ |
|||
public sealed class IndexMiddleware |
|||
{ |
|||
private readonly RequestDelegate next; |
|||
|
|||
public IndexMiddleware(RequestDelegate next) |
|||
{ |
|||
this.next = next; |
|||
} |
|||
|
|||
public async Task Invoke(HttpContext context) |
|||
{ |
|||
var basePath = context.Request.PathBase; |
|||
|
|||
if (context.IsIndex() && basePath.HasValue) |
|||
{ |
|||
var responseBuffer = new MemoryStream(); |
|||
var responseBody = context.Response.Body; |
|||
|
|||
context.Response.Body = responseBuffer; |
|||
|
|||
await next(context); |
|||
|
|||
context.Response.Body = responseBody; |
|||
|
|||
if (context.Response.StatusCode == 200 && context.IsIndexHtml()) |
|||
{ |
|||
var response = Encoding.UTF8.GetString(responseBuffer.ToArray()); |
|||
|
|||
response = AdjustBase(response, basePath); |
|||
|
|||
context.Response.ContentLength = Encoding.UTF8.GetByteCount(response); |
|||
context.Response.Body = responseBody; |
|||
|
|||
await context.Response.WriteAsync(response); |
|||
} |
|||
else if (context.Response.StatusCode != 304) |
|||
{ |
|||
await responseBuffer.CopyToAsync(responseBody); |
|||
} |
|||
} |
|||
else |
|||
{ |
|||
await next(context); |
|||
} |
|||
} |
|||
|
|||
private static string AdjustBase(string response, string baseUrl) |
|||
{ |
|||
return response.Replace("<base href=\"/\">", $"<base href=\"{baseUrl}/\">"); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue