Skip to content

Commit 32332b7

Browse files
authored
Merge pull request #5833 from Turbo87/conduit-request
conduit::Handler: Use fixed request type
2 parents a2e5489 + bdea678 commit 32332b7

35 files changed

+95
-103
lines changed

conduit-axum/examples/server.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use axum::body::Bytes;
44
use axum::routing::get;
5-
use conduit::{RequestExt, ResponseResult};
5+
use conduit::{ConduitRequest, ResponseResult};
66
use conduit_axum::ConduitAxumHandler;
77
use http::{header, Response};
88

@@ -30,7 +30,7 @@ pub fn wrap<H>(handler: H) -> ConduitAxumHandler<H> {
3030
ConduitAxumHandler::wrap(handler)
3131
}
3232

33-
fn endpoint(_: &mut dyn RequestExt) -> ResponseResult<http::Error> {
33+
fn endpoint(_: &mut ConduitRequest) -> ResponseResult<http::Error> {
3434
let body = b"Hello world!";
3535

3636
sleep(std::time::Duration::from_secs(2));
@@ -41,11 +41,11 @@ fn endpoint(_: &mut dyn RequestExt) -> ResponseResult<http::Error> {
4141
.body(Bytes::from_static(body))
4242
}
4343

44-
fn panic(_: &mut dyn RequestExt) -> ResponseResult<http::Error> {
44+
fn panic(_: &mut ConduitRequest) -> ResponseResult<http::Error> {
4545
// For now, connection is immediately closed
4646
panic!("message");
4747
}
4848

49-
fn error(_: &mut dyn RequestExt) -> ResponseResult<io::Error> {
49+
fn error(_: &mut ConduitRequest) -> ResponseResult<io::Error> {
5050
Err(io::Error::new(io::ErrorKind::Other, "io error, oops"))
5151
}

conduit-axum/src/fallback.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use axum::body::{Body, HttpBody};
1313
use axum::extract::{rejection::PathRejection, Extension, FromRequestParts, Path};
1414
use axum::handler::Handler as AxumHandler;
1515
use axum::response::IntoResponse;
16-
use conduit::{Handler, RequestExt};
16+
use conduit::{ConduitRequest, Handler};
1717
use http::header::CONTENT_LENGTH;
1818
use http::StatusCode;
1919
use hyper::Request;
@@ -158,7 +158,7 @@ pub trait RequestParamsExt<'a> {
158158
fn axum_params(self) -> Option<&'a Params>;
159159
}
160160

161-
impl<'a> RequestParamsExt<'a> for &'a (dyn RequestExt + 'a) {
161+
impl<'a> RequestParamsExt<'a> for &'a ConduitRequest {
162162
fn axum_params(self) -> Option<&'a Params> {
163163
self.extensions().get::<Params>()
164164
}

conduit-axum/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@
3838
//! #
3939
//! # use std::{error, io};
4040
//! # use axum::body::Bytes;
41-
//! # use conduit::{box_error, Response, RequestExt, HandlerResult};
41+
//! # use conduit::{box_error, Response, ConduitRequest, HandlerResult};
4242
//! #
4343
//! # struct Endpoint();
4444
//! # impl Handler for Endpoint {
45-
//! # fn call(&self, _: &mut dyn RequestExt) -> HandlerResult {
45+
//! # fn call(&self, _: &mut ConduitRequest) -> HandlerResult {
4646
//! # Response::builder().body(Bytes::new()).map_err(box_error)
4747
//! # }
4848
//! # }

conduit-axum/src/tests.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use axum::body::Bytes;
22
use axum::Router;
3-
use conduit::{box_error, Handler, HandlerResult, RequestExt};
3+
use conduit::{box_error, ConduitRequest, Handler, HandlerResult};
44
use http::{HeaderValue, Request, Response, StatusCode};
55
use hyper::{body::to_bytes, service::Service};
66
use tokio::{sync::oneshot, task::JoinHandle};
@@ -10,7 +10,7 @@ use crate::ConduitAxumHandler;
1010

1111
struct OkResult;
1212
impl Handler for OkResult {
13-
fn call(&self, _req: &mut dyn RequestExt) -> HandlerResult {
13+
fn call(&self, _req: &mut ConduitRequest) -> HandlerResult {
1414
Response::builder()
1515
.header("ok", "value")
1616
.body(Bytes::from_static(b"Hello, world!"))
@@ -20,22 +20,22 @@ impl Handler for OkResult {
2020

2121
struct ErrorResult;
2222
impl Handler for ErrorResult {
23-
fn call(&self, _req: &mut dyn RequestExt) -> HandlerResult {
23+
fn call(&self, _req: &mut ConduitRequest) -> HandlerResult {
2424
let error = ::std::io::Error::last_os_error();
2525
Err(Box::new(error))
2626
}
2727
}
2828

2929
struct Panic;
3030
impl Handler for Panic {
31-
fn call(&self, _req: &mut dyn RequestExt) -> HandlerResult {
31+
fn call(&self, _req: &mut ConduitRequest) -> HandlerResult {
3232
panic!()
3333
}
3434
}
3535

3636
struct InvalidHeader;
3737
impl Handler for InvalidHeader {
38-
fn call(&self, _req: &mut dyn RequestExt) -> HandlerResult {
38+
fn call(&self, _req: &mut ConduitRequest) -> HandlerResult {
3939
Response::builder()
4040
.header("invalid-value", "\r\n")
4141
.body(Bytes::from_static(b"discarded"))
@@ -45,7 +45,7 @@ impl Handler for InvalidHeader {
4545

4646
struct InvalidStatus;
4747
impl Handler for InvalidStatus {
48-
fn call(&self, _req: &mut dyn RequestExt) -> HandlerResult {
48+
fn call(&self, _req: &mut ConduitRequest) -> HandlerResult {
4949
Response::builder()
5050
.status(1000)
5151
.body(Bytes::new())
@@ -55,15 +55,15 @@ impl Handler for InvalidStatus {
5555

5656
struct Sleep;
5757
impl Handler for Sleep {
58-
fn call(&self, req: &mut dyn RequestExt) -> HandlerResult {
58+
fn call(&self, req: &mut ConduitRequest) -> HandlerResult {
5959
std::thread::sleep(std::time::Duration::from_millis(100));
6060
OkResult.call(req)
6161
}
6262
}
6363

6464
struct AssertPercentDecodedPath;
6565
impl Handler for AssertPercentDecodedPath {
66-
fn call(&self, req: &mut dyn RequestExt) -> HandlerResult {
66+
fn call(&self, req: &mut ConduitRequest) -> HandlerResult {
6767
if req.uri().path() == "/%3a" && req.uri().query() == Some("%3a") {
6868
OkResult.call(req)
6969
} else {

conduit/src/lib.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::io::{Cursor, Read};
66

77
pub use http::{header, Extensions, HeaderMap, Method, Request, Response, StatusCode, Uri};
88

9+
pub type ConduitRequest = Request<Cursor<Bytes>>;
910
pub type ResponseResult<Error> = Result<Response<Bytes>, Error>;
1011

1112
pub type BoxError = Box<dyn Error + Send>;
@@ -54,7 +55,7 @@ pub trait RequestExt {
5455
fn extensions_mut(&mut self) -> &mut Extensions;
5556
}
5657

57-
impl RequestExt for Request<Cursor<Bytes>> {
58+
impl RequestExt for ConduitRequest {
5859
fn method(&self) -> &Method {
5960
self.method()
6061
}
@@ -86,15 +87,15 @@ impl RequestExt for Request<Cursor<Bytes>> {
8687
/// A Handler takes a request and returns a response or an error.
8788
/// By default, a bare function implements `Handler`.
8889
pub trait Handler: Sync + Send + 'static {
89-
fn call(&self, request: &mut dyn RequestExt) -> HandlerResult;
90+
fn call(&self, request: &mut ConduitRequest) -> HandlerResult;
9091
}
9192

9293
impl<F, E> Handler for F
9394
where
94-
F: Fn(&mut dyn RequestExt) -> ResponseResult<E> + Sync + Send + 'static,
95+
F: Fn(&mut ConduitRequest) -> ResponseResult<E> + Sync + Send + 'static,
9596
E: Error + Send + 'static,
9697
{
97-
fn call(&self, request: &mut dyn RequestExt) -> HandlerResult {
98+
fn call(&self, request: &mut ConduitRequest) -> HandlerResult {
9899
(*self)(request).map_err(box_error)
99100
}
100101
}

src/auth.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::util::errors::{
88
account_locked, forbidden, internal, AppError, AppResult, InsecurelyGeneratedTokenRevoked,
99
};
1010
use chrono::Utc;
11-
use conduit::RequestExt;
11+
use conduit::ConduitRequest;
1212
use http::header;
1313

1414
#[derive(Debug, Clone)]
@@ -55,7 +55,7 @@ impl AuthCheck {
5555
}
5656
}
5757

58-
pub fn check(&self, request: &dyn RequestExt) -> AppResult<AuthenticatedUser> {
58+
pub fn check(&self, request: &ConduitRequest) -> AppResult<AuthenticatedUser> {
5959
controllers::util::verify_origin(request)?;
6060

6161
let auth = authenticate_user(request)?;
@@ -153,7 +153,7 @@ impl AuthenticatedUser {
153153
}
154154
}
155155

156-
fn authenticate_user(req: &dyn RequestExt) -> AppResult<AuthenticatedUser> {
156+
fn authenticate_user(req: &ConduitRequest) -> AppResult<AuthenticatedUser> {
157157
let conn = req.app().db_write()?;
158158

159159
let user_id_from_session = req

src/controllers.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ mod prelude {
1414
use axum::body::Bytes;
1515
pub use diesel::prelude::*;
1616

17-
pub use conduit::RequestExt;
17+
pub use conduit::{ConduitRequest, RequestExt};
1818
pub use http::{header, StatusCode};
1919

2020
pub use super::conduit_axum::conduit_compat;
@@ -43,7 +43,7 @@ mod prelude {
4343
fn query_with_params(&self, params: IndexMap<String, String>) -> String;
4444
}
4545

46-
impl<'a> RequestUtils for dyn RequestExt + 'a {
46+
impl RequestUtils for ConduitRequest {
4747
fn query(&self) -> IndexMap<String, String> {
4848
url::form_urlencoded::parse(self.uri().query().unwrap_or("").as_bytes())
4949
.into_owned()

src/controllers/category.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::schema::categories;
66
use crate::views::{EncodableCategory, EncodableCategoryWithSubcategories};
77

88
/// Handles the `GET /categories` route.
9-
pub fn index(req: &mut dyn RequestExt) -> EndpointResult {
9+
pub fn index(req: &mut ConduitRequest) -> EndpointResult {
1010
let query = req.query();
1111
// FIXME: There are 69 categories, 47 top level. This isn't going to
1212
// grow by an OoM. We need a limit for /summary, but we don't need
@@ -33,7 +33,7 @@ pub fn index(req: &mut dyn RequestExt) -> EndpointResult {
3333
}
3434

3535
/// Handles the `GET /categories/:category_id` route.
36-
pub fn show(req: &mut dyn RequestExt) -> EndpointResult {
36+
pub fn show(req: &mut ConduitRequest) -> EndpointResult {
3737
let slug = req.param("category_id").unwrap();
3838
let conn = req.app().db_read()?;
3939
let cat: Category = Category::by_slug(slug).first(&*conn)?;
@@ -64,7 +64,7 @@ pub fn show(req: &mut dyn RequestExt) -> EndpointResult {
6464
}
6565

6666
/// Handles the `GET /category_slugs` route.
67-
pub fn slugs(req: &mut dyn RequestExt) -> EndpointResult {
67+
pub fn slugs(req: &mut ConduitRequest) -> EndpointResult {
6868
let conn = req.app().db_read()?;
6969
let slugs: Vec<Slug> = categories::table
7070
.select((categories::slug, categories::slug, categories::description))

src/controllers/crate_owner_invitation.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use indexmap::IndexMap;
1616
use std::collections::{HashMap, HashSet};
1717

1818
/// Handles the `GET /api/v1/me/crate_owner_invitations` route.
19-
pub fn list(req: &mut dyn RequestExt) -> EndpointResult {
19+
pub fn list(req: &mut ConduitRequest) -> EndpointResult {
2020
let auth = AuthCheck::only_cookie().check(req)?;
2121
let user_id = auth.user_id();
2222

@@ -52,7 +52,7 @@ pub fn list(req: &mut dyn RequestExt) -> EndpointResult {
5252
}
5353

5454
/// Handles the `GET /api/private/crate_owner_invitations` route.
55-
pub fn private_list(req: &mut dyn RequestExt) -> EndpointResult {
55+
pub fn private_list(req: &mut ConduitRequest) -> EndpointResult {
5656
let auth = AuthCheck::only_cookie().check(req)?;
5757

5858
let filter = if let Some(crate_name) = req.query().get("crate_name") {
@@ -73,7 +73,7 @@ enum ListFilter {
7373
}
7474

7575
fn prepare_list(
76-
req: &mut dyn RequestExt,
76+
req: &mut ConduitRequest,
7777
auth: AuthenticatedUser,
7878
filter: ListFilter,
7979
) -> AppResult<PrivateListResponse> {
@@ -250,7 +250,7 @@ struct OwnerInvitation {
250250
}
251251

252252
/// Handles the `PUT /api/v1/me/crate_owner_invitations/:crate_id` route.
253-
pub fn handle_invite(req: &mut dyn RequestExt) -> EndpointResult {
253+
pub fn handle_invite(req: &mut ConduitRequest) -> EndpointResult {
254254
let crate_invite: OwnerInvitation =
255255
serde_json::from_reader(req.body()).map_err(|_| bad_request("invalid json request"))?;
256256

@@ -274,7 +274,7 @@ pub fn handle_invite(req: &mut dyn RequestExt) -> EndpointResult {
274274
}
275275

276276
/// Handles the `PUT /api/v1/me/crate_owner_invitations/accept/:token` route.
277-
pub fn handle_invite_with_token(req: &mut dyn RequestExt) -> EndpointResult {
277+
pub fn handle_invite_with_token(req: &mut ConduitRequest) -> EndpointResult {
278278
let state = req.app();
279279
let config = &state.config;
280280
let conn = state.db_write()?;

src/controllers/github/secret_scanning.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ pub enum GitHubSecretAlertFeedbackLabel {
232232
}
233233

234234
/// Handles the `POST /api/github/secret-scanning/verify` route.
235-
pub fn verify(req: &mut dyn RequestExt) -> EndpointResult {
235+
pub fn verify(req: &mut ConduitRequest) -> EndpointResult {
236236
let max_size = 8192;
237237
let length = req
238238
.content_length()

src/controllers/helpers/pagination.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl PaginationOptionsBuilder {
7272
self
7373
}
7474

75-
pub(crate) fn gather(self, req: &dyn RequestExt) -> AppResult<PaginationOptions> {
75+
pub(crate) fn gather(self, req: &ConduitRequest) -> AppResult<PaginationOptions> {
7676
let params = req.query();
7777
let page_param = params.get("page");
7878
let seek_param = params.get("seek");

src/controllers/keyword.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::models::Keyword;
1010
use crate::views::EncodableKeyword;
1111

1212
/// Handles the `GET /keywords` route.
13-
pub fn index(req: &mut dyn RequestExt) -> EndpointResult {
13+
pub fn index(req: &mut ConduitRequest) -> EndpointResult {
1414
use crate::schema::keywords;
1515

1616
let query = req.query();

src/controllers/krate/downloads.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::sql::to_char;
1313
use crate::views::EncodableVersionDownload;
1414

1515
/// Handles the `GET /crates/:crate_id/downloads` route.
16-
pub fn downloads(req: &mut dyn RequestExt) -> EndpointResult {
16+
pub fn downloads(req: &mut ConduitRequest) -> EndpointResult {
1717
use diesel::dsl::*;
1818
use diesel::sql_types::BigInt;
1919

src/controllers/krate/follow.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::models::{Crate, Follow};
99
use crate::schema::*;
1010

1111
fn follow_target(
12-
req: &dyn RequestExt,
12+
req: &ConduitRequest,
1313
conn: &DieselPooledConn<'_>,
1414
user_id: i32,
1515
) -> AppResult<Follow> {
@@ -21,7 +21,7 @@ fn follow_target(
2121
}
2222

2323
/// Handles the `PUT /crates/:crate_id/follow` route.
24-
pub fn follow(req: &mut dyn RequestExt) -> EndpointResult {
24+
pub fn follow(req: &mut ConduitRequest) -> EndpointResult {
2525
let user_id = AuthCheck::default().check(req)?.user_id();
2626
let conn = req.app().db_write()?;
2727
let follow = follow_target(req, &conn, user_id)?;
@@ -34,7 +34,7 @@ pub fn follow(req: &mut dyn RequestExt) -> EndpointResult {
3434
}
3535

3636
/// Handles the `DELETE /crates/:crate_id/follow` route.
37-
pub fn unfollow(req: &mut dyn RequestExt) -> EndpointResult {
37+
pub fn unfollow(req: &mut ConduitRequest) -> EndpointResult {
3838
let user_id = AuthCheck::default().check(req)?.user_id();
3939
let conn = req.app().db_write()?;
4040
let follow = follow_target(req, &conn, user_id)?;
@@ -44,7 +44,7 @@ pub fn unfollow(req: &mut dyn RequestExt) -> EndpointResult {
4444
}
4545

4646
/// Handles the `GET /crates/:crate_id/following` route.
47-
pub fn following(req: &mut dyn RequestExt) -> EndpointResult {
47+
pub fn following(req: &mut ConduitRequest) -> EndpointResult {
4848
use diesel::dsl::exists;
4949

5050
let user_id = AuthCheck::only_cookie().check(req)?.user_id();

0 commit comments

Comments
 (0)