|
| 1 | +use std::panic::Location; |
| 2 | + |
| 3 | +use thiserror::Error; |
| 4 | + |
| 5 | +use crate::http::axum_implementation::query::Query; |
| 6 | +use crate::http::axum_implementation::responses; |
| 7 | +use crate::http::percent_encoding::percent_decode_info_hash; |
| 8 | +use crate::located_error::{Located, LocatedError}; |
| 9 | +use crate::protocol::info_hash::{ConversionError, InfoHash}; |
| 10 | + |
| 11 | +pub type NumberOfBytes = i64; |
| 12 | + |
| 13 | +// Query param name |
| 14 | +const INFO_HASH_SCRAPE_PARAM: &str = "info_hash"; |
| 15 | + |
| 16 | +#[derive(Debug, PartialEq)] |
| 17 | +pub struct Scrape { |
| 18 | + pub info_hashes: Vec<InfoHash>, |
| 19 | +} |
| 20 | + |
| 21 | +#[derive(Error, Debug)] |
| 22 | +pub enum ParseScrapeQueryError { |
| 23 | + #[error("missing query params for scrape request in {location}")] |
| 24 | + MissingParams { location: &'static Location<'static> }, |
| 25 | + #[error("missing param {param_name} in {location}")] |
| 26 | + MissingParam { |
| 27 | + location: &'static Location<'static>, |
| 28 | + param_name: String, |
| 29 | + }, |
| 30 | + #[error("invalid param value {param_value} for {param_name} in {location}")] |
| 31 | + InvalidParam { |
| 32 | + param_name: String, |
| 33 | + param_value: String, |
| 34 | + location: &'static Location<'static>, |
| 35 | + }, |
| 36 | + #[error("invalid param value {param_value} for {param_name} in {source}")] |
| 37 | + InvalidInfoHashParam { |
| 38 | + param_name: String, |
| 39 | + param_value: String, |
| 40 | + source: LocatedError<'static, ConversionError>, |
| 41 | + }, |
| 42 | +} |
| 43 | + |
| 44 | +impl From<ParseScrapeQueryError> for responses::error::Error { |
| 45 | + fn from(err: ParseScrapeQueryError) -> Self { |
| 46 | + responses::error::Error { |
| 47 | + failure_reason: format!("Cannot parse query params for scrape request: {err}"), |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +impl TryFrom<Query> for Scrape { |
| 53 | + type Error = ParseScrapeQueryError; |
| 54 | + |
| 55 | + fn try_from(query: Query) -> Result<Self, Self::Error> { |
| 56 | + Ok(Self { |
| 57 | + info_hashes: extract_info_hashes(&query)?, |
| 58 | + }) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +fn extract_info_hashes(query: &Query) -> Result<Vec<InfoHash>, ParseScrapeQueryError> { |
| 63 | + match query.get_param(INFO_HASH_SCRAPE_PARAM) { |
| 64 | + Some(raw_param) => { |
| 65 | + let mut info_hashes = vec![]; |
| 66 | + |
| 67 | + // todo: multiple infohashes |
| 68 | + |
| 69 | + let info_hash = percent_decode_info_hash(&raw_param).map_err(|err| ParseScrapeQueryError::InvalidInfoHashParam { |
| 70 | + param_name: INFO_HASH_SCRAPE_PARAM.to_owned(), |
| 71 | + param_value: raw_param.clone(), |
| 72 | + source: Located(err).into(), |
| 73 | + })?; |
| 74 | + |
| 75 | + info_hashes.push(info_hash); |
| 76 | + |
| 77 | + Ok(info_hashes) |
| 78 | + } |
| 79 | + None => { |
| 80 | + return Err(ParseScrapeQueryError::MissingParam { |
| 81 | + location: Location::caller(), |
| 82 | + param_name: INFO_HASH_SCRAPE_PARAM.to_owned(), |
| 83 | + }) |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +#[cfg(test)] |
| 89 | +mod tests { |
| 90 | + |
| 91 | + mod scrape_request { |
| 92 | + |
| 93 | + use crate::http::axum_implementation::query::Query; |
| 94 | + use crate::http::axum_implementation::requests::scrape::{Scrape, INFO_HASH_SCRAPE_PARAM}; |
| 95 | + use crate::protocol::info_hash::InfoHash; |
| 96 | + |
| 97 | + #[test] |
| 98 | + fn should_be_instantiated_from_the_url_query_with_only_one_infohash() { |
| 99 | + let raw_query = Query::from(vec![( |
| 100 | + INFO_HASH_SCRAPE_PARAM, |
| 101 | + "%3B%24U%04%CF%5F%11%BB%DB%E1%20%1C%EAjk%F4Z%EE%1B%C0", |
| 102 | + )]) |
| 103 | + .to_string(); |
| 104 | + |
| 105 | + let query = raw_query.parse::<Query>().unwrap(); |
| 106 | + |
| 107 | + let scrape_request = Scrape::try_from(query).unwrap(); |
| 108 | + |
| 109 | + assert_eq!( |
| 110 | + scrape_request, |
| 111 | + Scrape { |
| 112 | + info_hashes: vec!["3b245504cf5f11bbdbe1201cea6a6bf45aee1bc0".parse::<InfoHash>().unwrap()], |
| 113 | + } |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + mod when_it_is_instantiated_from_the_url_query_params { |
| 118 | + |
| 119 | + use crate::http::axum_implementation::query::Query; |
| 120 | + use crate::http::axum_implementation::requests::scrape::{Scrape, INFO_HASH_SCRAPE_PARAM}; |
| 121 | + |
| 122 | + #[test] |
| 123 | + fn it_should_fail_if_the_query_does_not_include_the_info_hash_param() { |
| 124 | + let raw_query_without_info_hash = "another_param=NOT_RELEVANT"; |
| 125 | + |
| 126 | + assert!(Scrape::try_from(raw_query_without_info_hash.parse::<Query>().unwrap()).is_err()); |
| 127 | + } |
| 128 | + |
| 129 | + #[test] |
| 130 | + fn it_should_fail_if_the_info_hash_param_is_invalid() { |
| 131 | + let raw_query = Query::from(vec![(INFO_HASH_SCRAPE_PARAM, "INVALID_INFO_HASH_VALUE")]).to_string(); |
| 132 | + |
| 133 | + assert!(Scrape::try_from(raw_query.parse::<Query>().unwrap()).is_err()); |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments