Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

controllers/helpers/pagination: Remove generic parameter from gather() fn #10190

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/controllers/helpers/pagination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use diesel_async::AsyncPgConnection;
use futures_util::future::BoxFuture;
use futures_util::{FutureExt, TryStreamExt};
use http::header;
use http::request::Parts;
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -76,8 +77,8 @@ impl PaginationOptionsBuilder {
self
}

pub(crate) fn gather<T: RequestPartsExt>(self, req: &T) -> AppResult<PaginationOptions> {
let params = req.query();
pub(crate) fn gather(self, parts: &Parts) -> AppResult<PaginationOptions> {
let params = parts.query();
let page_param = params.get("page");
let seek_param = params.get("seek");

Expand All @@ -100,16 +101,16 @@ impl PaginationOptionsBuilder {
}

if numeric_page > MAX_PAGE_BEFORE_SUSPECTED_BOT {
req.request_log().add("bot", "suspected");
parts.request_log().add("bot", "suspected");
}

// Block large offsets for known violators of the crawler policy
if self.limit_page_numbers {
let config = &req.app().config;
let config = &parts.app().config;
if numeric_page > config.max_allowed_page_offset
&& is_useragent_or_ip_blocked(config, req)
&& is_useragent_or_ip_blocked(config, parts)
{
req.request_log().add("cause", "large page offset");
parts.request_log().add("cause", "large page offset");

let error =
format!("Page {numeric_page} is unavailable for performance reasons. Please take a look at https://crates.io/data-access for alternatives.");
Expand Down Expand Up @@ -739,12 +740,14 @@ mod tests {
);
}

fn mock(query: &str) -> Request<()> {
fn mock(query: &str) -> Parts {
Request::builder()
.method(Method::GET)
.uri(format!("/?{query}"))
.body(())
.unwrap()
.into_parts()
.0
}

fn assert_pagination_error(options: PaginationOptionsBuilder, query: &str, message: &str) {
Expand Down