forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(http): [torrust#184] bencoded error responses for announce request
HTTP tracker error responser must be bencoded. Fixed in the new Axum implementation.
- Loading branch information
1 parent
d0c8eb0
commit 056a2a7
Showing
6 changed files
with
163 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use axum::http::StatusCode; | ||
use axum::response::{IntoResponse, Response}; | ||
use serde::{self, Serialize}; | ||
|
||
#[derive(Serialize)] | ||
pub struct Error { | ||
#[serde(rename = "failure reason")] | ||
pub failure_reason: String, | ||
} | ||
|
||
impl Error { | ||
/// # Panics | ||
/// | ||
/// It would panic if the `Error` struct contained an inappropriate type. | ||
#[must_use] | ||
pub fn write(&self) -> String { | ||
serde_bencode::to_string(&self).unwrap() | ||
} | ||
} | ||
|
||
impl IntoResponse for Error { | ||
fn into_response(self) -> Response { | ||
(StatusCode::OK, self.write()).into_response() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
|
||
use super::Error; | ||
|
||
#[test] | ||
fn http_tracker_errors_can_be_bencoded() { | ||
let err = Error { | ||
failure_reason: "error message".to_owned(), | ||
}; | ||
|
||
assert_eq!(err.write(), "d14:failure reason13:error messagee"); // cspell:disable-line | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod ok; | ||
pub mod error; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.