Skip to content

Commit

Permalink
Added handling for delayed body data. (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
fschober-atg authored Jul 17, 2024
1 parent 23b8598 commit 149ff85
Showing 1 changed file with 68 additions and 10 deletions.
78 changes: 68 additions & 10 deletions XMAT/Engines/WebServiceProxy/Logic/Proxy/WebServiceProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,39 @@ private ClientRequest ParseRequestAndHeaders(int clientId, List<string> lines)
return clientRequest;
}

private int PrefetchContentLengthFromHeaders(int clientId, List<string> lines)
{
// Return 0 if not found or malformed
int contentLength = 0;

if (lines == null || lines.Count == 0)
{
_logger.Log(clientId, LogLevel.ERROR, "Connect request has no data.");
return contentLength;
}

// Isolate the content-length header only
for (int i = 1; i < lines.Count; i++)
{
string line = lines[i];
string[] header = line.Split(':', 2, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
if (header.Length < 2)
{
_logger.Log(clientId, LogLevel.ERROR, $"Malformed header: {line}");
}
else
{
if (header[0].Trim().ToLower() == "content-length")
{
contentLength = int.Parse(header[1].Trim());
_logger.Log(clientId, LogLevel.DEBUG, $"Prefetch of Content-Length: {contentLength}");
}
}
}

return contentLength;
}

private async Task<bool> ReturnResponseAsync(ClientRequest clientRequest, ClientState clientState, HttpResponseMessage responseMessage)
{
ServerResponse serverResponse = await ParseServerResponseAsync(clientState.ID, responseMessage).ConfigureAwait(false);
Expand Down Expand Up @@ -595,8 +628,6 @@ private async Task<Tuple<List<string>, byte[]>> ReadRequestAndHeadersAsync(Clien
{
do
{


readThisFrame = await stream.ReadAsync(buffer, readTotal, buffer.Length - readTotal);
readTotal += readThisFrame;

Expand All @@ -611,9 +642,9 @@ private async Task<Tuple<List<string>, byte[]>> ReadRequestAndHeadersAsync(Clien
}
while (clientState.TcpClient.Available > 0);
}
catch
catch (Exception ex)
{

_logger.Log(clientState.ID, LogLevel.ERROR, $"Failed reading client request: {ex}");
}

//File.WriteAllBytes($"XMAT_{clientState.ID}.dat", buffer);
Expand Down Expand Up @@ -654,13 +685,38 @@ private async Task<Tuple<List<string>, byte[]>> ReadRequestAndHeadersAsync(Clien
}
}

// Get the actual body length from the content-length header
int expectedBodyLength = 0;
expectedBodyLength = PrefetchContentLengthFromHeaders(clientState.ID, lines);

// Read the rest of the delayed body if needed
if (expectedBodyLength > readTotal - startIndex)
{
try
{
do
{
_logger.Log(clientState.ID, LogLevel.DEBUG, $"Delayed body bytes, reading {expectedBodyLength - (readTotal - startIndex)} at {readTotal - startIndex}");

readThisFrame = await stream.ReadAsync(buffer, expectedBodyLength - (readTotal - startIndex), buffer.Length - readTotal);
readTotal += readThisFrame;

// Do we need to resize?
if (readTotal >= buffer.Length)
{
_logger.Log(clientState.ID, LogLevel.DEBUG, $"Resizing buffer from {buffer.Length} bytes, to {buffer.Length * 2} bytes");
Array.Resize<byte>(ref buffer, buffer.Length * 2);
}
}
while (expectedBodyLength > readTotal - startIndex);
}
catch (Exception ex)
{
_logger.Log(clientState.ID, LogLevel.ERROR, $"Failed reading client request for delayed body: {ex}");
}
}

//string strHeadersTotal = strAllData.Substring(0, startIndex);
//string strBody = strAllData.Substring(startIndex);
//_logger.Log(clientState.ID, LogLevel.DEBUG, $"Whole buffer was {strAllData}");
//_logger.Log(clientState.ID, LogLevel.DEBUG, $"Headers buffer was {strHeadersTotal}");
//_logger.Log(clientState.ID, LogLevel.DEBUG, $"Body buffer was {strBody}");
//_logger.Log(clientState.ID, LogLevel.DEBUG, $"Done with read, read {readTotal} bytes total, and processed {startIndex} as headers, body remainder is {readTotal-startIndex}");
_logger.Log(clientState.ID, LogLevel.DEBUG, $"Done with reading, read {readTotal} bytes total, and processed {startIndex} as headers, {readTotal-startIndex} as body");

// resize the buffer, otherwise we're taking up unnecessary memory
Array.Resize<byte>(ref buffer, readTotal);
Expand Down Expand Up @@ -694,6 +750,8 @@ private void CloseClientState(ClientState clientState)
Timestamp = DateTime.Now
}
);

_logger.Log(clientState.ID, LogLevel.DEBUG, $"ConnectionClosed Event: {clientState.ID} | {DateTime.Now.ToString()}");
}

private bool RaiseReceivedInitialConnection(int connectionID, TcpClient client)
Expand Down

0 comments on commit 149ff85

Please sign in to comment.