Skip to content

Commit 448a619

Browse files
committed
perf: reduce cookie/header parsing across all handlers
1 parent b8a4d39 commit 448a619

2 files changed

Lines changed: 51 additions & 51 deletions

File tree

src/server.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,13 @@ impl RequestExt for Request<Body> {
236236
}
237237

238238
fn cookie(&self, name: &str) -> Option<Cookie<'_>> {
239-
self.cookies().into_iter().find(|c| c.name() == name)
239+
self.headers().get("Cookie").and_then(|header| {
240+
header
241+
.to_str()
242+
.unwrap_or_default()
243+
.split("; ")
244+
.find_map(|s| Cookie::parse(s).ok().filter(|c| c.name() == name))
245+
})
240246
}
241247
}
242248

src/utils.rs

Lines changed: 44 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -910,70 +910,64 @@ pub fn setting(req: &Request<Body>, name: &str) -> String {
910910
// Parse a cookie value from request
911911

912912
// If this was called with "subscriptions" and the "subscriptions" cookie has a value
913-
if name == "subscriptions" && req.cookie("subscriptions").is_some() {
914-
// Create subscriptions string
915-
let mut subscriptions = String::new();
913+
if name == "subscriptions" {
914+
if let Some(base) = req.cookie("subscriptions") {
915+
// Create subscriptions string
916+
let mut subscriptions = String::from(base.value());
916917

917-
// Default subscriptions cookie
918-
if req.cookie("subscriptions").is_some() {
919-
subscriptions.push_str(req.cookie("subscriptions").unwrap().value());
920-
}
918+
// Start with first numbered subscription cookie
919+
let mut subscriptions_number = 1;
921920

922-
// Start with first numbered subscription cookie
923-
let mut subscriptions_number = 1;
921+
// While whatever subscriptionsNUMBER cookie we're looking at has a value
922+
while let Some(cookie) = req.cookie(&format!("subscriptions{subscriptions_number}")) {
923+
// Push whatever subscriptionsNUMBER cookie we're looking at into the subscriptions string
924+
subscriptions.push_str(cookie.value());
924925

925-
// While whatever subscriptionsNUMBER cookie we're looking at has a value
926-
while req.cookie(&format!("subscriptions{subscriptions_number}")).is_some() {
927-
// Push whatever subscriptionsNUMBER cookie we're looking at into the subscriptions string
928-
subscriptions.push_str(req.cookie(&format!("subscriptions{subscriptions_number}")).unwrap().value());
926+
// Increment subscription cookie number
927+
subscriptions_number += 1;
928+
}
929929

930-
// Increment subscription cookie number
931-
subscriptions_number += 1;
930+
// Return the subscriptions cookies as one large string
931+
return subscriptions;
932932
}
933-
934-
// Return the subscriptions cookies as one large string
935-
subscriptions
936933
}
934+
937935
// If this was called with "filters" and the "filters" cookie has a value
938-
else if name == "filters" && req.cookie("filters").is_some() {
939-
// Create filters string
940-
let mut filters = String::new();
936+
if name == "filters" {
937+
if let Some(base) = req.cookie("filters") {
938+
// Create filters string
939+
let mut filters = String::from(base.value());
941940

942-
// Default filters cookie
943-
if req.cookie("filters").is_some() {
944-
filters.push_str(req.cookie("filters").unwrap().value());
945-
}
941+
// Start with first numbered filters cookie
942+
let mut filters_number = 1;
946943

947-
// Start with first numbered filters cookie
948-
let mut filters_number = 1;
944+
// While whatever filtersNUMBER cookie we're looking at has a value
945+
while let Some(cookie) = req.cookie(&format!("filters{filters_number}")) {
946+
// Push whatever filtersNUMBER cookie we're looking at into the filters string
947+
filters.push_str(cookie.value());
949948

950-
// While whatever filtersNUMBER cookie we're looking at has a value
951-
while req.cookie(&format!("filters{filters_number}")).is_some() {
952-
// Push whatever filtersNUMBER cookie we're looking at into the filters string
953-
filters.push_str(req.cookie(&format!("filters{filters_number}")).unwrap().value());
949+
// Increment filters cookie number
950+
filters_number += 1;
951+
}
954952

955-
// Increment filters cookie number
956-
filters_number += 1;
953+
// Return the filters cookies as one large string
954+
return filters;
957955
}
958-
959-
// Return the filters cookies as one large string
960-
filters
961956
}
957+
962958
// The above two still come to this if there was no existing value
963-
else {
964-
req
965-
.cookie(name)
966-
.unwrap_or_else(|| {
967-
// If there is no cookie for this setting, try receiving a default from the config
968-
if let Some(default) = get_setting(&format!("REDLIB_DEFAULT_{}", name.to_uppercase())) {
969-
Cookie::new(name, default)
970-
} else {
971-
Cookie::from(name)
972-
}
973-
})
974-
.value()
975-
.to_string()
976-
}
959+
req
960+
.cookie(name)
961+
.unwrap_or_else(|| {
962+
// If there is no cookie for this setting, try receiving a default from the config
963+
if let Some(default) = get_setting(&format!("REDLIB_DEFAULT_{}", name.to_uppercase())) {
964+
Cookie::new(name, default)
965+
} else {
966+
Cookie::from(name)
967+
}
968+
})
969+
.value()
970+
.to_string()
977971
}
978972

979973
/// Retrieve the value of a setting by name or the default value

0 commit comments

Comments
 (0)