From dc5cc89ae6001d5543dd5f49304a4930a8f892cf Mon Sep 17 00:00:00 2001 From: ScrubN <72096833+ScrubN@users.noreply.github.com> Date: Sat, 20 Jan 2024 22:09:37 -0500 Subject: [PATCH] Add overload to copy curl response to stream --- TwitchDownloaderCore/Tools/CurlImpersonate.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/TwitchDownloaderCore/Tools/CurlImpersonate.cs b/TwitchDownloaderCore/Tools/CurlImpersonate.cs index 026539b7..9b06f600 100644 --- a/TwitchDownloaderCore/Tools/CurlImpersonate.cs +++ b/TwitchDownloaderCore/Tools/CurlImpersonate.cs @@ -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(); @@ -29,7 +36,6 @@ 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; @@ -37,14 +43,13 @@ public static byte[] GetCurlResponseBytes(string url) 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 {