mirror of https://github.com/Squidex/squidex.git
9 changed files with 107 additions and 14 deletions
@ -0,0 +1,46 @@ |
|||||
|
// ==========================================================================
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
||||
|
// All rights reserved. Licensed under the MIT license.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.AspNetCore.Http; |
||||
|
using Microsoft.Extensions.Options; |
||||
|
using Squidex.Infrastructure; |
||||
|
|
||||
|
namespace Squidex.Pipeline.Robots |
||||
|
{ |
||||
|
public class RobotsTxtMiddleware : IMiddleware |
||||
|
{ |
||||
|
private readonly RobotsTxtOptions robotsTxtOptions; |
||||
|
|
||||
|
public RobotsTxtMiddleware(IOptions<RobotsTxtOptions> robotsTxtOptions) |
||||
|
{ |
||||
|
Guard.NotNull(robotsTxtOptions, nameof(robotsTxtOptions)); |
||||
|
|
||||
|
this.robotsTxtOptions = robotsTxtOptions.Value; |
||||
|
} |
||||
|
|
||||
|
public async Task InvokeAsync(HttpContext context, RequestDelegate next) |
||||
|
{ |
||||
|
if (CanServeRequest(context.Request) && !string.IsNullOrWhiteSpace(robotsTxtOptions.Text)) |
||||
|
{ |
||||
|
context.Response.ContentType = "text/plain"; |
||||
|
context.Response.StatusCode = 200; |
||||
|
|
||||
|
await context.Response.WriteAsync(robotsTxtOptions.Text); |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
await next(context); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private static bool CanServeRequest(HttpRequest request) |
||||
|
{ |
||||
|
return request.Method == "GET" && string.IsNullOrEmpty(request.Path); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,14 @@ |
|||||
|
// ==========================================================================
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
||||
|
// All rights reserved. Licensed under the MIT license.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
namespace Squidex.Pipeline.Robots |
||||
|
{ |
||||
|
public sealed class RobotsTxtOptions |
||||
|
{ |
||||
|
public string Text { get; set; } |
||||
|
} |
||||
|
} |
||||
@ -1,2 +0,0 @@ |
|||||
User-agent: * |
|
||||
Disallow: / |
|
||||
Loading…
Reference in new issue