Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
chrislearn committed Oct 1, 2023
1 parent 0718170 commit 598779e
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 41 deletions.
1 change: 1 addition & 0 deletions crates/core/src/http/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ impl Request {
self.extensions = extensions;
self.body = body;
}

/// Returns a reference to the associated URI.
///
/// # Examples
Expand Down
11 changes: 2 additions & 9 deletions crates/core/src/http/response.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! Http response.
use std::collections::VecDeque;
use std::error::Error as StdError;
use std::fmt::{self, Display, Formatter};
use std::path::PathBuf;

Expand All @@ -9,8 +8,7 @@ use cookie::{Cookie, CookieJar};
use futures_util::stream::Stream;
use http::header::{HeaderMap, HeaderValue, IntoHeaderName};
pub use http::response::Parts;
use http::version::Version;
use http::{status, Extensions};
use http::{version::Version, Extensions};
use mime::Mime;

use crate::fs::NamedFile;
Expand All @@ -20,8 +18,6 @@ use bytes::Bytes;

pub use crate::http::body::ResBody;

use super::ReqBody;

/// Represents an HTTP response
#[non_exhaustive]
pub struct Response {
Expand Down Expand Up @@ -270,10 +266,7 @@ impl Response {
self.version = version;
self.headers = headers;
self.extensions = extensions;
let body = body.into();
if !body.is_none() {
self.body = body.into();
}
self.body = body.into();
}

cfg_feature! {
Expand Down
44 changes: 21 additions & 23 deletions crates/core/src/tower_compat.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
//! Tower service compat.
use std::convert::Infallible;
use std::error::Error as StdError;
use std::fmt;
use std::future::Future;
use std::io::{Error as IoError, ErrorKind};
use std::ops::DerefMut;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};

use futures_util::future::{BoxFuture, FutureExt};
use http::uri::Scheme;
use http_body_util::BodyExt;
use hyper::body::{Body, Bytes, Frame};
use tokio::sync::Mutex;
use tower::buffer::Buffer;
use tower::{Layer, Service, ServiceExt};

Expand Down Expand Up @@ -95,16 +89,16 @@ where
}

