index : static-web-server.git

ascending towards madness

author Jose Quintana <joseluisquintana20@gmail.com> 2023-06-27 21:16:07.0 +00:00:00
committer Jose Quintana <joseluisquintana20@gmail.com> 2023-06-27 21:16:07.0 +00:00:00
commit
b8473aa45e30494899c932120130e5ea6eec6bae [patch]
tree
8631c13897ee4a85ce5dda416377062e5d79a225
parent
06955e9061413b14f39aee7c24259f51463e0d78
download
b8473aa45e30494899c932120130e5ea6eec6bae.tar.gz

fix: potential panic when invalid content range

it handles a "potential" panic when content rages are invalid replying
with a `416 Range Not Satisfiable`

Diff

 src/static_files.rs | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/src/static_files.rs b/src/static_files.rs
index ca9e602..9cb04e4 100644
--- a/src/static_files.rs
+++ b/src/static_files.rs
@@ -571,7 +571,17 @@ async fn response_body(
                    if sub_len != len {
                        *resp.status_mut() = StatusCode::PARTIAL_CONTENT;
                        resp.headers_mut().typed_insert(
                            ContentRange::bytes(start..end, len).expect("valid ContentRange"),
                            match ContentRange::bytes(start..end, len) {
                                Ok(range) => range,
                                Err(err) => {
                                    tracing::error!("invalid content range error: {:?}", err);
                                    let mut resp = Response::new(Body::empty());
                                    *resp.status_mut() = StatusCode::RANGE_NOT_SATISFIABLE;
                                    resp.headers_mut()
                                        .typed_insert(ContentRange::unsatisfied_bytes(len));
                                    return Ok(resp);
                                }
                            },
                        );

                        len = sub_len;