From ed480a71bca2bb6a4da4b88313476e5f864a4282 Mon Sep 17 00:00:00 2001 From: Beyley Thomas Date: Thu, 16 Nov 2023 18:10:40 -0800 Subject: [PATCH 1/3] Fix printing of XML on parse failure --- Bunkum.Core/Endpoints/Middlewares/MainMiddleware.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Bunkum.Core/Endpoints/Middlewares/MainMiddleware.cs b/Bunkum.Core/Endpoints/Middlewares/MainMiddleware.cs index 1ee56ee..aab7fd9 100644 --- a/Bunkum.Core/Endpoints/Middlewares/MainMiddleware.cs +++ b/Bunkum.Core/Endpoints/Middlewares/MainMiddleware.cs @@ -292,7 +292,7 @@ private bool TryAddBodyToInvocation(EndpointAttribute attribute, Type paramType, } catch (Exception e) { - this._logger.LogError(BunkumCategory.UserContent, $"Failed to parse object data: {e}\n\nXML: {body}"); + this._logger.LogError(BunkumCategory.UserContent, $"Failed to parse object data: {e}\n\nXML: {Encoding.UTF8.GetString(body.ToArray())}"); return false; } } From 365d6e95d5ce262d17bce8ebb4929c0dc92187c8 Mon Sep 17 00:00:00 2001 From: Beyley Thomas Date: Sat, 18 Nov 2023 14:34:56 -0800 Subject: [PATCH 2/3] MainMiddleware: Refactor TryAddBodyToInvocation + fix debug JSON printing --- .../Endpoints/Middlewares/MainMiddleware.cs | 48 +++++++------------ 1 file changed, 18 insertions(+), 30 deletions(-) diff --git a/Bunkum.Core/Endpoints/Middlewares/MainMiddleware.cs b/Bunkum.Core/Endpoints/Middlewares/MainMiddleware.cs index aab7fd9..91698b2 100644 --- a/Bunkum.Core/Endpoints/Middlewares/MainMiddleware.cs +++ b/Bunkum.Core/Endpoints/Middlewares/MainMiddleware.cs @@ -270,47 +270,41 @@ private Response GenerateResponseFromEndpoint(object? val, EndpointAttribute att return null; } - private bool TryAddBodyToInvocation(EndpointAttribute attribute, Type paramType, MemoryStream body, ICollection invokeList) + private bool TryAddBodyToInvocation(EndpointAttribute attribute, Type paramType, MemoryStream bodyStream, ICollection invokeList) { - if (paramType == typeof(Stream)) invokeList.Add(body); - else if (paramType == typeof(string)) - { - TrimToToFirstNullByte(body); - invokeList.Add(Encoding.Default.GetString(body.ToArray())); - } - else if (paramType == typeof(byte[])) invokeList.Add(body.GetBuffer()); + byte[] body = bodyStream.ToArray(); + + if (paramType == typeof(Stream)) invokeList.Add(new MemoryStream(body)); + else if (paramType == typeof(string)) invokeList.Add(Encoding.UTF8.GetString(TrimToFirstNullByte(body))); + else if (paramType == typeof(byte[])) invokeList.Add(body); else if (attribute.ContentType == ContentType.Xml) { - TrimToToFirstNullByte(body); - XmlSerializer serializer = new(paramType); try { - object? obj = serializer.Deserialize(new StreamReader(body)); + object? obj = serializer.Deserialize(new StringReader(Encoding.UTF8.GetString(TrimToFirstNullByte(body)))); if (obj == null) throw new Exception(); invokeList.Add(obj); } catch (Exception e) { - this._logger.LogError(BunkumCategory.UserContent, $"Failed to parse object data: {e}\n\nXML: {Encoding.UTF8.GetString(body.ToArray())}"); + this._logger.LogError(BunkumCategory.UserContent, $"Failed to parse object data: {e}\n\nXML: {Encoding.UTF8.GetString(body)}"); return false; } } else if (attribute.ContentType == ContentType.Json) { - TrimToToFirstNullByte(body); - try { JsonSerializer serializer = new(); - using JsonReader reader = new JsonTextReader(new StreamReader(body, null, false, -1, true)); + using JsonReader reader = new JsonTextReader(new StringReader(Encoding.UTF8.GetString(TrimToFirstNullByte(body)))); object? obj = serializer.Deserialize(reader, paramType); if (obj == null) throw new Exception(); invokeList.Add(obj); } catch (Exception e) { - this._logger.LogError(BunkumCategory.UserContent, $"Failed to parse object data: {e}\n\nJSON: {body}"); + this._logger.LogError(BunkumCategory.UserContent, $"Failed to parse object data: {e}\n\nJSON: {Encoding.UTF8.GetString(body)}"); return false; } } @@ -322,27 +316,21 @@ private bool TryAddBodyToInvocation(EndpointAttribute attribute, Type paramType, return false; } - body.Seek(0, SeekOrigin.Begin); return true; } - private static void TrimToToFirstNullByte(Stream body) + private static ReadOnlySpan TrimToFirstNullByte(ReadOnlySpan arr) { - long i = 0; - body.Seek(0, SeekOrigin.Begin); - int b; - while ((b = body.ReadByte()) != -1) - { - if (b == 0) break; + //Find the first null byte + int idx = arr.IndexOf((byte)0); - i += 1; + //If theres no null byte, do not trim + if (idx == -1) + { + idx = arr.Length; } - //Only call SetLength when necessary - if(i != body.Length) - body.SetLength(i); - - body.Seek(0, SeekOrigin.Begin); + return arr[..idx]; } private IBunkumSerializer? GetSerializerOrDefault(string contentType) From 335bc077dc8ec6d06ac6c257a2687437d1bc827c Mon Sep 17 00:00:00 2001 From: Beyley Thomas Date: Sat, 18 Nov 2023 14:35:20 -0800 Subject: [PATCH 3/3] Tests: Fix accidental use of GetBuffer in stream test --- BunkumTests.HttpServer/Endpoints/BodyEndpoints.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BunkumTests.HttpServer/Endpoints/BodyEndpoints.cs b/BunkumTests.HttpServer/Endpoints/BodyEndpoints.cs index a2562f1..e51f682 100644 --- a/BunkumTests.HttpServer/Endpoints/BodyEndpoints.cs +++ b/BunkumTests.HttpServer/Endpoints/BodyEndpoints.cs @@ -45,6 +45,6 @@ public string ByteArray(RequestContext context, byte[] body) public string Stream(RequestContext context, Stream body) { MemoryStream stream = (MemoryStream)body; - return Encoding.Default.GetString(stream.GetBuffer()); + return Encoding.Default.GetString(stream.ToArray()); } } \ No newline at end of file