Skip to content

Commit

Permalink
Add overload to copy curl response to stream
Browse files Browse the repository at this point in the history
  • Loading branch information
ScrubN committed Jan 21, 2024
1 parent 027e5ac commit dc5cc89
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions TwitchDownloaderCore/Tools/CurlImpersonate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ public static string GetCurlResponse(string url)
}

public static byte[] GetCurlResponseBytes(string url)
{
using var ms = new MemoryStream();
GetCurlResponse(url, ms);
return ms.ToArray();
}

public static void GetCurlResponse(string url, Stream destination)
{
var easy = CurlNative.Easy.Init();

Expand All @@ -29,22 +36,20 @@ public static byte[] GetCurlResponseBytes(string url)
CurlNative.Easy.SetOpt(easy, CURLoption.CAINFO, "curl-ca-bundle.crt");
CurlNative.Easy.SetOpt(easy, CURLoption.TIMEOUT_MS, 30000);

var stream = new MemoryStream();
CurlNative.Easy.SetOpt(easy, CURLoption.WRITEFUNCTION, (data, size, nmemb, user) =>
{
var length = (int)size * (int)nmemb;

unsafe
{
using var ums = new UnmanagedMemoryStream((byte*)data, length);
ums.CopyTo(stream);
ums.CopyTo(destination);
}

return (UIntPtr)length;
});

var result = CurlNative.Easy.Perform(easy);
return stream.ToArray();
}
finally
{
Expand Down

0 comments on commit dc5cc89

Please sign in to comment.