Doc comment in to_bytes uses < max instead of <= max
I happened to stumble on this piece of documentation: https://github.com/hyperium/hyper/blob/40c01dfb4f87342a6f86f07564ddc482194c6240/src/body/to_bytes.rs#L32-L42
if the size hint is None, the content length is set to MAX_ALLOWED_RESPONSE_SIZE + 1, implying that MAX_ALLOWED_RESPONSE_SIZE would be accepted. But it's not, because the condition is if response_content_length < MAX_ALLOWED_RESPONSE_SIZE, rather than <=.
we could:
a. change the condition to <=
b. rename the constant to MIN_REJECTED_RESPONSE_SIZE (or something else) and change the match to return the const without a + 1
c. immediately reject the response when it encounters None like so:
let response_is_small_enough = match response.body().size_hint().upper() {
Some(size_hint) => size_hint <= MAX_ALLOWED_RESPONSE_SIZE,
None => false, // we don't know how big the response is, so we reject it in case it is too large.
}
if response_is_small_enough {
let body_bytes = hyper::body::to_bytes(response.into_body()).await?;
println!("body: {:?}", body_bytes);
}
this is only one more way of writing the condition. i would've preferred is_some_and if it were stable.
thoughts?
Thanks for thinking about this and writing this up!
if the size hint is None, the content length is set to MAX_ALLOWED_RESPONSE_SIZE + 1, implying that MAX_ALLOWED_RESPONSE_SIZE would be accepted. But it's not, because the condition is if response_content_length < MAX_ALLOWED_RESPONSE_SIZE, rather than <=.
Either way, I think it accomplishes what we want: if there is no content-length (the None) case, then reject. If I were to have written the example myself, I might have reached for upper().or(u64::MAX), which might be a little clearer.