struct FlowCtrlInContext {
scheme: Scheme,
ctrl: FlowCtrl,
request: Request,
depot: Depot,
response: Response,
}
impl FlowCtrlInContext {
fn new(ctrl: FlowCtrl, scheme: Scheme, depot: Depot, response: Response) -> Self {
fn new(ctrl: FlowCtrl, request: Request, depot: Depot, response: Response) -> Self {
Self {
ctrl,
scheme,
request,
depot,
response,
}
Expand All @@ -121,6 +115,7 @@ impl FlowCtrlOutContext {
}
}

#[doc(hidden)]
pub struct FlowCtrlService;
impl Service<hyper::Request<ReqBody>> for FlowCtrlService {
type Response = hyper::Response<ResBody>;
Expand All @@ -133,8 +128,8 @@ impl Service<hyper::Request<ReqBody>> for FlowCtrlService {

fn call(&mut self, mut hyper_req: hyper::Request<ReqBody>) -> Self::Future {
let Some(FlowCtrlInContext {
scheme,
mut ctrl,
mut request,
mut depot,
mut response,
}) = hyper_req.extensions_mut().remove::<FlowCtrlInContext>()
Expand All @@ -145,10 +140,12 @@ impl Service<hyper::Request<ReqBody>> for FlowCtrlService {
)))
.boxed();
};
let mut req = Request::from_hyper(hyper_req, scheme.clone());
request.merge_hyper(hyper_req);
Box::pin(async move {
ctrl.call_next(&mut req, &mut depot, &mut response).await;
response.extensions.insert(FlowCtrlOutContext::new(ctrl, req, depot));
ctrl.call_next(&mut request, &mut depot, &mut response).await;
response
.extensions
.insert(FlowCtrlOutContext::new(ctrl, request, depot));
Ok(response.strip_to_hyper())
})
}
Expand All @@ -164,8 +161,7 @@ pub trait TowerLayerCompat {
<Self::Service as Service<hyper::Request<ReqBody>>>::Future: Send,
<Self::Service as Service<hyper::Request<ReqBody>>>::Error: StdError + Send + Sync,
{
let mut svc = self.layer(FlowCtrlService);
TowerLayerHandler(Buffer::new(svc, 32))
TowerLayerHandler(Buffer::new(self.layer(FlowCtrlService), 32))
}
}

Expand All @@ -186,27 +182,28 @@ where
Svc::Error: StdError + Send + Sync,
{
async fn handle(&self, req: &mut Request, depot: &mut Depot, res: &mut Response, ctrl: &mut FlowCtrl) {
let ctx = FlowCtrlInContext::new(
std::mem::take(ctrl),
req.scheme.clone(),
std::mem::take(depot),
std::mem::take(res),
);
req.extensions_mut().insert(ctx);
let mut svc = self.0.clone();
if let Err(_) = svc.ready().await {
tracing::error!("tower service not ready.");
res.render(StatusError::internal_server_error().cause("tower service not ready."));
return;
}
let hyper_req = match req.strip_to_hyper() {

let mut hyper_req = match req.strip_to_hyper() {
Ok(hyper_req) => hyper_req,
Err(_) => {
tracing::error!("strip request to hyper failed.");
res.render(StatusError::internal_server_error().cause("strip request to hyper failed."));
return;
}
};
let ctx = FlowCtrlInContext::new(
std::mem::take(ctrl),
std::mem::take(req),
std::mem::take(depot),
std::mem::take(res),
);
hyper_req.extensions_mut().insert(ctx);

let mut hyper_res = match svc.call(hyper_req).await {
Ok(hyper_res) => hyper_res,
Expand All @@ -233,6 +230,7 @@ where
{
*origin_depot = depot;
*origin_ctrl = ctrl;
*req = request;
} else {
tracing::error!("`FlowCtrlOutContext` should exists in response extensions.");
}
Expand Down
2 changes: 1 addition & 1 deletion crates/jwt-auth/src/finder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use super::ALL_METHODS;
pub trait JwtTokenFinder: Send + Sync {
/// Get token from request.
///
/// The token is returned as an Option<String> , where Some contains the token if found, and None if not found.
/// The token is returned as an `Option<String>`, where Some contains the token if found, and `None` if not found.
async fn find_token(&self, req: &mut Request) -> Option<String>;
}

Expand Down
2 changes: 1 addition & 1 deletion crates/jwt-auth/src/oidc/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use salvo_core::http::header::HeaderValue;
use super::{current_time, decode_jwk, DecodingInfo, JwkSetFetch};

/// Determines settings about updating the cached JWKS data.
/// The JWKS will be lazily revalidated every time [validate](crate::Validator) validates a token.
/// The JWKS will be lazily revalidated every time validator validates a token.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CachePolicy {
/// Time in Seconds to refresh the JWKS from the OIDC Provider
Expand Down
2 changes: 1 addition & 1 deletion crates/jwt-auth/src/oidc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub struct DecoderBuilder<T>
where
T: AsRef<str>,
{
/// The issuer URL of the token. eg: https://xx-xx.clerk.accounts.dev
/// The issuer URL of the token. eg: `https://xx-xx.clerk.accounts.dev`
pub issuer: T,
/// The http client for the decoder.
pub http_client: Option<reqwest::Client>,
Expand Down
2 changes: 1 addition & 1 deletion crates/oapi/src/openapi/schema/all_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl AllOf {
self
}

/// Add or change nullable flag for [`Object`].
/// Add or change nullable flag for [Object][crate::Object].
pub fn nullable(mut self, nullable: bool) -> Self {
self.nullable = nullable;
self
Expand Down
2 changes: 1 addition & 1 deletion crates/oapi/src/openapi/schema/any_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl AnyOf {
self
}

/// Add or change nullable flag for [`Object`].
/// Add or change nullable flag for [Object][crate::Object].
pub fn nullable(mut self, nullable: bool) -> Self {
self.nullable = nullable;
self
Expand Down
2 changes: 1 addition & 1 deletion crates/oapi/src/openapi/schema/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl Array {
self
}

/// Add or change nullable flag for [`Object`].
/// Add or change nullable flag for [Object][crate::Object].
pub fn nullable(mut self, nullable: bool) -> Self {
self.nullable = nullable;
self
Expand Down
2 changes: 1 addition & 1 deletion crates/oapi/src/openapi/schema/one_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl OneOf {
self
}

/// Add or change nullable flag for [`Object`].
/// Add or change nullable flag for [Object][crate::Object].
pub fn nullable(mut self, nullable: bool) -> Self {
self.nullable = nullable;
self
Expand Down
2 changes: 1 addition & 1 deletion crates/oapi/src/swagger_ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl SwaggerUi {

/// Add multiple [`Url`]s to Swagger UI.
///
/// Takes one [`Vec`] argument containing tuples of [`Url`] and [`OpenApi`].
/// Takes one [`Vec`] argument containing tuples of [`Url`] and [OpenApi][crate::OpenApi].
///
/// Situations where this comes handy is when there is a need or wish to separate different parts
/// of the api to separate api docs.
Expand Down
1 change: 0 additions & 1 deletion crates/proxy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

use std::convert::{Infallible, TryFrom};

use futures_util::TryStreamExt;
use hyper::upgrade::OnUpgrade;
use percent_encoding::{utf8_percent_encode, CONTROLS};
use reqwest::Client;
Expand Down

0 comments on commit 598779e

Please sign in to comment.