Skip to content

Commit

Permalink
Set the maximum request body size to 200MB (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
danjov authored Mar 26, 2024
2 parents 79e9531 + 6b12934 commit 8713c38
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Geopilot.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
using Geopilot.Api.Validation.Interlis;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc.Authorization;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Models;
Expand Down Expand Up @@ -168,6 +170,11 @@
.AddHealthChecks()
.AddCheck<DbHealthCheck>("Db");

// Set the maximum request body size to 200MB
const int MaxRequestBodySize = 209715200;
builder.Services.Configure<FormOptions>(options => options.MultipartBodyLengthLimit = MaxRequestBodySize);
builder.Services.Configure<KestrelServerOptions>(options => options.Limits.MaxRequestBodySize = MaxRequestBodySize);

var app = builder.Build();

// Migrate db changes on startup
Expand Down Expand Up @@ -218,6 +225,19 @@
}
});

// By default Kestrel responds with a HTTP 400 if payload is too large.
app.Use(async (context, next) =>
{
if (context.Request.ContentLength > MaxRequestBodySize)
{
context.Response.StatusCode = StatusCodes.Status413PayloadTooLarge;
await context.Response.WriteAsync("Payload Too Large");
return;
}

await next.Invoke();
});

app.UseAuthorization();

app.MapControllers();
Expand Down

0 comments on commit 8713c38

Please sign in to comment.