@@ -289,6 +289,7 @@ defmodule Plug.Static do
289289 { range_start , range_end } <- start_and_end ( bytes , file_size ) do
290290 send_range ( conn , path , range_start , range_end , file_size , options )
291291 else
292+ :unsatisfiable -> send_unsatisfiable_range ( conn , file_size , options )
292293 _ -> send_entire_file ( conn , path , options )
293294 end
294295 end
@@ -297,6 +298,8 @@ defmodule Plug.Static do
297298 send_entire_file ( conn , path , options )
298299 end
299300
301+ defp start_and_end ( _range , 0 ) , do: :error
302+
300303 defp start_and_end ( "-" <> rest , file_size ) do
301304 case Integer . parse ( rest ) do
302305 { last , "" } when last > 0 and last <= file_size -> { file_size - last , file_size - 1 }
@@ -306,15 +309,18 @@ defmodule Plug.Static do
306309
307310 defp start_and_end ( range , file_size ) do
308311 case Integer . parse ( range ) do
309- { first , "-" } when first >= 0 ->
312+ { first , "-" } when first >= 0 and first < file_size ->
310313 { first , file_size - 1 }
311314
312- { first , "-" <> rest } when first >= 0 ->
315+ { first , "-" <> rest } when first >= 0 and first < file_size ->
313316 case Integer . parse ( rest ) do
314317 { last , "" } when last >= first -> { first , min ( last , file_size - 1 ) }
315318 _ -> :error
316319 end
317320
321+ { first , "-" <> _ } when first >= file_size ->
322+ :unsatisfiable
323+
318324 _ ->
319325 :error
320326 end
@@ -333,6 +339,14 @@ defmodule Plug.Static do
333339 |> halt ( )
334340 end
335341
342+ defp send_unsatisfiable_range ( conn , file_size , options ) do
343+ conn
344+ |> maybe_add_vary ( options )
345+ |> put_resp_header ( "content-range" , "bytes */#{ file_size } " )
346+ |> send_resp ( 416 , "" )
347+ |> halt ( )
348+ end
349+
336350 defp send_entire_file ( conn , path , options ) do
337351 conn
338352 |> maybe_add_vary ( options )
0 commit comments