Speed up companion upload parsing
This commit is contained in:
+13
-9
@@ -64,16 +64,16 @@ function Read-HttpRequest {
|
|||||||
param([System.Net.Sockets.NetworkStream]$Stream)
|
param([System.Net.Sockets.NetworkStream]$Stream)
|
||||||
|
|
||||||
$buffer = New-Object byte[] 65536
|
$buffer = New-Object byte[] 65536
|
||||||
$received = New-Object System.Collections.Generic.List[byte]
|
$received = [System.IO.MemoryStream]::new()
|
||||||
$headerEnd = -1
|
$headerEnd = -1
|
||||||
|
|
||||||
while ($headerEnd -lt 0) {
|
while ($headerEnd -lt 0) {
|
||||||
$read = $Stream.Read($buffer, 0, $buffer.Length)
|
$read = $Stream.Read($buffer, 0, $buffer.Length)
|
||||||
if ($read -le 0) { break }
|
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())
|
$text = [Text.Encoding]::ASCII.GetString($received.ToArray())
|
||||||
$headerEnd = $text.IndexOf("`r`n`r`n", [StringComparison]::Ordinal)
|
$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" }
|
if ($headerEnd -lt 0) { throw "Invalid HTTP request" }
|
||||||
@@ -96,20 +96,24 @@ function Read-HttpRequest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$bodyStart = $headerEnd + 4
|
$bodyStart = $headerEnd + 4
|
||||||
$bodyBytes = New-Object System.Collections.Generic.List[byte]
|
$bodyStream = [System.IO.MemoryStream]::new()
|
||||||
for ($i = $bodyStart; $i -lt $allBytes.Length; $i++) { $bodyBytes.Add($allBytes[$i]) }
|
$initialBodyLength = $allBytes.Length - $bodyStart
|
||||||
|
if ($initialBodyLength -gt 0) {
|
||||||
|
$bodyStream.Write($allBytes, $bodyStart, $initialBodyLength)
|
||||||
|
}
|
||||||
|
|
||||||
while ($bodyBytes.Count -lt $contentLength) {
|
while ($bodyStream.Length -lt $contentLength) {
|
||||||
$read = $Stream.Read($buffer, 0, [Math]::Min($buffer.Length, $contentLength - $bodyBytes.Count))
|
$remaining = [int]([Math]::Min($buffer.Length, $contentLength - $bodyStream.Length))
|
||||||
|
$read = $Stream.Read($buffer, 0, $remaining)
|
||||||
if ($read -le 0) { break }
|
if ($read -le 0) { break }
|
||||||
for ($i = 0; $i -lt $read; $i++) { $bodyBytes.Add($buffer[$i]) }
|
$bodyStream.Write($buffer, 0, $read)
|
||||||
}
|
}
|
||||||
|
|
||||||
return [pscustomobject]@{
|
return [pscustomobject]@{
|
||||||
Method = $requestLine[0]
|
Method = $requestLine[0]
|
||||||
Path = $requestLine[1]
|
Path = $requestLine[1]
|
||||||
Headers = $headers
|
Headers = $headers
|
||||||
Body = [Text.Encoding]::UTF8.GetString($bodyBytes.ToArray())
|
Body = [Text.Encoding]::UTF8.GetString($bodyStream.ToArray())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user