A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
// SPDX-License-Identifier: AGPL-3.0-or-later
use crate::{range, server::download_stream::DownloadStreamPolicy, storage::StreamObject};
use axum::{
body::Body,
http::{HeaderValue, Method, StatusCode},
response::Response,
};
pub(in crate::server) fn passthrough_head_response(
content_type: &str,
total_len: usize,
byte_range: Option<range::ByteRange>,
disposition: Option<HeaderValue>,
) -> Response {
let body_len = byte_range.map(|r| r.end - r.start + 1).unwrap_or(total_len);
let status = if byte_range.is_some() {
StatusCode::PARTIAL_CONTENT
} else {
StatusCode::OK
};
media_body_response(
Body::empty(),
status,
total_len,
content_type,
byte_range,
Some(body_len as u64),
disposition,
)
}
pub(super) struct StreamingMediaResponse<'a> {
pub(super) method: Method,
pub(super) object: StreamObject,
pub(super) total_len: usize,
pub(super) content_type: &'a str,
pub(super) disposition: Option<HeaderValue>,
pub(super) stream_policy: DownloadStreamPolicy,
}
pub(super) fn streaming_media_response(response: StreamingMediaResponse<'_>) -> Response {
let StreamingMediaResponse {
method,
object,
total_len,
content_type,
disposition,
stream_policy,
} = response;
let status = if object.status == StatusCode::PARTIAL_CONTENT {
StatusCode::PARTIAL_CONTENT
} else {
StatusCode::OK
};
// Advertise the range the store actually satisfied, not one recomputed locally: an origin is
// free to answer a different span than the client asked for, and Content-Range must describe
// the bytes in this body.
let effective_byte_range = if status == StatusCode::PARTIAL_CONTENT {
object.byte_range
} else {
None
};
let expected_body_len = effective_byte_range
.map(|r| r.end - r.start + 1)
.unwrap_or(total_len);
let body_len = object
.content_length
.and_then(|value| usize::try_from(value).ok())
.unwrap_or(expected_body_len);
let response_content_type = if content_type.is_empty() {
object.content_type.as_str()
} else {
content_type
};
let body = if method == Method::HEAD {
Body::empty()
} else {
stream_policy.guard(object.body, body_len)
};
media_body_response(
body,
status,
total_len,
response_content_type,
effective_byte_range,
Some(body_len as u64),
disposition,
)
}
use crate::server::response::media_body_response;