diff --git a/suno-companion.ps1 b/suno-companion.ps1 index bee07eb..a40de64 100644 --- a/suno-companion.ps1 +++ b/suno-companion.ps1 @@ -64,16 +64,16 @@ function Read-HttpRequest { param([System.Net.Sockets.NetworkStream]$Stream) $buffer = New-Object byte[] 65536 - $received = New-Object System.Collections.Generic.List[byte] + $received = [System.IO.MemoryStream]::new() $headerEnd = -1 while ($headerEnd -lt 0) { $read = $Stream.Read($buffer, 0, $buffer.Length) if ($read -le 0) { break } - for ($i = 0; $i -lt $read; $i++) { $received.Add($buffer[$i]) } + $received.Write($buffer, 0, $read) $text = [Text.Encoding]::ASCII.GetString($received.ToArray()) $headerEnd = $text.IndexOf("`r`n`r`n", [StringComparison]::Ordinal) - if ($received.Count -gt 1048576) { throw "HTTP header too large" } + if ($received.Length -gt 1048576) { throw "HTTP header too large" } } if ($headerEnd -lt 0) { throw "Invalid HTTP request" } @@ -96,20 +96,24 @@ function Read-HttpRequest { } $bodyStart = $headerEnd + 4 - $bodyBytes = New-Object System.Collections.Generic.List[byte] - for ($i = $bodyStart; $i -lt $allBytes.Length; $i++) { $bodyBytes.Add($allBytes[$i]) } + $bodyStream = [System.IO.MemoryStream]::new() + $initialBodyLength = $allBytes.Length - $bodyStart + if ($initialBodyLength -gt 0) { + $bodyStream.Write($allBytes, $bodyStart, $initialBodyLength) + } - while ($bodyBytes.Count -lt $contentLength) { - $read = $Stream.Read($buffer, 0, [Math]::Min($buffer.Length, $contentLength - $bodyBytes.Count)) + while ($bodyStream.Length -lt $contentLength) { + $remaining = [int]([Math]::Min($buffer.Length, $contentLength - $bodyStream.Length)) + $read = $Stream.Read($buffer, 0, $remaining) if ($read -le 0) { break } - for ($i = 0; $i -lt $read; $i++) { $bodyBytes.Add($buffer[$i]) } + $bodyStream.Write($buffer, 0, $read) } return [pscustomobject]@{ Method = $requestLine[0] Path = $requestLine[1] Headers = $headers - Body = [Text.Encoding]::UTF8.GetString($bodyBytes.ToArray()) + Body = [Text.Encoding]::UTF8.GetString($bodyStream.ToArray()) } }