fix: Actix server fn redirect() duplicate Location headers (#1326)

This commit is contained in:
Greg Johnston 2023-07-11 13:57:44 -04:00 committed by GitHub
parent 3cdcc85c87
commit 576bb078f7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -223,18 +223,21 @@ pub fn handle_server_fns_with_context(
let res_options =
use_context::<ResponseOptions>(cx).unwrap();
let mut res: HttpResponseBuilder;
let mut res: HttpResponseBuilder =
HttpResponse::Ok();
let res_parts = res_options.0.write();
if accept_header == Some("application/json")
|| accept_header
== Some("application/x-www-form-urlencoded")
|| accept_header == Some("application/cbor")
// if accept_header isn't set to one of these, it's a form submit
// redirect back to the referrer if not redirect has been set
if accept_header != Some("application/json")
&& accept_header
!= Some("application/x-www-form-urlencoded")
&& accept_header != Some("application/cbor")
{
res = HttpResponse::Ok();
}
// otherwise, it's probably a <form> submit or something: redirect back to the referrer
else {
// Location will already be set if redirect() has been used
let has_location_set =
res_parts.headers.get("Location").is_some();
if !has_location_set {
let referer = req
.headers()
.get("Referer")
@ -243,6 +246,7 @@ pub fn handle_server_fns_with_context(
res = HttpResponse::SeeOther();
res.insert_header(("Location", referer))
.content_type("application/json");
}
};
// Override StatusCode if it was set in a Resource or Element
if let Some(status) = res_parts.status {