Skip to content

Commit

Permalink
Update 6.4.7
Browse files Browse the repository at this point in the history
- Fixed issue with Http Client streaming buffer
  • Loading branch information
PatrickRitchie committed Aug 21, 2024
1 parent e72d8a5 commit bf4c042
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 17 deletions.
4 changes: 2 additions & 2 deletions build/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Reflection;

[assembly: AssemblyVersion("6.4.6")]
[assembly: AssemblyFileVersion("6.4.6")]
[assembly: AssemblyVersion("6.4.7")]
[assembly: AssemblyFileVersion("6.4.7")]
[assembly: AssemblyCompany("TrakHound Inc.")]
[assembly: AssemblyCopyright("Copyright (c) 2024 TrakHound Inc., All Rights Reserved.")]
35 changes: 35 additions & 0 deletions libraries/MTConnect.NET-Common/Extensions/ObjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,41 @@ public static byte[] TrimBytes(byte[] inputBytes, byte[] trimBytes)
return null;
}

public static int TrimStartBytes(ref byte[] inputBytes, byte[] trimBytes)
{
if (inputBytes != null && inputBytes.Length > 0 && trimBytes != null && trimBytes.Length > 0)
{
// Look for Trim bytes
int i = 0;
bool found;

while (i < inputBytes.Length)
{
found = false;
for (var k = 0; k < trimBytes.Length; k++)
{
if (inputBytes[i] == trimBytes[k])
{
found = true;
break;
}
}
if (!found) break;
i++;
}

if (i > 0)
{
// Shift Array over past the initial Whitespace bytes
Array.Copy(inputBytes, i, inputBytes, 0, inputBytes.Length - i);
}

return i; // Return number of bytes shifted in array
}

return 0;
}

public static byte[] TrimStartBytes(byte[] inputBytes, byte[] trimBytes)
{
if (inputBytes != null && inputBytes.Length > 0 && trimBytes != null && trimBytes.Length > 0)
Expand Down
32 changes: 17 additions & 15 deletions libraries/MTConnect.NET-HTTP/Clients/MTConnectHttpClientStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class MTConnectHttpClientStream : IDisposable
private const byte LineFeed = 10;
private const byte CarriageReturn = 13;
private const byte Dash = 45;
private static readonly byte[] _trimBytes = new byte[] { 10, 13 };
private static readonly byte[] _trimBytes = new byte[] { LineFeed, CarriageReturn };
private readonly HttpClient _httpClient;

private CancellationTokenSource _stop;
Expand Down Expand Up @@ -310,31 +310,33 @@ private static Stream ReadBody(Stream stream, int length)
{
if (stream != null && length > 0)
{
var i = 0;
var size = length;
var isHeader = true;
int i = 0;
int size = length;
bool isHeader = true;
int j;
int k;

// Create a buffer to contain body of the response
// based on the size of the content-length received in the Http Headers
var body = new MemoryStream(size);
var body = new MemoryStream();

// Create a 255 byte buffer
var chunk = new byte[255];

while (i < size)
{
// Create a 512 byte buffer
var chunk = new byte[512];

// Read from the Network stream and store in the chunk buffer
var j = stream.Read(chunk, 0, chunk.Length);
j = stream.Read(chunk, 0, chunk.Length);

// Remove blank lines before header (can cause XML deserialization error if Xml Declaration is not the first line)
if (isHeader) chunk = ObjectExtensions.TrimStartBytes(chunk, _trimBytes);

// Verify bytes read doesn't exceed destination array
// (could be blank lines after document that gets read)
if (j > size - i) j = size - i;
if (isHeader)
{
k = ObjectExtensions.TrimStartBytes(ref chunk, _trimBytes);
j -= k;
}

// Add the chunk bytes to the body buffer
body.Write(chunk, 0, Math.Min(j, chunk.Length));
body.Write(chunk, 0, j);

// Increment the index of the body buffer based on the number of bytes read in this chunk
i += j;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,13 @@ public FormatReadResult<IDevicesResponseDocument> CreateDevicesResponseDocument(
catch (Exception ex)
{
messages.Add(ex.Message);

var innerException = ex.InnerException;
while (innerException != null)
{
messages.Add(innerException.Message);
innerException = innerException.InnerException;
}
}

return new FormatReadResult<IDevicesResponseDocument>(document, success, messages, warnings, errors);
Expand Down Expand Up @@ -310,6 +317,13 @@ public FormatReadResult<IStreamsResponseDocument> CreateStreamsResponseDocument(
catch (Exception ex)
{
messages.Add(ex.Message);

var innerException = ex.InnerException;
while (innerException != null)
{
messages.Add(innerException.Message);
innerException = innerException.InnerException;
}
}

return new FormatReadResult<IStreamsResponseDocument>(document, success, messages, warnings, errors);
Expand Down

0 comments on commit bf4c042

Please sign in to comment.