Skip to content

Commit 22e28a7

Browse files
committed
treat max receive len as a long
1 parent 976326c commit 22e28a7

File tree

4 files changed

+9
-5
lines changed

4 files changed

+9
-5
lines changed

src/Core/Settings.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,9 @@ public string GetExternalUrl()
236236

237237
[ConfigComment("Maximum megabytes that can be sent as a single message from a client to the Swarm server.\nSet this lower to limit above, set this higher to allow very large file uploads.")]
238238
public int MaxNetworkRequestMegabytes = 200;
239+
240+
/// <summary>Converts <see cref="MaxNetworkRequestMegabytes"/> to bytes as a long.</summary>
241+
public long MaxReceiveBytes => MaxNetworkRequestMegabytes * (1024L * 1024);
239242
}
240243

241244
/// <summary>Settings related to file paths.</summary>

src/Utils/Utilities.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ public static async Task<byte[]> ReceiveData(this WebSocket socket, long maxByte
311311
{
312312
result = await socket.ReceiveAsync(buffer, limit);
313313
ms.Write(buffer, 0, result.Count);
314-
if (ms.Length > maxBytes)
314+
if (ms.Length > maxBytes || ms.Length >= int.MaxValue)
315315
{
316316
throw new IOException($"Received too much data! (over {maxBytes} bytes)");
317317
}

src/WebAPI/API.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void Error(string message)
5151
if (context.WebSockets.IsWebSocketRequest)
5252
{
5353
socket = await context.WebSockets.AcceptWebSocketAsync();
54-
input = await socket.ReceiveJson(TimeSpan.FromMinutes(1), Program.ServerSettings.Network.MaxNetworkRequestMegabytes * (1024 * 1024));
54+
input = await socket.ReceiveJson(TimeSpan.FromMinutes(1), Program.ServerSettings.Network.MaxReceiveBytes);
5555
}
5656
else if (context.Request.Method == "POST")
5757
{
@@ -61,13 +61,14 @@ void Error(string message)
6161
context.Response.Redirect("/Error/BasicAPI");
6262
return;
6363
}
64-
if (context.Request.ContentLength <= 0 || context.Request.ContentLength >= Program.ServerSettings.Network.MaxNetworkRequestMegabytes * (1024 * 1024))
64+
// Note: int32 size limit due to array allocation. Can't singular read direct more than 2 gig.
65+
if (!context.Request.ContentLength.HasValue || context.Request.ContentLength <= 0 || context.Request.ContentLength >= Program.ServerSettings.Network.MaxReceiveBytes || context.Request.ContentLength >= int.MaxValue)
6566
{
6667
Error($"Request has invalid content length: {context.Request.ContentLength}");
6768
context.Response.Redirect("/Error/BasicAPI");
6869
return;
6970
}
70-
byte[] rawData = new byte[(int)context.Request.ContentLength];
71+
byte[] rawData = new byte[(int)context.Request.ContentLength.Value];
7172
await context.Request.Body.ReadExactlyAsync(rawData, 0, rawData.Length);
7273
input = JObject.Parse(Encoding.UTF8.GetString(rawData));
7374
}

src/WebAPI/T2IAPI.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ static int guessBatchSize(JObject input)
100100
int batchOffset = images * guessBatchSize(rawInput);
101101
while (!cancelTok.IsCancellationRequested)
102102
{
103-
byte[] rec = await socket.ReceiveData(Program.ServerSettings.Network.MaxNetworkRequestMegabytes * (1024 * 1024), linked.Token);
103+
byte[] rec = await socket.ReceiveData(Program.ServerSettings.Network.MaxReceiveBytes, linked.Token);
104104
Volatile.Write(ref retain, true);
105105
if (socket.State != WebSocketState.Open || cancelTok.IsCancellationRequested || Volatile.Read(ref ended))
106106
{

0 commit comments

Comments
 (0)