Skip to content

Commit f2125f2

Browse files
committed
refactor: return http announce errors in bencoded format instead of json
1 parent 9637661 commit f2125f2

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

src/torrust_http_tracker/handlers.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::convert::Infallible;
33
use std::sync::Arc;
44
use log::debug;
55
use warp::{reject, Rejection, Reply};
6-
use warp::http::{Response, StatusCode};
6+
use warp::http::{Response};
77
use crate::{InfoHash, TorrentError, TorrentPeer, TorrentStats, TorrentTracker};
88
use crate::key_manager::AuthKey;
99
use crate::torrust_http_tracker::{AnnounceRequest, AnnounceResponse, ErrorResponse, Peer, ScrapeRequest, ScrapeResponse, ScrapeResponseEntry, ServerError, WebResult};
@@ -87,11 +87,11 @@ pub async fn handle_scrape(scrape_request: ScrapeRequest, auth_key: Option<AuthK
8787
pub async fn handle_error(r: Rejection) -> std::result::Result<impl Reply, Infallible> {
8888
if let Some(e) = r.find::<ServerError>() {
8989
debug!("{:?}", e);
90-
let reply = warp::reply::json(&ErrorResponse { failure_reason: e.to_string() });
91-
Ok(warp::reply::with_status(reply, StatusCode::BAD_REQUEST))
90+
let body: String = ErrorResponse { failure_reason: e.to_string() }.write();
91+
Ok(Response::new(body))
9292
} else {
93-
let reply = warp::reply::json(&ErrorResponse { failure_reason: "internal server error".to_string() });
94-
Ok(warp::reply::with_status(reply, StatusCode::INTERNAL_SERVER_ERROR))
93+
let body: String = ErrorResponse { failure_reason: "internal server error".to_string() }.write();
94+
Ok(Response::new(body))
9595
}
9696
}
9797

src/torrust_http_tracker/response.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,9 @@ impl ScrapeResponse {
8585
pub struct ErrorResponse {
8686
pub failure_reason: String
8787
}
88+
89+
impl ErrorResponse {
90+
pub fn write(&self) -> String {
91+
serde_bencode::to_string(&self).unwrap()
92+
}
93+
}

0 commit comments

Comments
 (0)