Skip to content

Commit

Permalink
fix: range request with 0 rows and 0 offset return status 416 (#2991)
Browse files Browse the repository at this point in the history
  • Loading branch information
Strengthless authored and wolfgangwalther committed Oct 25, 2023
1 parent d07b5ac commit e1e0cea
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased

### Fixed

- #2824, Fix range request with 0 rows and 0 offset return status 416 - @strengthless

## [11.2.1] - 2023-10-03

### Fixed
Expand Down
6 changes: 3 additions & 3 deletions src/PostgREST/RangeQuery.hs
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ rangeStatusHeader topLevelRange queryTotal tableTotal =
rangeStatus :: Integer -> Integer -> Maybe Integer -> Status
rangeStatus _ _ Nothing = status200
rangeStatus lower upper (Just total)
| lower >= total && lower /= upper = status416 -- 416 Range Not Satisfiable
| (1 + upper - lower) < total = status206 -- 206 Partial Content
| otherwise = status200 -- 200 OK
| lower >= total && lower /= upper && lower /= 0 = status416 -- 416 Range Not Satisfiable
| (1 + upper - lower) < total = status206 -- 206 Partial Content
| otherwise = status200 -- 200 OK

contentRangeH :: (Integral a, Show a) => a -> a -> Maybe a -> Header
contentRangeH lower upper total =
Expand Down
9 changes: 9 additions & 0 deletions test/spec/Feature/Query/RangeSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,15 @@ spec = do
matchHeader "Content-Range" "10-14/*"
simpleStatus r `shouldBe` ok200

it "does not throw error when offset is 0 and and total is 0" $
request methodGet "/rpc/getitemrange?min=0&max=0"
(rangeHdrsWithCount $ ByteRangeFromTo 0 1) mempty
`shouldRespondWith`
[json|[]|]
{ matchStatus = 200
, matchHeaders = ["Content-Range" <:> "*/0"]
}

context "of invalid range" $ do
it "fails with 416 for offside range" $
request methodGet "/items"
Expand Down

0 comments on commit e1e0cea

Please sign in to comment.