File tree Expand file tree Collapse file tree 3 files changed +25
-1
lines changed Expand file tree Collapse file tree 3 files changed +25
-1
lines changed Original file line number Diff line number Diff line change @@ -1384,7 +1384,9 @@ func (c *DefaultCtx) Range(size int) (Range, error) {
1384
1384
})
1385
1385
}
1386
1386
if len (rangeData .Ranges ) < 1 {
1387
- return rangeData , ErrRangeUnsatisfiable
1387
+ c .Status (StatusRequestedRangeNotSatisfiable )
1388
+ c .Set (HeaderContentRange , "bytes */" + strconv .Itoa (size ))
1389
+ return rangeData , ErrRequestedRangeNotSatisfiable
1388
1390
}
1389
1391
1390
1392
return rangeData , nil
Original file line number Diff line number Diff line change @@ -2991,6 +2991,25 @@ func Test_Ctx_Range(t *testing.T) {
2991
2991
testRange ("seconds=0-1" )
2992
2992
}
2993
2993
2994
+ func Test_Ctx_Range_Unsatisfiable (t * testing.T ) {
2995
+ t .Parallel ()
2996
+ app := New ()
2997
+ app .Get ("/" , func (c Ctx ) error {
2998
+ _ , err := c .Range (10 )
2999
+ if err != nil {
3000
+ return err
3001
+ }
3002
+ return c .SendString ("ok" )
3003
+ })
3004
+
3005
+ req := httptest .NewRequest (MethodGet , "http://example.com/" , nil )
3006
+ req .Header .Set (HeaderRange , "bytes=20-30" )
3007
+ resp , err := app .Test (req )
3008
+ require .NoError (t , err )
3009
+ require .Equal (t , StatusRequestedRangeNotSatisfiable , resp .StatusCode )
3010
+ require .Equal (t , "bytes */10" , resp .Header .Get (HeaderContentRange ))
3011
+ }
3012
+
2994
3013
// go test -v -run=^$ -bench=Benchmark_Ctx_Range -benchmem -count=4
2995
3014
func Benchmark_Ctx_Range (b * testing.B ) {
2996
3015
app := New ()
Original file line number Diff line number Diff line change @@ -1216,6 +1216,9 @@ Returns a struct containing the type and a slice of ranges.
1216
1216
Only the canonical ` bytes` unit is recognized and any optional
1217
1217
whitespace around range specifiers will be ignored, as specified
1218
1218
in RFC 9110 .
1219
+ If none of the requested ranges are satisfiable, the method automatically
1220
+ sets the HTTP status code to **416 Range Not Satisfiable ** and populates the
1221
+ ` Content-Range` header with the current representation size.
1219
1222
1220
1223
` ` ` go title="Signature"
1221
1224
func (c fiber.Ctx) Range(size int) (Range, error)
You can’t perform that action at this time.
0 commit comments