Skip to content

Commit

Permalink
#655: Fix for request bodies that are huuge
Browse files Browse the repository at this point in the history
  • Loading branch information
Badgerati committed Jan 8, 2021
1 parent 5620547 commit 4fcd184
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pode_modules/
ps_modules/
docs/[Ff]unctions/
examples/state.json
examples/issue-*
pkg/


Expand Down
7 changes: 4 additions & 3 deletions src/Listener/PodeContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,8 @@ public void Dispose(bool force)
SmtpRequest.Reset();
}

if (!IsKeepAlive || force)
// dispose of request if not KeepAlive, and not waiting for body
if (((IsHttp && !HttpRequest.AwaitingBody) || !IsHttp) && (!IsKeepAlive || force))
{
State = PodeContextState.Closed;
Request.Dispose();
Expand All @@ -275,8 +276,8 @@ public void Dispose(bool force)
}
catch {}

// if keep-alive, setup for re-receive
if (IsKeepAlive && !force)
// if keep-alive, or awaiting body, setup for re-receive
if (((IsHttp && HttpRequest.AwaitingBody) || IsKeepAlive) && !force)
{
StartReceive();
}
Expand Down
15 changes: 11 additions & 4 deletions src/Listener/PodeHttpRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ protected override void Parse(byte[] bytes)

// parse the body
ParseBody(bytes, reqLines, newline, bodyIndex);
AwaitingBody = (ContentLength > 0 && RawBody.Length == 0);
AwaitingBody = (ContentLength > 0 && RawBody.Length < ContentLength);
}

private int ParseHeaders(string[] reqLines, string newline)
Expand Down Expand Up @@ -211,6 +211,7 @@ private void ParseBody(byte[] bytes, string[] reqLines, string newline, int body

// get the start index for raw bytes
var start = reqLines.Take(bodyIndex).Sum(x => x.Length) + ((bodyIndex) * newline.Length);
var hasBody = (RawBody != default(byte[]));

// parse for chunked
if (isChunked)
Expand Down Expand Up @@ -248,19 +249,25 @@ private void ParseBody(byte[] bytes, string[] reqLines, string newline, int body
start = (start + c_length - 1) + newline.Length + 1;
}

RawBody = c_rawBytes.ToArray();
RawBody = hasBody
? RawBody.Concat(c_rawBytes).ToArray()
: c_rawBytes.ToArray();
}

// else use content length
else if (ContentLength > 0)
{
RawBody = bytes.Skip(start).Take(ContentLength).ToArray();
RawBody = hasBody
? RawBody.Concat(bytes.Skip(start).Take(ContentLength)).ToArray()
: bytes.Skip(start).Take(ContentLength).ToArray();
}

// else just read all
else
{
RawBody = bytes.Skip(start).ToArray();
RawBody = hasBody
? RawBody.Concat(bytes.Skip(start)).ToArray()
: bytes.Skip(start).ToArray();
}

// set the body
Expand Down

0 comments on commit 4fcd184

Please sign in to comment.