Skip to content

Commit

Permalink
feat: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSuunny committed Mar 9, 2024
1 parent 97d4949 commit 57a5f69
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
17 changes: 16 additions & 1 deletion Content.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class Content : IDisposable

#region Internal

private readonly RecyclableMemoryStreamManager _streamManager = new();
private RecyclableMemoryStreamManager _streamManager = new();

internal RecyclableMemoryStream Stream { get; set; }

Expand All @@ -44,6 +44,9 @@ internal Content(HttpResponse response)

public async Task<string> ReadAsString()
{
if (IsDisposed)
throw new ObjectDisposedException("Object disposed.");

if (_response.IsEmpytyBody)
throw new HttpResponseException("Content not found.");
else if (_response.ContentLength.HasValue && _response.ContentLength > int.MaxValue)
Expand Down Expand Up @@ -104,6 +107,9 @@ public async Task<byte[]> ReadAsBytes()

public async Task<Stream> ReadAsStream()
{
if (IsDisposed)
throw new ObjectDisposedException("Object disposed.");

if (_response.IsEmpytyBody)
throw new HttpResponseException("Content not found.");

Expand All @@ -122,6 +128,9 @@ public async Task<Stream> ReadAsStream()

public async IAsyncEnumerable<Memory<byte>> ReadAsStreamEnumerable()
{
if (IsDisposed)
throw new ObjectDisposedException("Object disposed.");

if (_response.IsEmpytyBody)
throw new HttpResponseException("Content not found.");

Expand All @@ -142,6 +151,9 @@ public async Task<string> ToFile(string filepath)

public async Task<string> ToFile(string localPath, string filename = null)
{
if (IsDisposed)
throw new ObjectDisposedException("Object disposed.");

if (_response.IsEmpytyBody)
throw new NullReferenceException("Content not found.");

Expand Down Expand Up @@ -194,6 +206,9 @@ protected virtual void Dispose(bool disposing)
Stream?.Dispose();
}

_streamManager = null;
Stream = null;

IsDisposed = true;
}
}
Expand Down
2 changes: 2 additions & 0 deletions HttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,8 @@ public async Task<HttpResponse> Raw(HttpMethod method, string url, HttpContent b
{
_isReceivedHeader = false;

_response?.Content?.Dispose();

_response = new HttpResponse(this);

_isReceivedHeader = true;
Expand Down
2 changes: 1 addition & 1 deletion Yove.Http.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<PackageId>Yove.Http</PackageId>
<VersionPrefix>1.7.7</VersionPrefix>
<VersionPrefix>1.7.8</VersionPrefix>
<VersionSuffix>beta</VersionSuffix>
<Authors>Sunny</Authors>
<NeutralLanguage>en-US, ru-RU</NeutralLanguage>
Expand Down

0 comments on commit 57a5f69

Please sign in to comment.