Skip to content

Commit

Permalink
Merge pull request Badgerati#835 from Badgerati/Issue-829
Browse files Browse the repository at this point in the history
Fixes for the Range header for files that dont exist
  • Loading branch information
Badgerati authored Sep 22, 2021
2 parents 7e9e6ac + 2af763f commit c076aad
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/Public/Responses.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,13 @@ function Write-PodeTextResponse
}

# check if we only need a range of the bytes
if ($null -ne $WebEvent.Ranges) {
if (($null -ne $WebEvent.Ranges) -and ($WebEvent.Response.StatusCode -eq 200) -and ($StatusCode -eq 200)) {
$lengths = @()
$size = $Bytes.Length

$Bytes = @(foreach ($range in $WebEvent.Ranges) {
# ensure range not invalid
if ([int]$range.End -ge $size) {
if (([int]$range.Start -lt 0) -or ([int]$range.Start -ge $size) -or ([int]$range.End -lt 0)) {
Set-PodeResponseStatus -Code 416 -NoErrorPage
return
}
Expand All @@ -261,12 +261,26 @@ function Write-PodeTextResponse

# end bytes only
elseif ([string]::IsNullOrWhiteSpace($range.Start)) {
$Bytes[$($size - 1 - $range.End)..($size - 1)]
$lengths += "$($size - 1 - $range.End)-$($size - 1)/$($size)"
if ([int]$range.End -gt $size) {
$range.End = $size
}

if ([int]$range.End -gt 0) {
$Bytes[$($size - $range.End)..($size - 1)]
$lengths += "$($size - $range.End)-$($size - 1)/$($size)"
}
else {
$lengths += "0-0/$($size)"
}
}

# normal range
else {
if ([int]$range.End -ge $size) {
Set-PodeResponseStatus -Code 416 -NoErrorPage
return
}

$Bytes[$range.Start..$range.End]
$lengths += "$($range.Start)-$($range.End)/$($size)"
}
Expand Down

0 comments on commit c076aad

Please sign in to comment.