Skip to content

Commit c248caf

Browse files
committed
Merge #1321: Refactor: move code from axum-http-tracker-server package to http-protocol package
229f9ee refactor: [#1320] move code from axum-http-tracker-server pkg to http-protocol pkg (Jose Celano) Pull request description: Refactor: move code from `axum-http-tracker-server` package to `http-protocol` package. ACKs for top commit: josecelano: ACK 229f9ee Tree-SHA512: bf6497646561b6b0006c209cf34899b377a25127a697317d36b612d360aa546feb91350aa4f32695f97060b61cfdbf1d8ad7080e9444a8b5c28903740964b1aa
2 parents 6577424 + 229f9ee commit c248caf

File tree

10 files changed

+47
-79
lines changed

10 files changed

+47
-79
lines changed

Cargo.lock

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/axum-http-tracker-server/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ futures = "0"
2727
hyper = "1"
2828
reqwest = { version = "0", features = ["json"] }
2929
serde = { version = "1", features = ["derive"] }
30-
thiserror = "2"
3130
tokio = { version = "1", features = ["macros", "net", "rt-multi-thread", "signal", "sync"] }
3231
torrust-axum-server = { version = "3.0.0-develop", path = "../axum-server" }
3332
torrust-server-lib = { version = "3.0.0-develop", path = "../server-lib" }

packages/axum-http-tracker-server/src/v1/extractors/authentication_key.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,11 @@ use axum::extract::rejection::PathRejection;
4949
use axum::extract::{FromRequestParts, Path};
5050
use axum::http::request::Parts;
5151
use axum::response::{IntoResponse, Response};
52-
use bittorrent_http_tracker_protocol::v1::responses;
52+
use bittorrent_http_tracker_protocol::v1::{auth, responses};
5353
use bittorrent_tracker_core::authentication::Key;
5454
use hyper::StatusCode;
5555
use serde::Deserialize;
5656

57-
use crate::v1::handlers::common::auth;
58-
5957
/// Extractor for the [`Key`] struct.
6058
pub struct Extract(pub Key);
6159

packages/axum-http-tracker-server/src/v1/handlers/common/auth.rs

-40
This file was deleted.

packages/axum-http-tracker-server/src/v1/handlers/common/mod.rs

-3
This file was deleted.

packages/axum-http-tracker-server/src/v1/handlers/common/peer_ip.rs

-30
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//! Axum [`handlers`](axum#handlers) for the HTTP server.
22
pub mod announce;
3-
pub mod common;
43
pub mod health_check;
54
pub mod scrape;

packages/http-protocol/src/v1/auth.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use std::panic::Location;
2+
3+
use thiserror::Error;
4+
5+
/// Authentication error.
6+
///
7+
/// When the tracker is private, the authentication key is required in the URL
8+
/// path. These are the possible errors that can occur when extracting the key
9+
/// from the URL path.
10+
#[derive(Debug, Error)]
11+
pub enum Error {
12+
#[error("Invalid format for authentication key param. Error in {location}")]
13+
InvalidKeyFormat { location: &'static Location<'static> },
14+
15+
#[error("Cannot extract authentication key param from URL path. Error in {location}")]
16+
CannotExtractKeyParam { location: &'static Location<'static> },
17+
}

packages/http-protocol/src/v1/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pub mod auth;
12
pub mod query;
23
pub mod requests;
34
pub mod responses;

packages/http-protocol/src/v1/responses/error.rs

+28
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
//! > code.
1414
use serde::Serialize;
1515

16+
use crate::v1::auth;
1617
use crate::v1::services::peer_ip_resolver::PeerIpResolutionError;
1718

1819
/// `Error` response for the HTTP tracker.
@@ -47,6 +48,14 @@ impl Error {
4748
}
4849
}
4950

51+
impl From<auth::Error> for Error {
52+
fn from(err: auth::Error) -> Self {
53+
Self {
54+
failure_reason: format!("Tracker authentication error: {err}"),
55+
}
56+
}
57+
}
58+
5059
impl From<PeerIpResolutionError> for Error {
5160
fn from(err: PeerIpResolutionError) -> Self {
5261
Self {
@@ -89,8 +98,11 @@ impl From<bittorrent_tracker_core::authentication::Error> for Error {
8998

9099
#[cfg(test)]
91100
mod tests {
101+
use std::panic::Location;
92102

93103
use super::Error;
104+
use crate::v1::responses;
105+
use crate::v1::services::peer_ip_resolver::PeerIpResolutionError;
94106

95107
#[test]
96108
fn http_tracker_errors_can_be_bencoded() {
@@ -100,4 +112,20 @@ mod tests {
100112

101113
assert_eq!(err.write(), "d14:failure reason13:error messagee"); // cspell:disable-line
102114
}
115+
116+
fn assert_error_response(error: &responses::error::Error, error_message: &str) {
117+
assert!(
118+
error.failure_reason.contains(error_message),
119+
"Error response does not contain message: '{error_message}'. Error: {error:?}"
120+
);
121+
}
122+
123+
#[test]
124+
fn it_should_map_a_peer_ip_resolution_error_into_an_error_response() {
125+
let response = responses::error::Error::from(PeerIpResolutionError::MissingRightMostXForwardedForIp {
126+
location: Location::caller(),
127+
});
128+
129+
assert_error_response(&response, "Error resolving peer IP");
130+
}
103131
}

0 commit comments

Comments
 (0)