Skip to content

Commit cf4fb22

Browse files
committed
Merge #243: Add a custom header with info-hash to the download endpoints
f7f76ff fix: clippy warning (Jose Celano) 414c6c1 feat: add a custom header with infohash to the download endpoints (Jose Celano) Pull request description: Endpoints, where you can download a torrent file, have now a custom header containing the torrent infohash. The name of the header is: `x-torrust-torrent-infohash`. It is added because [in the frontend we need the infohash in a cypress test](torrust/torrust-index-gui#185) and this way we do not need to parse the downloaded torrent which is faster and simpler. ACKs for top commit: da2ce7: ACK f7f76ff Tree-SHA512: 4be7ff27cbf96098a6e8d42f4f5064866bc0f8c5f02b58e5d70ed65c0cac17ecb1df9b3795d4b9e6af684dbd81ef1037cf1a056395e2d46bad0df7e109bfb26a
2 parents e2a0ed4 + f7f76ff commit cf4fb22

File tree

3 files changed

+31
-14
lines changed

3 files changed

+31
-14
lines changed

src/services/torrent.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,6 @@ impl Index {
333333
let page_size = request.page_size.unwrap_or(default_torrent_page_size);
334334

335335
// Guard that page size does not exceed the maximum
336-
let max_torrent_page_size = max_torrent_page_size;
337336
let page_size = if page_size > max_torrent_page_size {
338337
max_torrent_page_size
339338
} else {

src/web/api/v1/contexts/torrent/handlers.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ pub async fn download_torrent_handler(
9494
return ServiceError::InternalServerError.into_response();
9595
};
9696

97-
torrent_file_response(bytes, &format!("{}.torrent", torrent.info.name))
97+
torrent_file_response(bytes, &format!("{}.torrent", torrent.info.name), &torrent.info_hash())
9898
}
9999

100100
/// It returns a list of torrents matching the search criteria.
@@ -244,7 +244,7 @@ pub async fn create_random_torrent_handler(State(_app_data): State<Arc<AppData>>
244244
return ServiceError::InternalServerError.into_response();
245245
};
246246

247-
torrent_file_response(bytes, &format!("{}.torrent", torrent.info.name))
247+
torrent_file_response(bytes, &format!("{}.torrent", torrent.info.name), &torrent.info_hash())
248248
}
249249

250250
/// Extracts the [`TorrentRequest`] from the multipart form payload.

src/web/api/v1/contexts/torrent/responses.rs

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use axum::response::{IntoResponse, Response};
22
use axum::Json;
3-
use hyper::{header, StatusCode};
3+
use hyper::{header, HeaderMap, StatusCode};
44
use serde::{Deserialize, Serialize};
55

66
use crate::models::torrent::TorrentId;
@@ -23,15 +23,33 @@ pub fn new_torrent_response(torrent_id: TorrentId, info_hash: &str) -> Json<OkRe
2323
})
2424
}
2525

26+
/// Builds the binary response for a torrent file.
27+
///
28+
/// # Panics
29+
///
30+
/// Panics if the filename is not a valid header value for the `content-disposition`
31+
/// header.
2632
#[must_use]
27-
pub fn torrent_file_response(bytes: Vec<u8>, filename: &str) -> Response {
28-
(
29-
StatusCode::OK,
30-
[
31-
(header::CONTENT_TYPE, "application/x-bittorrent"),
32-
(header::CONTENT_DISPOSITION, &format!("attachment; filename={filename}")),
33-
],
34-
bytes,
35-
)
36-
.into_response()
33+
pub fn torrent_file_response(bytes: Vec<u8>, filename: &str, info_hash: &str) -> Response {
34+
let mut headers = HeaderMap::new();
35+
headers.insert(
36+
header::CONTENT_TYPE,
37+
"application/x-bittorrent"
38+
.parse()
39+
.expect("HTTP content type header should be valid"),
40+
);
41+
headers.insert(
42+
header::CONTENT_DISPOSITION,
43+
format!("attachment; filename={filename}")
44+
.parse()
45+
.expect("Torrent filename should be a valid header value for the content disposition header"),
46+
);
47+
headers.insert(
48+
"x-torrust-torrent-infohash",
49+
info_hash
50+
.parse()
51+
.expect("Torrent infohash should be a valid header value for the content disposition header"),
52+
);
53+
54+
(StatusCode::OK, headers, bytes).into_response()
3755
}

0 commit comments

Comments
 (0)