diff --git a/MemwLib/Http/HttpServer.cs b/MemwLib/Http/HttpServer.cs index a35f3bc..00b4128 100644 --- a/MemwLib/Http/HttpServer.cs +++ b/MemwLib/Http/HttpServer.cs @@ -285,7 +285,6 @@ void RunMiddlewareFromAttributes(IEnumerable? middlewar : LogType.FailedRequest, $"{parsedRequest.Path.Route} returned {responseEntity.ResponseCode.GetCode()} {responseEntity.ResponseCode.GetName()}")); - continue; void WriteAndClose(ResponseEntity entity) diff --git a/MemwLib/Http/Types/Content/BodyConverter.cs b/MemwLib/Http/Types/Content/BodyConverter.cs index e289e9b..388a666 100644 --- a/MemwLib/Http/Types/Content/BodyConverter.cs +++ b/MemwLib/Http/Types/Content/BodyConverter.cs @@ -31,7 +31,10 @@ internal BodyConverter(byte[] raw) ContentType = IsEmpty ? "none/none" : "text/plain"; } - internal BodyConverter(IBody? body) : this(body?.ToArray() ?? Array.Empty()) {} + internal BodyConverter(IBody? body) : this(body?.ToArray() ?? Array.Empty()) + { + ContentType = body?.ContentType ?? "none/none"; + } /// Reads the current BodyConverter instance as a body instance. /// The type of body to convert to. @@ -53,6 +56,16 @@ public bool TryReadAs([NotNullWhen(true)] out TBody? body) where TBody : public override string ToString() => Stream.GetRaw(); + /// Get the body contained in this converter as an array. + /// The converted inner body. + public byte[] ToArray() + { + byte[] result = new byte[Length]; + Stream.Seek(0, SeekOrigin.Begin); + _ = Stream.Read(result, 0, (int)Length); + return result; + } + /// public void Dispose() { diff --git a/MemwLib/Http/Types/Content/Common/StreamBody.cs b/MemwLib/Http/Types/Content/Common/StreamBody.cs index 9a59f8b..2ecf66b 100644 --- a/MemwLib/Http/Types/Content/Common/StreamBody.cs +++ b/MemwLib/Http/Types/Content/Common/StreamBody.cs @@ -9,14 +9,14 @@ public class StreamBody : IBody, IDisposable /// public string ContentType { get; } - private MemoryStream _stream; + private Stream _stream; private bool _closeOnFinish; /// Create a new instance of Stream body /// The stream to handle. /// The stream mime type. /// Whether to close or not on dispose this instance. - public StreamBody(MemoryStream stream, string contentType, bool closeOnFinish = false) + public StreamBody(Stream stream, string contentType, bool closeOnFinish = false) { _stream = stream; _closeOnFinish = closeOnFinish; @@ -26,7 +26,7 @@ public StreamBody(MemoryStream stream, string contentType, bool closeOnFinish = /// Create a new instance of Stream body /// The stream to handle. /// Whether to close or not on dispose this instance. - public StreamBody(MemoryStream stream, bool closeOnFinish = false) : this(stream, "application/octet-stream", closeOnFinish) { } + public StreamBody(Stream stream, bool closeOnFinish = false) : this(stream, "application/octet-stream", closeOnFinish) { } /// public static IBody ParseImpl(MemoryStream content) @@ -35,6 +35,7 @@ public static IBody ParseImpl(MemoryStream content) /// public byte[] ToArray() { + _stream.Seek(0, SeekOrigin.Begin); byte[] result = new byte[_stream.Length]; _ = _stream.Read(result, 0, result.Length); return result; diff --git a/MemwLib/Http/Types/Entities/BaseEntity.cs b/MemwLib/Http/Types/Entities/BaseEntity.cs index 3e67432..816ca6d 100644 --- a/MemwLib/Http/Types/Entities/BaseEntity.cs +++ b/MemwLib/Http/Types/Entities/BaseEntity.cs @@ -34,7 +34,10 @@ public override string ToString() /// Builds a Byte[] from the String version of the entity prepared for streams. /// The entity as a Byte[] prepared to be sent in a TCP stream. public byte[] ToArray() - => Encoding.ASCII.GetBytes(ToString()); + => [ + ..Encoding.ASCII.GetBytes($"{Start}\r\n{(string)Headers}\r\n"), + ..Body.ToArray() + ]; /// Runs the ToString() method of the specified instance. /// The instance to run the method on. diff --git a/MemwLib/MemwLib.csproj b/MemwLib/MemwLib.csproj index 16f6958..b1d19fe 100644 --- a/MemwLib/MemwLib.csproj +++ b/MemwLib/MemwLib.csproj @@ -6,7 +6,7 @@ true true - 3.0.0 + 3.0.1 true MemwLib Memw1