Skip to content

Commit

Permalink
fix: [torrust#419] url-encode tracker API token in the HTTP requests
Browse files Browse the repository at this point in the history
When the tracker API token contains specials characters, the token must
be URL-encoded. For example:

Wrong URL:
http://tracker.torrust-demo.com:1212/v1/stats?token=123g7#@agJtWFSgkdA5R$K22yeo$k6IhNq%T2$r

Rigth URL with encoded token:
http://tracker.torrust-demo.com:1212/v1/stats?token=123g7%23%40agJtWFSgkdA5R%24K22yeo%24k6IhNq%25T2%24r
  • Loading branch information
josecelano committed Dec 26, 2023
1 parent 2c9c3dd commit 860de14
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions src/tracker/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,13 @@ impl Client {
///
/// Will return an error if the HTTP request fails.
pub async fn whitelist_torrent(&self, info_hash: &str) -> Result<Response, Error> {
let request_url = format!(
"{}/whitelist/{}?token={}",
self.base_url, info_hash, self.connection_info.token
);
let request_url = format!("{}/whitelist/{}", self.base_url, info_hash);

let client = reqwest::Client::new();

client.post(request_url).send().await
let params = [("token", &self.connection_info.token)];

client.post(request_url).query(&params).send().await
}

/// Remove a torrent from the tracker whitelist.
Expand All @@ -50,14 +49,13 @@ impl Client {
///
/// Will return an error if the HTTP request fails.
pub async fn remove_torrent_from_whitelist(&self, info_hash: &str) -> Result<Response, Error> {
let request_url = format!(
"{}/whitelist/{}?token={}",
self.base_url, info_hash, self.connection_info.token
);
let request_url = format!("{}/whitelist/{}", self.base_url, info_hash);

let client = reqwest::Client::new();

client.delete(request_url).send().await
let params = [("token", &self.connection_info.token)];

client.delete(request_url).query(&params).send().await
}

/// Retrieve a new tracker key.
Expand All @@ -66,14 +64,13 @@ impl Client {
///
/// Will return an error if the HTTP request fails.
pub async fn retrieve_new_tracker_key(&self, token_valid_seconds: u64) -> Result<Response, Error> {
let request_url = format!(
"{}/key/{}?token={}",
self.base_url, token_valid_seconds, self.connection_info.token
);
let request_url = format!("{}/key/{}", self.base_url, token_valid_seconds);

let client = reqwest::Client::new();

client.post(request_url).send().await
let params = [("token", &self.connection_info.token)];

client.post(request_url).query(&params).send().await
}

/// Retrieve the info for a torrent.
Expand All @@ -82,10 +79,12 @@ impl Client {
///
/// Will return an error if the HTTP request fails.
pub async fn get_torrent_info(&self, info_hash: &str) -> Result<Response, Error> {
let request_url = format!("{}/torrent/{}?token={}", self.base_url, info_hash, self.connection_info.token);
let request_url = format!("{}/torrent/{}", self.base_url, info_hash);

let client = reqwest::Client::new();

client.get(request_url).send().await
let params = [("token", &self.connection_info.token)];

client.get(request_url).query(&params).send().await
}
}

0 comments on commit 860de14

Please sign in to comment.