feat: add task ID response header in Dragonfly client proxy (#1256)

Signed-off-by: Gaius <gaius.qi@gmail.com>
This commit is contained in:
Gaius 2025-07-24 18:16:34 +08:00 committed by GitHub
parent 777c131fbe
commit bf6f49e0e9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 2 deletions

View File

@ -79,11 +79,15 @@ pub const DRAGONFLY_PIECE_LENGTH_HEADER: &str = "X-Dragonfly-Piece-Length";
pub const DRAGONFLY_CONTENT_FOR_CALCULATING_TASK_ID_HEADER: &str = pub const DRAGONFLY_CONTENT_FOR_CALCULATING_TASK_ID_HEADER: &str =
"X-Dragonfly-Content-For-Calculating-Task-ID"; "X-Dragonfly-Content-For-Calculating-Task-ID";
/// DRAGONFLY_TASK_DOWNLOAD_FINISHED_HEADER is the header key to indicate whether the task download finished. /// DRAGONFLY_TASK_DOWNLOAD_FINISHED_HEADER is the response header key to indicate whether the task download finished.
/// When the task download is finished, the response will include this header with the value `"true"`, /// When the task download is finished, the response will include this header with the value `"true"`,
/// indicating that the download hit the local cache. /// indicating that the download hit the local cache.
pub const DRAGONFLY_TASK_DOWNLOAD_FINISHED_HEADER: &str = "X-Dragonfly-Task-Download-Finished"; pub const DRAGONFLY_TASK_DOWNLOAD_FINISHED_HEADER: &str = "X-Dragonfly-Task-Download-Finished";
/// DRAGONFLY_TASK_ID_HEADER is the response header key of task id. Client will calculate the task ID
/// based on `url`, `piece_length`, `tag`, `application`, and `filtered_query_params`.
pub const DRAGONFLY_TASK_ID_HEADER: &str = "X-Dragonfly-Task-ID";
/// get_tag gets the tag from http header. /// get_tag gets the tag from http header.
pub fn get_tag(header: &HeaderMap) -> Option<String> { pub fn get_tag(header: &HeaderMap) -> Option<String> {
header header

View File

@ -768,7 +768,10 @@ async fn proxy_via_dfdaemon(
// Construct the response. // Construct the response.
let mut response = Response::new(boxed_body); let mut response = Response::new(boxed_body);
*response.headers_mut() = make_response_headers(download_task_started_response.clone())?; *response.headers_mut() = make_response_headers(
message.task_id.as_str(),
download_task_started_response.clone(),
)?;
*response.status_mut() = http::StatusCode::OK; *response.status_mut() = http::StatusCode::OK;
// Return the response if the client return the first piece. // Return the response if the client return the first piece.
@ -1151,6 +1154,7 @@ fn make_download_url(
/// make_response_headers makes the response headers. /// make_response_headers makes the response headers.
fn make_response_headers( fn make_response_headers(
task_id: &str,
mut download_task_started_response: DownloadTaskStartedResponse, mut download_task_started_response: DownloadTaskStartedResponse,
) -> ClientResult<hyper::header::HeaderMap> { ) -> ClientResult<hyper::header::HeaderMap> {
// Insert the content range header to the response header. // Insert the content range header to the response header.
@ -1178,6 +1182,11 @@ fn make_response_headers(
); );
} }
download_task_started_response.response_header.insert(
header::DRAGONFLY_TASK_ID_HEADER.to_string(),
task_id.to_string(),
);
hashmap_to_headermap(&download_task_started_response.response_header) hashmap_to_headermap(&download_task_started_response.response_header)
} }