Skip to content

Commit

Permalink
enhancement: make content-type header optional for json payloads
Browse files Browse the repository at this point in the history
  • Loading branch information
aaryanpunia authored and skeptrunedev committed Jul 23, 2024
1 parent 32ef0e4 commit 5029e94
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
4 changes: 2 additions & 2 deletions server/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ pub fn custom_json_error_handler(
) -> actix_web::Error {
let (error_message, solution) = match &err {
JsonPayloadError::ContentType => (
"Content type error",
"Ensure that the content type of the request body is set to application/json."
"Content type header error",
"Ensure the content type request header of the HTTP request is set as `Content-Type: application/json`."
),
JsonPayloadError::Payload(_) => (
"Payload error",
Expand Down
1 change: 1 addition & 0 deletions server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,7 @@ pub fn main() -> std::io::Result<()> {
App::new()
.wrap(Cors::permissive())
.app_data(PayloadConfig::new(134200000))
.wrap(middleware::json_middleware::JsonMiddlewareFactory)
.app_data(json_cfg.clone())
.app_data(
web::PathConfig::default()
Expand Down
66 changes: 66 additions & 0 deletions server/src/middleware/json_middleware.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use actix_http::header::HeaderValue;
use actix_web::{
dev::{forward_ready, Service, ServiceRequest, ServiceResponse, Transform},
Error,
};
use futures_util::future::LocalBoxFuture;
use std::{
future::{ready, Ready},
rc::Rc,
};

pub struct JsonMiddleware<S> {
service: Rc<S>,
}

impl<S, B> Service<ServiceRequest> for JsonMiddleware<S>
where
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
S::Future: 'static,
B: 'static,
{
type Response = ServiceResponse<B>;
type Error = Error;
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;

forward_ready!(service);
fn call(&self, mut req: ServiceRequest) -> Self::Future {
// Clone the Rc pointers so we can move them into the async block.
let srv = self.service.clone();
Box::pin(async move {
if req
.headers()
.get(actix_http::header::CONTENT_TYPE)
.is_none()
{
req.headers_mut().insert(
actix_http::header::CONTENT_TYPE,
HeaderValue::from_static("application/json"),
);
}
let res = srv.call(req).await?;
Ok(res)
})
}
}

pub struct JsonMiddlewareFactory;

impl<S, B> Transform<S, ServiceRequest> for JsonMiddlewareFactory
where
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
S::Future: 'static,
B: 'static,
{
type Response = ServiceResponse<B>;
type Error = Error;
type InitError = ();
type Transform = JsonMiddleware<S>;
type Future = Ready<Result<Self::Transform, Self::InitError>>;

fn new_transform(&self, service: S) -> Self::Future {
ready(Ok(JsonMiddleware {
service: Rc::new(service),
}))
}
}
1 change: 1 addition & 0 deletions server/src/middleware/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod api_version;
pub mod auth_middleware;
pub mod json_middleware;

0 comments on commit 5029e94

Please sign in to comment.