Skip to content

Commit

Permalink
refactor: move service to http-protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Jan 11, 2025
1 parent 09add2f commit 9142063
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 28 deletions.
1 change: 1 addition & 0 deletions packages/http-protocol/src/v1/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod query;
pub mod requests;
pub mod responses;
pub mod services;
10 changes: 10 additions & 0 deletions packages/http-protocol/src/v1/responses/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
//! > code.
use serde::Serialize;

use crate::v1::services::peer_ip_resolver::PeerIpResolutionError;

/// `Error` response for the [`HTTP tracker`](crate::servers::http).
#[derive(Serialize, Debug, PartialEq)]
pub struct Error {
Expand Down Expand Up @@ -45,6 +47,14 @@ impl Error {
}
}

impl From<PeerIpResolutionError> for Error {
fn from(err: PeerIpResolutionError) -> Self {
Self {
failure_reason: format!("Error resolving peer IP: {err}"),
}
}
}

#[cfg(test)]
mod tests {

Expand Down
1 change: 1 addition & 0 deletions packages/http-protocol/src/v1/services/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod peer_ip_resolver;
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ mod tests {
use std::str::FromStr;

use super::invoke;
use crate::servers::http::v1::services::peer_ip_resolver::{ClientIpSources, PeerIpResolutionError};
use crate::v1::services::peer_ip_resolver::{ClientIpSources, PeerIpResolutionError};

#[test]
fn it_should_get_the_peer_ip_from_the_connection_info() {
Expand Down Expand Up @@ -181,7 +181,7 @@ mod tests {
use std::net::IpAddr;
use std::str::FromStr;

use crate::servers::http::v1::services::peer_ip_resolver::{invoke, ClientIpSources, PeerIpResolutionError};
use crate::v1::services::peer_ip_resolver::{invoke, ClientIpSources, PeerIpResolutionError};

#[test]
fn it_should_get_the_peer_ip_from_the_right_most_ip_in_the_x_forwarded_for_header() {
Expand Down
3 changes: 1 addition & 2 deletions src/servers/http/v1/extractors/client_ip_sources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ use axum::extract::{ConnectInfo, FromRequestParts};
use axum::http::request::Parts;
use axum::response::Response;
use axum_client_ip::RightmostXForwardedFor;

use crate::servers::http::v1::services::peer_ip_resolver::ClientIpSources;
use bittorrent_http_protocol::v1::services::peer_ip_resolver::ClientIpSources;

/// Extractor for the [`ClientIpSources`]
/// struct.
Expand Down
13 changes: 8 additions & 5 deletions src/servers/http/v1/handlers/announce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use axum::extract::State;
use axum::response::{IntoResponse, Response};
use bittorrent_http_protocol::v1::requests::announce::{Announce, Compact, Event};
use bittorrent_http_protocol::v1::responses::{self};
use bittorrent_http_protocol::v1::services::peer_ip_resolver;
use bittorrent_http_protocol::v1::services::peer_ip_resolver::ClientIpSources;
use hyper::StatusCode;
use torrust_tracker_clock::clock::Time;
use torrust_tracker_primitives::core::AnnounceData;
Expand All @@ -25,8 +27,7 @@ use crate::servers::http::v1::extractors::announce_request::ExtractRequest;
use crate::servers::http::v1::extractors::authentication_key::Extract as ExtractKey;
use crate::servers::http::v1::extractors::client_ip_sources::Extract as ExtractClientIpSources;
use crate::servers::http::v1::handlers::common::auth;
use crate::servers::http::v1::services::peer_ip_resolver::ClientIpSources;
use crate::servers::http::v1::services::{self, peer_ip_resolver};
use crate::servers::http::v1::services::{self};
use crate::CurrentClock;

/// It handles the `announce` request when the HTTP tracker does not require
Expand Down Expand Up @@ -180,12 +181,12 @@ mod tests {
use aquatic_udp_protocol::PeerId;
use bittorrent_http_protocol::v1::requests::announce::Announce;
use bittorrent_http_protocol::v1::responses;
use bittorrent_http_protocol::v1::services::peer_ip_resolver::ClientIpSources;
use bittorrent_primitives::info_hash::InfoHash;
use torrust_tracker_test_helpers::configuration;

use crate::core::services::tracker_factory;
use crate::core::Tracker;
use crate::servers::http::v1::services::peer_ip_resolver::ClientIpSources;

fn private_tracker() -> Tracker {
tracker_factory(&configuration::ephemeral_private())
Expand Down Expand Up @@ -305,10 +306,11 @@ mod tests {

use std::sync::Arc;

use bittorrent_http_protocol::v1::services::peer_ip_resolver::ClientIpSources;

use super::{sample_announce_request, tracker_on_reverse_proxy};
use crate::servers::http::v1::handlers::announce::handle_announce;
use crate::servers::http::v1::handlers::announce::tests::assert_error_response;
use crate::servers::http::v1::services::peer_ip_resolver::ClientIpSources;

#[tokio::test]
async fn it_should_fail_when_the_right_most_x_forwarded_for_header_ip_is_not_available() {
Expand All @@ -334,10 +336,11 @@ mod tests {

use std::sync::Arc;

use bittorrent_http_protocol::v1::services::peer_ip_resolver::ClientIpSources;

use super::{sample_announce_request, tracker_not_on_reverse_proxy};
use crate::servers::http::v1::handlers::announce::handle_announce;
use crate::servers::http::v1::handlers::announce::tests::assert_error_response;
use crate::servers::http::v1::services::peer_ip_resolver::ClientIpSources;

#[tokio::test]
async fn it_should_fail_when_the_client_ip_from_the_connection_info_is_not_available() {
Expand Down
16 changes: 2 additions & 14 deletions src/servers/http/v1/handlers/common/peer_ip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,15 @@
//!
//! The HTTP tracker may fail to resolve the peer IP address. This module
//! contains the logic to convert those
//! [`PeerIpResolutionError`]
//! [`PeerIpResolutionError`](bittorrent_http_protocol::v1::services::peer_ip_resolver::PeerIpResolutionError)
//! errors into responses.
use bittorrent_http_protocol::v1::responses;

use crate::servers::http::v1::services::peer_ip_resolver::PeerIpResolutionError;

impl From<PeerIpResolutionError> for responses::error::Error {
fn from(err: PeerIpResolutionError) -> Self {
responses::error::Error {
failure_reason: format!("Error resolving peer IP: {err}"),
}
}
}
#[cfg(test)]
mod tests {
use std::panic::Location;

use bittorrent_http_protocol::v1::responses;

use crate::servers::http::v1::services::peer_ip_resolver::PeerIpResolutionError;
use bittorrent_http_protocol::v1::services::peer_ip_resolver::PeerIpResolutionError;

fn assert_error_response(error: &responses::error::Error, error_message: &str) {
assert!(
Expand Down
10 changes: 6 additions & 4 deletions src/servers/http/v1/handlers/scrape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use axum::extract::State;
use axum::response::{IntoResponse, Response};
use bittorrent_http_protocol::v1::requests::scrape::Scrape;
use bittorrent_http_protocol::v1::responses;
use bittorrent_http_protocol::v1::services::peer_ip_resolver::{self, ClientIpSources};
use hyper::StatusCode;
use torrust_tracker_primitives::core::ScrapeData;

Expand All @@ -20,7 +21,6 @@ use crate::servers::http::v1::extractors::authentication_key::Extract as Extract
use crate::servers::http::v1::extractors::client_ip_sources::Extract as ExtractClientIpSources;
use crate::servers::http::v1::extractors::scrape_request::ExtractRequest;
use crate::servers::http::v1::services;
use crate::servers::http::v1::services::peer_ip_resolver::{self, ClientIpSources};

/// It handles the `scrape` request when the HTTP tracker is configured
/// to run in `public` mode.
Expand Down Expand Up @@ -117,12 +117,12 @@ mod tests {

use bittorrent_http_protocol::v1::requests::scrape::Scrape;
use bittorrent_http_protocol::v1::responses;
use bittorrent_http_protocol::v1::services::peer_ip_resolver::ClientIpSources;
use bittorrent_primitives::info_hash::InfoHash;
use torrust_tracker_test_helpers::configuration;

use crate::core::services::tracker_factory;
use crate::core::Tracker;
use crate::servers::http::v1::services::peer_ip_resolver::ClientIpSources;

fn private_tracker() -> Tracker {
tracker_factory(&configuration::ephemeral_private())
Expand Down Expand Up @@ -232,10 +232,11 @@ mod tests {
mod with_tracker_on_reverse_proxy {
use std::sync::Arc;

use bittorrent_http_protocol::v1::services::peer_ip_resolver::ClientIpSources;

use super::{sample_scrape_request, tracker_on_reverse_proxy};
use crate::servers::http::v1::handlers::scrape::handle_scrape;
use crate::servers::http::v1::handlers::scrape::tests::assert_error_response;
use crate::servers::http::v1::services::peer_ip_resolver::ClientIpSources;

#[tokio::test]
async fn it_should_fail_when_the_right_most_x_forwarded_for_header_ip_is_not_available() {
Expand All @@ -260,10 +261,11 @@ mod tests {
mod with_tracker_not_on_reverse_proxy {
use std::sync::Arc;

use bittorrent_http_protocol::v1::services::peer_ip_resolver::ClientIpSources;

use super::{sample_scrape_request, tracker_not_on_reverse_proxy};
use crate::servers::http::v1::handlers::scrape::handle_scrape;
use crate::servers::http::v1::handlers::scrape::tests::assert_error_response;
use crate::servers::http::v1::services::peer_ip_resolver::ClientIpSources;

#[tokio::test]
async fn it_should_fail_when_the_client_ip_from_the_connection_info_is_not_available() {
Expand Down
1 change: 0 additions & 1 deletion src/servers/http/v1/services/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@
//!
//! Refer to [`torrust_tracker`](crate) documentation.
pub mod announce;
pub mod peer_ip_resolver;
pub mod scrape;

0 comments on commit 9142063

Please sign in to comment.