Skip to content

Commit

Permalink
Merge #1112: Extract duplicate code into new package: `bittorrent-htt…
Browse files Browse the repository at this point in the history
…p-protocol`

2bc44af test: add test for percent_encode_byte_array (Jose Celano)
a62ae82 fix: cargo machete warning (Jose Celano)
39716a8 ci: fix testing workflow. Run doc tests for all packages (Jose Celano)
c61cc9a fix: doc tests (Jose Celano)
555d5b8 fix: [#1097] by extracting duplicate module (Jose Celano)

Pull request description:

  For now, it only contains percent encoding functions.

  This removes duplicate code and fixes the problem of slow doc tests. On my machine, it takes 40 seconds to run all doc tests with one job.

  ```console
  time cargo test --doc --workspace --jobs=1

  real    0m40.443s
  user    6m6.863s
  sys     0m22.609s
  ```

  I have also fixed the `testing` workflow, it was using `cargo test --doc` to run doc-tests without including doc-test is workspace packages.

ACKs for top commit:
  josecelano:
    ACK 2bc44af

Tree-SHA512: 82b6cfb3ac40ffce437717d800388ab3f4968dc0e73dbb25eae4160378f8280ec3569381d5b58f8d3884ad839b5be320f43e2e246b527932e72ca8572fa79397
  • Loading branch information
josecelano committed Nov 29, 2024
2 parents e0245f0 + 2bc44af commit 17e86c6
Show file tree
Hide file tree
Showing 16 changed files with 734 additions and 144 deletions.
1 change: 1 addition & 0 deletions .github/workflows/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ jobs:
env:
CARGO_REGISTRY_TOKEN: "${{ secrets.TORRUST_UPDATE_CARGO_REGISTRY_TOKEN }}"
run: |
cargo publish -p bittorrent-http-protocol
cargo publish -p bittorrent-tracker-client
cargo publish -p torrust-tracker
cargo publish -p torrust-tracker-client
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/testing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ jobs:

- id: test-docs
name: Run Documentation Tests
run: cargo test --doc
run: cargo test --doc --workspace

- id: test
name: Run Unit Tests
Expand Down
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ axum = { version = "0", features = ["macros"] }
axum-client-ip = "0"
axum-extra = { version = "0", features = ["query"] }
axum-server = { version = "0", features = ["tls-rustls-no-provider"] }
bittorrent-http-protocol = { version = "3.0.0-develop", path = "packages/http-protocol" }
bittorrent-primitives = "0.1.0"
bittorrent-tracker-client = { version = "3.0.0-develop", path = "packages/tracker-client" }
blowfish = "0"
Expand Down
6 changes: 3 additions & 3 deletions contrib/bencode/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
//! Decoding bencoded data:
//!
//! ```rust
//! extern crate bencode;
//! extern crate torrust_tracker_contrib_bencode;
//!
//! use bencode::{BencodeRef, BRefAccess, BDecodeOpt};
//! use torrust_tracker_contrib_bencode::{BencodeRef, BRefAccess, BDecodeOpt};
//!
//! fn main() {
//! let data = b"d12:lucky_numberi7ee"; // cspell:disable-line
Expand All @@ -22,7 +22,7 @@
//!
//! ```rust
//! #[macro_use]
//! extern crate bencode;
//! extern crate torrust_tracker_contrib_bencode;
//!
//! fn main() {
//! let message = (ben_map!{
Expand Down
21 changes: 21 additions & 0 deletions packages/http-protocol/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
description = "A library with the primitive types and functions for the BitTorrent HTTP tracker protocol."
keywords = ["api", "library", "primitives"]
name = "bittorrent-http-protocol"
readme = "README.md"

authors.workspace = true
documentation.workspace = true
edition.workspace = true
homepage.workspace = true
license.workspace = true
publish.workspace = true
repository.workspace = true
rust-version.workspace = true
version.workspace = true

[dependencies]
aquatic_udp_protocol = "0"
bittorrent-primitives = "0.1.0"
percent-encoding = "2"
torrust-tracker-primitives = { version = "3.0.0-develop", path = "../primitives" }
661 changes: 661 additions & 0 deletions packages/http-protocol/LICENSE

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions packages/http-protocol/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# BitTorrent HTTP Tracker Protocol

A library with the primitive types and functions used by BitTorrent HTTP trackers.

## Documentation

