Skip to content

Commit 591020e

Browse files
StartAutomatingStartAutomating
StartAutomating
authored and
StartAutomating
committed
feat: HttpListenerRequest.get_File(s) ( Fixes #1115 )
1 parent f23352f commit 591020e

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

PipeScript.types.ps1xml

+61
Original file line numberDiff line numberDiff line change
@@ -7777,6 +7777,10 @@ $this.'.Topics'
77777777
<Type>
77787778
<Name>System.Net.HttpListenerRequest</Name>
77797779
<Members>
7780+
<AliasProperty>
7781+
<Name>Files</Name>
7782+
<ReferencedMemberName>File</ReferencedMemberName>
7783+
</AliasProperty>
77807784
<AliasProperty>
77817785
<Name>Form</Name>
77827786
<ReferencedMemberName>FormData</ReferencedMemberName>
@@ -7881,6 +7885,63 @@ if ($this.ContentType -match 'multipart.+?;') {
78817885

78827886
</GetScriptBlock>
78837887
</ScriptProperty>
7888+
<ScriptProperty>
7889+
<Name>File</Name>
7890+
<GetScriptBlock>
7891+
$boundary = $this.Boundary
7892+
if (-not $boundary) { return }
7893+
if ($this.'.CachedFiles') {
7894+
return $this.'.CachedFiles'
7895+
}
7896+
if (-not $this.'.CachedRequestBody') {
7897+
$body = $this.Body
7898+
$rawData = $this.'.CachedRequestBody'
7899+
}
7900+
if (-not $rawData) { return}
7901+
7902+
$uploadedFiles = [Ordered]@{}
7903+
7904+
$boundaries = [Regex]::Matches($rawData, $boundary)
7905+
for ($i = 0; $i -lt ($boundaries.Count - 1); $i++) {
7906+
$startBoundary, $endBoundary = $boundaries[$i], $boundaries[$i + 1]
7907+
$realStart = $startBoundary.Index + $startBoundary.Length
7908+
$realEnd= $endBoundary.Index - 2
7909+
$boundryContent = $rawData.Substring($realStart, $realEnd - $realStart)
7910+
$s = $boundryContent.IndexOf("`r`n`r`n")
7911+
if ($s -ne -1) {
7912+
$preamble = $boundryContent.Substring(0, $s)
7913+
$s+=4
7914+
$name =
7915+
foreach ($_ in $preamble -split ';') {
7916+
$_ = $_.Trim()
7917+
if ($_.StartsWith('name=')) {
7918+
$_.Substring(5).Trim('"').Trim("'")
7919+
break
7920+
}
7921+
}
7922+
7923+
$null= $ms.Seek($realStart + $s, 0)
7924+
$buffer = [byte[]]::new($realEnd - ($realStart + $s))
7925+
$bytesRead = $ms.Read($buffer,0, $buffer.Length)
7926+
if ($preamble.IndexOf('Content-Type', [StringComparison]::OrdinalIgnoreCase) -eq -1) {
7927+
$ms2 = [io.memorystream]::new($buffer)
7928+
$sr2 = [io.StreamReader]::new($ms2, $this.ContentEncoding)
7929+
$uploadedFiles[$name] = $sr2.ReadToEnd().TrimEnd()
7930+
$sr2.Dispose()
7931+
$ms2.Dispose()
7932+
} else {
7933+
$uploadedFiles[$name] = $buffer
7934+
}
7935+
}
7936+
}
7937+
7938+
$this.psobject.properties.add((
7939+
[psnoteproperty]::new('.CachedFiles', $uploadedFiles)
7940+
))
7941+
7942+
return $uploadedFiles
7943+
</GetScriptBlock>
7944+
</ScriptProperty>
78847945
<ScriptProperty>
78857946
<Name>FormData</Name>
78867947
<GetScriptBlock>

0 commit comments

Comments
 (0)