Skip to content

Commit a471a5b

Browse files
committed
Merge #422: Encode tracker API token in HTTP requests
860de14 fix: [#419] url-encode tracker API token in the HTTP requests (Jose Celano) 2c9c3dd fix: [#391] patch to identify 'torrent not found' tracker API response (Jose Celano) Pull request description: When the API token contains special characters like: `123g7#@agJtWFSgkdA5R$K22yeo$k6IhNq%T2$r` The value must be URL-encoded like this: http://tracker.torrust-demo.com:1212/v1/stats?token=123g7%23%40agJtWFSgkdA5R%24K22yeo%24k6IhNq%25T2%24r Instead of using the token directly: http://tracker.torrust-demo.com:1212/v1/stats?token=123g7#@agJtWFSgkdA5R$K22yeo$k6IhNq%T2$r This PR also fixes the issue #391 ACKs for top commit: josecelano: ACK 860de14 Tree-SHA512: 7c4e2361f3d27f9cfc05e38659a1bd1b7bd5668bed59f7b6bec0c8c05e9143398054d906a9bf9980fcda2db548562aec689ee3c76a535c98b0cd0db83aaabfce
2 parents 373493f + 860de14 commit a471a5b

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

src/tracker/api.rs

+16-17
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,13 @@ impl Client {
3434
///
3535
/// Will return an error if the HTTP request fails.
3636
pub async fn whitelist_torrent(&self, info_hash: &str) -> Result<Response, Error> {
37-
let request_url = format!(
38-
"{}/whitelist/{}?token={}",
39-
self.base_url, info_hash, self.connection_info.token
40-
);
37+
let request_url = format!("{}/whitelist/{}", self.base_url, info_hash);
4138

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

44-
client.post(request_url).send().await
41+
let params = [("token", &self.connection_info.token)];
42+
43+
client.post(request_url).query(&params).send().await
4544
}
4645

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

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

60-
client.delete(request_url).send().await
56+
let params = [("token", &self.connection_info.token)];
57+
58+
client.delete(request_url).query(&params).send().await
6159
}
6260

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

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

76-
client.post(request_url).send().await
71+
let params = [("token", &self.connection_info.token)];
72+
73+
client.post(request_url).query(&params).send().await
7774
}
7875

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

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

89-
client.get(request_url).send().await
86+
let params = [("token", &self.connection_info.token)];
87+
88+
client.get(request_url).query(&params).send().await
9089
}
9190
}

src/tracker/service.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ async fn map_torrent_info_response(response: Response) -> Result<TorrentInfo, Se
184184
ServiceError::InternalServerError
185185
})?;
186186

187-
if body == *"torrent not known" {
187+
if body == "\"torrent not known\"" {
188188
// todo: temporary fix. the service should return a 404 (StatusCode::NOT_FOUND).
189189
return Err(ServiceError::TorrentNotFound);
190190
}
@@ -209,7 +209,7 @@ mod tests {
209209

210210
#[tokio::test]
211211
async fn it_should_return_a_torrent_not_found_response_when_the_tracker_returns_the_current_torrent_not_known_response() {
212-
let tracker_response = Response::new("torrent not known");
212+
let tracker_response = Response::new("\"torrent not known\"");
213213

214214
let result = map_torrent_info_response(tracker_response.into()).await.unwrap_err();
215215

0 commit comments

Comments
 (0)