[Crate documentation](https://docs.rs/bittorrent-http-protocol).

## License

The project is licensed under the terms of the [GNU AFFERO GENERAL PUBLIC LICENSE](./LICENSE).
2 changes: 2 additions & 0 deletions packages/http-protocol/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//! Primitive types and function for `BitTorrent` HTTP trackers.
pub mod percent_encoding;
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use torrust_tracker_primitives::peer;
///
/// ```rust
/// use std::str::FromStr;
/// use torrust_tracker::servers::http::percent_encoding::percent_decode_info_hash;
/// use bittorrent_http_protocol::percent_encoding::percent_decode_info_hash;
/// use bittorrent_primitives::info_hash::InfoHash;
/// use torrust_tracker_primitives::peer;
///
Expand Down Expand Up @@ -60,7 +60,7 @@ pub fn percent_decode_info_hash(raw_info_hash: &str) -> Result<InfoHash, info_ha
/// use std::str::FromStr;
///
/// use aquatic_udp_protocol::PeerId;
/// use torrust_tracker::servers::http::percent_encoding::percent_decode_peer_id;
/// use bittorrent_http_protocol::percent_encoding::percent_decode_peer_id;
/// use bittorrent_primitives::info_hash::InfoHash;
///
/// let encoded_peer_id = "%2DqB00000000000000000";
Expand All @@ -85,7 +85,7 @@ mod tests {
use aquatic_udp_protocol::PeerId;
use bittorrent_primitives::info_hash::InfoHash;

use crate::servers::http::percent_encoding::{percent_decode_info_hash, percent_decode_peer_id};
use crate::percent_encoding::{percent_decode_info_hash, percent_decode_peer_id};

#[test]
fn it_should_decode_a_percent_encoded_info_hash() {
Expand Down
2 changes: 1 addition & 1 deletion packages/located-error/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
//! let b: LocatedError<TestError> = Located(e).into();
//! let l = get_caller_location();
//!
//! assert!(b.to_string().contains("Test, src/lib.rs"));
//! assert!(b.to_string().contains("src/lib.rs"));
//! ```
//!
//! # Credits
Expand Down
17 changes: 16 additions & 1 deletion packages/tracker-client/src/http/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub mod client;
pub mod url_encoding;

use percent_encoding::NON_ALPHANUMERIC;

Expand All @@ -25,3 +24,19 @@ impl InfoHash {
self.0
}
}

#[cfg(test)]
mod tests {
use crate::http::percent_encode_byte_array;

#[test]
fn it_should_encode_a_20_byte_array() {
assert_eq!(
percent_encode_byte_array(&[
0x3b, 0x24, 0x55, 0x04, 0xcf, 0x5f, 0x11, 0xbb, 0xdb, 0xe1, 0x20, 0x1c, 0xea, 0x6a, 0x6b, 0xf4, 0x5a, 0xee, 0x1b,
0xc0,
]),
"%3B%24U%04%CF%5F%11%BB%DB%E1%20%1C%EAjk%F4Z%EE%1B%C0"
);
}
}
132 changes: 0 additions & 132 deletions packages/tracker-client/src/http/url_encoding.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/servers/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,6 @@
//! - [Bencode to Json Online converter](https://chocobo1.github.io/bencode_online).
use serde::{Deserialize, Serialize};

pub mod percent_encoding;
pub mod server;
pub mod v1;

Expand Down
2 changes: 1 addition & 1 deletion src/servers/http/v1/requests/announce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ use std::panic::Location;
use std::str::FromStr;

use aquatic_udp_protocol::{NumberOfBytes, PeerId};
use bittorrent_http_protocol::percent_encoding::{percent_decode_info_hash, percent_decode_peer_id};
use bittorrent_primitives::info_hash::{self, InfoHash};
use thiserror::Error;
use torrust_tracker_located_error::{Located, LocatedError};
use torrust_tracker_primitives::peer;

use crate::servers::http::percent_encoding::{percent_decode_info_hash, percent_decode_peer_id};
use crate::servers::http::v1::query::{ParseQueryError, Query};
use crate::servers::http::v1::responses;

Expand Down
2 changes: 1 addition & 1 deletion src/servers/http/v1/requests/scrape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
//! Data structures and logic for parsing the `scrape` request.
use std::panic::Location;

use bittorrent_http_protocol::percent_encoding::percent_decode_info_hash;
use bittorrent_primitives::info_hash::{self, InfoHash};
use thiserror::Error;
use torrust_tracker_located_error::{Located, LocatedError};

use crate::servers::http::percent_encoding::percent_decode_info_hash;
use crate::servers::http::v1::query::Query;
use crate::servers::http::v1::responses;

Expand Down

0 comments on commit 17e86c6

Please sign in to comment.