Skip to content

Commit

Permalink
Merge pull request #4 from stifskere/dev
Browse files Browse the repository at this point in the history
Body fix
  • Loading branch information
stifskere authored Mar 30, 2024
2 parents 943a49a + 041b58c commit 43354aa
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 7 deletions.
1 change: 0 additions & 1 deletion MemwLib/Http/HttpServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,6 @@ void RunMiddlewareFromAttributes(IEnumerable<UsesMiddlewareAttribute>? middlewar
: LogType.FailedRequest,
$"{parsedRequest.Path.Route} returned {responseEntity.ResponseCode.GetCode()} {responseEntity.ResponseCode.GetName()}"));


continue;

void WriteAndClose(ResponseEntity entity)
Expand Down
15 changes: 14 additions & 1 deletion MemwLib/Http/Types/Content/BodyConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ internal BodyConverter(byte[] raw)
ContentType = IsEmpty ? "none/none" : "text/plain";
}

internal BodyConverter(IBody? body) : this(body?.ToArray() ?? Array.Empty<byte>()) {}
internal BodyConverter(IBody? body) : this(body?.ToArray() ?? Array.Empty<byte>())
{
ContentType = body?.ContentType ?? "none/none";
}

/// <summary>Reads the current BodyConverter instance as a body instance.</summary>
/// <typeparam name="TBody">The type of body to convert to.</typeparam>
Expand All @@ -53,6 +56,16 @@ public bool TryReadAs<TBody>([NotNullWhen(true)] out TBody? body) where TBody :
public override string ToString()
=> Stream.GetRaw();

/// <summary>Get the body contained in this converter as an array.</summary>
/// <returns>The converted inner body.</returns>
public byte[] ToArray()
{
byte[] result = new byte[Length];
Stream.Seek(0, SeekOrigin.Begin);
_ = Stream.Read(result, 0, (int)Length);
return result;
}

/// <inheritdoc cref="IDisposable.Dispose"/>
public void Dispose()
{
Expand Down
7 changes: 4 additions & 3 deletions MemwLib/Http/Types/Content/Common/StreamBody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ public class StreamBody : IBody, IDisposable
/// <inheritdoc cref="IBody.ContentType"/>
public string ContentType { get; }

private MemoryStream _stream;
private Stream _stream;
private bool _closeOnFinish;

/// <summary>Create a new instance of Stream body</summary>
/// <param name="stream">The stream to handle.</param>
/// <param name="contentType">The stream mime type.</param>
/// <param name="closeOnFinish">Whether to close or not on dispose this instance.</param>
public StreamBody(MemoryStream stream, string contentType, bool closeOnFinish = false)
public StreamBody(Stream stream, string contentType, bool closeOnFinish = false)
{
_stream = stream;
_closeOnFinish = closeOnFinish;
Expand All @@ -26,7 +26,7 @@ public StreamBody(MemoryStream stream, string contentType, bool closeOnFinish =
/// <summary>Create a new instance of Stream body</summary>
/// <param name="stream">The stream to handle.</param>
/// <param name="closeOnFinish">Whether to close or not on dispose this instance.</param>
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) { }

/// <inheritdoc cref="IBody.ParseImpl"/>
public static IBody ParseImpl(MemoryStream content)
Expand All @@ -35,6 +35,7 @@ public static IBody ParseImpl(MemoryStream content)
/// <inheritdoc cref="IBody.ToArray"/>
public byte[] ToArray()
{
_stream.Seek(0, SeekOrigin.Begin);
byte[] result = new byte[_stream.Length];
_ = _stream.Read(result, 0, result.Length);
return result;
Expand Down
5 changes: 4 additions & 1 deletion MemwLib/Http/Types/Entities/BaseEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ public override string ToString()
/// <summary>Builds a Byte[] from the String version of the entity prepared for streams.</summary>
/// <returns>The entity as a Byte[] prepared to be sent in a TCP stream.</returns>
public byte[] ToArray()
=> Encoding.ASCII.GetBytes(ToString());
=> [
..Encoding.ASCII.GetBytes($"{Start}\r\n{(string)Headers}\r\n"),
..Body.ToArray()
];

/// <summary>Runs the ToString() method of the specified instance.</summary>
/// <param name="instance">The instance to run the method on.</param>
Expand Down
2 changes: 1 addition & 1 deletion MemwLib/MemwLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>

<Version>3.0.0</Version>
<Version>3.0.1</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Title>MemwLib</Title>
<Authors>Memw1</Authors>
Expand Down

0 comments on commit 43354aa

Please sign in to comment.