Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
SleeplessOne1917 committed Dec 15, 2023
1 parent 3fe47ce commit ccd43ce
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
49 changes: 37 additions & 12 deletions integrations/actix/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
//! [`examples`](https://github.com/leptos-rs/leptos/tree/main/examples)
//! directory in the Leptos repository.
use actix_http::{header::{self, HeaderName, HeaderValue}, h1};
use actix_http::{
h1,
header::{self, HeaderName, HeaderValue},
};
use actix_web::{
body::BoxBody,
dev::{ServiceFactory, ServiceRequest},
Expand All @@ -32,9 +35,9 @@ use std::{
pin::Pin,
sync::Arc,
};
use url::Url;
#[cfg(debug_assertions)]
use tracing::instrument;
use url::Url;
/// This struct lets you define headers and override the status of the Response from an Element or a Server Function
/// Typically contained inside of a ResponseOptions. Setting this is useful for cookies and custom responses.
#[derive(Debug, Clone, Default)]
Expand Down Expand Up @@ -242,17 +245,22 @@ pub fn handle_server_fns_with_context(
&& accept_header != Some("application/cbor")
{
// Location will already be set if redirect() has been used
let has_location_set =
res_parts.headers.get(header::LOCATION).is_some();
let has_location_set = res_parts
.headers
.get(header::LOCATION)
.is_some();
if !has_location_set {
let referer = req
.headers()
.get(header::REFERER)
.and_then(|value| value.to_str().ok())
.unwrap_or("/");
res = HttpResponse::SeeOther();
res.insert_header((header::LOCATION, referer))
.content_type("application/json");
res.insert_header((
header::LOCATION,
referer,
))
.content_type("application/json");
}
};
// Override StatusCode if it was set in a Resource or Element
Expand Down Expand Up @@ -291,15 +299,32 @@ pub fn handle_server_fns_with_context(
let url = req
.headers()
.get(header::REFERER)
.and_then(|value|
Url::parse(&Regex::new(r"(?:\?|&)?server_fn_error=[^&]+").unwrap().replace(value.to_str().ok()?, "")).ok()
);
.and_then(|value| {
Url::parse(
&Regex::new(
r"(?:\?|&)?server_fn_error=[^&]+",
)
.unwrap()
.replace(value.to_str().ok()?, ""),
)
.ok()
});

if let Some(mut url) = url {
url.query_pairs_mut()
.append_pair("server_fn_error", serde_qs::to_string(&e).expect("Could not serialize server fn error!").as_str());
url.query_pairs_mut().append_pair(
"server_fn_error",
serde_qs::to_string(&e)
.expect(
"Could not serialize server fn \
error!",
)
.as_str(),
);
HttpResponse::SeeOther()
.insert_header((header::LOCATION, url.to_string()))
.insert_header((
header::LOCATION,
url.to_string(),
))
.finish()
} else {
HttpResponse::InternalServerError().body(
Expand Down
10 changes: 6 additions & 4 deletions server_fn/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,14 +386,16 @@ where
// serialize the output
let result = match Self::encoding() {
Encoding::Url | Encoding::GetJSON => {
let url = serde_json::to_string(&result)
.map_err(|e| ServerFnError::Serialization(e.to_string()))?;
let url = serde_json::to_string(&result).map_err(|e| {
ServerFnError::Serialization(e.to_string())
})?;
Payload::Url(url)
}
Encoding::Cbor | Encoding::GetCBOR => {
let mut buffer: Vec<u8> = Vec::new();
ciborium::ser::into_writer(&result, &mut buffer)
.map_err(|e| ServerFnError::Serialization(e.to_string()))?;
ciborium::ser::into_writer(&result, &mut buffer).map_err(
|e| ServerFnError::Serialization(e.to_string()),
)?;
Payload::Binary(buffer)
}
};
Expand Down

0 comments on commit ccd43ce

Please sign in to comment.